Exemple #1
0
        /// <summary>Gets a IniFileSection object from it's name</summary>
        /// <param name="sectionName">Name of section to search for. If not found, new one is created.</param>
        public IniFileSection this[string sectionName]
        {
            get
            {
                IniFileSection sect = GetSection(sectionName);
                if (sect != null)
                {
                    return(sect);
                }

                IniFileSectionStart start;
                if (sections.Count > 0)
                {
                    IniFileSectionStart prev = sections[sections.Count - 1].sectionStart;
                    start = prev.CreateNew(sectionName);
                }
                else
                {
                    start = IniFileSectionStart.FromName(sectionName);
                }
                elements.Add(start);
                sect = new IniFileSection(this, start);
                sections.Add(sect);
                return(sect);
            }
        }
Exemple #2
0
        /// <summary>Joins sections which are definied more than one time.</summary>
        public void UnifySections()
        {
            var dict = new Dictionary <string, int>();

            for (int i = 0; i < sections.Count; i++)
            {
                IniFileSection sect = sections[i];
                if (dict.ContainsKey(sect.Name))
                {
                    int index = dict[sect.Name] + 1;
                    elements.Remove(sect.sectionStart);
                    sections.Remove(sect);
                    for (int j = sect.elements.Count - 1; j >= 0; j--)
                    {
                        IniFileElement el = sect.elements[j];
                        if (!(j == sect.elements.Count - 1 && el is IniFileCommentary))
                        {
                            elements.Remove(el);
                        }
                        if (!(el is IniFileBlankLine))
                        {
                            elements.Insert(index, el);
                            IniFileValue val = this[sect.Name].FirstValue();
                            el.Intendation = (val != null) ? val.Intendation : this[sect.Name].sectionStart.Intendation;
                        }
                    }
                }
                else
                {
                    dict.Add(sect.Name, elements.IndexOf(sect.sectionStart));
                }
            }
        }
Exemple #3
0
        /// <summary>Reads a INI file from a stream.</summary>
        public static IniFileEx FromStream(StreamReader reader)
        {
            IEnumerable <IniFileElement> elemes = ParseText(reader);

            IniFileEx ret = new IniFileEx();

            ret.elements.AddRange(elemes);
            if (ret.elements.Count > 0)
            {
                IniFileSection section = null;

                if (ret.elements[ret.elements.Count - 1] is IniFileBlankLine)
                {
                    ret.elements.RemoveAt(ret.elements.Count - 1);
                }

                for (int i = 0; i < ret.elements.Count; i++)
                {
                    IniFileElement el = ret.elements[i];
                    if (el is IniFileSectionStart)
                    {
                        section = new IniFileSection(ret, (IniFileSectionStart)el);
                        ret.sections.Add(section);
                    }
                    else if (section != null)
                    {
                        section.elements.Add(el);
                    }
                    else if (ret.sections.Exists(delegate(IniFileSection a) { return(a.Name == ""); }))
                    {
                        ret.sections[0].elements.Add(el);
                    }
                    else if (el is IniFileValue)
                    {
                        section = new IniFileSection(ret, IniFileSectionStart.FromName(""));
                        section.elements.Add(el);
                        ret.sections.Add(section);
                    }
                }
            }
            return(ret);
        }
Exemple #4
0
        /// <summary>Deletes a section and all it's values and comments. No exception is thrown if there is no section of requested name.</summary>
        /// <param name="name">Name of section to delete.</param>
        public void DeleteSection(string name)
        {
            IniFileSection section = GetSection(name);

            if (section == null)
            {
                return;
            }
            IniFileSectionStart sect = section.sectionStart;

            elements.Remove(sect);
            for (int i = elements.IndexOf(sect) + 1; i < elements.Count; i++)
            {
                if (elements[i] is IniFileSectionStart)
                {
                    break;
                }
                elements.RemoveAt(i);
            }
        }