private void SetComment(IniElement el, string comment) { var index = Parent.Elements.IndexOf(el); if (IniSettings.CommentChars.Length == 0) { throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them."); } IniCommentary com; if (index > 0 && Parent.Elements[index - 1] is IniCommentary) { com = ((IniCommentary)Parent.Elements[index - 1]); if (comment == "") { Parent.Elements.Remove(com); } else { com.Comment = comment; com.Indentation = el.Indentation; } } else if (comment != "") { com = IniCommentary.FromComment(comment); com.Indentation = el.Indentation; Parent.Elements.Insert(index, com); } }
public static IniElement ParseLine(string line) { if (line == null) { return(null); } if (line.Contains("\n")) { throw new ArgumentException("String passed to the ParseLine method cannot contain more than one line."); } var trim = line.Trim(); IniElement elem = null; if (IniBlankLine.IsLineValid(trim)) { elem = new IniBlankLine(1); } else if (IniCommentary.IsLineValid(line)) { elem = new IniCommentary(line); } else if (IniSectionStart.IsLineValid(trim)) { elem = new IniSectionStart(line); } else if (IniValue.IsLineValid(trim)) { elem = new IniValue(line); } return(elem ?? new IniElement(line)); }
public List <IniElement> ReadSection() { if (_current == null || !(_current is IniSectionStart)) { throw new InvalidOperationException("The current position of the reader must be at IniSectionStart. Use GotoSection method"); } var ret = new List <IniElement>(); var theCurrent = _current; ret.Add(theCurrent); string text = "", temp; while ((temp = base.ReadLine()) != null) { if (IniSectionStart.IsLineValid(temp.Trim())) { _current = new IniSectionStart(temp); break; } text += temp + IniSettings.NewLine; } if (text.EndsWith(IniSettings.NewLine) && text != IniSettings.NewLine) { text = text.Substring(0, text.Length - IniSettings.NewLine.Length); } ret.AddRange(ParseText(text)); return(ret); }
private string GetComment(IniElement el) { var index = Parent.Elements.IndexOf(el); if (index != 0 && Parent.Elements[index - 1] is IniCommentary) { return(((IniCommentary)Parent.Elements[index - 1]).Comment); } return(""); }
public void WriteElement(IniElement element) { if (!IniSettings.PreserveFormatting) { element.FormatDefault(); } // do not write if: if (!( // 1) element is a blank line AND blank lines are not allowed (element is IniBlankLine && !IniSettings.AllowBlankLines) // 2) element is an empty value AND empty values are not allowed || (!IniSettings.AllowEmptyValues && element is IniValue && ((IniValue)element).Value == ""))) { base.WriteLine(element.Line); } }
public static List <IniElement> ParseText(string text) { if (text == null) { return(null); } var ret = new List <IniElement>(); IniElement currEl, lastEl = null; var lines = text.Split(new[] { IniSettings.NewLine }, StringSplitOptions.None); foreach (var line in lines) { currEl = ParseLine(line); if (IniSettings.GroupElements) { if (lastEl != null) { if (currEl is IniBlankLine && lastEl is IniBlankLine) { ((IniBlankLine)lastEl).LinesCount++; continue; } if (currEl is IniCommentary && lastEl is IniCommentary) { ((IniCommentary)lastEl).Comment += IniSettings.NewLine + ((IniCommentary)currEl).Comment; continue; } } else { lastEl = currEl; } } lastEl = currEl; ret.Add(currEl); } return(ret); }
public IniSectionStart GotoSection(string sectionName) { while (true) { var str = ReadLine(); if (str == null) { _current = null; return(null); } if (IniSectionStart.IsLineValid(str)) { var sect = ParseLine(str) as IniSectionStart; if (sect != null && (sect.SectionName == sectionName || (!IniSettings.CaseSensitive && sect.SectionName.ToLowerInvariant() == sectionName))) { _current = sect; return(sect); } } } }
public IniElement ReadNextElement() { _current = ParseLine(base.ReadLine()); return _current; }
private string GetComment(IniElement el) { var index = Parent.Elements.IndexOf(el); if (index != 0 && Parent.Elements[index - 1] is IniCommentary) return ((IniCommentary)Parent.Elements[index - 1]).Comment; return ""; }
private void SetComment(IniElement el, string comment) { var index = Parent.Elements.IndexOf(el); if (IniSettings.CommentChars.Length == 0) throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them."); IniCommentary com; if (index > 0 && Parent.Elements[index - 1] is IniCommentary) { com = ((IniCommentary)Parent.Elements[index - 1]); if (comment == "") Parent.Elements.Remove(com); else { com.Comment = comment; com.Indentation = el.Indentation; } } else if (comment != "") { com = IniCommentary.FromComment(comment); com.Indentation = el.Indentation; Parent.Elements.Insert(index, com); } }
public void WriteElement(IniElement element) { if (!IniSettings.PreserveFormatting) element.FormatDefault(); // do not write if: if (!( // 1) element is a blank line AND blank lines are not allowed (element is IniBlankLine && !IniSettings.AllowBlankLines) // 2) element is an empty value AND empty values are not allowed || (!IniSettings.AllowEmptyValues && element is IniValue && ((IniValue)element).Value == ""))) base.WriteLine(element.Line); }
public List<IniElement> ReadSection() { if (_current == null || !(_current is IniSectionStart)) throw new InvalidOperationException("The current position of the reader must be at IniSectionStart. Use GotoSection method"); var ret = new List<IniElement>(); var theCurrent = _current; ret.Add(theCurrent); string text = "", temp; while ((temp = base.ReadLine()) != null) { if (IniSectionStart.IsLineValid(temp.Trim())) { _current = new IniSectionStart(temp); break; } text += temp + IniSettings.NewLine; } if (text.EndsWith(IniSettings.NewLine) && text != IniSettings.NewLine) text = text.Substring(0, text.Length - IniSettings.NewLine.Length); ret.AddRange(ParseText(text)); return ret; }
public IniSectionStart GotoSection(string sectionName) { while (true) { var str = ReadLine(); if (str == null) { _current = null; return null; } if (IniSectionStart.IsLineValid(str)) { var sect = ParseLine(str) as IniSectionStart; if (sect != null && (sect.SectionName == sectionName || (!IniSettings.CaseSensitive && sect.SectionName.ToLowerInvariant() == sectionName))) { _current = sect; return sect; } } } }
public IniElement ReadNextElement() { _current = ParseLine(base.ReadLine()); return(_current); }