Adilol

Untitled

Jul 10th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System;
  4. using ElephantEmulator;
  5.  
  6. namespace Configuration
  7. {
  8.     public class ConfigurationModule
  9.     {
  10.         #region Fields
  11.         private Dictionary<string, string> mContent = new Dictionary<string, string>();
  12.         #endregion
  13.  
  14.         #region Properties
  15.         /// <summary>
  16.         /// Gets or sets the configuration value as string for a given key.
  17.         /// </summary>
  18.         /// <param name="sKey">The key (string) to get or set the value string of.</param>
  19.         public string this[string sKey]
  20.         {
  21.             get
  22.             {
  23.                 return this.GetValue(sKey);
  24.             }
  25.             set
  26.             {
  27.                 this.SetValue(sKey, value);
  28.             }
  29.         }
  30.         #endregion
  31.  
  32.         #region Methods
  33.         /// <summary>
  34.         /// Returns the configuration value string of a given key. A blank string is returned if the configuration key is not defined.
  35.         /// </summary>
  36.         /// <param name="sKey">The configuration key as a string.</param>
  37.         /// <returns>The configuration value of a given key as a string.</returns>
  38.         public string GetValue(string sKey)
  39.         {
  40.             if (!mContent.ContainsKey(sKey))
  41.                 return "";
  42.  
  43.             return mContent[sKey];
  44.         }
  45.         public void SetValue(string sKey, string sValue)
  46.         {
  47.             if (mContent.ContainsKey(sKey))
  48.                 mContent[sKey] = sValue;
  49.             else
  50.                 mContent.Add(sKey, sValue);
  51.         }
  52.         public bool TryParseInt32(string sField, out int i)
  53.         {
  54.             bool Success = int.TryParse(this[sField], out i);
  55.             if (!Success)
  56.                 Console.WriteLine(sField);
  57.  
  58.             return Success;
  59.         }
  60.         public bool TryParseUInt32(string sField, out uint i)
  61.         {
  62.             bool Success = uint.TryParse(this[sField], out i);
  63.             if (!Success)
  64.                 Console.WriteLine(sField);
  65.  
  66.             return Success;
  67.         }
  68.         public int TryParseInt32(string sField)
  69.         {
  70.             int i = 0; TryParseInt32(sField, out i);
  71.             return i;
  72.         }
  73.         public uint TryParseUInt32(string sField)
  74.         {
  75.             uint i = 0; TryParseUInt32(sField, out i);
  76.             return i;
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Loads a ConfigurationModule from a file at a given path.
  81.         /// </summary>
  82.         /// <param name="sPath">The path of the configuration file to load.</param>
  83.         /// <returns>ConfigurationModule holding the loaded configurations.</returns>
  84.         public static ConfigurationModule LoadFromFile(string sPath)
  85.         {
  86.             if (!File.Exists(sPath))
  87.                 throw new FileNotFoundException("File at path \"" + sPath + "\" does not exist.");
  88.  
  89.             ConfigurationModule pConfig = new ConfigurationModule();
  90.             using (StreamReader pReader = new StreamReader(sPath))
  91.             {
  92.                 string sLine = null;
  93.                 while ((sLine = pReader.ReadLine()) != null)
  94.                 {
  95.                     if (sLine.Length == 0 || sLine[0] == '#')
  96.                         continue; // This line is empty/a comment
  97.  
  98.                     int indexOfDelimiter = sLine.IndexOf('=');
  99.                     if (indexOfDelimiter != -1)
  100.                     {
  101.                         string sKey = sLine.Substring(0, indexOfDelimiter);
  102.                         if (!pConfig.mContent.ContainsKey(sKey))
  103.                         {
  104.                             string sValue = sLine.Substring(indexOfDelimiter + 1);
  105.                             pConfig.mContent.Add(sKey, sValue);
  106.                         }
  107.                     }
  108.                 }
  109.                 pReader.Close();
  110.             }
  111.  
  112.             return pConfig;
  113.         }
  114.         #endregion
  115.     }
  116. }
  117.  
  118. I get an error when debugging.
  119. "The namespace configuration already has a definition for ConfigurationModule
Advertisement
Add Comment
Please, Sign In to add comment