Exemple #1
0
 private bool TryParseCore(string content, string section, ref MyIniParseResult result)
 {
     content = content ?? "";
     if (!string.Equals(this.m_content, content, StringComparison.Ordinal))
     {
         this.Clear();
         TextPtr ptr = new TextPtr(content);
         if (section != null)
         {
             int num = FindSection(content, section);
             if (num == -1)
             {
                 if (result.IsDefined)
                 {
                     result = new MyIniParseResult(new TextPtr(content), $"Cannot find section " { section } "");
                 }
                 return(false);
             }
             ptr += num;
         }
         while (!ptr.IsOutOfBounds())
         {
             if (!this.TryParseSection(ref ptr, ref result, section == null))
             {
                 if (result.IsDefined && !result.Success)
                 {
                     return(false);
                 }
                 break;
             }
             if (section != null)
             {
                 this.m_content = null;
                 return(true);
             }
         }
         this.m_content = content;
     }
     return(true);
 }
 public bool Equals(MyIniParseResult other) =>
 ((this.LineNo == other.LineNo) && string.Equals(this.Error, other.Error));
Exemple #3
0
        private bool TryParseSection(ref TextPtr ptr, ref MyIniParseResult result, bool parseEndContent)
        {
            StringSegment segment;
            TextPtr       ptr2 = ptr;

            this.ReadPrefix(ref ptr2, out segment);
            this.m_endComment = segment;
            if (ptr2.Char != '[')
            {
                if (result.IsDefined)
                {
                    result = new MyIniParseResult(ptr, "Expected [section] definition");
                }
                return(false);
            }
            TextPtr ptr3 = ptr2.FindEndOfLine(false);

            while ((ptr3.Index > ptr2.Index) && (ptr3.Char != ']'))
            {
                ptr3 = TextPtr.op_Decrement(ptr3);
            }
            if (ptr3.Char != ']')
            {
                if (result.IsDefined)
                {
                    result = new MyIniParseResult(ptr, "Expected [section] definition");
                }
                return(false);
            }
            ptr2 = TextPtr.op_Increment(ptr2);
            StringSegment segment2 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index);
            string        str      = MyIniKey.ValidateSection(ref segment2);

            if (str == null)
            {
                ptr2 = (ptr3 + 1).SkipWhitespace(false);
                if (!ptr2.IsEndOfLine())
                {
                    if (result.IsDefined)
                    {
                        result = new MyIniParseResult(ptr2, "Expected newline");
                    }
                    return(false);
                }
                ptr2 = ptr2.FindEndOfLine(true);
                this.AddSection(ref segment2);
                if (!segment.IsEmpty)
                {
                    this.m_sectionComments[segment2] = segment;
                    this.m_endComment = new StringSegment();
                }
                while (this.TryParseItem(ref segment2, ref ptr2, ref result, parseEndContent))
                {
                }
                if (result.IsDefined && !result.Success)
                {
                    return(false);
                }
                ptr = ptr2;
                return(true);
            }
            if (result.IsDefined)
            {
                result = new MyIniParseResult(ptr2, $"Section {str}");
            }
            return(false);
        }
Exemple #4
0
        private bool TryParseItem(ref StringSegment section, ref TextPtr ptr, ref MyIniParseResult result, bool parseEndContent)
        {
            StringSegment segment;
            TextPtr       ptr3;
            TextPtr       ptr2 = ptr;

            this.ReadPrefix(ref ptr2, out segment);
            this.m_endComment = segment;
            if (ptr2.StartsWith("---"))
            {
                ptr3 = (ptr2 + 3).SkipWhitespace(false);
                if (ptr3.IsEndOfLine())
                {
                    this.m_endComment = segment;
                    ptr2 = ptr3;
                    ptr3 = new TextPtr(ptr2.Content, ptr2.Content.Length);
                    ptr  = ptr3 = ptr3;
                    if (parseEndContent)
                    {
                        ptr2 = ptr2.FindEndOfLine(true);
                        this.m_endContent = new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index);
                    }
                    return(false);
                }
            }
            ptr2 = ptr2.TrimStart();
            if (!ptr2.IsOutOfBounds() && (ptr2.Char != '['))
            {
                ptr3 = ptr2.FindInLine('=');
                if (ptr3.IsOutOfBounds())
                {
                    if (result.IsDefined)
                    {
                        result = new MyIniParseResult(ptr2, "Expected key=value definition");
                    }
                    return(false);
                }
                StringSegment segment2 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.TrimEnd().Index - ptr2.Index);
                string        str      = MyIniKey.ValidateKey(ref segment2);
                if (str == null)
                {
                    ptr2 = ptr3 + 1;
                    ptr2 = ptr2.TrimStart();
                    ptr3 = ptr2.FindEndOfLine(false);
                    StringSegment segment3 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.TrimEnd().Index - ptr2.Index);
                    if (segment3.Length == 0)
                    {
                        TextPtr ptr4 = ptr3.FindEndOfLine(true);
                        if (ptr4.Char == '|')
                        {
                            TextPtr ptr5 = ptr4;
                            do
                            {
                                ptr4 = ptr5.FindEndOfLine(false);
                                ptr5 = ptr4.FindEndOfLine(true);
                            }while (ptr5.Char == '|');
                            ptr3 = ptr4;
                        }
                        segment3 = new StringSegment(ptr2.Content, ptr2.Index, ptr3.Index - ptr2.Index);
                    }
                    MyIniKey key = new MyIniKey(section, segment2);
                    if (this.m_items.ContainsKey(key))
                    {
                        if (result.IsDefined)
                        {
                            result = new MyIniParseResult(new TextPtr(segment2.Text, segment2.Start), $"Duplicate key {key}");
                        }
                        return(false);
                    }
                    this.m_items[key] = segment3;
                    if (!segment.IsEmpty)
                    {
                        this.m_itemComments[key] = segment;
                        this.m_endComment        = new StringSegment();
                    }
                    ptr = ptr3.FindEndOfLine(true);
                    return(true);
                }
                if (result.IsDefined)
                {
                    result = new MyIniParseResult(ptr2, $"Key {str}");
                }
            }
            return(false);
        }
Exemple #5
0
 public bool TryParse(string content, string section, out MyIniParseResult result)
 {
     result = new MyIniParseResult(new TextPtr(content), null);
     return(this.TryParseCore(content, section, ref result));
 }
Exemple #6
0
        public bool TryParse(string content, string section)
        {
            MyIniParseResult result = new MyIniParseResult();

            return(this.TryParseCore(content, section, ref result));
        }
Exemple #7
0
 public bool TryParse(string content, out MyIniParseResult result) =>
 this.TryParse(content, null, out result);
Exemple #8
0
        public bool TryParse(string content)
        {
            MyIniParseResult result = new MyIniParseResult();

            return(this.TryParseCore(content, null, ref result));
        }