/// <include file='../docs.xml' /// path='docs/doc[@name="M:PeterO.IniSection.Add(PeterO.IniEntry,PeterO.IniMergeBehavior)"]/*'/> public void Add(IniEntry entry, IniMergeBehavior behavior) { if (entry == null) { throw new ArgumentNullException("entry"); } if (!entry.IsComment) { var lastNonComment = -1; for (int i = 0; i < this.entries.Count; ++i) { if (behavior == IniMergeBehavior.Merge) { if (Object.Equals(this.entries[i].Key, entry.Key)) { this.entries[i] = entry; return; } } if (!this.entries[i].IsComment) { lastNonComment = i; } } // Insert after the last non-comment this.entries.Insert(lastNonComment + 1, entry); } else { this.entries.Add(entry); } }
/// <include file='../docs.xml' /// path='docs/doc[@name="M:PeterO.IniSection.AddEntry(System.String,System.String,PeterO.IniMergeBehavior)"]/*'/> public void AddEntry(string key, string value, IniMergeBehavior behavior) { if (key == null) { throw new ArgumentNullException("key"); } this.Add(new IniEntry(key, value), behavior); }
/// <include file='../docs.xml' /// path='docs/doc[@name="M:PeterO.IniFile.#ctor(System.String,PeterO.IniMergeBehavior)"]/*'/> public IniFile(string path, IniMergeBehavior behavior) { if (path == null) { throw new ArgumentNullException("path"); } // NOTE: Will merge sections with the same name into a single section this.sections = new List <IniSection>(); var currentSection = this.AddSection(null, behavior); using (var reader = new StreamReader(path)) { string line; while ((line = reader.ReadLine()) != null) { if (this.StartsWith(line, ';') || this.StartsWith(line, '#')) { // Found a comment currentSection.AddComment(line); continue; } if (this.StartsWith(line, '[')) { var endBracket = line.LastIndexOf(']'); if (endBracket > 0) { // Found new section var secName = line.Substring(1, endBracket - 1); currentSection = this.AddSection(secName, behavior); } } var equalSign = line.IndexOf('='); if (equalSign > 0) { // Found an entry var key = line.Substring(0, equalSign); var value = line.Substring(equalSign + 1); currentSection.AddEntry(key, value, behavior); } } } }
/// <include file='../docs.xml' /// path='docs/doc[@name="M:PeterO.IniFile.AddSection(System.String,PeterO.IniMergeBehavior)"]/*'/> public IniSection AddSection(string sectionName, IniMergeBehavior behavior) { if (behavior == IniMergeBehavior.Merge) { foreach (IniSection section in this.sections) { if (Object.Equals(section.Name, sectionName)) { return(section); } } } var newSection = new IniSection(sectionName); if (newSection == null) { this.sections.Insert(0, newSection); } else { this.sections.Add(newSection); } return(newSection); }