Пример #1
0
        public static IniElement 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.");
            }
            var        trim = line.Trim();
            IniElement elem = null;

            if (IniBlankLine.IsLineValid(trim))
            {
                elem = new IniBlankLine(1);
            }
            else if (IniCommentary.IsLineValid(line))
            {
                elem = new IniCommentary(line);
            }
            else if (IniSectionStart.IsLineValid(trim))
            {
                elem = new IniSectionStart(line);
            }
            else if (IniValue.IsLineValid(trim))
            {
                elem = new IniValue(line);
            }
            return(elem ?? new IniElement(line));
        }
Пример #2
0
 public IniSection this[string sectionName]
 {
     get
     {
         var sect = GetSection(sectionName);
         if (sect != null)
         {
             return(sect);
         }
         IniSectionStart start;
         if (Sections.Count > 0)
         {
             var prev = Sections[Sections.Count - 1].SectionStart;
             start = prev.CreateNew(sectionName);
         }
         else
         {
             start = IniSectionStart.FromName(sectionName);
         }
         if (IniSettings.SeparateSection)
         {
             Elements.Add(new IniBlankLine(1));
         }
         Elements.Add(start);
         sect = new IniSection(this, start);
         Sections.Add(sect);
         return(sect);
     }
 }
Пример #3
0
        public List <IniElement> ReadSection()
        {
            if (_current == null || !(_current is IniSectionStart))
            {
                throw new InvalidOperationException("The current position of the reader must be at IniSectionStart. Use GotoSection method");
            }
            var ret        = new List <IniElement>();
            var theCurrent = _current;

            ret.Add(theCurrent);
            string text = "", temp;

            while ((temp = base.ReadLine()) != null)
            {
                if (IniSectionStart.IsLineValid(temp.Trim()))
                {
                    _current = new IniSectionStart(temp);
                    break;
                }
                text += temp + IniSettings.NewLine;
            }
            if (text.EndsWith(IniSettings.NewLine) && text != IniSettings.NewLine)
            {
                text = text.Substring(0, text.Length - IniSettings.NewLine.Length);
            }
            ret.AddRange(ParseText(text));
            return(ret);
        }
Пример #4
0
        public static IniSectionStart FromName(string sectionName)
        {
            var ret = new IniSectionStart();

            ret.SectionName = sectionName;
            ret.FormatDefault();
            return(ret);
        }
Пример #5
0
        public IniSectionStart CreateNew(string sectName)
        {
            var ret = new IniSectionStart();

            ret.sectionName = sectName;
            if (IniSettings.PreserveFormatting)
            {
                ret.formatting = formatting;
                ret.Format();
            }
            else
            {
                ret.Format();
            }
            return(ret);
        }
Пример #6
0
		public static IniElement 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.");
			var trim = line.Trim();
			IniElement elem = null;
			if (IniBlankLine.IsLineValid(trim))
				elem = new IniBlankLine(1);
			else if (IniCommentary.IsLineValid(line))
				elem = new IniCommentary(line);
			else if (IniSectionStart.IsLineValid(trim))
				elem = new IniSectionStart(line);
			else if (IniValue.IsLineValid(trim))
				elem = new IniValue(line);
			return elem ?? new IniElement(line);
		}
Пример #7
0
        public static Ini FromElements(IEnumerable <IniElement> elemes)
        {
            var ret = new Ini();

            ret.Elements.AddRange(elemes);
            if (ret.Elements.Count > 0)
            {
                IniSection section = null;

                if (ret.Elements[ret.Elements.Count - 1] is IniBlankLine)
                {
                    ret.Elements.RemoveAt(ret.Elements.Count - 1);
                }
                foreach (var element in ret.Elements)
                {
                    var el = element;
                    if (el is IniSectionStart)
                    {
                        section = new IniSection(ret, (IniSectionStart)el);
                        ret.Sections.Add(section);
                    }
                    else if (section != null)
                    {
                        section.Elements.Add(el);
                    }
                    else if (ret.Sections.Exists(a => a.Name == ""))
                    {
                        ret.Sections[0].Elements.Add(el);
                    }
                    else if (el is IniValue)
                    {
                        section = new IniSection(ret, IniSectionStart.FromName(""));
                        section.Elements.Add(el);
                        ret.Sections.Add(section);
                    }
                }
            }
            return(ret);
        }
Пример #8
0
 public IniSectionStart GotoSection(string sectionName)
 {
     while (true)
     {
         var str = ReadLine();
         if (str == null)
         {
             _current = null;
             return(null);
         }
         if (IniSectionStart.IsLineValid(str))
         {
             var sect = ParseLine(str) as IniSectionStart;
             if (sect != null &&
                 (sect.SectionName == sectionName ||
                  (!IniSettings.CaseSensitive && sect.SectionName.ToLowerInvariant() == sectionName)))
             {
                 _current = sect;
                 return(sect);
             }
         }
     }
 }
Пример #9
0
 public IniValue GotoValue(string key, bool searchWholeFile)
 {
     while (true)
     {
         var str = ReadLine();
         if (str == null)
         {
             return(null);
         }
         if (IniValue.IsLineValid(str.Trim()))
         {
             var val = ParseLine(str) as IniValue;
             if (val != null &&
                 (val.Key == key || (!IniSettings.CaseSensitive && val.Key.ToLowerInvariant() == key.ToLowerInvariant())))
             {
                 return(val);
             }
         }
         if (!searchWholeFile && IniSectionStart.IsLineValid(str.Trim()))
         {
             return(null);
         }
     }
 }
Пример #10
0
		public static IniSectionStart FromName(string sectionName)
		{
			var ret = new IniSectionStart();
			ret.SectionName = sectionName;
			ret.FormatDefault();
			return ret;
		}
Пример #11
0
		public IniSectionStart CreateNew(string sectName)
		{
			var ret = new IniSectionStart();
			ret.sectionName = sectName;
			if (IniSettings.PreserveFormatting)
			{
				ret.formatting = formatting;
				ret.Format();
			}
			else
				ret.Format();
			return ret;
		}
Пример #12
0
		internal IniSection(Ini parent, IniSectionStart sect)
		{
			SectionStart = sect;
			Parent = parent;
		}
Пример #13
0
 internal IniSection(Ini parent, IniSectionStart sect)
 {
     SectionStart = sect;
     Parent       = parent;
 }