/// <summary> /// Parses a string containing valid ini data /// </summary> /// <param Name="iniDataString"> /// String with data /// </param> /// <returns> /// An <see cref="IniData"/> instance with the data contained in /// the <paramref Name="iniDataString"/> correctly parsed an structured. /// </returns> /// <exception cref="ParsingException"> /// Thrown if the data could not be parsed /// </exception> public IniData Parse(string iniDataString) { IniData iniData = new IniData(); iniData.Configuration = this.Configuration.Clone(); if (string.IsNullOrEmpty(iniDataString)) { return iniData; } _currentCommentListTemp.Clear(); _currentSectionNameTemp = null; try { foreach (var line in iniDataString.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) ProcessLine(line, iniData); } catch { if (Configuration.ThrowExceptionsOnError) throw; return null; } return (IniData)iniData.Clone(); }
/// <summary> /// Writes the ini data to a stream. /// </summary> /// <param Name="writer">A write stream where the ini data will be stored</param> /// <param Name="iniData">An <see cref="IniData"/> instance.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref Name="writer"/> is <c>null</c>. /// </exception> public void WriteData(StreamWriter writer, IniData iniData) { if (writer == null) throw new ArgumentNullException("reader"); writer.Write(iniData.ToString()); }
/// <summary> /// Creates a string from the INI data. /// </summary> /// <param Name="iniData">An <see cref="IniData"/> instance.</param> /// <returns> /// A formatted string with the contents of the /// <see cref="IniData"/> instance object. /// </returns> public string WriteString(IniData iniData) { return iniData.ToString(); }
/// <summary> /// Processes a string containing an ini key/value pair. /// </summary> /// <param Name="line"> /// The string to be processed /// </param> protected void ProcessKeyValuePair(string line, IniData currentIniData) { //string sectionToUse = _currentSectionNameTemp; // get key and value data string key = ExtractKey(line); string value = ExtractValue(line); // Check if we haven't read any section yet if (string.IsNullOrEmpty(_currentSectionNameTemp)) { if (!Configuration.AllowKeysWithoutSection) { throw new ParsingException("key value pairs must be enclosed in a section"); } AddKeyToKeyValueCollection(key, value, currentIniData.Global, "global"); } else { var currentSection = currentIniData.Sections.GetSectionData(_currentSectionNameTemp); AddKeyToKeyValueCollection(key, value, currentSection.Keys, _currentSectionNameTemp); } }
/// <summary> /// Proccess a string which contains an ini section. /// </summary> /// <param Name="line"> /// The string to be processed /// </param> protected void ProcessSection(string line, IniData currentIniData) { // Get section Name with delimiters from line... string sectionName = Configuration.SectionRegex.Match(line).Value.Trim(); // ... and remove section's delimiters to get just the Name sectionName = sectionName.Substring(1, sectionName.Length - 2).Trim(); // Check that the section's Name is not empty if (sectionName == string.Empty) { throw new ParsingException("Section Name is empty"); } // Temporally save section Name. _currentSectionNameTemp = sectionName; //Checks if the section already exists if (currentIniData.Sections.ContainsSection(sectionName)) { if (Configuration.AllowDuplicateSections) { return; } throw new ParsingException(string.Format("Duplicate section with Name '{0}' on line '{1}'", sectionName, line)); } // If the section does not exists, add it to the ini data currentIniData.Sections.AddSection(sectionName); // Save comments read until now and assign them to this section currentIniData.Sections.GetSectionData(sectionName).Comments = _currentCommentListTemp; _currentCommentListTemp.Clear(); }
/// <summary> /// Processes one line and parses the data found in that line /// (section or key/value pair who may or may not have comments) /// </summary> /// <param Name="currentLine">The string with the line to process</param> protected void ProcessLine(string currentLine, IniData currentIniData) { currentLine = currentLine.Trim(); // Extract comments from current line and store them in a tmp field if (LineContainsAComment(currentLine)) currentLine = ExtractComment(currentLine); // If the entire line is a comment now should be empty, // so no further processing is needed. if (currentLine == String.Empty) return; //Process sections if (LineMatchesASection(currentLine)) { ProcessSection(currentLine, currentIniData); return; } //Process keys if (LineMatchesAKeyValuePair(currentLine)) { ProcessKeyValuePair(currentLine, currentIniData); return; } if (Configuration.SkipInvalidLines) return; throw new ParsingException( "Unknown file format. Couldn't parse the line: '" + currentLine + "'."); }
public IniData(IniData ori): this((SectionDataCollection) ori.Sections) { Global = (KeyDataCollection) ori.Global.Clone(); Configuration = ori.Configuration.Clone(); }
/// <summary> /// Saves INI data to a file. /// </summary> /// <remarks> /// Creats an ASCII encoded file by default. /// </remarks> /// <param Name="fileName"> /// Name of the file. /// </param> /// <param Name="parsedData"> /// IniData to be saved as an INI file. /// </param> public void SaveFile(string fileName, IniData parsedData) { SaveFile(fileName, parsedData, System.Text.Encoding.ASCII); }
/// <summary> /// Saves INI data to a file. /// </summary> /// <param Name="fileName"> /// Name of the file. /// </param> /// <param Name="parsedData"> /// IniData to be saved as an INI file. /// </param> /// <param Name="fileEncoding"> /// Specifies the encoding used to create the file. /// </param> public void SaveFile(string fileName, IniData parsedData, System.Text.Encoding fileEncoding) { if (string.IsNullOrEmpty(fileName)) throw new ArgumentException("Bad filename."); if (parsedData == null) throw new ArgumentNullException("parsedData"); using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write)) { using (StreamWriter sr = new StreamWriter(fs, fileEncoding)) { WriteData(sr, parsedData); } } }
public IniData(IniData ori) : this((SectionDataCollection)ori.Sections) { Global = (KeyDataCollection)ori.Global.Clone(); Configuration = ori.Configuration.Clone(); }
public void Load() { iniLoc = Application.StartupPath + @"\settings.ini"; parser = new FileIniDataParser(); if (Exists) { iniData = parser.ReadFile(iniLoc); if (Int32.Parse(iniData["General"]["Version"]) != IniVersion) { TryDelete(); this.iniData = DefaultConfig; Save(); } } else { this.iniData = DefaultConfig; Save(); } }