public EditorConfigFile(string file, Encoding?encoding = null, Version?developmentVersion = null) { if (string.IsNullOrWhiteSpace(file)) { throw new ArgumentException("The given path must be non-empty", nameof(file)); } if (!File.Exists(file)) { throw new ArgumentException($"The given file {file} does not exist", nameof(file)); } FullPath = file; _encoding = encoding ?? Encoding.Default; Directory = Path.GetDirectoryName(file); _lines.AddRange(File.ReadAllLines(FullPath, _encoding)); Global = IniSectionData.Global(); ParseVersion = developmentVersion ?? EditorConfigParser.Version; Parse(); FileConfiguration = new FileConfiguration(ParseVersion, file, Global.Properties.ToDictionary(p => p.Prop.Key, p => p.Prop.Value)); }
public IEnumerable <IniLine <IniPropertyData> > GetSectionProperties(IniSectionData section) { if (section is null) { throw new ArgumentNullException(nameof(section)); } return(section.Properties.Select(tuple => new IniLine <IniPropertyData>(GetLineNumber(section, tuple.Offset), tuple.Prop))); }
private void ResetTo(IEnumerable <string> lines) { _lines.Clear(); _sections.Clear(); _lines.AddRange(lines); Global = IniSectionData.Global(); Parse(); }
protected IniSectionData(IniSectionData section) : base(IniLineType.SectionHeader, section?.ExistingText) { if (section is null) { throw new ArgumentNullException(nameof(section)); } Name = section.Name; IsGlobal = section.IsGlobal; Lines.AddRange(section.Lines); }
public SectionEditContext AddSection(IniSectionData sectionData) { if (sectionData is null) { throw new ArgumentNullException(nameof(sectionData)); } var sectionEdit = new SectionEditContext(sectionData); Sections.Add(sectionData.Name, sectionEdit); return(sectionEdit); }
public bool TryGetProperty(string key, IniSectionData section, [NotNullWhen(true)] out IniLine <IniPropertyData>?property) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentException("message", nameof(key)); } if (section == null) { throw new ArgumentNullException(nameof(section)); } if (!section.TryGetProperty(key, out var propertyData, out var offset)) { property = null; return(false); } var lineNumber = GetLineNumber(section, offset); property = new IniLine <IniPropertyData>(lineNumber, propertyData); return(true); }
public bool TryGetComment(string commentText, IniSectionData section, [NotNullWhen(true)] out IniLine <IniCommentData>?comment) { if (string.IsNullOrWhiteSpace(commentText)) { throw new ArgumentException("message", nameof(commentText)); } if (section == null) { throw new ArgumentNullException(nameof(section)); } if (!section.TryGetComment(commentText, out var commentData, out var offset)) { comment = null; return(false); } var lineNumber = GetLineNumber(section, offset); comment = new IniLine <IniCommentData>(lineNumber, commentData); return(true); }
public SectionEditContext(IniSectionData section) : base(section) { Section = section ?? throw new ArgumentNullException(nameof(section)); }
private void Parse() { int currentLineNumber = 0; IniSectionData activeSection = Global; foreach (var line in _lines) { try { var matches = CommentRegex.Matches(line); if (matches.Count > 0) { IniCommentData iniComment = IniCommentData.FromLine(line); // We will discard any comments from before the first section activeSection.AddLine(iniComment); continue; } matches = PropertyRegex.Matches(line); if (matches.Count > 0) { var prop = new IniPropertyData(line); activeSection.AddLine(prop); continue; } matches = SectionRegex.Matches(line); if (matches.Count > 0) { var sectionName = matches[0].Groups[1].Value; activeSection = IniSectionData.FromLine(line); var iniSection = new IniLine <IniSectionData>(currentLineNumber, activeSection); _sections.Add(sectionName, iniSection); continue; } activeSection.AddLine(new IniEmptyLine()); } finally { currentLineNumber++; } } if (!TryGetProperty("root", Global, out var rootProp)) { return; } if (bool.TryParse(rootProp.Data.Value, out var isRoot)) { IsRoot = isRoot; } }
private int GetLineNumber(IniSectionData section, int offset) => GetSectionLineNumber(section) + offset + 1;
private int GetSectionLineNumber(IniSectionData section) => section.IsGlobal ? 0 : _sections[section.Name].LineNumber;