Exemplo n.º 1
0
        /// <summary>Parses a single line.</summary>
        /// <param name="line">Text to parse.</param>
        private 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         trim = line.Trim();
            IniFileElement elem = null;

            if (IniFileBlankLine.IsLineValid(trim))
            {
                elem = new IniFileBlankLine(1);
            }
            else if (IniFileCommentary.IsLineValid(line))
            {
                elem = new IniFileCommentary(line);
            }
            else if (IniFileSectionStart.IsLineValid(trim))
            {
                elem = new IniFileSectionStart(line);
            }
            else if (IniFileValue.IsLineValid(trim))
            {
                elem = new IniFileValue(line);
            }
            return(elem ?? new IniFileElement(line));
        }
Exemplo n.º 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));
                }
            }
        }
Exemplo n.º 3
0
        private void setComment(IniFileElement el, string comment)
        {
            int index = parent.elements.IndexOf(el);

            if (IniFileEx.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);
            }
        }
Exemplo n.º 4
0
        private 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);
            }

            return("");
        }
Exemplo n.º 5
0
 /// <summary>Formats whole section.</summary>
 /// <param name="preserveIntendation">Determines whether intendation should be preserved.</param>
 public void Format(bool preserveIntendation)
 {
     for (int i = 0; i < elements.Count; i++)
     {
         IniFileElement el         = elements[i];
         string         lastIntend = el.Intendation;
         el.FormatDefault();
         if (preserveIntendation)
         {
             el.Intendation = lastIntend;
         }
     }
 }
Exemplo n.º 6
0
        /// <summary>Writes INI file element to the file.</summary>
        /// <param name="element">Element to write.</param>
        private static void WriteElement(StreamWriter writer, IniFileElement element)
        {
            if (!PreserveFormatting)
            {
                element.FormatDefault();
            }

            // do not write if:
            if (!( // 1) element is a blank line AND blank lines are not allowed
                    (element is IniFileBlankLine && !AllowBlankLines)
                    // 2) element is an empty value AND empty values are not allowed
                    || (!AllowEmptyValues && element is IniFileValue && ((IniFileValue)element).Value == "")))
            {
                writer.WriteLine(element.Line);
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        /// <summary>Parses given text.</summary>
        private static List <IniFileElement> ParseText(StreamReader reader)
        {
            var ret = new List <IniFileElement>();

            if (reader == null)
            {
                return(ret);
            }

            IniFileElement lastEl = null;

            while (reader.Peek() != -1)
            {
                IniFileElement currEl = ParseLine(reader.ReadLine());

                if (GroupElements)
                {
                    if (lastEl != null)
                    {
                        if (currEl is IniFileBlankLine && lastEl is IniFileBlankLine)
                        {
                            ((IniFileBlankLine)lastEl).Amount++;
                            continue;
                        }

                        if (currEl is IniFileCommentary && lastEl is IniFileCommentary)
                        {
                            ((IniFileCommentary)lastEl).Comment += Environment.NewLine + ((IniFileCommentary)currEl).Comment;
                            continue;
                        }
                    }
                    else
                    {
                        lastEl = currEl;
                    }
                }
                lastEl = currEl;
                ret.Add(currEl);
            }
            return(ret);
        }
Exemplo n.º 9
0
        /// <summary>Formats whole INI file.</summary>
        /// <param name="preserveIntendation">If true, old intendation will be standarized but not removed.</param>
        public void Format(bool preserveIntendation)
        {
            string lastSectIntend = "";
            string lastValIntend  = "";

            for (int i = 0; i < elements.Count; i++)
            {
                IniFileElement el = elements[i];
                if (preserveIntendation)
                {
                    if (el is IniFileSectionStart)
                    {
                        lastValIntend = lastSectIntend = el.Intendation;
                    }
                    else if (el is IniFileValue)
                    {
                        lastValIntend = el.Intendation;
                    }
                }
                el.FormatDefault();
                if (preserveIntendation)
                {
                    if (el is IniFileSectionStart)
                    {
                        el.Intendation = lastSectIntend;
                    }
                    else if (el is IniFileCommentary && i != elements.Count - 1 && !(elements[i + 1] is IniFileBlankLine))
                    {
                        el.Intendation = elements[i + 1].Intendation;
                    }
                    else
                    {
                        el.Intendation = lastValIntend;
                    }
                }
            }
        }