/// <summary> /// Return the section with the given name, creating it if necessary. /// </summary> /// <param name="name">The name of the section.</param> /// <returns></returns> public IniFileSection FindOrCreateSection(string name) { IniFileSection section; if (!SectionsByName.TryGetValue(name, out section)) { section = new IniFileSection(this, name); } return(section); }
/// <summary>Initialise the setting.</summary> /// <param name="section"></param> /// <param name="name"></param> /// <param name="value"></param> public IniFileSetting(IniFileSection section, string name, string value) { if (section == null) throw new ArgumentNullException("section"); if (name == null) throw new ArgumentNullException("name"); if (value == null) throw new ArgumentNullException("value"); Section = section; Name = name; Value = value; IniFileSetting first; // Insert this into section.SettingsByName using the duplicate behavior setting. if (!section.SettingsByName.TryGetValue(name, out first)) section.SettingsByName[name] = this; else { switch (File.Behavior.Duplicates) { case IniFileDuplicateBehavior.AllowAll: case IniFileDuplicateBehavior.AllowChooseFirst: for (var last = first; ; last = last.NextSettingWithSameName) if (last.NextSettingWithSameName == null) { last.NextSettingWithSameName = this; break; } break; case IniFileDuplicateBehavior.Abort: throw new InvalidOperationException("This IniFile may not have duplicate keys within a section."); case IniFileDuplicateBehavior.AllowChooseLast: NextSettingWithSameName = first; section.SettingsByName[name] = this; break; case IniFileDuplicateBehavior.IgnoreChooseFirst: return; case IniFileDuplicateBehavior.IgnoreChooseLast: first.Remove(); section.SettingsByName[name] = this; break; default: throw new Exception(); } } Section.Settings.Add(this); }
/// <summary> /// Return the section with the given name, creating it if necessary. /// </summary> /// <param name="name">The name of the section.</param> /// <returns></returns> public IniFileSection FindOrCreateSection(string name) { IniFileSection section; if (!SectionsByName.TryGetValue(name, out section)) section = new IniFileSection(this, name); return section; }
/// <summary>Initialise the setting.</summary> /// <param name="section"></param> /// <param name="name"></param> /// <param name="value"></param> public IniFileSetting(IniFileSection section, string name, string value) { if (section == null) { throw new ArgumentNullException("section"); } if (name == null) { throw new ArgumentNullException("name"); } if (value == null) { throw new ArgumentNullException("value"); } Section = section; Name = name; Value = value; IniFileSetting first; // Insert this into section.SettingsByName using the duplicate behavior setting. if (!section.SettingsByName.TryGetValue(name, out first)) { section.SettingsByName[name] = this; } else { switch (File.Behavior.Duplicates) { case IniFileDuplicateBehavior.AllowAll: case IniFileDuplicateBehavior.AllowChooseFirst: for (var last = first; ; last = last.NextSettingWithSameName) { if (last.NextSettingWithSameName == null) { last.NextSettingWithSameName = this; break; } } break; case IniFileDuplicateBehavior.Abort: throw new InvalidOperationException("This IniFile may not have duplicate keys within a section."); case IniFileDuplicateBehavior.AllowChooseLast: NextSettingWithSameName = first; section.SettingsByName[name] = this; break; case IniFileDuplicateBehavior.IgnoreChooseFirst: return; case IniFileDuplicateBehavior.IgnoreChooseLast: first.Remove(); section.SettingsByName[name] = this; break; default: throw new Exception(); } } Section.Settings.Add(this); }
/// <summary>Initialise the file, loading it from the system.</summary> /// <param name="path"></param> /// <param name="behavior"></param> public IniFile(string path, IniFileBehavior behavior) : this(behavior) { var lines = File.ReadAllLines(path, Encoding.ASCII); IniFileSection section = null; for (var index = 0; index < lines.Length; index++) { var line = lines[index].Trim(); if (line.Length == 0 || line[0] == Behavior.CommentDelimiter) { continue; } if (line[0] == '[') { int end = line.IndexOf(']'); if (end < 0) { throw new Exception("Expected ']' to terminate a section name."); } section = FindOrCreateSection(line.Substring(1, end - 1)); } else { var comment = -1; var split = line.IndexOf(behavior.SettingValueDelimiter); string name, value; if (Behavior.CommentInValue) { comment = line.IndexOf(behavior.CommentDelimiter); if (behavior.CommentDelimiter2 != null) { comment = Extensions.GetSmallerIndex(comment, line.IndexOf(behavior.CommentDelimiter2)); } } if (split < 0 && comment >= 0) { for (int chIndex = 0; chIndex < comment; chIndex++) { if (!char.IsWhiteSpace(line[chIndex])) { throw new Exception("This line has non-whitespace before a comment, but is not a setting."); } } continue; } if (split < 0 || (comment >= 0 && comment < split)) { throw new Exception("This line cannot be parsed as a setting, but it's the only thing it could be."); } name = line.Substring(0, split).TrimEnd(); if (comment >= 0) { value = line.Substring(split + 1, comment - split - 1).Trim(); } else { value = line.Substring(split + 1).Trim(); } new IniFileSetting(section, name, value); } } }