Ejemplo n.º 1
0
        public static IniFile Read(string InputPath)
        {
            IniFile File = new IniFile();

            using (StreamReader Reader = new StreamReader(InputPath))
            {
                IniSection CurrentSection = null;
                while (!Reader.EndOfStream)
                {
                    string Line = Reader.ReadLine().Trim();
                    if (Line.Length > 0 && !Line.StartsWith(";"))
                    {
                        // Check if it's a section heading or key/value pair
                        if (Line[0] == '[')
                        {
                            if (Line[Line.Length - 1] != ']')
                            {
                                throw new InvalidDataException();
                            }

                            string Name = Line.Substring(1, Line.Length - 2);
                            CurrentSection = new IniSection(Name);

                            File.Sections.Add(CurrentSection);
                            if (File.NameToSection.ContainsKey(Name.ToLowerInvariant()))
                            {
                                Console.WriteLine("{0} has already been added", Name);
                            }
                            File.NameToSection.Add(Name.ToLowerInvariant(), CurrentSection);
                        }
                        else
                        {
                            string Key, Value;

                            // Split the line into key/value
                            int ValueIdx = Line.IndexOf('=');
                            if (ValueIdx == -1)
                            {
                                Key   = Line;
                                Value = "";
                            }
                            else
                            {
                                Key   = Line.Substring(0, ValueIdx);
                                Value = Line.Substring(ValueIdx + 1);
                            }

                            // If it's an append operation, add the existing value
                            if (Key.Length > 0 && Key[0] == '+')
                            {
                                CurrentSection.Append(Key.Substring(1), Value);
                            }
                            else
                            {
                                CurrentSection.Set(Key, Value);
                            }
                        }
                    }
                }
            }
            return(File);
        }