Example #1
0
        private void MergeSectionDictionary(Dictionary <string, IniSectionNode> sectionDictionary)
        {
            foreach (KeyValuePair <string, IniSectionNode> section in sectionDictionary)
            {
                IniSectionNode sectionNode = null;

                if (m_sectionDictionary.TryGetValue(section.Key.ToLower(), out sectionNode))
                {
                    foreach (KeyValuePair <string, IniKeyNode> key in section.Value.KeyDictionary)
                    {
                        if (sectionNode.KeyDictionary.ContainsKey(key.Key.ToLower()))
                        {
                            sectionNode.KeyDictionary[key.Key] = key.Value;
                        }
                        else
                        {
                            sectionNode.KeyDictionary.Add(key.Key.ToLower(), key.Value);
                        }
                    }
                }
                else
                {
                    m_sectionDictionary.Add(section.Key.ToLower(), section.Value);
                }
            }
        }
Example #2
0
        public bool TryRead(string section, string key, string defaultValue, out string value)
        {
            value = defaultValue;
            IniSectionNode iniSectionNode = null;

            if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
            {
#if WRITE_DEFAULT
                Write(section, key, defaultValue);
#endif

                return(false);
            }

            IniKeyNode iniKeyNode = null;

            if (!iniSectionNode.KeyDictionary.TryGetValue(key.ToLower(), out iniKeyNode))
            {
#if WRITE_DEFAULT
                Write(section, key, defaultValue);
#endif

                return(false);
            }

            value = iniKeyNode.Value;

            return(true);
        }
Example #3
0
        private void ReadIni(string[] lineArray, Dictionary <string, IniSectionNode> sectionDictionary)
        {
            IniSectionNode sectionNode = null;

            char[]        charArray = new char[] { '=' };
            List <string> textList  = new List <string>();

            foreach (string line in lineArray)
            {
                string strLine = line.Trim();

                if (String.IsNullOrEmpty(line) || strLine.StartsWith(";"))
                {
                    textList.Add(line);

                    continue;
                }

                if (strLine.StartsWith("[") && strLine.EndsWith("]"))
                {
                    string strSection = strLine.Substring(1, strLine.Length - 2);

                    if (!sectionDictionary.TryGetValue(strSection.ToLower(), out sectionNode))
                    {
                        sectionNode = new IniSectionNode(textList.ToArray(), strSection);
                        sectionDictionary.Add(strSection.ToLower(), sectionNode);
                        textList.Clear();
                    }

                    continue;
                }

                if (strLine.Contains("="))
                {
                    if (sectionNode != null)
                    {
                        string[] splitArray = strLine.Split(charArray, 2);

                        if (splitArray.Length >= 2)
                        {
                            string key   = splitArray[0].Trim();
                            string value = String.Join("=", splitArray, 1, splitArray.Length - 1);

                            if (!sectionNode.KeyDictionary.ContainsKey(key.ToLower()))
                            {
                                IniKeyNode keyNode = new IniKeyNode(textList.ToArray(), key, value);
                                sectionNode.KeyDictionary.Add(key.ToLower(), keyNode);
                                textList.Clear();
                            }
                        }
                    }

                    continue;
                }

                textList.Add(line);
            }
        }
Example #4
0
        public bool TryGetKeyDictionary(string section, out Dictionary <string, IniKeyNode> keyDictionary)
        {
            keyDictionary = null;
            IniSectionNode iniSectionNode = null;

            if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
            {
                return(false);
            }

            keyDictionary = iniSectionNode.KeyDictionary;

            return(true);
        }
Example #5
0
        public bool TryDeleteKey(string section, string key)
        {
            IniSectionNode iniSectionNode = null;

            if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
            {
                return(false);
            }

            iniSectionNode.KeyDictionary.Remove(key.ToLower());

            m_fileChanged = true;

            return(true);
        }
Example #6
0
        public bool TryGetKeyList(string section, out List <IniKeyNode> keyList)
        {
            keyList = null;
            IniSectionNode iniSectionNode = null;

            if (!m_sectionDictionary.TryGetValue(section.ToLower(), out iniSectionNode))
            {
                return(false);
            }

            keyList = new List <IniKeyNode>();

            foreach (KeyValuePair <string, IniKeyNode> key in iniSectionNode.KeyDictionary)
            {
                keyList.Add(key.Value);
            }

            return(true);
        }
Example #7
0
        public bool TryRenameSection(string oldSection, string newSection)
        {
            IniSectionNode iniSectionNode = null;

            if (!m_sectionDictionary.TryGetValue(oldSection.ToLower(), out iniSectionNode))
            {
                return(false);
            }

            foreach (KeyValuePair <string, IniKeyNode> key in iniSectionNode.KeyDictionary)
            {
                Write(newSection, key.Value.Key, key.Value.Value);
            }

            m_sectionDictionary.Remove(oldSection.ToLower());

            m_fileChanged = true;

            return(true);
        }
Example #8
0
        public bool Write(string section, string key, string value)
        {
            if (key == null && value == null)
            {
                return(TryDeleteSection(section));
            }

            if (value == null)
            {
                return(TryDeleteKey(section, key));
            }

            IniSectionNode sectionNode = null;

            if (m_sectionDictionary.TryGetValue(section.ToLower(), out sectionNode))
            {
                IniKeyNode valueNode = null;

                if (sectionNode.KeyDictionary.TryGetValue(key.ToLower(), out valueNode))
                {
                    valueNode.Value = value;
                }
                else
                {
                    valueNode = new IniKeyNode(key, value);

                    sectionNode.KeyDictionary.Add(key.ToLower(), valueNode);
                }
            }
            else
            {
                sectionNode = new IniSectionNode(section);
                sectionNode.KeyDictionary.Add(key.ToLower(), new IniKeyNode(key, value));

                m_sectionDictionary.Add(section.ToLower(), sectionNode);
            }

            m_fileChanged = true;

            return(true);
        }