Beispiel #1
0
        public IniParser(string iniPath)
        {
            string section = "ROOT";

            FilePath = iniPath;

            IniSection currentSection = null;

            bool inroot = true;

            List <string> comments = new List <string>();

            Name = Path.GetFileNameWithoutExtension(iniPath);

            if (!File.Exists(iniPath))
            {
                throw new FileNotFoundException("Unable to locate " + iniPath);
            }


            using (TextReader reader = new StreamReader(iniPath)) {
                for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {
                    line = line.Trim();
                    if (line == String.Empty)
                    {
                        continue;
                    }

                    if (line.StartsWith("[") && line.EndsWith("]"))
                    {
                        section = line.Substring(1, line.Length - 2);

                        if (!Sections.ContainsKey(section))
                        {
                            Sections.Add(section, new IniSection(section));
                            if (comments.Count != 0)
                            {
                                Sections[section].Comments.AddRange(comments);
                                comments = new List <string>();
                            }
                        }
                        currentSection = Sections[section];
                        inroot         = false;
                    }
                    else
                    {
                        if (line.StartsWith(";"))
                        {
                            comments.Add(line);
                            continue;
                        }

                        string[] ConfEqValue = line.Split(new char[] { '=' }, 2);

                        if (ConfEqValue.Length == 0)
                        {
                            continue;
                        }

                        if (inroot)
                        {
                            if (!Sections.ContainsKey(section))
                            {
                                Sections.Add(section, new IniSection(section));
                                if (comments.Count != 0)
                                {
                                    Sections[section].Comments.AddRange(comments);
                                    comments = new List <string>();
                                }
                            }
                            currentSection = Sections[section];
                            inroot         = false;
                        }


                        if (ConfEqValue.Length == 2)
                        {
                            currentSection.AddSetting(ConfEqValue[0], ConfEqValue[1]);
                        }

                        else if (ConfEqValue.Length == 1)
                        {
                            currentSection.AddSetting(ConfEqValue[0], null);
                        }

                        if (comments.Count != 0)
                        {
                            currentSection.Settings[ConfEqValue[0]].Comments.AddRange(comments);
                            comments = new List <string>();
                        }
                    }
                }
            }
        }