예제 #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);
            }
        }
예제 #2
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));
        }
예제 #3
0
        /// <summary>Crates a IniFileSectionStart object from name of a section.</summary>
        /// <param name="sectionName">Name of a section</param>
        public static IniFileSectionStart FromName(string sectionName)
        {
            IniFileSectionStart ret = new IniFileSectionStart();

            ret.SectionName = sectionName;
            ret.FormatDefault();
            return(ret);
        }
예제 #4
0
        /// <summary>Creates a new IniFileSectionStart object basing on a name of section and the formatting style of this section.</summary>
        /// <param name="sectName">Name of the new section</param>
        public IniFileSectionStart CreateNew(string sectName)
        {
            IniFileSectionStart ret = new IniFileSectionStart();

            ret.fSectionName = sectName;

            if (IniFileEx.PreserveFormatting)
            {
                ret.fFormatting = fFormatting;
                ret.Format();
            }
            else
            {
                ret.Format();
            }

            return(ret);
        }
예제 #5
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);
        }
예제 #6
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);
            }
        }
예제 #7
0
 internal IniFileSection(IniFileEx _parent, IniFileSectionStart sect)
 {
     sectionStart = sect;
     parent       = _parent;
 }