Advertisement
tolikpunkoff

INI PARSER, the class for INI-files, no API

Feb 28th, 2016
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.10 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. namespace SomeNameNamespace //zamenite na svojo
  7. {
  8.     public class ini_parser
  9.     {
  10.         private Hashtable keyPairs = new Hashtable();
  11.         private String iniFilePath;
  12.  
  13.         private struct SectionPair
  14.         {
  15.             public String Section;
  16.             public String Key;
  17.         }
  18.         /// <summary>
  19.         /// Opens the INI file at the given path and enumerates the values in the IniParser.
  20.         /// </summary>
  21.         /// <param name="iniPath">Full path to INI file.</param>
  22.         public ini_parser(String iniPath)
  23.         {
  24.             TextReader iniFile = null;
  25.             String strLine = null;
  26.             String currentRoot = null;
  27.             String[] keyPair = null;
  28.             String comment = "#";
  29.             this.iniFilePath = iniPath;
  30.  
  31.             if (File.Exists(iniPath))
  32.             {
  33.                 try
  34.                 {
  35.                     iniFile = new StreamReader(iniPath);
  36.  
  37.                     strLine = iniFile.ReadLine();
  38.  
  39.                     while (strLine != null)
  40.                     {
  41.                         strLine = strLine.Trim();
  42.  
  43.                         if (strLine != "")
  44.                         {
  45.                             if (strLine.StartsWith("[") && strLine.EndsWith("]"))
  46.                             {
  47.                                 currentRoot = strLine.Substring(1, strLine.Length - 2);
  48.                             }
  49.                             else
  50.                             {
  51.                                 if (!strLine.StartsWith(comment))
  52.                                 {
  53.                                     keyPair = strLine.Split(new char[] { '=' }, 2);
  54.  
  55.                                     SectionPair sectionPair;
  56.                                     String value = null;
  57.  
  58.                                     if (currentRoot == null)
  59.                                         currentRoot = "ROOT";
  60.  
  61.                                     sectionPair.Section = currentRoot;
  62.                                     sectionPair.Key = keyPair[0];
  63.  
  64.                                     if (keyPair.Length > 1)
  65.                                         value = keyPair[1];
  66.  
  67.                                     this.keyPairs.Add(sectionPair, value);
  68.                                 }
  69.                             }
  70.                         }
  71.  
  72.                         strLine = iniFile.ReadLine();
  73.                     }
  74.  
  75.                 }
  76.                 catch (Exception ex)
  77.                 {
  78.                     throw ex;
  79.                 }
  80.                 finally
  81.                 {
  82.                     if (iniFile != null)
  83.                         iniFile.Close();
  84.                 }
  85.             }
  86.             else
  87.             //       throw new FileNotFoundException("Unable to locate " + iniPath);
  88.             //Взял смелость допилить, если файла нет - создается пустой
  89.             {
  90.                 try
  91.                 {
  92.                     TextWriter tw = new StreamWriter(iniPath);
  93.                     tw.Write("");
  94.                     tw.Close();
  95.                 }
  96.                 catch (Exception ex)
  97.                 {
  98.                     throw ex;
  99.                 }
  100.             }
  101.  
  102.         }
  103.         /// <summary>
  104.         /// Returns the value for the given section, key pair.
  105.         /// </summary>
  106.         /// <param name="sectionName">Section name.</param>
  107.         /// <param name="settingName">Key name.</param>
  108.         public String GetSetting(String sectionName, String settingName)
  109.         {
  110.             SectionPair sectionPair;
  111.             sectionPair.Section = sectionName.ToUpper();
  112.             sectionPair.Key = settingName.ToUpper();
  113.             return (String)this.keyPairs[sectionPair];
  114.         }
  115.         /// <summary>
  116.         /// Enumerates all lines for given section.
  117.         /// </summary>
  118.         /// <param name="sectionName">Section to enum.</param>
  119.         public String[] EnumSection(String sectionName)
  120.         {
  121.             ArrayList tmpArray = new ArrayList();
  122.  
  123.             foreach (SectionPair pair in this.keyPairs.Keys)
  124.             {
  125.                 if (pair.Section == sectionName.ToUpper())
  126.                     tmpArray.Add(pair.Key);
  127.             }
  128.  
  129.             return (String[])tmpArray.ToArray(typeof(String));
  130.         }
  131.         /// <summary>
  132.         /// Enumerates all key-value pairs for given section.
  133.         /// </summary>
  134.         /// <param name="sectionName">Section to enum.</param>
  135.         /// <returns>Dictionary equals to INI-section.</returns>
  136.         public Dictionary<String, String> EnumSectionKeyValue(String sectionName)
  137.         {
  138.             Dictionary<String, String> ret = new Dictionary<String, String>();
  139.  
  140.             foreach (SectionPair pair in this.keyPairs.Keys)
  141.             {
  142.                 if (pair.Section == sectionName.ToUpper())
  143.                 {
  144.                     ret.Add(pair.Key, this.GetSetting(sectionName, pair.Key));
  145.                 }
  146.             }
  147.             return ret;
  148.         }
  149.         /// <summary>
  150.         /// Enumarates all section-key pairs equal given value.
  151.         /// </summary>
  152.         /// <param name="value">Value to enum.</param>
  153.         /// <returns>Dictionary.</returns>
  154.         public Dictionary<String, String> EnumValue(String value)
  155.         {
  156.             Dictionary<String, String> ret = new Dictionary<String, String>();
  157.             foreach (SectionPair pair in this.keyPairs.Keys)
  158.             {
  159.                 if (this.GetSetting(pair.Section, pair.Key) == value)
  160.                 {
  161.                     ret.Add(pair.Section, pair.Key);
  162.                 }
  163.             }
  164.             return ret;
  165.         }
  166.         /// <summary>
  167.         /// Adds or replaces a setting to the table to be saved.
  168.         /// </summary>
  169.         /// <param name="sectionName">Section to add under.</param>
  170.         /// <param name="settingName">Key name to add.</param>
  171.         /// <param name="settingValue">Value of key.</param>
  172.         public void AddSetting(String sectionName, String settingName, String settingValue)
  173.         {
  174.             SectionPair sectionPair;
  175.             sectionPair.Section = sectionName.ToUpper();
  176.             sectionPair.Key = settingName.ToUpper();
  177.  
  178.             if (this.keyPairs.ContainsKey(sectionPair))
  179.                 this.keyPairs.Remove(sectionPair);
  180.  
  181.             this.keyPairs.Add(sectionPair, settingValue);
  182.         }
  183.         /// <summary>
  184.         /// Adds or replaces a setting to the table to be saved with a null value.
  185.         /// </summary>
  186.         /// <param name="sectionName">Section to add under.</param>
  187.         /// <param name="settingName">Key name to add.</param>
  188.         public void AddSetting(String sectionName, String settingName)
  189.         {
  190.             AddSetting(sectionName, settingName, null);
  191.         }
  192.         /// <summary>
  193.         /// Remove a setting.
  194.         /// </summary>
  195.         /// <param name="sectionName">Section to add under.</param>
  196.         /// <param name="settingName">Key name to add.</param>
  197.         public void DeleteSetting(String sectionName, String settingName)
  198.         {
  199.             SectionPair sectionPair;
  200.             sectionPair.Section = sectionName.ToUpper();
  201.             sectionPair.Key = settingName.ToUpper();
  202.  
  203.             if (this.keyPairs.ContainsKey(sectionPair))
  204.                 this.keyPairs.Remove(sectionPair);
  205.         }
  206.         /// <summary>
  207.         /// Save settings to new file.
  208.         /// </summary>
  209.         /// <param name="newFilePath">New file path.</param>
  210.         public void SaveSettings(String newFilePath)
  211.         {
  212.             ArrayList sections = new ArrayList();
  213.             String tmpValue = String.Empty;
  214.             String strToSave = String.Empty;
  215.  
  216.             foreach (SectionPair sectionPair in this.keyPairs.Keys)
  217.             {
  218.                 if (!sections.Contains(sectionPair.Section))
  219.                     sections.Add(sectionPair.Section);
  220.             }
  221.  
  222.             foreach (String section in sections)
  223.             {
  224.                 strToSave += ("[" + section + "]\r\n");
  225.  
  226.                 foreach (SectionPair sectionPair in this.keyPairs.Keys)
  227.                 {
  228.                     if (sectionPair.Section == section)
  229.                     {
  230.                         tmpValue = (String)this.keyPairs[sectionPair];
  231.  
  232.                         if (tmpValue != null)
  233.                             tmpValue = "=" + tmpValue;
  234.  
  235.                         strToSave += (sectionPair.Key + tmpValue + "\r\n");
  236.                     }
  237.                 }
  238.  
  239.                 strToSave += "\r\n";
  240.             }
  241.  
  242.             try
  243.             {
  244.                 TextWriter tw = new StreamWriter(newFilePath);
  245.                 tw.Write(strToSave);
  246.                 tw.Close();
  247.             }
  248.             catch (Exception ex)
  249.             {
  250.                 throw ex;
  251.             }
  252.         }
  253.         /// <summary>
  254.         /// Save settings back to ini file.
  255.         /// </summary>
  256.         public void SaveSettings()
  257.         {
  258.             SaveSettings(this.iniFilePath);
  259.         }
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement