# HG changeset patch # User IBBoard # Date 1231792447 0 # Node ID 2dde4c1d19d940bc658075e268fd3b358335fc77 # Parent f9444f1786cdd8d343c0291f5426375b2ad92c43 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 diff -r f9444f1786cd -r 2dde4c1d19d9 IniFile.cs --- 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 @@ } } - + /// + /// Checks whether the IniFile contains an with the specified name + /// + /// + /// The name of the section to look for + /// + /// + /// true if the section is in the file, else false + /// public bool HasSection(string sectionName) { return sections.ContainsKey(sectionName.Trim()); } - + + /// + /// Gets an array of the s in the file + /// public IniSection[] Sections { get diff -r f9444f1786cd -r 2dde4c1d19d9 IniFileReader.cs --- 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)); } diff -r f9444f1786cd -r 2dde4c1d19d9 IniKeyValuePairLine.cs --- 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; } + //// + /// Gets the key part of the property (the part before the equals sign) + /// public string Key { get @@ -25,6 +28,9 @@ } } + /// + /// Gets the value part of the property (the part after the first equals sign) + /// public string Value { get diff -r f9444f1786cd -r 2dde4c1d19d9 IniSection.cs --- 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 { + /// + /// The IniSection holds a header from an INI file ("[header]") and all INI lines until either the end of the file + /// or to the next IniSection. + /// public class IniSection : IEnumerable { private string name; @@ -37,7 +41,10 @@ throw new ArgumentException("sectionName cannot be an empty string"); } } - + + /// + /// Gets the name of the INI section, as written between the square brackets + /// public string Name { get @@ -45,7 +52,10 @@ return name; } } - + + /// + /// Gets an array of the keys of all of the s in the IniSection + /// public string[] Keys { get @@ -62,7 +72,7 @@ } } - //// + /// /// Gets an by key, or null if the key doesn't exist /// public IniKeyValuePairLine this[string key] @@ -74,7 +84,11 @@ return keyValuePair; } } - + + /// + /// Adds an to the IniSection. If iniLine is also an + /// then it becomes available through this[key] + /// public void AddIniLine(IIniLine iniLine) { if (iniLine is IniKeyValuePairLine) diff -r f9444f1786cd -r 2dde4c1d19d9 IniSectionParser.cs --- 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 @@ /// public class IniSectionParser { + /// + /// Checks whether line appears to be the start of a new INI section + /// + /// + /// The string to check to see if it matches the INI section header format + /// + /// + /// true if the string looks like an INI section header, else false + /// public static bool IsLineStartOfNewSection(string line) { line = line.Trim(); return line.StartsWith("[") && line.EndsWith("]"); } - + + /// + /// Checks whether a string looks like an INI section by starting with a section header and zero or more lines + /// + /// + /// The string to check to see if it matches the INI section format + /// + /// + /// true if the string looks like an INI section, else false + /// 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); } /// /// Creates an IniSection object from a string. The new line (\n) character is used to determine line breaks. /// /// - /// A + /// The text of a section including the section header and any additional lines /// /// - /// A + /// An with the header and INI lines that were parsed from sectionString /// public static IniSection CreateSection(string sectionString) { diff -r f9444f1786cd -r 2dde4c1d19d9 InvalidIniSectionException.cs --- 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 { + /// + /// An exception to indicate that a string did not match the INI section format - see + /// public InvalidIniSectionException(string message) : base(message) { } diff -r f9444f1786cd -r 2dde4c1d19d9 NonExistantIniSection.cs --- 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 { + /// + /// A special case sub-class of for when 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 + /// public class NonExistantIniSection : IniSection - { - public NonExistantIniSection() : base("") + { + public NonExistantIniSection() : base("NonExistant") { } }