Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Data.InputOutput.XFile
- {
- public class INIFile
- {
- #region Field Region
- //Declare variables
- string
- fileAndPathLocation;
- Dictionary<string, Dictionary<string,string>>
- contents;
- Dictionary<string, string>
- keys;
- bool
- hasHeading;
- #endregion
- #region Property Region
- /// <summary>
- /// Gets the Full path and File Name of this INI File.
- /// </summary>
- public string FullFileName
- {
- get { return fileAndPathLocation; }
- private set { fileAndPathLocation = value; }
- }
- /// <summary>
- /// Gets just the Name of this INI File.
- /// </summary>
- public string FileName
- {
- get { return fileAndPathLocation.Substring(fileAndPathLocation.LastIndexOf("\\") + 1); }
- private set{fileAndPathLocation = fileAndPathLocation.Substring(0, fileAndPathLocation.LastIndexOf("\\") + 1) + value;}
- }
- #endregion
- #region Constructor Region
- public INIFile(string fileAndPathLocation)
- {
- //Declare variables
- string[] rawFileContentLines;
- string
- rawFileContentText,
- newLine;
- //Check the passed string isn't null
- if (fileAndPathLocation == null)
- //if it is, exit the function
- { return;/*/ERROR// STRING ISN'T VALID/*/}
- //Check that the file exists
- if (!System.IO.File.Exists(fileAndPathLocation))
- //if not exit the function
- { return; /*/ERROR// FILE DOESN'T EXIST/*/}
- //Set class variables
- this.fileAndPathLocation = fileAndPathLocation;
- //Get all the contents
- rawFileContentText = File.ReadAllText(fileAndPathLocation);
- //Get the newline character or characters
- newLine = rawFileContentText.Contains("\r\n") ?
- "\r\n" :
- "\n";
- //Get all the lines
- rawFileContentLines = rawFileContentText.Split(newLine.ToCharArray());
- //Check if there is any "Headings"
- hasHeading = rawFileContentText.Contains(']') && rawFileContentText.Contains('[');
- #region File Load WITH Menu Headings
- //If the file does have headings begin using the dictionary
- if (hasHeading)
- {
- //Declare variables
- string
- mainContents,
- menuName,
- value;
- string[]
- menuWithKeys,
- KeysInMenu,
- keyWithValue;
- //Setup the content dictionary
- contents = new Dictionary<string, Dictionary<string, string>>();
- //Remove all the blank spaces
- mainContents = rawFileContentText.Trim();
- //Split the parts into seperate menus
- menuWithKeys = mainContents.Split("[".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
- //Loop through the menus
- for (int menuIndex = 0; menuIndex < menuWithKeys.Length; menuIndex++)
- {
- //Get the current menu Name
- menuName = menuWithKeys[menuIndex].Substring(0, menuWithKeys[menuIndex].IndexOf(']'));
- //Make the sure the menu doesn't already exist
- if (contents.ContainsKey(menuName))
- {
- //Todo:
- //Let the user know that the menu name already exists
- //Continue to the next menu
- continue;
- }
- //Setup the current menu
- contents.Add(menuName, new Dictionary<string, string>());
- //Break the menu text up into keys and valeus
- KeysInMenu = menuWithKeys[menuIndex].Substring(menuName.Length).Split(newLine.ToCharArray());
- //Check if there were any keys
- if (KeysInMenu == null)
- {
- //Todo:
- //Let the user know that the menu had no keys/values
- //continue to the next menu
- continue;
- }
- //Loop throguh each key/value
- for (int keyIndex = 0; keyIndex < KeysInMenu.Length; keyIndex++)
- {
- //Check that there is a key on the left side of the = sign
- if (KeysInMenu[keyIndex].Trim().IndexOf('=') < 1)
- {
- //Todo:
- //Let the user know that the current "key" had no identifier
- //Continue to the next key
- continue;
- }
- //Get the key with value
- keyWithValue = KeysInMenu[keyIndex].Split('=');
- //Check if the current key already exists
- if (contents[menuName].ContainsKey(keyWithValue[0]))
- {
- //Todo:
- //Let the user know that the key already exists in the current menu
- //continue to next key
- continue;
- }
- //Check that there is a value on the right side of the = sign
- if (KeysInMenu[keyIndex].Trim().Replace(newLine, "").IndexOf('=') == KeysInMenu[keyIndex].Trim().Replace(newLine, "").Length - 1)
- {
- //Todo:
- //Let the user know that there is no value assigned to the current key
- //add the key with an empty value
- contents[menuName].Add(keyWithValue[0], "");
- //Continue to the next key
- continue;
- }
- //Get the value
- value = keyWithValue[1];
- //Incase part of the value contained an "=" loop through each value
- for (int valueIndex = 2; valueIndex < keyWithValue.Length; valueIndex++)
- {
- //Add the current value to the value string (with the missing "=")
- value += "=" + keyWithValue[valueIndex];
- }
- //Add the key with its value
- contents[menuName].Add(keyWithValue[0], value);
- }
- }
- }
- #endregion
- #region File Load WITHOUT Menu Headings
- //Todo:
- //Add the ability to have an ini file that has just keys and values (see 3dtexture.ini for an example)
- #endregion
- }
- #endregion
- #region Method Region
- public string GetValue(string menuName, string keyName)
- {
- //Check if the keys exist first
- if (!contents.ContainsKey(menuName))
- { return null; }
- if (!contents[menuName].ContainsKey(keyName))
- { return null; }
- //If the code has reached this far return the value
- return contents[menuName][keyName];
- }
- public string[] GetKeys(string menuName)
- {
- //Check if the menu exists
- if (!contents.ContainsKey(menuName))
- { return null; }
- if (contents[menuName].Keys.Count < 1)
- { return null; }
- //Return all the keys in the current menu
- return contents[menuName].Keys.ToArray<string>();
- }
- public string[] GetMenus()
- {
- //Check if there are any menus
- if (!hasHeading)
- { return null; }
- //return all the menu names
- return contents.Keys.ToArray<string>();
- }
- //Todo:
- //Add methods such as:
- //Get Values string[] - Return arrray of all the values found in the given menu
- //Get Keys string[] - Return array of all the keys found in the given menu
- //Get Menus string[] - Return array of all the menus found in this file
- //SetValue bool - Return whether the updating of a value within a key within a menu was valid
- //SetKeyName bool - return wehther the updating of a key's name within a menu was valid
- //SetMenuName bool - return wehther the updating of a menu's name within this file was valid
- //etc etc, save/load/gettext/lines so forth
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement