diff --git a/lib/INIFileParser.XML b/lib/INIFileParser.XML new file mode 100644 index 00000000..262ec7f3 --- /dev/null +++ b/lib/INIFileParser.XML @@ -0,0 +1,1105 @@ + + + + INIFileParser + + + + + Asserts that a strings has no blank spaces. + + The string to be checked. + + + + + Represents an INI data parser for files. + + + + + Represents an INI data parser for streams. + + + + + Ctor + + + + + Ctor + + + + + + Reads data in INI format from a stream. + + Reader stream. + + And instance with the readed ini data parsed. + + + Thrown if is null. + + + + + Writes the ini data to a stream. + + A write stream where the ini data will be stored + An instance. + + Thrown if is null. + + + + + This instance will handle ini data parsing and writing + + + + + Implements reading ini data from a file. + + + Uses codification for the file. + + + Path to the file + + + + + Implements reading ini data from a file. + + + Path to the file + + + File's encoding. + + + + + Saves INI data to a file. + + + Creats an ASCII encoded file by default. + + + Path to the file. + + + IniData to be saved as an INI file. + + + + + Writes INI data to a text file. + + + Path to the file. + + + IniData to be saved as an INI file. + + + Specifies the encoding used to create the file. + + + + + Represents all data from an INI file + + + + + Represents all sections from an INI file + + + + + Initializes an empty IniData instance. + + + + + Initializes a new IniData instance using a previous + . + + + object containing the + data with the sections of the file + + + + Creates a new object that is a copy of the current instance. + + + A new object that is a copy of this instance. + + + + + See property for more information. + + + + + Configuration used to write an ini file with the proper + delimiter characters and data. + + + If the instance was created by a parser, + this instance is a copy of the used + by the parser (i.e. different objects instances) + If this instance is created programatically without using a parser, this + property returns an instance of + + + + + Global sections. Contains key/value pairs which are not + enclosed in any section (i.e. they are defined at the beginning + of the file, before any section. + + + + + Gets the instance + with the specified section name. + + + + + Gets or sets all the + for this IniData instance. + + + + + Information associated to a key from an INI file. + Includes both the value and the comments associated to the key. + + + + + Initializes a new instance of the class. + + + + + Initializes a new instance of the class + from a previous instance of . + + + Data is deeply copied + + + The instance of the class + used to create the new instance. + + + + Creates a new object that is a copy of the current instance. + + + A new object that is a copy of this instance. + + + + + List with comment lines associated to this key + + + + + Unique value associated to this key + + + + + Name of the current key + + + + + Gets or sets the comment list associated to this key. + + + + + Gets or sets the value associated to this key. + + + + + Gets or sets the name of the key. + + + + + Represents a collection of Keydata. + + + + + Initializes a new instance of the class. + + + + + Initializes a new instance of the class + from a previous instance of . + + + Data is deeply copied + + + The instance of the class + used to create the new instance. + + + + Adds a new key with the specified name and empty value and comments + + + A valid key name is a string with NO blank spaces. + + New key to be added. + + true if a new empty key was added + false otherwise. + + If the key name is not valid. + + + + Adds a new key with the specified name and value and comments + + + A valid key name is a string with NO blank spaces. + + New key to be added. + KeyData instance. + + true if a new empty key was added + false otherwise. + + If the key name is not valid. + + + + Adds a new key with the specified name and value and comments + + + A valid key name is a string with NO blank spaces. + + New key to be added. + Value associated to the kyy. + + true if a new empty key was added + false otherwise. + + If the key name is not valid. + + + + Gets if a specifyed key name exists in the collection. + + Key name to search + true if a key with the specified name exists in the collectoin + false otherwise + + + + Retrieves the data for a specified key given its name + + Name of the key to retrieve. + + A instance holding + the key information or null if the key wasn't found. + + + + + Deletes all keys in this collection. + + + + + Deletes a previously existing key, including its associated data. + + The key to be removed. + + true if a key with the specified name was removed + false otherwise. + + + + + Sets the key data associated to a specified key. + + The new for the key. + + + + Allows iteration througt the collection. + + A strong-typed IEnumerator + + + + Implementation needed + + A weak-typed IEnumerator. + + + + Creates a new object that is a copy of the current instance. + + + A new object that is a copy of this instance. + + + + + Collection of KeyData for a given section + + + + + Gets or sets the value of a concrete key. + + + If we try to assign the value of a key which doesn't exists, + a new key is added with the name and the value is assigned to it. + + Name of the key + + The string with key's value or null + if the key was not found. + + + + + Return the number of keys in the collection + + An integer with the number of keys in the collection. + + + + Default values used if an instance of + is created without specifying a configuration. + + + By default the various delimiters for the data are setted: + ';' for one-line comments + '[' ']' for delimiting a section + '=' for linking key / value pairs + + An example of well formed data with the default values: + + ;section comment
+ [section] ; section comment
+
+ ; key comment
+ key = value ;key comment
+
+ ;key2 comment
+ key2 = value
+
+
+
+
+ + + Configuration data used for a class instance. + + + This class allows changing the behaviour of a instance. + The class exposes an instance of this class via + + + + + Defines data for a Parser configuration object. + + With a configuration object you can redefine how the parser + will detect special items in the ini file by defining new regex + (e.g. you can redefine the comment regex so it just treat text as + a comment iff the comment caracter is the first in the line) + or changing the set of characters used to define elements in + the ini file (e.g. change the 'comment' caracter from ';' to '#') + You can also define how the parser should treat errors, or how liberal + or conservative should it be when parsing files with "strange" formats. + + + + Clones this instance. + + + + + If set to false and the finds duplicate keys in a + section the parser will stop with an error. + If set to true, duplicated keys are allowed in the file. The value + of the duplicate key will be the last value asigned to the key in the file. + + + Defaults to false. + + + + + If set to false and the finds a duplicate section + the parser will stop with an error. + If set to true, duplicated sections are allowed in the file, but only a + element will be created in the + collection. + + + Defaults to false. + + + + + Allows having keys in the file that don't belong to any section. + i.e. allows defining keys before defining a section. + If set to false and keys without a section are defined, + the will stop with an error. + + + Defaults to true. + + + + + Sets the string around KeyValuesAssignmentChar + + + Defaults to string ' ' + + + + + Regular expression used to match a comment string + + + + + Sets the char that defines the start of a comment. + A comment spans from the comment character to the end of the line. + + + Defaults to character '#' + + + + + Sets the char that defines a value assigned to a key + + + Defaults to character '=' + + + + + Only used if is also true + If set to true when the parser finds a duplicate key, it overrites + the previous value, so the key will always contain the value of the + last key read in the file + If set to false the first value is preserved, so the key will + always contain the value of the first key read in the file + + + Defaults to false. + + + + + Sets the char that defines the end of a section name. + + + Defaults to character ']' + + + + + Regular expression used to match a section string + + + + + Sets the char that defines the start of a section name. + + + Defaults to character '[' + + + + + IF set to true, when the parser finds an invalid line, it just skips it + instead of throwing a or returning null when parsing + (if exceptions are disabled because the property + is set to false). + + + Defaults to false + + + + + If true the instance will thrown an exception + if an error is found. + If false the parser will just stop execution and return a null value. + + + Defaults to true. + + + + + Ctor. + + + + + Copy ctor. + + + Original instance to be copied. + + + + + Creates a new object that is a copy of the current instance. + + + A new object that is a copy of this instance. + + 2 + + + + Regular expression for matching a comment string + + + + + Regular expression for matching a section string + + + + + Sets the char that defines the start of a section name. + + + Defaults to character '[' + + + + + Sets the char that defines the end of a section name. + + + Defaults to character ']' + + + + + Sets the char that defines the start of a comment. + A comment spans from the comment character to the end of the line. + + + Defaults to character '#' + + + + + Sets the char that defines a value assigned to a key + + + Defaults to character '=' + + + + + Sets the string around KeyValuesAssignmentChar + + + Defaults to string ' ' + + + + + Allows having keys in the file that don't belong to any section. + i.e. allows defining keys before defining a section. + If set to false and keys without a section are defined, + the will stop with an error. + + + Defaults to true. + + + + + If set to false and the finds duplicate keys in a + section the parser will stop with an error. + If set to true, duplicated keys are allowed in the file. The value + of the duplicate key will be the last value asigned to the key in the file. + + + Defaults to false. + + + + + Only used if is also true + If set to true when the parser finds a duplicate key, it overrites + the previous value, so the key will always contain the value of the + last key readed in the file + If set to false the first readed value is preserved, so the key will + always contain the value of the first key readed in the file + + + Defaults to false. + + + + + If true the instance will thrown an exception + if an error is found. + If false the parser will just stop execution and return a null value. + + + Defaults to true. + + + + + If set to false and the finds a duplicate section + the parser will stop with an error. + If set to true, duplicated sections are allowed in the file, but only a + element will be created in the + collection. + + + Defaults to false. + + + + + Ctor. + + + + + Responsible for parsing an string from an ini file, and creating + an structure. + + + + + Ctor + + + The parser uses a by default + + + + + Ctor + + + Parser's instance. + + + + + Parses a string containing valid ini data + + + String with data + + + An instance with the data contained in + the correctly parsed an structured. + + + Thrown if the data could not be parsed + + + + + Checks if a given string contains a comment. + + + The string to be checked. + + + true if any substring from s is a comment, false otherwise. + + + + + Checks if a given string represents a section delimiter. + + + The string to be checked. + + + true if the string represents a section, false otherwise. + + + + + Checks if a given string represents a key / value pair. + + + The string to be checked. + + + true if the string represents a key / value pair, false otherwise. + + + + + Removes a comment from a string if exist, and returns the string without + the comment substring. + + + The string we want to remove the comments from. + + + The string s without comments. + + + + + Processes one line and parses the data found in that line + (section or key/value pair who may or may not have comments) + + The string with the line to process + + + + Proccess a string which contains an ini section. + + + The string to be processed + + + + + Processes a string containing an ini key/value pair. + + + The string to be processed + + + + + Extracts the key portion of a string containing a key/value pair.. + + + The string to be processed, which contains a key/value pair + + + The name of the extracted key. + + + + + Extracts the value portion of a string containing a key/value pair.. + + + The string to be processed, which contains a key/value pair + + + The name of the extracted value. + + + + + Adds a key to a concrete instance, checking + if duplicate keys are allowed in the configuration + + + Key name + + + Key's value + + + collection where the key should be inserted + + + Name of the section where the is contained. + Used only for logging purposes. + + + + + Temp list of comments + + + + + Tmp var with the name of the seccion which is being process + + + + + Configuration that defines the behaviour and constraints + that the parser must follow. + + + + + Information associated to a section in a INI File + Includes both the value and the comments associated to the key. + + + + + Initializes a new instance of the class. + + + + + Initializes a new instance of the class + from a previous instance of . + + + Data is deeply copied + + + The instance of the class + used to create the new instance. + + + + Creates a new object that is a copy of the current instance. + + + A new object that is a copy of this instance. + + + + + Comments associated to this section + + + + + Keys associated to this section + + + + + Name of the u + + + + + Gets or sets the name of the section. + + The name of the section. + + + + Gets or sets the comment list associated to this section. + + A list of strings. + + + + Gets or sets the keys associated to this section. + + A collection of KeyData objects. + + + + Represents a collection of SectionData. + + + + + Initializes a new instance of the class. + + + + + Initializes a new instance of the class + from a previous instance of . + + + Data is deeply copied + + + The instance of the class + used to create the new instance. + + + + Creates a new section with empty data. + + + If a section with the same name exists, this operation has no effect. + + Name of the section to be created + true if the a new section with the specified name was added, + false otherwise + If the section name is not valid. + + + + Gets if a section with a specified name exists in the collection. + + Name of the section to search + + true if a section with the specified name exists in the + collection false otherwise + + + + + Returns the section data from a specify section given its name. + + Name of the section. + + An instance of a class + holding the section data for the currently INI data + + + + + Sets the section data for given a section name. + + + The new instance. + + + + + + + true if the section with the specified name was removed, + false otherwise + + + + Removes all entries from this collection + + + + + Returns an enumerator that iterates through the collection. + + + A that can be used to iterate through the collection. + + + + + Returns an enumerator that iterates through a collection. + + + An object that can be used to iterate through the collection. + + + + + Creates a new object that is a copy of the current instance. + + + A new object that is a copy of this instance. + + + + + Data associated to this section + + + + + Returns the number of SectionData elements in the collection + + + + + Gets the key data associated to a specified section name. + + An instance of as class + holding the key data from the current parsed INI data, or a null + value if the section doesn't exist. + + + + Represents an INI data parser for strings. + + + + This class is deprecated and kept for backwards compatibility. + It's just a wrapper around class. + Please, replace your code. + + + + + Ctor + + + + + Ctor + + + + + + Parses a string containing data formatted as an INI file. + + The string containing the data. + + A new instance with the data parsed from the string. + + + + + Creates a string from the INI data. + + An instance. + + A formatted string with the contents of the + instance object. + + + + + This instance will handle ini data parsing and writing + + + + + Represents an error ococcurred while parsing data + + + + + Initializes a new instance of the class. + + The message describing the exception cause. + + + + Initializes a new instance of the class. + + The message describing the exception cause. + An inner exception. + +
+
diff --git a/src/IniFileParser.Tests/Unit/Configuration/ConfigurationTests.cs b/src/IniFileParser.Tests/Unit/Configuration/ConfigurationTests.cs index 1c804643..13d20150 100644 --- a/src/IniFileParser.Tests/Unit/Configuration/ConfigurationTests.cs +++ b/src/IniFileParser.Tests/Unit/Configuration/ConfigurationTests.cs @@ -4,7 +4,6 @@ using IniParser.Parser; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; -using IniParser.Model.Formatting; namespace IniFileParser.Tests.Unit.Configuration { @@ -103,6 +102,7 @@ public void simple_configuration() public void check_configuration_is_correct() { Assert.That(_parser.Configuration, Is.InstanceOfType(typeof (LiberalTestConfiguration))); + Assert.That(_parser.Parse(iniFileStr).Configuration, Is.InstanceOfType(typeof(LiberalTestConfiguration))); } [Test] @@ -154,13 +154,10 @@ public void parse_ini_with_new_configuration() [Test] public void check_ini_writing() { - var configuration = new LiberalTestConfiguration(); - IniData data = new IniDataParser(configuration).Parse(iniFileStr); + IniData data = new IniDataParser(new LiberalTestConfiguration()).Parse(iniFileStr); - var formatter = new DefaultIniDataFormatter(configuration); - Assert.That( - data.ToString(formatter).Replace(Environment.NewLine, string.Empty), + data.ToString().Replace(Environment.NewLine, string.Empty), Is.EqualTo(iniFileStr.Replace(Environment.NewLine, string.Empty))); } } diff --git a/src/IniFileParser.Tests/issues/Issue32Tests.cs b/src/IniFileParser.Tests/issues/Issue32Tests.cs index 31ebdb2e..abe58a7e 100644 --- a/src/IniFileParser.Tests/issues/Issue32Tests.cs +++ b/src/IniFileParser.Tests/issues/Issue32Tests.cs @@ -1,7 +1,6 @@ using IniParser; using IniParser.Model; using NUnit.Framework; -using IniParser.Model.Formatting; namespace IniFileParser.Tests.issues { @@ -15,15 +14,13 @@ public void write_key_and_value_without_blanks() var data = new IniData(); data.Sections.AddSection("section1"); data["section1"].AddKey("key1", "value1"); + data.Configuration.AssigmentSpacer = ""; - var formatter = new DefaultIniDataFormatter(); - formatter.Configuration.AssigmentSpacer = ""; - var parser = new StreamIniDataParser(); string tempPath = System.IO.Path.GetTempFileName(); using (var sw = new System.IO.StreamWriter(tempPath)) { - parser.WriteData(sw, data, formatter); + parser.WriteData(sw, data); } Assert.AreEqual(@"[section1] diff --git a/src/IniFileParser/Model/IniData.cs b/src/IniFileParser/Model/IniData.cs index ab330dfe..8a9a113a 100644 --- a/src/IniFileParser/Model/IniData.cs +++ b/src/IniFileParser/Model/IniData.cs @@ -39,14 +39,39 @@ public IniData(SectionDataCollection sdc) Global = new KeyDataCollection(); } - public IniData(IniData ori): this(ori.Sections) + public IniData(IniData ori): this((SectionDataCollection) ori.Sections) { Global = (KeyDataCollection) ori.Global.Clone(); + Configuration = ori.Configuration.Clone(); } #endregion #region Properties + /// + /// Configuration used to write an ini file with the proper + /// delimiter characters and data. + /// + /// + /// If the instance was created by a parser, + /// this instance is a copy of the used + /// by the parser (i.e. different objects instances) + /// If this instance is created programatically without using a parser, this + /// property returns an instance of + /// + public IIniParserConfiguration Configuration + { + get + { + // Lazy initialization + if (_configuration == null) + _configuration = new DefaultIniParserConfiguration(); + + return _configuration; + } + + set { _configuration = value.Clone(); } + } /// /// Global sections. Contains key/value pairs which are not @@ -56,7 +81,7 @@ public IniData(IniData ori): this(ori.Sections) public KeyDataCollection Global { get; private set; } /// - /// Gets the instance + /// Gets the instance /// with the specified section name. /// public KeyDataCollection this[string sectionName] @@ -84,7 +109,7 @@ public SectionDataCollection Sections #region Object Methods public override string ToString() { - return this.ToString(new DefaultIniDataFormatter(new DefaultIniParserConfiguration())); + return ToString(new DefaultIniDataFormatter(Configuration)); } @@ -111,5 +136,11 @@ public object Clone() #endregion + #region Fields + /// + /// See property for more information. + /// + private IIniParserConfiguration _configuration; + #endregion } } \ No newline at end of file diff --git a/src/IniFileParser/Model/IniData.cs.rej b/src/IniFileParser/Model/IniData.cs.rej new file mode 100644 index 00000000..0410730d --- /dev/null +++ b/src/IniFileParser/Model/IniData.cs.rej @@ -0,0 +1,48 @@ +diff a/src/IniFileParser/Model/IniData.cs b/src/IniFileParser/Model/IniData.cs (rejected hunks) +@@ -36,45 +36,20 @@ + public IniData(SectionDataCollection sdc) + { + _sections = (SectionDataCollection)sdc.Clone(); + Global = new KeyDataCollection(); + } + +- public IniData(IniData ori): this((SectionDataCollection) ori.Sections) ++ public IniData(IniData ori): this(ori.Sections) + { + Global = (KeyDataCollection) ori.Global.Clone(); +- Configuration = ori.Configuration.Clone(); + } + #endregion + + #region Properties + +- /// +- /// Configuration used to write an ini file with the proper +- /// delimiter characters and data. +- /// +- /// +- /// If the instance was created by a parser, +- /// this instance is a copy of the used +- /// by the parser (i.e. different objects instances) +- /// If this instance is created programatically without using a parser, this +- /// property returns an instance of +- /// +- public IIniParserConfiguration Configuration +- { +- get +- { +- // Lazy initialization +- if (_configuration == null) +- _configuration = new DefaultIniParserConfiguration(); +- +- return _configuration; +- } +- +- set { _configuration = (IIniParserConfiguration) value.Clone(); } +- } + + /// + /// Global sections. Contains key/value pairs which are not + /// enclosed in any section (i.e. they are defined at the beginning + /// of the file, before any section. + /// diff --git a/src/IniFileParser/Parser/IniDataParser.cs b/src/IniFileParser/Parser/IniDataParser.cs index 229da125..0d6b3cf0 100644 --- a/src/IniFileParser/Parser/IniDataParser.cs +++ b/src/IniFileParser/Parser/IniDataParser.cs @@ -64,6 +64,7 @@ public IniDataParser(IIniParserConfiguration parserConfiguration) public IniData Parse(string iniDataString) { IniData iniData = new IniData(); + iniData.Configuration = this.Configuration.Clone(); if (string.IsNullOrEmpty(iniDataString)) {