public Config Parse(string path) { var configBuilder = new Config.Builder(); using (var fs = File.OpenRead(path)) using (var sr = new StreamReader(fs, Encoding.UTF8)) { var validSection = false; BaseSection.Builder currentSectionBuilder = null; while (!sr.EndOfStream) { var line = sr.ReadLine(); if (line.Length == 0) { continue; } var lineType = CheckLineType(line); if (lineType == LineType.COMMENT) { continue; } if (lineType == LineType.SECTION) { var sectionParser = new SectionParser(); validSection = sectionParser.TryParse(line, out var sectionBuilder); if (validSection) { sectionBuilder.AddToConfigBuilder(configBuilder); currentSectionBuilder = sectionBuilder; } } else if (lineType == LineType.PARAMETER) { if (validSection) { var kvParser = new KeyValueParser(); if (kvParser.TryParse(line, out var kv)) { currentSectionBuilder.SetValue(kv); } } } } } return(configBuilder.Build()); }
public bool TryParse(string line, out BaseSection.Builder sectionBuilder) { sectionBuilder = null; var body = comRegex.Replace(line.Trim(), string.Empty); var match = regex.Match(body); if (!match.Success) { return(false); } var name = match.Groups["name"].Value.Trim(); if (!int.TryParse(match.Groups["no"].Value.Trim(), out var no)) { no = 0; } switch (name) { case "section": sectionBuilder = new Section.SectionBuilder { Name = name, }; return(true); case "sec": sectionBuilder = new Sec.SecBuilder { Name = name, No = no }; return(true); default: return(false); } }