Deserialize code optimisations

1. reading ini files
2. regex match of section names.
This commit is contained in:
Brett Hewitson 2021-12-15 21:31:04 +10:00
parent f73b8c530d
commit 5c06fd00d5
2 changed files with 143 additions and 80 deletions

View file

@ -85,10 +85,14 @@ namespace ServerManagerTool.Common.Utils
public static IniFile ReadFromFile(string file)
{
if (string.IsNullOrWhiteSpace(file))
{
return null;
}
if (!File.Exists(file))
{
return new IniFile();
}
var iniFile = new IniFile();
@ -96,18 +100,24 @@ namespace ServerManagerTool.Common.Utils
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var line = reader.ReadLine().Trim();
if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";") || line.StartsWith("#"))
{
continue;
}
var sectionName = Regex.Match(line, @"(?<=^\[).*(?=\]$)").Value.Trim();
var sectionName = string.Empty;
if (line.StartsWith("[") && line.EndsWith("]"))
{
sectionName = Regex.Match(line, @"(?<=^\[).*(?=\]$)").Value.Trim();
}
var section = iniFile.AddSection(sectionName);
if (section != null)
continue;
iniFile.AddKey(line);
if (section is null)
{
iniFile.AddKey(line);
}
}
reader.Close();