Example #1
0
        public List <IniFileElement> ReadSection()
        {
            if (current == null || !(current is IniFileSectionStart))
            {
                throw new InvalidOperationException("The current position of the reader must be at IniFileSectionStart. Use GotoSection method");
            }
            List <IniFileElement> list = new List <IniFileElement>();
            IniFileElement        item = current;

            list.Add(item);
            string text = "";
            string text2;

            while ((text2 = ReadLine()) != null)
            {
                if (IniFileSectionStart.IsLineValid(text2.Trim()))
                {
                    current = new IniFileSectionStart(text2);
                    break;
                }
                text = text + text2 + Environment.NewLine;
            }
            if (text.EndsWith(Environment.NewLine) && text != Environment.NewLine)
            {
                text = text.Substring(0, text.Length - Environment.NewLine.Length);
            }
            list.AddRange(ParseText(text));
            return(list);
        }
Example #2
0
        public static IniFileElement ParseLine(string line)
        {
            if (line == null)
            {
                return(null);
            }
            if (line.Contains("\n"))
            {
                throw new ArgumentException("String passed to the ParseLine method cannot contain more than one line.");
            }
            string         testString     = line.Trim();
            IniFileElement iniFileElement = null;

            if (IniFileBlankLine.IsLineValid(testString))
            {
                iniFileElement = new IniFileBlankLine(1);
            }
            else if (IniFileCommentary.IsLineValid(line))
            {
                iniFileElement = new IniFileCommentary(line);
            }
            else if (IniFileSectionStart.IsLineValid(testString))
            {
                iniFileElement = new IniFileSectionStart(line);
            }
            else if (IniFileValue.IsLineValid(testString))
            {
                iniFileElement = new IniFileValue(line);
            }
            return(iniFileElement ?? new IniFileElement(line));
        }
Example #3
0
        private void setComment(IniFileElement el, string comment)
        {
            int num = parent.elements.IndexOf(el);

            if (IniFileSettings.CommentChars.Length == 0)
            {
                throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them.");
            }
            if (num > 0 && parent.elements[num - 1] is IniFileCommentary)
            {
                IniFileCommentary iniFileCommentary = (IniFileCommentary)parent.elements[num - 1];
                if (comment == "")
                {
                    parent.elements.Remove(iniFileCommentary);
                    return;
                }
                iniFileCommentary.Comment     = comment;
                iniFileCommentary.Intendation = el.Intendation;
            }
            else if (comment != "")
            {
                IniFileCommentary iniFileCommentary = IniFileCommentary.FromComment(comment);
                iniFileCommentary.Intendation = el.Intendation;
                parent.elements.Insert(num, iniFileCommentary);
            }
        }
Example #4
0
        private string getComment(IniFileElement el)
        {
            int num = parent.elements.IndexOf(el);

            if (num != 0 && parent.elements[num - 1] is IniFileCommentary)
            {
                return(((IniFileCommentary)parent.elements[num - 1]).Comment);
            }
            return("");
        }
Example #5
0
 public void WriteElement(IniFileElement element)
 {
     if (!IniFileSettings.PreserveFormatting)
     {
         element.FormatDefault();
     }
     if ((!(element is IniFileBlankLine) || IniFileSettings.AllowBlankLines) && (IniFileSettings.AllowEmptyValues || !(element is IniFileValue) || !(((IniFileValue)element).Value == "")))
     {
         base.WriteLine(element.Line);
     }
 }
Example #6
0
 public void Format(bool preserveIntendation)
 {
     for (int i = 0; i < elements.Count; i++)
     {
         IniFileElement iniFileElement = elements[i];
         string         intendation    = iniFileElement.Intendation;
         iniFileElement.FormatDefault();
         if (preserveIntendation)
         {
             iniFileElement.Intendation = intendation;
         }
     }
 }
