예제 #1
0
        /// <summary>
        /// Merges an another INI file into this one. Other INI file's values are picked over current INI file's.
        /// </summary>
        /// <param name="iniFileToMerge">INI file to merge into this one.</param>
        public void Merge(INIFile iniFileToMerge)
        {
            foreach (INISection newSection in iniFileToMerge.iniSections)
            {
                if (newSection == null)
                {
                    continue;
                }

                INISection section = iniSections.Find(i => i.Name == newSection.Name);

                if (section == null)
                {
                    iniSections.Add(newSection);
                    _altered = true;
                    continue;
                }

                foreach (INIKeyValuePair newKvp in newSection.KeyValuePairs)
                {
                    if (newKvp.Key == null)
                    {
                        continue;
                    }

                    INIKeyValuePair kvp = section.KeyValuePairs.Find(i => i.Key == newKvp.Key);

                    if (kvp != null)
                    {
                        kvp.Value = newKvp.Value;

                        List <INIComment> comments = newKvp.GetAllCommentsAtPosition(INICommentPosition.Before);
                        comments.AddRange(newKvp.GetAllCommentsAtPosition(INICommentPosition.Middle));
                        comments.AddRange(newKvp.GetAllCommentsAtPosition(INICommentPosition.After));

                        foreach (INIComment comment in comments)
                        {
                            if (!kvp.HasComment(comment))
                            {
                                kvp.AddComment(comment);
                            }
                        }

                        _altered = true;
                    }
                    else
                    {
                        section.KeyValuePairs.Add(newKvp);
                        _altered = true;
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Gets INI key-value pair from line of text and adds it to the specified section.
        /// </summary>
        /// <param name="section">INI section to add the key-value pair to.</param>
        /// <param name="line">Line of text.</param>
        /// <returns>INI key-value pair that was added to the section.</returns>
        private INIKeyValuePair AddKeyValuePairFromLine(INISection section, string line)
        {
            string key             = null;
            string value           = null;
            string comments        = null;
            int    whiteSpaceCount = 0;
            string lineMod         = line;

            if (lineMod.Contains(";"))
            {
                int index = lineMod.IndexOf(';');
                comments        = lineMod.Substring(index, line.Length - index);
                lineMod         = line.Replace(comments, "").Trim();
                whiteSpaceCount = line.Replace(lineMod, "").Replace(comments, "").Length;
                comments        = comments.ReplaceFirst(";", "");
            }

            if (line.Contains("="))
            {
                key   = lineMod.Substring(0, lineMod.IndexOf('=')).Trim();
                value = lineMod.Substring(lineMod.IndexOf('=') + 1, lineMod.Length - lineMod.IndexOf('=') - 1).Trim();
            }
            else
            {
                key = lineMod.Trim();
            }

            INIKeyValuePair kvp      = new INIKeyValuePair(key, value);
            INIKeyValuePair kvpMatch = section.KeyValuePairs.Find(x => x.Key == kvp.Key);

            if (kvpMatch == null)
            {
                section.KeyValuePairs.Add(kvp);
            }
            else
            {
                kvpMatch.Value = kvp.Value;
                kvp            = kvpMatch;
            }

            if (comments != null)
            {
                INIComment iniComment = new INIComment(comments, INICommentPosition.Middle, whiteSpaceCount);

                if (!kvp.HasComment(iniComment))
                {
                    kvp.AddComment(iniComment);
                }
            }

            return(kvp);
        }
예제 #3
0
        /// <summary>
        /// Get value of INI key as a string from a INI file section.
        /// </summary>
        /// <param name="sectionName">Name of the INI file section.</param>
        /// <param name="keyName">Name of the INI key.</param>
        /// <param name="defaultValue">Default value returned if section, key or value was not found.</param>
        /// <returns>Returns the value of the key as a string if successful, otherwise <paramref name="defaultValue"/> is returned.</returns>
        public string GetKey(string sectionName, string keyName, string defaultValue)
        {
            INISection section = iniSections.Find(i => i.Name == sectionName);

            if (section == null)
            {
                return(defaultValue);
            }

            INIKeyValuePair kvp = section.KeyValuePairs.Find(i => i.Key == keyName);

            if (kvp == null)
            {
                return(defaultValue);
            }

            return(kvp.Value);
        }
예제 #4
0
        /// <summary>
        /// Removes INI key from a INI file section.
        /// </summary>
        /// <param name="sectionName">Name of the INI file section.</param>
        /// <param name="keyName">Name of the INI key.</param>
        /// <returns>True if key was removed, otherwise false.</returns>
        public bool RemoveKey(string sectionName, string keyName)
        {
            INISection section = iniSections.Find(i => i.Name == sectionName);

            if (section == null)
            {
                return(false);
            }

            INIKeyValuePair kvp = section.KeyValuePairs.Find(i => i.Key == keyName);

            if (section.KeyValuePairs.Remove(kvp))
            {
                _altered = true;
                return(true);
            }

            return(false);
        }
예제 #5
0
        /// <summary>
        /// Moves a key in INI file section matching specific name to a specific position in order in INI file.
        /// </summary>
        /// <param name="sectionName">Name of the INI file section.</param>
        /// <param name="keyName">Name of the INI key.</param>
        /// <param name="positionIndex">The position to move the key into. Values higher than lower than the currently available amount of keys get constricted into that range.</param>
        /// <returns>True if position was changed, otherwise false.</returns>
        public bool SetKeyPosition(string sectionName, string keyName, int positionIndex)
        {
            INISection section = iniSections.Find(i => i.Name == sectionName);

            if (section != null)
            {
                INIKeyValuePair kvp = section.KeyValuePairs.Find(x => x.Key == keyName);

                if (kvp == null)
                {
                    return(false);
                }

                section.KeyValuePairs.Remove(kvp);
                section.KeyValuePairs.Insert(positionIndex, kvp);
                _altered = true;
                return(true);
            }

            return(false);
        }
예제 #6
0
        /// <summary>
        /// Set value of INI key in a INI file section. If section or key do not exist, they are created.
        /// </summary>
        /// <param name="sectionName">Name of the INI file section.</param>
        /// <param name="keyName">Name of the INI key.</param>
        /// <param name="value">The new value of the key.</param>
        public void SetKey(string sectionName, string keyName, string value)
        {
            INISection section = iniSections.Find(i => i.Name == sectionName);

            if (section == null)
            {
                AddSection(sectionName);
                section = iniSections.Find(i => i.Name == sectionName);
            }

            INIKeyValuePair kvp = section.KeyValuePairs.Find(i => i.Key == keyName);

            if (kvp == null)
            {
                section.KeyValuePairs.Add(new INIKeyValuePair(keyName, value));
            }
            else
            {
                kvp.Value = value;
            }

            _altered = true;
        }
예제 #7
0
        /// <summary>
        /// Attempts to parse the INI file from a file with specified filename.
        /// </summary>
        /// <param name="filename">Filename of the INI file to parse.</param>
        /// <param name="clearExistingSections">If set, removes all existing INI sections & keys before parsing INI file.</param>
        /// <returns>Error message if something went wrong, null otherwise.</returns>
        private string Parse(string filename, bool clearExistingSections = false)
        {
            string[] lines = null;

            try
            {
                lines = File.ReadAllLines(filename);
            }
            catch (Exception e)
            {
                return(e.Message);
            }

            if (clearExistingSections)
            {
                iniSections.Clear();
            }

            INISection        currentSection        = null;
            INIKeyValuePair   lastLine              = null;
            bool              lastLineWasWhiteSpace = false;
            bool              lastLineWasSection    = false;
            List <INIComment> comments              = new List <INIComment>();

            foreach (string line in lines)
            {
                string lineTrimmed = line.Trim();

                if (string.IsNullOrEmpty(lineTrimmed))
                {
                    lastLineWasWhiteSpace = true;

                    if (lastLineWasSection)
                    {
                        currentSection.EmptyLineCount++;
                    }
                    else if (lastLine != null)
                    {
                        lastLine.EmptyLineCount++;
                    }

                    continue;
                }

                if (lineTrimmed.StartsWith(";"))
                {
                    int whiteSpaceCount = line.Length - line.TrimStart().Length;
                    if (lastLineWasWhiteSpace || currentSection == null)
                    {
                        comments.Add(new INIComment(lineTrimmed.ReplaceFirst(";", ""), INICommentPosition.Before, whiteSpaceCount));
                    }
                    else if (lastLineWasSection)
                    {
                        currentSection.AddComment(lineTrimmed.ReplaceFirst(";", ""), INICommentPosition.After, whiteSpaceCount);
                    }
                    else if (lastLine != null)
                    {
                        lastLine.AddComment(lineTrimmed.ReplaceFirst(";", ""), INICommentPosition.After, whiteSpaceCount);
                    }

                    continue;
                }

                if (lineTrimmed.StartsWith("[") && lineTrimmed.Contains("]"))
                {
                    currentSection = new INISection();

                    int start = lineTrimmed.IndexOf('[') + 1;
                    int end   = lineTrimmed.IndexOf(']') - 1;
                    currentSection.Name = lineTrimmed.Substring(start, end);
                    INISection sectionMatch = iniSections.Find(x => x.Name == currentSection.Name);

                    if (lineTrimmed.Contains(";"))
                    {
                        string commentLine     = line.Replace("[" + currentSection.Name + "]", "");
                        int    whiteSpaceCount = commentLine.Length - commentLine.TrimStart().Length;
                        string comment         = commentLine.Trim().TrimStart(';');
                        currentSection.AddComment(comment, INICommentPosition.Middle, whiteSpaceCount);
                    }

                    if (sectionMatch == null)
                    {
                        iniSections.Add(currentSection);
                    }
                    else
                    {
                        currentSection = sectionMatch;
                    }

                    foreach (INIComment comment in comments)
                    {
                        if (!currentSection.HasComment(comment))
                        {
                            currentSection.AddComment(comment);
                        }
                    }

                    comments.Clear();
                    lastLineWasSection = true;
                    lastLine           = null;
                }
                else
                {
                    lastLineWasSection = false;
                    lastLine           = AddKeyValuePairFromLine(currentSection, line.Trim());
                }

                lastLineWasWhiteSpace = false;
            }

            _altered = false;
            return(null);
        }