Exemple #1
0
        private void SetComment(IniElement el, string comment)
        {
            var index = Parent.Elements.IndexOf(el);

            if (IniSettings.CommentChars.Length == 0)
            {
                throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them.");
            }
            IniCommentary com;

            if (index > 0 && Parent.Elements[index - 1] is IniCommentary)
            {
                com = ((IniCommentary)Parent.Elements[index - 1]);
                if (comment == "")
                {
                    Parent.Elements.Remove(com);
                }
                else
                {
                    com.Comment     = comment;
                    com.Indentation = el.Indentation;
                }
            }
            else if (comment != "")
            {
                com             = IniCommentary.FromComment(comment);
                com.Indentation = el.Indentation;
                Parent.Elements.Insert(index, com);
            }
        }
Exemple #2
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));
        }
Exemple #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);
        }
Exemple #4
0
        private string GetComment(IniElement el)
        {
            var index = Parent.Elements.IndexOf(el);

            if (index != 0 && Parent.Elements[index - 1] is IniCommentary)
            {
                return(((IniCommentary)Parent.Elements[index - 1]).Comment);
            }
            return("");
        }
Exemple #5
0
 public void WriteElement(IniElement element)
 {
     if (!IniSettings.PreserveFormatting)
     {
         element.FormatDefault();
     }
     // do not write if:
     if (!(             // 1) element is a blank line AND blank lines are not allowed
             (element is IniBlankLine && !IniSettings.AllowBlankLines)
             // 2) element is an empty value AND empty values are not allowed
             || (!IniSettings.AllowEmptyValues && element is IniValue && ((IniValue)element).Value == "")))
     {
         base.WriteLine(element.Line);
     }
 }
Exemple #6
0
        public static List <IniElement> ParseText(string text)
        {
            if (text == null)
            {
                return(null);
            }
            var        ret = new List <IniElement>();
            IniElement currEl, lastEl = null;
            var        lines = text.Split(new[] { IniSettings.NewLine }, StringSplitOptions.None);

            foreach (var line in lines)
            {
                currEl = ParseLine(line);
                if (IniSettings.GroupElements)
                {
                    if (lastEl != null)
                    {
                        if (currEl is IniBlankLine && lastEl is IniBlankLine)
                        {
                            ((IniBlankLine)lastEl).LinesCount++;
                            continue;
                        }
                        if (currEl is IniCommentary && lastEl is IniCommentary)
                        {
                            ((IniCommentary)lastEl).Comment += IniSettings.NewLine + ((IniCommentary)currEl).Comment;
                            continue;
                        }
                    }
                    else
                    {
                        lastEl = currEl;
                    }
                }
                lastEl = currEl;
                ret.Add(currEl);
            }
            return(ret);
        }
Exemple #7
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);
             }
         }
     }
 }
Exemple #8
0
		public IniElement ReadNextElement()
		{
			_current = ParseLine(base.ReadLine());
			return _current;
		}
Exemple #9
0
		private string GetComment(IniElement el)
		{
			var index = Parent.Elements.IndexOf(el);
			if (index != 0 && Parent.Elements[index - 1] is IniCommentary)
				return ((IniCommentary)Parent.Elements[index - 1]).Comment;
			return "";
		}
Exemple #10
0
		private void SetComment(IniElement el, string comment)
		{
			var index = Parent.Elements.IndexOf(el);
			if (IniSettings.CommentChars.Length == 0)
				throw new NotSupportedException("Comments are currently disabled. Setup ConfigFileSettings.CommentChars property to enable them.");
			IniCommentary com;
			if (index > 0 && Parent.Elements[index - 1] is IniCommentary)
			{
				com = ((IniCommentary)Parent.Elements[index - 1]);
				if (comment == "")
					Parent.Elements.Remove(com);
				else
				{
					com.Comment = comment;
					com.Indentation = el.Indentation;
				}
			}
			else if (comment != "")
			{
				com = IniCommentary.FromComment(comment);
				com.Indentation = el.Indentation;
				Parent.Elements.Insert(index, com);
			}
		}
Exemple #11
0
		public void WriteElement(IniElement element)
		{
			if (!IniSettings.PreserveFormatting)
				element.FormatDefault();
			// do not write if: 
			if (!( // 1) element is a blank line AND blank lines are not allowed
				(element is IniBlankLine && !IniSettings.AllowBlankLines)
				// 2) element is an empty value AND empty values are not allowed
				|| (!IniSettings.AllowEmptyValues && element is IniValue && ((IniValue)element).Value == "")))
				base.WriteLine(element.Line);
		}
Exemple #12
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;
		}
Exemple #13
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;
					}
				}
			}
		}
Exemple #14
0
 public IniElement ReadNextElement()
 {
     _current = ParseLine(base.ReadLine());
     return(_current);
 }