internal static INILine ReadLine(string line, INIFile src) { INILine ret = new INILine(); ret.Parse(line, src); return(ret); }
private void loadINI() { iniContent.Clear(); StreamReader sr = null; try { sr = new StreamReader(File.OpenRead(fileName), encoding); } catch { } INILine.setSection = ""; if (sr != null) { while (!sr.EndOfStream) { INILine line = new INILine(sr.ReadLine()); INIVariableDef vd = getVarDefn(line.section, line.key); line.defVal = vd.defValue; line.varType = vd.type; line.varDict = vd.dict; line.varBool = vd.boolType; line.useMinMax = vd.useMinMax; line.max = vd.max; line.min = vd.min; iniContent.Add(line); } sr.Close(); } }
public void setSectOrderedList(string section, string[] tab) { List <string> sections = new List <string>(); foreach (INILine line in iniContent) { sections.Add(line.section); } string iSection = section.ToLower(Statics.Culture); int firstIndex = sections.IndexOf(iSection); int lastIndex = sections.LastIndexOf(iSection); if (firstIndex == -1) { if (iniContent.Count > 0) { INILine line = iniContent[iniContent.Count - 1]; if (line.entry != "" || line.comment != "") { INILine.setSection = line.section; iniContent.Add(new INILine("")); firstIndex++; } } INILine.setSection = section; firstIndex = lastIndex = iniContent.Count; iniContent.Add(new INILine("[" + section + "]")); } int tabIndex = 0; int tabCount = tab.Length - 1; while (++firstIndex <= lastIndex && tabIndex <= tabCount) { INILine line = iniContent[firstIndex]; if (line.entry != "" && line.comment != "") { line.entry = INIComment + line.entry; } else if (line.comment != "") { continue; } else { line.entry = tab[tabIndex++]; } } while (firstIndex <= lastIndex) { iniContent.RemoveAt(lastIndex--); } INILine.setSection = section; iniContent.Insert(firstIndex, new INILine("")); while (tabCount >= tabIndex) { iniContent.Insert(firstIndex, new INILine(tab[tabCount--])); } modified = true; }
private void loadINI() { iniContent.Clear(); // Explicitly test for NUL instead of catching exceptions if (string.Equals(fileName, "NUL", StringComparison.OrdinalIgnoreCase)) { return; } // Open file and check for unwanted UTF-8 BOM try { using (var f = File.OpenRead(fileName)) { byte[] preamble = new byte[3]; f.Read(preamble, 0, 3); // Mark for resaving without BOM if present, seek back to start if BOM not present if (preamble[0] == 0xEF && preamble[1] == 0xBB && preamble[2] == 0xBF) { modified = true; } else { f.Seek(0, SeekOrigin.Begin); } // Switch to text stream using (var sr = new StreamReader(f, encoding)) { INILine.setSection = ""; while (!sr.EndOfStream) { INILine line = new INILine(sr.ReadLine()); INIVariableDef vd = getVarDefn(line.section, line.key); line.defVal = vd.defValue; line.varType = vd.type; line.varDict = vd.dict; line.varBool = vd.boolType; line.useMinMax = vd.useMinMax; line.max = vd.max; line.min = vd.min; iniContent.Add(line); } } } } catch (FileNotFoundException) { // Assume generating new INI } }
/// <summary>Adds or sets the value associated with the key in the section</summary> /// <param name="key">The key name</param> /// <param name="value">The new value to be assigned to the key</param> public void SetValue(string key, string value) { for (int i = 0; i < m_lines.Count; i++) { if (m_lines[i].Key == key) { m_lines[i] = new INILine { Key = key, Value = value }; return; } } m_lines.Add(new INILine { Key = key, Value = value }); }
private void loadINI() { iniContent.Clear(); // Open file and check for unwanted UTF-8 BOM var f = File.OpenRead(fileName); byte[] preamble = new byte[3]; f.Read(preamble, 0, 3); // Mark for resaving without BOM if present, seek back to start if BOM not present if (preamble[0] == 0xEF && preamble[1] == 0xBB && preamble[2] == 0xBF) { modified = true; } else { f.Seek(0, SeekOrigin.Begin); } // Switch to text stream var sr = new StreamReader(f, encoding); INILine.setSection = ""; if (sr != null) { while (!sr.EndOfStream) { INILine line = new INILine(sr.ReadLine()); INIVariableDef vd = getVarDefn(line.section, line.key); line.defVal = vd.defValue; line.varType = vd.type; line.varDict = vd.dict; line.varBool = vd.boolType; line.useMinMax = vd.useMinMax; line.max = vd.max; line.min = vd.min; iniContent.Add(line); } sr.Close(); } }
public void setSectList(string section, string[] tab) { List <string> sections = new List <string>(); foreach (INILine line in iniContent) { sections.Add(line.section); } string iSection = section.ToLower(Statics.Culture); int firstIndex = sections.IndexOf(iSection); int lastIndex = sections.LastIndexOf(iSection); List <string> iTab = new List <string>(); List <string> lTab = new List <string>(); foreach (string tmpStr in tab) { iTab.Add(tmpStr.ToLower(Statics.Culture)); lTab.Add(tmpStr); } if (firstIndex == -1) { if (iniContent.Count > 0) { INILine line = iniContent[iniContent.Count - 1]; if (line.entry != "" || line.comment != "") { INILine.setSection = line.section; iniContent.Add(new INILine("")); firstIndex++; } } INILine.setSection = section; firstIndex = lastIndex = iniContent.Count; iniContent.Add(new INILine("[" + section + "]")); } for (int i = lastIndex; i > firstIndex; i--) { INILine line = iniContent[i]; int index = iTab.IndexOf(line.iEntry); if (index != -1) { iTab.RemoveAt(index); lTab.RemoveAt(index); } else if (line.comment != "") { line.entry = INIComment + line.entry; modified = true; } else { iniContent.RemoveAt(i); lastIndex--; modified = true; } } INILine.setSection = section; if (iniContent[lastIndex].entry != "" || iniContent[lastIndex].comment != "") { iniContent.Insert(++lastIndex, new INILine("")); } for (int i = lTab.Count - 1; i >= 0; i--) { iniContent.Insert(lastIndex, new INILine(lTab[i])); modified = true; } }
private void keySet(string section, string key, bool useString, string sValue, double dValue) { List <string> sections = new List <string>(); List <string> keys = new List <string>(); List <string> comments = new List <string>(); foreach (INILine line in iniContent) { sections.Add(line.section); keys.Add(line.iKey); comments.Add(line.comment); } bool isDef = useString ? isDefault(section, key, sValue) : isDefault(section, key, dValue); INIVariableDef vd = getVarDefn(section, key); string iSection = section.ToLower(Statics.Culture); string iKey = key.ToLower(Statics.Culture); int firstIndex = sections.IndexOf(iSection); int lastIndex = sections.LastIndexOf(iSection); INILine.setSection = section; INILine tmp = new INILine(key + "="); tmp.defVal = vd.defValue; tmp.varType = vd.type; tmp.varDict = vd.dict; tmp.varBool = vd.boolType; tmp.useMinMax = vd.useMinMax; tmp.min = vd.min; tmp.max = vd.max; if (useString) { tmp.sValue = sValue; } else { if (vd.precision != -1) { dValue = double.Parse(dValue.ToString("F" + vd.precision.ToString())); } tmp.dValue = dValue; } if (firstIndex == -1) { if (!saveDef && isDef) { return; } if (iniContent.Count > 0) { INILine line = iniContent[iniContent.Count - 1]; if (line.entry != "" || line.comment != "") { INILine.setSection = line.section; iniContent.Add(new INILine("")); firstIndex++; } } INILine.setSection = section; iniContent.Add(new INILine("[" + section + "]")); iniContent.Add(tmp); iniContent.Add(new INILine("")); } else { int index = keys.IndexOf(iKey, firstIndex, lastIndex - firstIndex); if (index >= firstIndex && index <= lastIndex) { if (!saveDef && isDef && comments[index] == "") { iniContent.RemoveAt(index); } else if (useString && iniContent[index].value == sValue || !useString && iniContent[index].dValue == dValue) { return; } else if (useString) { iniContent[index].sValue = sValue; } else { iniContent[index].dValue = dValue; } } else { if (!saveDef && isDef) { return; } INILine line = iniContent[lastIndex++]; if (line.entry != "" || line.comment != "") { iniContent.Insert(lastIndex, new INILine("")); iniContent.Insert(lastIndex, tmp); } else { iniContent.Insert(lastIndex - 1, tmp); } } } modified = true; }
private void keySet(string section, string key, bool useString, string sValue, double dValue) { List<string> sections = new List<string>(); List<string> keys = new List<string>(); List<string> comments = new List<string>(); foreach (INILine line in iniContent) { sections.Add(line.section); keys.Add(line.iKey); comments.Add(line.comment); } bool isDef = useString ? isDefault(section, key, sValue) : isDefault(section, key, dValue); INIVariableDef vd = getVarDefn(section, key); string iSection = section.ToLower(Statics.Culture); string iKey = key.ToLower(Statics.Culture); int firstIndex = sections.IndexOf(iSection); int lastIndex = sections.LastIndexOf(iSection); int index = keys.IndexOf(iKey); INILine.setSection = section; INILine tmp = new INILine(key + "="); tmp.defVal = vd.defValue; tmp.varType = vd.type; tmp.varDict = vd.dict; tmp.varBool = vd.boolType; tmp.useMinMax = vd.useMinMax; tmp.min = vd.min; tmp.max = vd.max; if (useString) tmp.sValue = sValue; else { if (vd.precision != -1) dValue = double.Parse (dValue.ToString ("F" + vd.precision.ToString ())); tmp.dValue = dValue; } if (firstIndex == -1) { if (!saveDef && isDef) return; if (iniContent.Count > 0) { INILine line = iniContent[iniContent.Count - 1]; if (line.entry != "" || line.comment != "") { INILine.setSection = line.section; iniContent.Add(new INILine("")); firstIndex++; } } INILine.setSection = section; iniContent.Add(new INILine("[" + section + "]")); iniContent.Add(tmp); iniContent.Add(new INILine("")); } else if (index >= firstIndex && index <= lastIndex) { if (!saveDef && isDef && comments[index] == "") iniContent.RemoveAt(index); else if (useString && iniContent[index].value == sValue || !useString && iniContent[index].dValue == dValue) return; else if (useString) iniContent[index].sValue = sValue; else iniContent[index].dValue = dValue; } else { if (!saveDef && isDef) return; INILine line = iniContent[lastIndex++]; if (line.entry != "" || line.comment != "") { iniContent.Insert(lastIndex, new INILine("")); iniContent.Insert(lastIndex, tmp); } else iniContent.Insert(lastIndex - 1, tmp); } modified = true; }
public INIFile(INIVariableDef[] varDefn) { this.fileName = ""; this.varDefn = varDefn; this.saveDef = false; this.iniContent = new List<INILine>(); INILine.setSection = ""; foreach(INIVariableDef vd in varDefn) { if (vd.section == null) continue; INILine line = new INILine(""); line.section = vd.section.ToLower(); line.key = vd.key; line.value = vd.defValue; line.defVal = vd.defValue; line.varType = vd.type; line.varDict = vd.dict; line.varBool = vd.boolType; line.useMinMax = vd.useMinMax; line.max = vd.max; line.min = vd.min; iniContent.Add(line); } }
internal void ReadLine(string line, INIFile src) { INILine newiniline = INILine.ReadLine(line, src); m_lines.Add(newiniline); }