Beispiel #1
0
        private static int FindSection(string config, string section)
        {
            TextPtr ptr = new TextPtr(config);

            if (!MatchesSection(ref ptr, section))
            {
                while (!ptr.IsOutOfBounds())
                {
                    ptr = TextPtr.op_Increment(ptr.Find("\n"));
                    if (ptr.Char == '[')
                    {
                        if (MatchesSection(ref ptr, section))
                        {
                            return(ptr.Index);
                        }
                    }
                    else if (ptr.StartsWith("---"))
                    {
                        ptr = (ptr + 3).SkipWhitespace(false);
                        if (ptr.IsEndOfLine())
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                return(ptr.Index);
            }
            return(-1);
        }
Beispiel #2
0
        private void ReadPrefix(ref TextPtr ptr, out StringSegment prefix)
        {
            bool    flag = false;
            TextPtr ptr2 = ptr;

            while (!ptr.IsOutOfBounds())
            {
                if (ptr.IsStartOfLine() && (ptr.Char == ';'))
                {
                    if (!flag)
                    {
                        flag = true;
                        ptr2 = ptr;
                    }
                    ptr = ptr.FindEndOfLine(false);
                }
                TextPtr ptr3 = ptr.SkipWhitespace(false);
                if (!ptr3.IsNewLine())
                {
                    break;
                }
                if (ptr3.Char == '\r')
                {
                    ptr = ptr3 + 2;
                }
                else
                {
                    ptr = ptr3 + 1;
                }
            }
            if (flag)
            {
                TextPtr ptr4 = ptr;
                while (char.IsWhiteSpace(ptr4.Char) && (ptr4 > ptr2))
                {
                    ptr4 = TextPtr.op_Decrement(ptr4);
                }
                int length = ptr4.Index - ptr2.Index;
                if (length > 0)
                {
                    prefix = new StringSegment(ptr.Content, ptr2.Index, length);
                    return;
                }
            }
            prefix = new StringSegment();
        }
Beispiel #3
0
        public bool TryParse(string argument)
        {
            this.Clear();
            if (string.IsNullOrEmpty(argument))
            {
                return(false);
            }
            TextPtr ptr = new TextPtr(argument);

Label_001D:
            ptr = ptr.SkipWhitespace(false);
            if (!ptr.IsOutOfBounds())
            {
                if (!this.TryParseSwitch(ref ptr))
                {
                    this.ParseParameter(ref ptr);
                }
                goto Label_001D;
            }
            return(this.Items.Count > 0);
        }
Beispiel #4
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);
 }
Beispiel #5
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);
        }