/// <summary> /// Try to parse a key/value pair from the given line, and add it to a config section /// </summary> /// <param name="Section">The section to receive the parsed config line</param> /// <param name="Line">Text to parse</param> /// <param name="StartIdx">Index of the first non-whitespace character in this line</param> /// <param name="EndIdx">Index of the last (exclusive) non-whitespace character in this line</param> /// <param name="DefaultAction">The default action to take if '+' or '-' is not specified on a config line</param> /// <returns>True if the line was parsed correctly, false otherwise</returns> static bool TryAddConfigLine(ConfigFileSection Section, string Line, int StartIdx, int EndIdx, ConfigLineAction DefaultAction) { // Find the '=' character separating key and value int EqualsIdx = Line.IndexOf('=', StartIdx, EndIdx - StartIdx); if (EqualsIdx == -1 && Line[StartIdx] != '!') { return(false); } // Keep track of the start of the key name int KeyStartIdx = StartIdx; // Remove the +/-/! prefix, if present ConfigLineAction Action = DefaultAction; if (Line[KeyStartIdx] == '+' || Line[KeyStartIdx] == '-' || Line[KeyStartIdx] == '!') { Action = (Line[KeyStartIdx] == '+')? ConfigLineAction.Add : (Line[KeyStartIdx] == '!') ? ConfigLineAction.RemoveKey : ConfigLineAction.RemoveKeyValue; KeyStartIdx++; while (Line[KeyStartIdx] == ' ' || Line[KeyStartIdx] == '\t') { KeyStartIdx++; } } // RemoveKey actions do not require a value if (Action == ConfigLineAction.RemoveKey && EqualsIdx == -1) { Section.Lines.Add(new ConfigLine(Action, Line.Substring(KeyStartIdx).Trim(), "")); return(true); } // Remove trailing spaces after the name of the key int KeyEndIdx = EqualsIdx; for (; KeyEndIdx > KeyStartIdx; KeyEndIdx--) { if (Line[KeyEndIdx - 1] != ' ' && Line[KeyEndIdx - 1] != '\t') { break; } } // Make sure there's a non-empty key name if (KeyStartIdx == EqualsIdx) { return(false); } // Skip whitespace between the '=' and the start of the value int ValueStartIdx = EqualsIdx + 1; for (; ValueStartIdx < EndIdx; ValueStartIdx++) { if (Line[ValueStartIdx] != ' ' && Line[ValueStartIdx] != '\t') { break; } } // Strip quotes around the value if present int ValueEndIdx = EndIdx; if (ValueEndIdx >= ValueStartIdx + 2 && Line[ValueStartIdx] == '"' && Line[ValueEndIdx - 1] == '"') { ValueStartIdx++; ValueEndIdx--; } // Add it to the config section string Key = Line.Substring(KeyStartIdx, KeyEndIdx - KeyStartIdx); string Value = Line.Substring(ValueStartIdx, ValueEndIdx - ValueStartIdx); Section.Lines.Add(new ConfigLine(Action, Key, Value)); return(true); }
/// <summary> /// Constructor. /// </summary> /// <param name="Action">Action to take when merging this key/value pair with an existing value</param> /// <param name="Key">Name of the key to modify</param> /// <param name="Value">Value to assign</param> public ConfigLine(ConfigLineAction Action, string Key, string Value) { this.Action = Action; this.Key = Key; this.Value = Value; }
/// <summary> /// Reads config data from the given file. /// </summary> /// <param name="Location">File to read from</param> /// <param name="DefaultAction">The default action to take when encountering arrays without a '+' prefix</param> public ConfigFile(FileReference Location, ConfigLineAction DefaultAction = ConfigLineAction.Set) { using (StreamReader Reader = new StreamReader(Location.FullName)) { ConfigFileSection CurrentSection = null; while (!Reader.EndOfStream) { // Find the first non-whitespace character string Line = Reader.ReadLine(); for (int StartIdx = 0; StartIdx < Line.Length; StartIdx++) { if (Line[StartIdx] != ' ' && Line[StartIdx] != '\t') { // Find the last non-whitespace character. If it's an escaped newline, merge the following line with it. int EndIdx = Line.Length; for (; EndIdx > StartIdx; EndIdx--) { if (Line[EndIdx - 1] == '\\') { string NextLine = Reader.ReadLine(); if (NextLine == null) { break; } Line += NextLine; EndIdx = Line.Length; } if (Line[EndIdx - 1] != ' ' && Line[EndIdx - 1] != '\t') { break; } } // Break out if we've got a comment if (Line[StartIdx] == ';') { break; } if (Line[StartIdx] == '/' && StartIdx + 1 < Line.Length && Line[StartIdx + 1] == '/') { break; } // Check if it's the start of a new section if (Line[StartIdx] == '[') { CurrentSection = null; if (Line[EndIdx - 1] == ']') { string SectionName = Line.Substring(StartIdx + 1, EndIdx - StartIdx - 2); if (!Sections.TryGetValue(SectionName, out CurrentSection)) { CurrentSection = new ConfigFileSection(SectionName); Sections.Add(SectionName, CurrentSection); } } break; } // Otherwise add it to the current section or add a new one if (CurrentSection != null) { if (!TryAddConfigLine(CurrentSection, Line, StartIdx, EndIdx, DefaultAction)) { Log.TraceWarning("Couldn't parse '{0}' in {1} of {2}", Line, CurrentSection, Location.FullName); } break; } // Otherwise just ignore it break; } } } } }