All pastes #737391 Raw Edit

tobz

public text v1 · immutable
·published 2007-10-15 12:11 UTC
rendered paste body
/*************************************************************************
 *                             Configuration.cs
 *                            ------------------
 *   begin                : Jan 24, 2007
 *   copyright            : (C) The WCell Team
 *   email                : info@wcell.org
 *************************************************************************/

/*************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;
using log4net;
using WCell.Core.Localization;

namespace WCell.Core
{
    /// <summary>
    /// Defines a configuration made up of key/value values
    /// </summary>
    public abstract class Configuration : IEnumerable<string>
    {
        protected static ILog s_log = LogManager.GetLogger(typeof (Configuration));
        private string m_nullString;
        private System.Configuration.Configuration m_cfg;

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="callingAsm">the entry assembly to load the configuration file of</param>
        public Configuration(Assembly callingAsm)
        {
            m_nullString = "None";

            m_cfg = ConfigurationManager.OpenExeConfiguration(callingAsm.Location);

            LoadConfigDefaults();
        }

        /// <summary>
        /// Loads default values in the configuration if they don't already exist
        /// </summary>
        public abstract void LoadConfigDefaults();

        private int GetNoneNesting(string val)
        {
            int num1 = 0;
            int num2 = val.Length;
            if (num2 > 1)
            {
                while ((val[num1] == '(') && (val[(num2 - num1) - 1] == ')'))
                {
                    num1++;
                }
                if ((num1 > 0) &&
                    (string.Compare(m_nullString, 0, val, num1, num2 - (2*num1), StringComparison.Ordinal) != 0))
                {
                    num1 = 0;
                }
            }
            return num1;
        }

        protected bool AddValue(string key, string value)
        {
            m_cfg.AppSettings.Settings.Add(key, value);

            return true;
        }

        protected string GetValue(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            string cfgVal = m_cfg.AppSettings.Settings[key].Value;

            if (cfgVal == null)
            {
                s_log.Error(string.Format(Resources.KeyNotFound, key));

                return "";
            }

            switch (GetNoneNesting(cfgVal))
            {
                case 0:
                    return cfgVal;

                case 1:
                    return null;
            }

            return cfgVal.Substring(1, cfgVal.Length - 2);
        }

        protected bool SetValue(string key, object value)
        {
            if (m_cfg.AppSettings.Settings[key] != null)
            {
                m_cfg.AppSettings.Settings.Remove(key);
                m_cfg.AppSettings.Settings.Add(key, value.ToString());

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Creates a config entry with the supplied value if one doesn't already exist
        /// </summary>
        /// <param name="key">the key</param>
        /// <param name="value">the value</param>
        protected void CreateValue(string key, object value)
        {
            KeyValueConfigurationElement cfgVal = m_cfg.AppSettings.Settings[key];

            if (cfgVal == null)
            {
                m_cfg.AppSettings.Settings.Add(key, value.ToString());
            }

            m_cfg.Save(ConfigurationSaveMode.Full);
        }

        #region IEnumerable members

        IEnumerator<string> IEnumerable<string>.GetEnumerator()
        {
            string configLine = "";

            foreach (string cfgEle in m_cfg.AppSettings.Settings.AllKeys)
            {
                configLine = cfgEle + ": " + m_cfg.AppSettings.Settings[cfgEle].Value;

                yield return configLine;
            }
        }

        /// <summary>
        /// Get an enumerator that represents the key/value pairs of this configuration
        /// </summary>
        /// <returns>an IEnumerator object to enumerate through this configuration</returns>
        public IEnumerator GetEnumerator()
        {
            StringCollection strCol = new StringCollection();
            string configLine = "";

            foreach (string cfgEle in m_cfg.AppSettings.Settings.AllKeys)
            {
                configLine = cfgEle + ": " + m_cfg.AppSettings.Settings[cfgEle].Value;

                strCol.Add(configLine);
            }

            return (IEnumerator) strCol.GetEnumerator();
        }

        #endregion
    }
}