public static ACBrIniFile Parse(string iniData, Encoding encoding = null) { if (string.IsNullOrEmpty(iniData) || string.IsNullOrWhiteSpace(iniData)) { throw new ArgumentNullException(nameof(iniData)); } encoding = encoding ?? Encoding.GetEncoding("ISO-8859-1"); var iniFile = new ACBrIniFile { Encoding = encoding }; using (var reader = new StringReader(iniData)) { string line; var section = string.Empty; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (string.IsNullOrEmpty(line) || string.IsNullOrWhiteSpace(line)) { continue; } if (line.StartsWith(";")) { continue; } if (line.StartsWith("[")) { section = line.Substring(1, line.Length - 2); iniFile.sections.Add(new ACBrIniSection(iniFile, section)); } else { if (string.IsNullOrEmpty(section) || string.IsNullOrWhiteSpace(section)) { continue; } var iniSection = iniFile[section]; var idx = line.IndexOf('=', 0); if (idx < 0) { continue; } var key = line.Substring(0, idx); var value = idx >= line.Length - 1 ? "" : line.Substring(idx + 1); iniSection.Add(key, value); } } } return(iniFile); }
public static void WriteToIni <T>(this ACBrIniFile iniData, T obj, string sectionName) where T : class { if (obj == null) { return; } iniData.WriteToIni(typeof(T), obj, sectionName); }
public static void ReadFromIni <T>(this ACBrIniFile iniData, T obj, string secionName) where T : class { if (!iniData.Contains(secionName)) { return; } var section = iniData[secionName]; section.ReadFromINi(obj); }
public static object ReadFromIni(this ACBrIniFile iniData, Type tipo, string secionName) { if (!iniData.Contains(secionName)) { return(null); } var section = iniData[secionName]; var ret = Activator.CreateInstance(tipo); section.ReadFromINi(tipo, ret); return(ret); }
public static ACBrIniFile ParseIni(string iniData, Encoding encoding = null) { if (string.IsNullOrEmpty(iniData) || string.IsNullOrWhiteSpace(iniData)) { throw new ArgumentNullException(nameof(iniData)); } encoding = encoding ?? Encoding.GetEncoding("ISO-8859-1"); var iniFile = new ACBrIniFile { Encoding = encoding }; using (var reader = new StringReader(iniData)) { string line; var section = string.Empty; while ((line = reader.ReadLine()) != null) { line = line.Trim(); if (string.IsNullOrEmpty(line) || string.IsNullOrWhiteSpace(line)) { continue; } if (line.StartsWith(";")) { continue; } if (line.StartsWith("[")) { section = line.Substring(1, line.Length - 2); iniFile.sections.Add(new ACBrIniSection(iniFile, section)); } else { if (string.IsNullOrEmpty(section) || string.IsNullOrWhiteSpace(section)) { continue; } var iniSection = iniFile[section]; var properties = line.Split('='); iniSection.Add(properties[0], properties[1]); } } } return(iniFile); }
public static ACBrIniFile Load(Stream stream, Encoding encoding = null) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } encoding = encoding ?? Encoding.GetEncoding("ISO-8859-1"); var iniFile = new ACBrIniFile { Encoding = encoding }; using (var reader = new StreamReader(stream, iniFile.Encoding)) return(Parse(reader.ReadToEnd(), encoding)); }
public static void WriteToIni(this ACBrIniFile iniData, Type tipo, object obj, string sectionName) { if (obj == null) { return; } var sectionData = iniData.SingleOrDefault(x => x.Name == sectionName) ?? new ACBrIniSection(sectionName); sectionData.WriteToIni(tipo, obj); if (sectionData.Count > 0 && !iniData.Contains(sectionName)) { iniData.Add(sectionData); } }
public ACBrIniSection(ACBrIniFile parent, string name) { Parent = parent; Name = name; }
public static T ReadFromIni <T>(this ACBrIniFile iniData, string secionName) where T : class, new() { return((T)iniData.ReadFromIni(typeof(T), secionName)); }