Ejemplo n.º 1
0
        /// <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;
                        }
                    }
                }
            }
        }