public static void Merge([NotNull] this IniSection current, [NotNull] IniSection other, IniMergeOption options) { foreach (var otherEntry in other.Entries) { var currentEntry = current.GetEntry(otherEntry.GetName()); if (currentEntry is null) { current.Entries.Add(otherEntry); } else { currentEntry.Merge(otherEntry, options); } } if ((options & IniMergeOption.OverwriteComments) != 0) { current.Comment.AddRange(other.Comment); } }
[CanBeNull] public static IniEntry GetEntry([NotNull] this IniSection section, [NotNull] string key) => GetEntry(section, key, out _);
public static IniFile Read([NotNull] Func <string> readLine) { var ini = new IniFile(); List <string> comment = new List <string>(); IniSection section = new IniSection(); int start = 0; string line; while ((line = readLine()) != null) { int state = 0; for (int i = 0; i < line.Length; i++) { var c = line[i]; switch (state) { case 0: if (c == ';' || c == '#') // comment { comment.Add(line); goto nextLine; } if (c == '[') // start of section line { state = 1; start = i; break; } if (char.IsWhiteSpace(c)) { continue; // continue looking for the start of the line } state = 2; // probably a entry start = i; goto case 2; // in case the key is a empty string case 1: if (c == ']') // end of section line { if (section.Name is object || section.Entries.Count > 0) { ini.Sections.Add(section); } section = new IniSection() { Name = line, Comment = comment, Start = start, End = i }; comment = new List <string>(); goto nextLine; } break; case 2: if (c == '=') // line has values => valid entry { section.Entries.Add(new IniEntry() { Entry = line, Comment = comment, Start = start, Separator = i }); comment = new List <string>(); goto nextLine; } break; } } // line did not parse as anything => either empty or invalid => treat as comment comment.Add(line); nextLine :; } if (section.Name is object || section.Entries.Count > 0) { ini.Sections.Add(section); } ini.EofComment = comment; return(ini); }
public static string GetName([NotNull] this IniSection section) { return(section.Name?.Substring(section.Start + 1, section.End - section.Start - 1)); }
public static string GetValue([NotNull] this IniSection section, [NotNull] string key) { return(section.GetEntry(key)?.GetValue()); }