Mercurial > repos > IBBoard.Ini
changeset 2:2dde4c1d19d9
Closes #6 - Create INI parser
* Add more documentation
* Change NonExistantIniSection so that the constructor won't cause an exception
* Fix IniFileReader so that the reading from string actually reads from the parameter
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 12 Jan 2009 20:34:07 +0000 |
parents | f9444f1786cd |
children | 3c26f463977a |
files | IniFile.cs IniFileReader.cs IniKeyValuePairLine.cs IniSection.cs IniSectionParser.cs InvalidIniSectionException.cs NonExistantIniSection.cs |
diffstat | 7 files changed, 69 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/IniFile.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/IniFile.cs Mon Jan 12 20:34:07 2009 +0000 @@ -73,12 +73,23 @@ } } - + /// <summary> + /// Checks whether the IniFile contains an <see cref=" IniSection"/> with the specified name + /// </summary> + /// <param name="sectionName"> + /// The name of the section to look for + /// </param> + /// <returns> + /// <code>true</code> if the section is in the file, else <code>false</code> + /// </returns> public bool HasSection(string sectionName) { return sections.ContainsKey(sectionName.Trim()); } - + + /// <summary> + /// Gets an array of the <see cref=" IniSection"/>s in the file + /// </summary> public IniSection[] Sections { get
--- a/IniFileReader.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/IniFileReader.cs Mon Jan 12 20:34:07 2009 +0000 @@ -12,7 +12,7 @@ { public static IniFile ReadFile(string iniFileContent) { - MemoryStream memoryStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes("test")); + MemoryStream memoryStream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(iniFileContent)); return LoadIniFileFromStream(new StreamReader(memoryStream)); }
--- a/IniKeyValuePairLine.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/IniKeyValuePairLine.cs Mon Jan 12 20:34:07 2009 +0000 @@ -17,6 +17,9 @@ this.valueString = valueString; } + //// <value> + /// Gets the key part of the property (the part before the equals sign) + /// </value> public string Key { get @@ -25,6 +28,9 @@ } } + /// <summary> + /// Gets the value part of the property (the part after the first equals sign) + /// </summary> public string Value { get
--- a/IniSection.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/IniSection.cs Mon Jan 12 20:34:07 2009 +0000 @@ -9,6 +9,10 @@ namespace IBBoard.Ini { + /// <summary> + /// The <code>IniSection</code> holds a header from an INI file ("[header]") and all INI lines until either the end of the file + /// or to the next <code>IniSection</code>. + /// </summary> public class IniSection : IEnumerable<IIniLine> { private string name; @@ -37,7 +41,10 @@ throw new ArgumentException("sectionName cannot be an empty string"); } } - + + /// <value> + /// Gets the name of the INI section, as written between the square brackets + /// </value> public string Name { get @@ -45,7 +52,10 @@ return name; } } - + + /// <value> + /// Gets an array of the keys of all of the <see cref=" IniKeyValuePairLine"/>s in the <code>IniSection</code> + /// </value> public string[] Keys { get @@ -62,7 +72,7 @@ } } - //// <value> + /// <value> /// Gets an <see cref=" IniKeyValuePairLine"/> by key, or <code>null</code> if the key doesn't exist /// </value> public IniKeyValuePairLine this[string key] @@ -74,7 +84,11 @@ return keyValuePair; } } - + + /// <summary> + /// Adds an <see cref=" IIniLine"/> to the <code>IniSection</code>. If <code>iniLine</code> is also an <see cref=" IniKeyValuePairLine"/> + /// then it becomes available through <code>this[key]</code> + /// </summary> public void AddIniLine(IIniLine iniLine) { if (iniLine is IniKeyValuePairLine)
--- a/IniSectionParser.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/IniSectionParser.cs Mon Jan 12 20:34:07 2009 +0000 @@ -12,25 +12,43 @@ /// </summary> public class IniSectionParser { + /// <summary> + /// Checks whether <code>line</code> appears to be the start of a new INI section + /// </summary> + /// <param name="line"> + /// The string to check to see if it matches the INI section header format + /// </param> + /// <returns> + /// <code>true</code> if the string looks like an INI section header, else <code>false</code> + /// </returns> public static bool IsLineStartOfNewSection(string line) { line = line.Trim(); return line.StartsWith("[") && line.EndsWith("]"); } - + + /// <summary> + /// Checks whether a string looks like an INI section by starting with a section header and zero or more lines + /// </summary> + /// <param name="potentialSection"> + /// The string to check to see if it matches the INI section format + /// </param> + /// <returns> + /// <code>true</code> if the string looks like an INI section, else <code>false</code> + /// </returns> public static bool IsStringAnIniSection(string potentialSection) { - return Regex.IsMatch(potentialSection, "^\\s*\\[[a-zA-Z0-9]+\\]\\s*\\n", RegexOptions.Multiline); + return Regex.IsMatch(potentialSection, "^\\s*\\[[a-zA-Z0-9]+\\]\\s*(\\n|$)", RegexOptions.Multiline); } /// <summary> /// Creates an IniSection object from a string. The new line (\n) character is used to determine line breaks. /// </summary> /// <param name="sectionString"> - /// A <see cref="System.String"/> + /// The text of a section including the section header and any additional lines /// </param> /// <returns> - /// A <see cref="IniSection"/> + /// An <see cref="IniSection"/> with the header and INI lines that were parsed from <code>sectionString</code> /// </returns> public static IniSection CreateSection(string sectionString) {
--- a/InvalidIniSectionException.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/InvalidIniSectionException.cs Mon Jan 12 20:34:07 2009 +0000 @@ -8,6 +8,9 @@ { public class InvalidIniSectionException : Exception { + /// <summary> + /// An exception to indicate that a string did not match the INI section format - see <see cref=" IniSection"/> + /// </summary> public InvalidIniSectionException(string message) : base(message) { }
--- a/NonExistantIniSection.cs Sun Jan 11 15:37:14 2009 +0000 +++ b/NonExistantIniSection.cs Mon Jan 12 20:34:07 2009 +0000 @@ -6,9 +6,13 @@ namespace IBBoard.Ini { + /// <summary> + /// A special case sub-class of <see cref=" IniSection"/> for when <see cref=" IniFile"/> does not contain the required section. + /// Used to reduce "if not null" checks as they won't contain any lines or properties, as recommended by Rober C Martin's "Cleaner Code" book + /// </summary> public class NonExistantIniSection : IniSection - { - public NonExistantIniSection() : base("") + { + public NonExistantIniSection() : base("NonExistant") { } }