Example #7
0
        private void setValue(string key, string value)
        {
            IniFileValue iniFileValue  = null;
            IniFileValue iniFileValue2 = lastValue();

            if (IniFileSettings.PreserveFormatting)
            {
                if (iniFileValue2 != null && iniFileValue2.Intendation.Length >= sectionStart.Intendation.Length)
                {
                    iniFileValue = iniFileValue2.CreateNew(key, value);
                }
                else
                {
                    bool flag = false;
                    for (int num = parent.elements.IndexOf(sectionStart) - 1; num >= 0; num--)
                    {
                        IniFileElement iniFileElement = parent.elements[num];
                        if (iniFileElement is IniFileValue)
                        {
                            iniFileValue = ((IniFileValue)iniFileElement).CreateNew(key, value);
                            flag         = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        iniFileValue = IniFileValue.FromData(key, value);
                    }
                    if (iniFileValue.Intendation.Length < sectionStart.Intendation.Length)
                    {
                        iniFileValue.Intendation = sectionStart.Intendation;
                    }
                }
            }
            else
            {
                iniFileValue = IniFileValue.FromData(key, value);
            }
            if (iniFileValue2 == null)
            {
                elements.Insert(elements.IndexOf(sectionStart) + 1, iniFileValue);
                parent.elements.Insert(parent.elements.IndexOf(sectionStart) + 1, iniFileValue);
            }
            else
            {
                elements.Insert(elements.IndexOf(iniFileValue2) + 1, iniFileValue);
                parent.elements.Insert(parent.elements.IndexOf(iniFileValue2) + 1, iniFileValue);
            }
        }
Example #8
0
        public static List <IniFileElement> ParseText(string text)
        {
            if (text == null)
            {
                return(null);
            }
            List <IniFileElement> list           = new List <IniFileElement>();
            IniFileElement        iniFileElement = null;

            string[] array = text.Split(new string[1]
            {
                Environment.NewLine
            }, StringSplitOptions.None);
            for (int i = 0; i < array.Length; i++)
            {
                IniFileElement iniFileElement2 = ParseLine(array[i]);
                if (IniFileSettings.GroupElements)
                {
                    if (iniFileElement != null)
                    {
                        if (iniFileElement2 is IniFileBlankLine && iniFileElement is IniFileBlankLine)
                        {
                            ((IniFileBlankLine)iniFileElement).Amount++;
                            continue;
                        }
                        if (iniFileElement2 is IniFileCommentary && iniFileElement is IniFileCommentary)
                        {
                            IniFileCommentary obj = (IniFileCommentary)iniFileElement;
                            obj.Comment = obj.Comment + Environment.NewLine + ((IniFileCommentary)iniFileElement2).Comment;
                            continue;
                        }
                    }
                    else
                    {
                        iniFileElement = iniFileElement2;
                    }
                }
                iniFileElement = iniFileElement2;
                list.Add(iniFileElement2);
            }
            return(list);
        }
Example #9
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));
                }
            }
        }
Example #10
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);
        }
Example #11
0
        public void Format(bool preserveIntendation)
        {
            string intendation  = "";
            string intendation2 = "";

            for (int i = 0; i < elements.Count; i++)
            {
                IniFileElement iniFileElement = elements[i];
                if (preserveIntendation)
                {
                    if (iniFileElement is IniFileSectionStart)
                    {
                        intendation2 = (intendation = iniFileElement.Intendation);
                    }
                    else if (iniFileElement is IniFileValue)
                    {
                        intendation2 = iniFileElement.Intendation;
                    }
                }
                iniFileElement.FormatDefault();
                if (preserveIntendation)
                {
                    if (iniFileElement is IniFileSectionStart)
                    {
                        iniFileElement.Intendation = intendation;
                    }
                    else if (iniFileElement is IniFileCommentary && i != elements.Count - 1 && !(elements[i + 1] is IniFileBlankLine))
                    {
                        iniFileElement.Intendation = elements[i + 1].Intendation;
                    }
                    else
                    {
                        iniFileElement.Intendation = intendation2;
                    }
                }
            }
        }
Example #12
0
        public IniFileSectionStart GotoSection(string sectionName)
        {
            IniFileSectionStart iniFileSectionStart = null;

            while (true)
            {
                string text = ReadLine();
                if (text == null)
                {
                    current = null;
                    return(null);
                }
                if (IniFileSectionStart.IsLineValid(text))
                {
                    iniFileSectionStart = (ParseLine(text) as IniFileSectionStart);
                    if (iniFileSectionStart != null && (iniFileSectionStart.SectionName == sectionName || (!IniFileSettings.CaseSensitive && iniFileSectionStart.SectionName.ToLowerInvariant() == sectionName)))
                    {
                        break;
                    }
                }
            }
            current = iniFileSectionStart;
            return(iniFileSectionStart);
        }
