Advertisement
Guest User

INIFile

a guest
Dec 15th, 2011
47
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.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace Data.InputOutput.XFile
  8. {
  9.     public class INIFile
  10.     {
  11.         #region Field Region
  12.  
  13.         //Declare variables
  14.         string
  15.             fileAndPathLocation;
  16.         Dictionary<string, Dictionary<string,string>>
  17.             contents;
  18.         Dictionary<string, string>
  19.             keys;
  20.         bool
  21.             hasHeading;
  22.  
  23.         #endregion
  24.  
  25.         #region Property Region
  26.  
  27.         /// <summary>
  28.         /// Gets the Full path and File Name of this INI File.
  29.         /// </summary>
  30.         public string FullFileName
  31.         {
  32.             get { return fileAndPathLocation; }
  33.             private set { fileAndPathLocation = value; }
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Gets just the Name of this INI File.
  38.         /// </summary>
  39.         public string FileName
  40.         {
  41.             get { return fileAndPathLocation.Substring(fileAndPathLocation.LastIndexOf("\\") + 1); }
  42.             private set{fileAndPathLocation = fileAndPathLocation.Substring(0, fileAndPathLocation.LastIndexOf("\\") + 1) + value;}
  43.         }
  44.  
  45.         #endregion
  46.  
  47.         #region Constructor Region
  48.  
  49.         public INIFile(string fileAndPathLocation)
  50.         {
  51.             //Declare variables
  52.             string[] rawFileContentLines;
  53.             string
  54.                 rawFileContentText,
  55.                 newLine;
  56.  
  57.             //Check the passed string isn't null
  58.             if (fileAndPathLocation == null)
  59.             //if it is, exit the function
  60.             { return;/*/ERROR// STRING ISN'T VALID/*/}
  61.  
  62.             //Check that the file exists
  63.             if (!System.IO.File.Exists(fileAndPathLocation))
  64.             //if not exit the function
  65.             { return; /*/ERROR// FILE DOESN'T EXIST/*/}
  66.  
  67.             //Set class variables
  68.             this.fileAndPathLocation = fileAndPathLocation;
  69.  
  70.             //Get all the contents
  71.             rawFileContentText = File.ReadAllText(fileAndPathLocation);
  72.  
  73.             //Get the newline character or characters
  74.             newLine =   rawFileContentText.Contains("\r\n") ?
  75.                 "\r\n" :
  76.                 "\n";
  77.  
  78.             //Get all the lines
  79.             rawFileContentLines = rawFileContentText.Split(newLine.ToCharArray());
  80.  
  81.             //Check if there is any "Headings"
  82.             hasHeading = rawFileContentText.Contains(']') && rawFileContentText.Contains('[');
  83.  
  84.             #region File Load WITH Menu Headings
  85.             //If the file does have headings begin using the dictionary
  86.             if (hasHeading)
  87.             {
  88.                 //Declare variables
  89.                 string
  90.                     mainContents,
  91.                     menuName,
  92.                     value;
  93.                 string[]
  94.                     menuWithKeys,
  95.                     KeysInMenu,
  96.                     keyWithValue;
  97.  
  98.                 //Setup the content dictionary
  99.                 contents = new Dictionary<string, Dictionary<string, string>>();
  100.  
  101.                 //Remove all the blank spaces
  102.                 mainContents = rawFileContentText.Trim();
  103.  
  104.                 //Split the parts into seperate menus
  105.                 menuWithKeys = mainContents.Split("[".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
  106.                
  107.                 //Loop through the menus
  108.                 for (int menuIndex = 0; menuIndex < menuWithKeys.Length; menuIndex++)
  109.                 {
  110.                     //Get the current menu Name
  111.                     menuName = menuWithKeys[menuIndex].Substring(0, menuWithKeys[menuIndex].IndexOf(']'));
  112.  
  113.                     //Make the sure the menu doesn't already exist
  114.                     if (contents.ContainsKey(menuName))
  115.                     {
  116.                         //Todo:
  117.                         //Let the user know that the menu name already exists
  118.  
  119.                         //Continue to the next menu
  120.                         continue;
  121.                     }
  122.  
  123.                     //Setup the current menu
  124.                     contents.Add(menuName, new Dictionary<string, string>());
  125.  
  126.                     //Break the menu text up into keys and valeus
  127.                     KeysInMenu = menuWithKeys[menuIndex].Substring(menuName.Length).Split(newLine.ToCharArray());
  128.  
  129.                     //Check if there were any keys
  130.                     if (KeysInMenu == null)
  131.                     {
  132.                         //Todo:
  133.                         //Let the user know that the menu had no keys/values
  134.  
  135.                         //continue to the next menu
  136.                         continue;
  137.                     }
  138.  
  139.                     //Loop throguh each key/value
  140.                     for (int keyIndex = 0; keyIndex < KeysInMenu.Length; keyIndex++)
  141.                     {
  142.                         //Check that there is a key on the left side of the = sign
  143.                         if (KeysInMenu[keyIndex].Trim().IndexOf('=') < 1)
  144.                         {
  145.                             //Todo:
  146.                             //Let the user know that the current "key" had no identifier
  147.  
  148.                             //Continue to the next key
  149.                             continue;
  150.                         }
  151.  
  152.                         //Get the key with value
  153.                         keyWithValue = KeysInMenu[keyIndex].Split('=');
  154.  
  155.                         //Check if the current key already exists
  156.                         if (contents[menuName].ContainsKey(keyWithValue[0]))
  157.                         {
  158.                             //Todo:
  159.                             //Let the user know that the key already exists in the current menu
  160.  
  161.                             //continue to next key
  162.                             continue;
  163.                         }
  164.  
  165.                         //Check that there is a value on the right side of the = sign
  166.                         if (KeysInMenu[keyIndex].Trim().Replace(newLine, "").IndexOf('=') == KeysInMenu[keyIndex].Trim().Replace(newLine, "").Length - 1)
  167.                         {
  168.                             //Todo:
  169.                             //Let the user know that there is no value assigned to the current key
  170.  
  171.                             //add the key with an empty value
  172.                             contents[menuName].Add(keyWithValue[0], "");
  173.  
  174.                             //Continue to the next key
  175.                             continue;
  176.                         }
  177.  
  178.                         //Get the value
  179.                         value = keyWithValue[1];
  180.  
  181.                         //Incase part of the value contained an "=" loop through each value
  182.                         for (int valueIndex = 2; valueIndex < keyWithValue.Length; valueIndex++)
  183.                         {
  184.                             //Add the current value to the value string (with the missing "=")
  185.                             value += "=" + keyWithValue[valueIndex];
  186.                         }
  187.  
  188.                         //Add the key with its value
  189.                         contents[menuName].Add(keyWithValue[0], value);
  190.                     }
  191.                 }
  192.             }
  193.             #endregion
  194.  
  195.             #region File Load WITHOUT Menu Headings
  196.  
  197.             //Todo:
  198.             //Add the ability to have an ini file that has just keys and values (see 3dtexture.ini for an example)
  199.  
  200.             #endregion
  201.         }
  202.  
  203.         #endregion
  204.  
  205.         #region Method Region
  206.  
  207.         public string GetValue(string menuName, string keyName)
  208.         {
  209.             //Check if the keys exist first
  210.             if (!contents.ContainsKey(menuName))
  211.             { return null; }
  212.             if (!contents[menuName].ContainsKey(keyName))
  213.             { return null; }
  214.  
  215.             //If the code has reached this far return the value
  216.             return contents[menuName][keyName];
  217.         }
  218.  
  219.         public string[] GetKeys(string menuName)
  220.         {
  221.             //Check if the menu exists
  222.             if (!contents.ContainsKey(menuName))
  223.             { return null; }
  224.             if (contents[menuName].Keys.Count < 1)
  225.             { return null; }
  226.  
  227.             //Return all the keys in the current menu
  228.             return contents[menuName].Keys.ToArray<string>();
  229.         }
  230.  
  231.         public string[] GetMenus()
  232.         {
  233.             //Check if there are any menus
  234.             if (!hasHeading)
  235.             { return null; }
  236.  
  237.             //return all the menu names
  238.             return contents.Keys.ToArray<string>();
  239.         }
  240.  
  241.         //Todo:
  242.         //Add methods such as:
  243.         //Get Values string[] - Return arrray of all the values found in the given menu
  244.         //Get Keys string[] - Return array of all the keys found in the given menu
  245.         //Get Menus string[] - Return array of all the menus found in this file
  246.         //SetValue bool - Return whether the updating of a value within a key within a menu was valid
  247.         //SetKeyName bool - return wehther the updating of a key's name within a menu was valid
  248.         //SetMenuName bool - return wehther the updating of a menu's name within this file was valid
  249.         //etc etc, save/load/gettext/lines so forth
  250.  
  251.         #endregion
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement