Beispiel #1
0
        public void Read()
        {
            if (IniData == null && IniFile == null)
            {
                return;
            }

            string[] lines = new string[0];
            if (IniFile == null)
            {
                lines = IniData.Split("\n".ToCharArray());
            }
            else
            {
                lines = File.ReadAllLines(IniFile);
            }

            // Set property to 'main'
            Group = "Main";

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];
                // Is there a comment at the end of this line? Remove.
                if (line.Contains("//"))
                {
                    line = line.Substring(0, line.LastIndexOf("//"));
                }
                line = line.TrimEnd();

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

                string first_char = line.Substring(0, 1);
                if (first_char == "[" && line.EndsWith("]"))
                {
                    Group = line.Substring(1, line.Length - 2);
                }
                else if (first_char == "{")
                {
                    // Ignore
                }
                else if (i < lines.Length - 1 && lines[i + 1].Trim() == "{")
                {
                    // This starts a new group at this line.
                    PushGroup(line);
                }
                else if (first_char == "}")
                {
                    // This ends the group
                    PopGroup();
                }
                else if (line.Contains("="))
                {
                    string[] key_data = line.Split("=".ToCharArray(), 2);
                    key_data[0] = key_data[0].ToLower().Trim();
                    string[] data = ParseParameter(key_data[1]);

                    // Is this line in the cusotm keys list?
                    if (FireEventsForKeys.Contains(Group + "." + key_data[0]))
                    {
                        if (HandleCustomKeys != null)
                        {
                            HandleCustomKeys(new object[2] {
                                Group + "." + key_data[0], data
                            });
                        }
                    }
                    else
                    {
                        // If group doesn't exist; create it in the data dict.
                        if (Data.ContainsKey(Group) == false)
                        {
                            Data.Add(Group, new Dictionary <string, string[]>());
                        }

                        // Check if data exists. Otherwise throw event DuplicateKey
                        if (Data[Group].ContainsKey(key_data[0]) == false)
                        {
                            Data[Group].Add(key_data[0], data);
                        }
                        else if (HandleDuplicateKey != null)
                        {
                            HandleDuplicateKey(key_data);
                        }
                    }
                }
                else if (line.Trim() != "" && first_char != "#")
                {
                    // No comment, not empty.. Dunno what to do with this!
                    if (HandleUnknownLine != null)
                    {
                        HandleUnknownLine(line);
                    }
                }
            }
        }