Example #13
0
 /// <summary>Seeks to the section of specified name. If such section is not found,
 /// the function returns NULL and leaves the stream at the end of file.</summary>
 /// <param name="sectionName">Name of section to find.</param>
 public IniFileSectionStart GotoSection(string sectionName)
 {
     IniFileSectionStart sect = null;
     string str;
     while (true) {
         str = ReadLine();
         if (str == null) {
             current = null;
             return null;
         }
         if (IniFileSectionStart.IsLineValid(str)) {
             sect = ParseLine(str) as IniFileSectionStart;
             if (sect != null && (sect.SectionName == sectionName || (!IniFileSettings.CaseSensitive && sect.SectionName.ToLowerInvariant() == sectionName))) {
                 current = sect;
                 return sect;
             }
         }
     }
 }
Example #14
0
 public IniFileElement ReadElement()
 {
     current = ParseLine(ReadLine());
     return(current);
 }
Example #15
0
 /// <summary>Writes INI file element to the file.</summary>
 /// <param name="element">Element to write.</param>
 public void WriteElement(IniFileElement element)
 {
     if (!IniFileSettings.PreserveFormatting)
         element.FormatDefault();
     // do not write if:
     if (!( // 1) element is a blank line AND blank lines are not allowed
         (element is IniFileBlankLine && !IniFileSettings.AllowBlankLines)
          // 2) element is an empty value AND empty values are not allowed
         || (!IniFileSettings.AllowEmptyValues && element is IniFileValue && ((IniFileValue)element).Value == "")))
         base.WriteLine(element.Line);
 }
Example #16
0
 void setComment(IniFileElement el, string comment)
 {
     int index = parent.elements.IndexOf(el);
     if (IniFileSettings.CommentChars.Length == 0)
         throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them.");
     IniFileCommentary com;
     if (index > 0 && parent.elements[index - 1] is IniFileCommentary) {
         com = ((IniFileCommentary)parent.elements[index - 1]);
         if (comment == "")
             parent.elements.Remove(com);
         else {
             com.Comment = comment;
             com.Intendation = el.Intendation;
         }
     }
     else if (comment != "") {
         com = IniFileCommentary.FromComment(comment);
         com.Intendation = el.Intendation;
         parent.elements.Insert(index, com);
     }
 }
Example #17
0
 string getComment(IniFileElement el)
 {
     int index = parent.elements.IndexOf(el);
     if (index != 0 && parent.elements[index - 1] is IniFileCommentary)
         return ((IniFileCommentary)parent.elements[index - 1]).Comment;
     else return "";
 }
Example #18
0
 /// <summary>Returns a list of IniFileElement object in the currect section. The first element of
 /// returned collection will be a IniFileSectionStart.</summary>
 /// <exception cref="System.InvalidOperationException">A stream is not currently at the IniFileSectionStart.</exception>
 public List<IniFileElement> ReadSection()
 {
     if (current == null || !(current is IniFileSectionStart))
         throw new InvalidOperationException("The current position of the reader must be at IniFileSectionStart. Use GotoSection method");
     List<IniFileElement> ret = new List<IniFileElement>();
     IniFileElement theCurrent = current;
     ret.Add(theCurrent);
     string text = "", temp;
     while ((temp = base.ReadLine()) != null) {
         if (IniFileSectionStart.IsLineValid(temp.Trim())) {
             current = new IniFileSectionStart(temp);
             break;
         }
         text += temp + Environment.NewLine;
     }
     if (text.EndsWith(Environment.NewLine) && text != Environment.NewLine)
         text = text.Substring(0, text.Length - Environment.NewLine.Length);
     ret.AddRange(ParseText(text));
     return ret;
 }
Example #19
0
 /// <summary>Reads and parses next line from the config file.</summary>
 /// <returns>Created ConfigFileElement.</returns>
 public IniFileElement ReadElement()
 {
     current = ParseLine(base.ReadLine());
     return current;
 }