private static void CatalogIniNeeded() { if (CatalogIni == null) { CatalogIni = new PersistentIni(Path.Combine(SettingsManager.SpecialFolders.DesktopIni, "catalog.ini")); CatalogIni.Read(); } if (CatalogSection == null) { CatalogSection = CatalogIni[SectionDesktopIni]; if (CatalogSection == null) { CatalogSection = CatalogIni.AddSection(SectionDesktopIni); } } }
/// <summary> /// Adds or sets the value of the property. /// </summary> /// <param name="section">The section of the property.</param> /// <param name="key">The key of the property.</param> /// <param name="value">The property value.</param> public void Put(string section, string key, string value) { IniSection sect = null; if (p_Sections.Contains(section)) { sect = this.GetSection(section); } else { if (p_Sections.Count > 0) { IniSection lastSect = (IniSection)p_Sections[p_Sections.Count - 1]; // Insert empy line? if (!string.IsNullOrEmpty(lastSect[lastSect.Count - 1].Value)) { lastSect.Add(new IniProperty(section, IniType.EmptyLine, "")); } } sect = new IniSection(section); p_Sections.Add(section, sect); } if (sect.Contains(key)) { sect.Get(key).Value = value; } else { sect.Add(new IniProperty(section, key, value)); } }
/// <summary> /// Loads the properties from the configuration file. /// </summary> public void Load() { if (!File.Exists(this.Filename)) return; this.p_Sections.Clear(); using (var reader = new StreamReader(this.Filename)) { string key = string.Empty; string line; int lineCount = 0; IniSection section = this.GetSection(SectionHeader); while ((line = reader.ReadLine()) != null) { lineCount++; line = line.Trim(); // Empty line. if (string.IsNullOrEmpty(line)) { key = IniType.EmptyLine.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) section.Add(new IniProperty(section.Name, IniType.EmptyLine, line)); else section.Add(new IniProperty(section.Name, IniType.EmptyLine, key, line)); } else { char firstChar = char.Parse(line.Substring(0, 1)); switch (firstChar) { case ';': case '#': // Comment line key = IniType.Comment.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) section.Add(new IniProperty(section.Name, IniType.Comment, line)); else section.Add(new IniProperty(section.Name, IniType.Comment, key, line)); break; case '[': // Section line if (!line.EndsWith("]")) // Line doesn't end with a closing bracket. goto default; else { int start = line.IndexOf('[') + 1; int length = line.IndexOf(']') - line.IndexOf('[') - 1; string name = line.Substring(start, length); if (p_Sections.Contains(name)) { section = (IniSection)p_Sections[name]; } else { section = new IniSection(name); p_Sections.Add(name, section); } } break; default: // Valid property line if (line.Contains("=") && section.Name != SectionHeader) { string[] split = line.Split('='); string value = split[1]; key = split[0]; if (section.Contains(split[0])) throw new Exception(string.Format("Section '{0}' already contains property '{1}'. Value: {2}", section.Name, key, value)); section.Add(new IniProperty(section.Name, key, value)); } else // Invalid line { key = IniType.Invalid.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) section.Add(new IniProperty(section.Name, IniType.Invalid, line)); else section.Add(new IniProperty(section.Name, IniType.Invalid, key, line)); } break; } } } } }
/// <summary> /// Returns a read-only list of all sections. /// </summary> public ICollection<IniSection> GetSections() { IniSection[] sections = new IniSection[this.p_Sections.Count]; this.p_Sections.Values.CopyTo(sections, 0); return Array.AsReadOnly<IniSection>(sections); }
/// <summary> /// Loads the properties from the configuration file. /// </summary> public void Load() { if (!File.Exists(this.Filename)) { return; } this.p_Sections.Clear(); using (var reader = new StreamReader(this.Filename)) { string key = string.Empty; string line; int lineCount = 0; IniSection section = this.GetSection(SectionHeader); while ((line = reader.ReadLine()) != null) { lineCount++; line = line.Trim(); // Empty line. if (string.IsNullOrEmpty(line)) { key = IniType.EmptyLine.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) { section.Add(new IniProperty(section.Name, IniType.EmptyLine, line)); } else { section.Add(new IniProperty(section.Name, IniType.EmptyLine, key, line)); } } else { char firstChar = char.Parse(line.Substring(0, 1)); switch (firstChar) { case ';': case '#': // Comment line key = IniType.Comment.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) { section.Add(new IniProperty(section.Name, IniType.Comment, line)); } else { section.Add(new IniProperty(section.Name, IniType.Comment, key, line)); } break; case '[': // Section line if (!line.EndsWith("]")) { // Line doesn't end with a closing bracket. goto default; } else { int start = line.IndexOf('[') + 1; int length = line.IndexOf(']') - line.IndexOf('[') - 1; string name = line.Substring(start, length); if (p_Sections.Contains(name)) { section = (IniSection)p_Sections[name]; } else { section = new IniSection(name); p_Sections.Add(name, section); } } break; default: // Valid property line if (line.Contains("=") && section.Name != SectionHeader) { string[] split = line.Split('='); string value = split[1]; key = split[0]; if (section.Contains(split[0])) { throw new Exception(string.Format("Section '{0}' already contains property '{1}'. Value: {2}", section.Name, key, value)); } section.Add(new IniProperty(section.Name, key, value)); } else // Invalid line { key = IniType.Invalid.ToString() + lineCount; // Use key above if available, otherwise generate random if (section.Contains(key)) { section.Add(new IniProperty(section.Name, IniType.Invalid, line)); } else { section.Add(new IniProperty(section.Name, IniType.Invalid, key, line)); } } break; } } } } }
public Enumerator(IniSection l) { this.l = l; }
/// <summary> /// Reads the whole document. /// </summary> public void ReadDocument(IniDocument document) { if (document == null) { throw new ArgumentNullException("document"); } // Tartalom törlése if (this.ClearDocument) { document.Sections.Clear(); document.CommentLines.Clear(); } // Nullázás this.CurrentLineNumber = this.CurrentLinePosition = 0; this.CurrentLineText = String.Empty; IniSection currentSection = null; while (true) { // Újabb sor beolvasása this.CurrentLineText = reader.ReadLine(); // Megszakítás if (this.CurrentLineText == null) { break; } this.CurrentLineNumber++; this.CurrentLinePosition = 0; string trimmedLine = this.CurrentLineText.TrimStart(); // Üres sor if (trimmedLine.Length == 0) { continue; } // Szekció beolvasása if (trimmedLine.StartsWith("[")) { // Előző szekció hozzáadása a dokumentumhoz if (currentSection != null) { document.Sections.Add(currentSection); } this.CurrentLinePosition = this.CurrentLineText.IndexOf('[') + 1; // Új szekció hozzáadása currentSection = new IniSection(this.ReadSectionHeader()); // Hiányzó ] if (this.ReadChar() != ']') { throw new IniReadException("Missing ']'.", this.CurrentLineNumber, this.CurrentLinePosition); } } // Komment beolvasása else if (trimmedLine.StartsWith(";")) { this.CurrentLinePosition = this.CurrentLineText.IndexOf(';'); if (currentSection != null) { currentSection.CommentLines.Add(this.ReadComment()); } else { document.CommentLines.Add(this.ReadComment()); } } // Paraméter beolvasása else { // White space átugrása this.ReadWhiteSpace(); // Név string name = this.ReadParameterName(); if (name.Length == 0) { throw new IniReadException("Parameter name is missing.", this.CurrentLineNumber, this.CurrentLinePosition); } this.ReadWhiteSpace(); // Név/érték elválasztó if (this.ReadChar() != this.SyntaxDefinition.NameValueDelimiter) { throw new IniReadException("Parameter name can contain only alphabetical and numerical characters.", this.CurrentLineNumber, this.CurrentLinePosition); } this.ReadWhiteSpace(); // Érték string value = this.ReadParameterValue(); if (value.Length == 0) { throw new IniReadException("Parameter value is missing.", this.CurrentLineNumber, this.CurrentLinePosition); } this.ReadWhiteSpace(); // Komment string description = null; if (this.ReadChar() == this.SyntaxDefinition.CommentStartChar) { description = this.ReadComment(); } // Új paraméter hozzáadása currentSection.Parameters.Add( new IniParameter(name, value) { Description = description } ); } } if (currentSection != null) { document.Sections.Add(currentSection); } }