/// <summary> /// Merges an another INI file into this one. Other INI file's values are picked over current INI file's. /// </summary> /// <param name="iniFileToMerge">INI file to merge into this one.</param> public void Merge(INIFile iniFileToMerge) { foreach (INISection newSection in iniFileToMerge.iniSections) { if (newSection == null) { continue; } INISection section = iniSections.Find(i => i.Name == newSection.Name); if (section == null) { iniSections.Add(newSection); _altered = true; continue; } foreach (INIKeyValuePair newKvp in newSection.KeyValuePairs) { if (newKvp.Key == null) { continue; } INIKeyValuePair kvp = section.KeyValuePairs.Find(i => i.Key == newKvp.Key); if (kvp != null) { kvp.Value = newKvp.Value; List <INIComment> comments = newKvp.GetAllCommentsAtPosition(INICommentPosition.Before); comments.AddRange(newKvp.GetAllCommentsAtPosition(INICommentPosition.Middle)); comments.AddRange(newKvp.GetAllCommentsAtPosition(INICommentPosition.After)); foreach (INIComment comment in comments) { if (!kvp.HasComment(comment)) { kvp.AddComment(comment); } } _altered = true; } else { section.KeyValuePairs.Add(newKvp); _altered = true; } } } }
/// <summary> /// Gets INI key-value pair from line of text and adds it to the specified section. /// </summary> /// <param name="section">INI section to add the key-value pair to.</param> /// <param name="line">Line of text.</param> /// <returns>INI key-value pair that was added to the section.</returns> private INIKeyValuePair AddKeyValuePairFromLine(INISection section, string line) { string key = null; string value = null; string comments = null; int whiteSpaceCount = 0; string lineMod = line; if (lineMod.Contains(";")) { int index = lineMod.IndexOf(';'); comments = lineMod.Substring(index, line.Length - index); lineMod = line.Replace(comments, "").Trim(); whiteSpaceCount = line.Replace(lineMod, "").Replace(comments, "").Length; comments = comments.ReplaceFirst(";", ""); } if (line.Contains("=")) { key = lineMod.Substring(0, lineMod.IndexOf('=')).Trim(); value = lineMod.Substring(lineMod.IndexOf('=') + 1, lineMod.Length - lineMod.IndexOf('=') - 1).Trim(); } else { key = lineMod.Trim(); } INIKeyValuePair kvp = new INIKeyValuePair(key, value); INIKeyValuePair kvpMatch = section.KeyValuePairs.Find(x => x.Key == kvp.Key); if (kvpMatch == null) { section.KeyValuePairs.Add(kvp); } else { kvpMatch.Value = kvp.Value; kvp = kvpMatch; } if (comments != null) { INIComment iniComment = new INIComment(comments, INICommentPosition.Middle, whiteSpaceCount); if (!kvp.HasComment(iniComment)) { kvp.AddComment(iniComment); } } return(kvp); }