// Loads the Reads the data in the ini file into the IniFile object public void Load(string sFileName, bool bMerge) { if (sFileName == null) { return; } FilePath = sFileName; if (!bMerge) { RemoveAllSections(); } // Clear the object... IniSection tempsection = null; StreamReader oReader = new StreamReader(sFileName, Encoding.Default); Regex regexcomment = new Regex("^([\\s]*//.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); // ^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$ Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); //Regex regexsection = new Regex("\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\]", (RegexOptions.Singleline | RegexOptions.IgnoreCase) ); Regex regexkey = new Regex(@"\s*(\S*[^=]+\S*)\s*=\s*(\S+.*\S*)\s*", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); while (!oReader.EndOfStream) { string line = oReader.ReadLine(); if (line != string.Empty) { Match m = null; if (regexcomment.Match(line).Success) { m = regexcomment.Match(line); //Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value) ); } else if (regexsection.Match(line).Success) { m = regexsection.Match(line); //Trace.WriteLine(string.Format("Adding section [{0}]", m.Groups[1].Value) ); tempsection = AddSection(m.Groups[1].Value); } else if (regexkey.Match(line).Success&& tempsection != null) { m = regexkey.Match(line); //Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value) ); tempsection.AddKey(m.Groups[1].Value.Trim()).Value = m.Groups[2].Value.Trim(); } else if (tempsection != null) { // Handle Key without value //Trace.WriteLine(string.Format("Adding Key [{0}]", line) ); tempsection.AddKey(line); } else { // This should not occur unless the tempsection is not created yet... //Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line) ); } } } oReader.Close(); }
public IniFile LoadString(string str, bool bMerge = false, string section_pre_expresion = "") { if (!bMerge) { RemoveAllSections(); } changed = false; // Clear the object... IniSection tempsection = null; Regex regexcomment = new Regex("^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexsection = new Regex("\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\]", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexkey = new Regex("^\\s*([^=\\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); var Lines = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in Lines) { if (line != String.Empty) { Match m = null; if (regexcomment.Match(line).Success) { m = regexcomment.Match(line); // Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value)); } else if (regexsection.Match(line).Success) { m = regexsection.Match(line); // Trace.WriteLine(string.Format("Adding section [{0}]", section_pre_expresion + m.Groups[1].Value)); tempsection = AddSection(section_pre_expresion + m.Groups[1].Value); } else if (regexkey.Match(line).Success&& tempsection != null) { m = regexkey.Match(line); // Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value)); tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value; } else if (tempsection != null) { // Handle Key without value // Trace.WriteLine(string.Format("Adding Key [{0}]", line)); tempsection.AddKey(line); } else { // This should not occur unless the tempsection is not created yet... // Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line)); } } } if (!bMerge) { changed = false; } return(this); }
/// <summary> /// Loads the Reads the data in the ini file into the IniFile object /// </summary> public void Load(string sFileName, bool bMerge = false) { if (!bMerge) { RemoveAllSections(); } // Clear the object... IniSection tempsection = null; StreamReader oReader = new StreamReader(sFileName); Regex regexcomment = new Regex("^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); // Broken but left for history //Regex regexsection = new Regex("\[[\s]*([^\[\s].*[^\s\]])[\s]*\]", (RegexOptions.Singleline Or RegexOptions.IgnoreCase)) Regex regexsection = new Regex(@"^[\s]*\[[\s]*([^\[\s].*[^\s\]])[\s]*\][\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexkey = new Regex(@"^\s*([^=\s]*)[^=]*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); while (!oReader.EndOfStream) { string line = oReader.ReadLine(); if (line != string.Empty) { Match m; if (regexcomment.Match(line).Success) { m = regexcomment.Match(line); Trace.WriteLine(string.Format("Skipping Comment: {0}", m.Groups[0].Value)); } else if (regexsection.Match(line).Success) { m = regexsection.Match(line); Trace.WriteLine(string.Format("Adding section [{0}]", m.Groups[1].Value)); tempsection = AddSection(m.Groups[1].Value); } else if (regexkey.Match(line).Success&& tempsection != null) { m = regexkey.Match(line); Trace.WriteLine(string.Format("Adding Key [{0}]=[{1}]", m.Groups[1].Value, m.Groups[2].Value)); tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value; } else if (tempsection != null) { // Handle Key without value Trace.WriteLine(string.Format("Adding Key [{0}]", line)); tempsection.AddKey(line); } else { // This should not occur unless the tempsection is not created yet... Trace.WriteLine(string.Format("Skipping unknown type of data: {0}", line)); } } } oReader.Close(); }
public void Load(string sFileName, bool bMerge = false) { if (!bMerge) { this.RemoveAllSections(); } IniSection section = null; StreamReader reader = new StreamReader(sFileName); Regex regex = new Regex(@"^([\s]*#.*)", RegexOptions.Singleline | RegexOptions.IgnoreCase); Regex regex2 = new Regex(@"^[\s]*\[[\s]*([^\[\s].*[^\s\]])[\s]*\][\s]*$", RegexOptions.Singleline | RegexOptions.IgnoreCase); Regex regex3 = new Regex(@"^\s*([^=]*[^\s=])\s*=(.*)", RegexOptions.Singleline | RegexOptions.IgnoreCase); while (!reader.EndOfStream) { string input = reader.ReadLine(); if (input != string.Empty) { Match match = null; if (regex.Match(input).Success) { match = regex.Match(input); Trace.WriteLine($"Skipping Comment: {match.Groups[0].Value}"); } else { if (regex2.Match(input).Success) { match = regex2.Match(input); Trace.WriteLine($"Adding section [{match.Groups[1].Value}]"); section = this.AddSection(match.Groups[1].Value); continue; } if (regex3.Match(input).Success&& (section != null)) { match = regex3.Match(input); Trace.WriteLine($"Adding Key [{match.Groups[1].Value}]=[{match.Groups[2].Value}]"); section.AddKey(match.Groups[1].Value).Value = match.Groups[2].Value; continue; } if (section != null) { Trace.WriteLine($"Adding Key [{input}]"); section.AddKey(input); continue; } Trace.WriteLine($"Skipping unknown type of data: {input}"); } } } reader.Close(); }
/// <summary> /// Processes an INI section and applies its potential base section. /// Returns the INI section. Works recursively. /// </summary> /// <param name="iniFile">The INI file.</param> /// <param name="sectionName">The name of the section to process.</param> private IniSection ProcessSection(IniFile iniFile, string sectionName) { IniSection section = iniFile.GetSection(sectionName); if (section == null) { return(null); } string baseSectionName = section.GetStringValue("BaseSection", string.Empty); if (string.IsNullOrWhiteSpace(baseSectionName)) { return(section); } IniSection baseSection = ProcessSection(iniFile, baseSectionName); if (baseSection == null) { return(section); } foreach (var kvp in baseSection.Keys) { if (!section.KeyExists(kvp.Key)) { section.AddKey(kvp.Key, kvp.Value); } } return(section); }
//======================================================================= // IniFile Methods //======================================================================= //Loads the Reads the data in the ini file into the IniFile object public void Load(string sFileName, bool bMerge = false) { if (!bMerge) { RemoveAllSections(); } //Clear the object IniSection tempsection = null; StreamReader oReader = new StreamReader(sFileName); Regex regexcomment = new Regex("^([\\s]*#.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexkey = new Regex("^\\s*([^=]*[^\\s=])\\s*=(.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); while (!oReader.EndOfStream) { string line = oReader.ReadLine(); if (line != string.Empty) { Match m = null; if (regexcomment.Match(line).Success) { m = regexcomment.Match(line); } else if (regexsection.Match(line).Success) { m = regexsection.Match(line); tempsection = AddSection(m.Groups[1].Value); } else if (regexkey.Match(line).Success&& tempsection != null) { m = regexkey.Match(line); tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value; } else if (tempsection != null) { //Handle Key without value tempsection.AddKey(line); } else { //This should not occur unless the tempsection is not created yet... } } } oReader.Close(); }
// Sets a KeyValuePair in a certain section public bool SetKeyValue(string sSection, string sKey, string sValue) { IniSection s = AddSection(sSection); IniSection.IniKey k = s?.AddKey(sKey); if (k != null) { k.Value = sValue; return true; } return false; }
public bool SetKeyValue(string sSection, string sKey, string sValue) { IniSection section = this.AddSection(sSection); if (section != null) { IniSection.IniKey key = section.AddKey(sKey); if (key != null) { key.Value = sValue; return(true); } } return(false); }
/// <summary> /// Sets a KeyValuePair in a certain section /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public bool SetKeyValue(string section, string key, string value) { IniSection s = AddSection(section); if (s == null) { return(false); } IniSection.IniKey k = s.AddKey(key); if (k == null) { return(false); } k.Value = value; return(true); }
// Sets a KeyValuePair in a certain section public bool SetKeyValue(string sSection, string sKey, string sValue) { IniSection s = AddSection(sSection); if (s != null) { IniSection.IniKey k = s.AddKey(sKey); if (k != null) { if (k.Value != sValue) { k.Value = sValue; changed = true; } return(true); } } return(false); }
// Sets a KeyValuePair in a certain section public bool SetKeyValue(string sSection, string sKey, string sValue, string DefaultValue = "") { IniSection s = AddSection(sSection); if (s != null) { IniSection.IniKey k = s.AddKey(sKey); if (k != null) { k.Value = sValue; return(true); } else { k.Value = DefaultValue; } } return(false); }
public bool ForceRenameKey(string sSection, string sKey, string sNewKey) { // Note string trims are done in lower calls. IniSection s = GetSection(sSection); if (s != null) { IniSection.IniKey k = s.GetKey(sKey); if (k != null) { return(k.ForceSetName(sNewKey)); } if (sKey.Length == 0) { s.AddKey(sNewKey); return(true); } } return(false); }
/// <summary> /// Processes an INI section and applies its potential base section. /// Returns the INI section. Works recursively. /// </summary> /// <param name="iniFile">The INI file.</param> /// <param name="sectionName">The name of the section to process.</param> private IniSection ProcessSection(IniFile iniFile, string sectionName) { IniSection section = iniFile.GetSection(sectionName); if (section == null) { return(null); } string baseSectionName = section.GetStringValue("BaseSection", string.Empty); if (string.IsNullOrWhiteSpace(baseSectionName)) { return(section); } if (baseSectionName == sectionName) { throw new InvalidOperationException(sectionName + " is pointing to itself via BaseSection=!"); } IniSection baseSection = ProcessSection(iniFile, baseSectionName); if (baseSection == null) { return(section); } foreach (var kvp in baseSection.Keys) { if (!section.KeyExists(kvp.Key)) { section.AddKey(kvp.Key, kvp.Value); } } return(section); }
/// <summary> /// Loads the Reads the data in the ini file into the IniFile object /// </summary> /// <param name="sFileName">Name of the s file.</param> /// <param name="bMerge">if set to <c>fase</c>, remove all sections.</param> public void Load(string sFileName, bool bMerge = false) { if (!File.Exists(sFileName)) { throw new FileNotFoundException("Ini not fount", sFileName); } if (!bMerge) { RemoveAllSections(); } // Clear the object... IniSection tempsection = null; Regex regexcomment = new Regex("^([\\s]*#.*|;.*)", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); // Broken but left for history //Dim regexsection As New Regex("\[[\s]*([^\[\s].*[^\s\]])[\s]*\]", (RegexOptions.Singleline Or RegexOptions.IgnoreCase)) Regex regexsection = new Regex("^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); Regex regexkey = new Regex("^\\s*([^=]*)[^=]*=\\s?\"?(.*)\"?", (RegexOptions.Singleline | RegexOptions.IgnoreCase)); using (var oReader = new StreamReader(sFileName)) { while (!oReader.EndOfStream) { string line = oReader.ReadLine(); if (line != string.Empty) { Match m = null; if (regexcomment.Match(line).Success) { m = regexcomment.Match(line); Logger.Debug <IniFile>($"Skipping Comment: {m.Groups[0].Value}"); } else if (regexsection.Match(line).Success) { m = regexsection.Match(line); if (m.Groups[1].Value.ToLower() == "code") { Logger.Debug <IniFile>($"Copying Code Section [{m.Groups[1].Value}]"); _code = oReader.ReadToEnd(); } else { Logger.Debug <IniFile>($"Adding section [{m.Groups[1].Value}]"); tempsection = AddSection(m.Groups[1].Value); } } else if (regexkey.Match(line).Success&& tempsection != null) { m = regexkey.Match(line); Logger.Debug <IniFile>($"Adding Key [{m.Groups[1].Value}]=[{m.Groups[2].Value}]"); tempsection.AddKey(m.Groups[1].Value).Value = m.Groups[2].Value; } else if (tempsection != null) { // Handle Key without value Logger.Debug <IniFile>($"Adding Key [{line}]"); tempsection.AddKey(line); } else { // This should not occur unless the tempsection is // not created yet... Logger.Debug <IniFile>($"Skipping unknown type of data: {line}"); } } } } }