Beispiel #1
0
 public IniFileSection this[string sectionName]
 {
     get
     {
         IniFileSection section = getSection(sectionName);
         if (section != null)
         {
             return(section);
         }
         IniFileSectionStart iniFileSectionStart;
         if (sections.Count > 0)
         {
             IniFileSectionStart sectionStart = sections[sections.Count - 1].sectionStart;
             iniFileSectionStart = sectionStart.CreateNew(sectionName);
         }
         else
         {
             iniFileSectionStart = IniFileSectionStart.FromName(sectionName);
         }
         elements.Add(iniFileSectionStart);
         section = new IniFileSection(this, iniFileSectionStart);
         sections.Add(section);
         return(section);
     }
 }
Beispiel #2
0
 public void WriteSection(IniFileSection section)
 {
     WriteElement(section.sectionStart);
     for (int i = section.parent.elements.IndexOf(section.sectionStart) + 1; i < section.parent.elements.Count && !(section.parent.elements[i] is IniFileSectionStart); i++)
     {
         WriteElement(section.parent.elements[i]);
     }
 }
Beispiel #3
0
        public void DeleteSection(string name)
        {
            IniFileSection section = getSection(name);

            if (section != null)
            {
                IniFileSectionStart sectionStart = section.sectionStart;
                elements.Remove(sectionStart);
                for (int i = elements.IndexOf(sectionStart) + 1; i < elements.Count && !(elements[i] is IniFileSectionStart); i++)
                {
                    elements.RemoveAt(i);
                }
            }
        }
Beispiel #4
0
        public void UnifySections()
        {
            Dictionary <string, int> dictionary = new Dictionary <string, int>();

            for (int i = 0; i < sections.Count; i++)
            {
                IniFileSection iniFileSection = sections[i];
                if (dictionary.ContainsKey(iniFileSection.Name))
                {
                    int index = dictionary[iniFileSection.Name] + 1;
                    elements.Remove(iniFileSection.sectionStart);
                    sections.Remove(iniFileSection);
                    for (int num = iniFileSection.elements.Count - 1; num >= 0; num--)
                    {
                        IniFileElement iniFileElement = iniFileSection.elements[num];
                        if (num != iniFileSection.elements.Count - 1 || !(iniFileElement is IniFileCommentary))
                        {
                            elements.Remove(iniFileElement);
                        }
                        if (!(iniFileElement is IniFileBlankLine))
                        {
                            elements.Insert(index, iniFileElement);
                            IniFileValue iniFileValue = this[iniFileSection.Name].firstValue();
                            if (iniFileValue != null)
                            {
                                iniFileElement.Intendation = iniFileValue.Intendation;
                            }
                            else
                            {
                                iniFileElement.Intendation = this[iniFileSection.Name].sectionStart.Intendation;
                            }
                        }
                    }
                }
                else
                {
                    dictionary.Add(iniFileSection.Name, elements.IndexOf(iniFileSection.sectionStart));
                }
            }
        }
Beispiel #5
0
        public static IniFile FromElements(IEnumerable <IniFileElement> elemes)
        {
            IniFile iniFile = new IniFile();

            iniFile.elements.AddRange(elemes);
            if (iniFile.elements.Count > 0)
            {
                IniFileSection iniFileSection = null;
                if (iniFile.elements[iniFile.elements.Count - 1] is IniFileBlankLine)
                {
                    iniFile.elements.RemoveAt(iniFile.elements.Count - 1);
                }
                for (int i = 0; i < iniFile.elements.Count; i++)
                {
                    IniFileElement iniFileElement = iniFile.elements[i];
                    if (iniFileElement is IniFileSectionStart)
                    {
                        iniFileSection = new IniFileSection(iniFile, (IniFileSectionStart)iniFileElement);
                        iniFile.sections.Add(iniFileSection);
                    }
                    else if (iniFileSection != null)
                    {
                        iniFileSection.elements.Add(iniFileElement);
                    }
                    else if (iniFile.sections.Exists((IniFileSection a) => a.Name == ""))
                    {
                        iniFile.sections[0].elements.Add(iniFileElement);
                    }
                    else if (iniFileElement is IniFileValue)
                    {
                        iniFileSection = new IniFileSection(iniFile, IniFileSectionStart.FromName(""));
                        iniFileSection.elements.Add(iniFileElement);
                        iniFile.sections.Add(iniFileSection);
                    }
                }
            }
            return(iniFile);
        }
Beispiel #6
0
        /// <summary>Creates a new IniFile from elements collection (Advanced member).</summary>
        /// <param name="elemes">Elements collection.</param>
        public static IniFile FromElements(IEnumerable<IniFileElement> elemes)
        {
            IniFile ret = new IniFile();
            ret.elements.AddRange(elemes);
            if (ret.elements.Count > 0) {
                IniFileSection section = null;
                IniFileElement el;

                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++) {
                    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;
        }
Beispiel #7
0
 /// <summary>Writes a section to a file</summary>
 /// <param name="section">Section to write.</param>
 public void WriteSection(IniFileSection section)
 {
     WriteElement(section.sectionStart);
     for (int i = section.parent.elements.IndexOf(section.sectionStart) + 1; i < section.parent.elements.Count; i++) {
         if (section.parent.elements[i] is IniFileSectionStart)
             break;
         WriteElement(section.parent.elements[i]);
     }
 }
Beispiel #8
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;
     }
 }