public void WriteTo(StreamWriter writer) { foreach (IniEntity entity in _entities) { IniKeyValue ikv = entity as IniKeyValue; if (ikv != null) { writer.Write($"{ikv.Key}{IniKeyValue.KeyValueSeparator}{ikv.Value}"); if (ikv.Comment != null) { writer.Write(" "); writer.Write(IniComment.CommentSeparator); writer.Write(ikv.Comment.Value); } writer.WriteLine(); continue; } IniComment comment = entity as IniComment; if (comment != null) { writer.Write(IniComment.CommentSeparator); writer.WriteLine(comment.Value); } } }
public IniKeyValue Set(string key, string value) { if (value == null) { IniKeyValue ikv; if (_keyToValue.TryGetValue(key, out ikv)) { _keyToValue.Remove(key); return(ikv); } return(null); } else { IniKeyValue ikv; if (_keyToValue.TryGetValue(key, out ikv)) { ikv.Value = value; } else { ikv = new IniKeyValue(key, value, null); Add(ikv); } return(ikv); } }
public void Add(IniEntity entity) { _entities.Add(entity); IniKeyValue ikv = entity as IniKeyValue; if (ikv != null) { _keyToValue[ikv.Key] = ikv; } }
public static StructuredIniFile FromStream(Stream inputStream, bool parseInlineComments = true) { if (inputStream == null) { throw new ArgumentNullException(nameof(inputStream)); } var file = new StructuredIniFile(); using (var reader = new StreamReader(inputStream)) { IniSection section = file._globalSection; string line; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (line.StartsWith(_sectionBegin)) { //start new section line = line.Trim(); section = new IniSection(line); file._sections.Add(section); } else if (line.StartsWith(IniComment.CommentSeparator)) { //whole line is a comment string comment = line.Substring(1).Trim(); section.Add(new IniComment(comment)); } else { IniKeyValue ikv = IniKeyValue.FromLine(line, parseInlineComments); if (ikv == null) { continue; } section.Add(ikv); string fullKey = section.Name == null ? ikv.Key : $"{section.Name}{IniSection.SectionKeySeparator}{ikv.Key}"; file._fullKeyNameToValue[fullKey] = ikv; } } } return(file); }
public string this[string key] { get { if (key == null) { return(null); } IniKeyValue value; return(!_fullKeyNameToValue.TryGetValue(key, out value) ? null : value.Value); } set { if (key == null) { return; } string sectionName; string keyName; IniSection.SplitKey(key, out sectionName, out keyName); IniSection section = sectionName == null ? _globalSection : _sections.FirstOrDefault(s => s.Name == sectionName); if (section == null) { section = new IniSection(sectionName); _sections.Add(section); } IniKeyValue ikv = section.Set(keyName, value); //update the local cache if (ikv != null) { if (value == null) { _fullKeyNameToValue.Remove(key); } else { _fullKeyNameToValue[key] = ikv; } } } }