Exemplo n.º 1
0
        private int _getInsertIndex(LibconfigArray confElement)
        {
            if (_writeArrays.Count == 0)
            {
                return(((LibconfigKeyValue)Output).Value.Line + ((LibconfigKeyValue)Output).Value.Length - 1);
            }

            var last = _writeArrays.LastOrDefault(p => !p.Added);

            if (last == null)
            {
                return(((LibconfigKeyValue)Output).Value.Line + ((LibconfigKeyValue)Output).Value.Length - 1);
            }

            int lineIndex = last.Line + last.Length;

            int arrayIndex = _writeArrays.IndexOf(confElement);

            arrayIndex++;

            while (arrayIndex < _writeArrays.Count && _writeArrays[arrayIndex].Added)
            {
                arrayIndex++;
            }

            if (arrayIndex < _writeArrays.Count - 1)
            {
                return(_writeArrays[arrayIndex].Line);
            }

            return(lineIndex);
        }
Exemplo n.º 2
0
        public void Write(string key, string line)
        {
            if (!_list)
            {
                LibconfigKeyValue conf = _writeKeyValues.FirstOrDefault(p => p.Key == key);

                if (conf == null)
                {
                    // Add a new one!
                    _writeKeyValues.Add(new LibconfigKeyValue(key, Int32.MaxValue)
                    {
                        Value  = new LibconfigString(line, Int32.MaxValue),
                        Added  = true,
                        Length = 1
                    });

                    return;
                }

                conf.Modified = true;
                conf.Value    = new LibconfigString(line, conf.Value.Line);
            }
            else
            {
                LibconfigArray conf = _writeArrays.FirstOrDefault(p => p[_idKey] == key);

                if (conf == null)
                {
                    _writeArrays.Add(new LibconfigArray(Int32.MaxValue)
                    {
                        Objects = new List <LibconfigObject> {
                            new LibconfigKeyValue(_idKey, Int32.MaxValue)
                            {
                                Value = new LibconfigString(key, Int32.MaxValue),
                            },
                            new LibconfigKeyValue("Content__", Int32.MaxValue)
                            {
                                Value = new LibconfigString(line, Int32.MaxValue),
                            }
                        },
                        Added  = true,
                        Length = 1
                    });

                    return;
                }

                conf.Modified = true;
                conf.Objects  = new List <LibconfigObject> {
                    new LibconfigKeyValue(_idKey, Int32.MaxValue)
                    {
                        Value = new LibconfigString(key, Int32.MaxValue),
                    },
                    new LibconfigKeyValue("Content__", Int32.MaxValue)
                    {
                        Value = new LibconfigString(line, Int32.MaxValue),
                    }
                };
            }
        }
Exemplo n.º 3
0
        public void Delete <TKey>(TKey key)
        {
            var sKey = key.ToString();

            if (!_list)
            {
                LibconfigKeyValue conf = _writeKeyValues.FirstOrDefault(p => p.Key == sKey);

                if (conf != null)
                {
                    for (int i = 0; i < conf.Length; i++)
                    {
                        _allLines[i + conf.Line - 1] = null;
                    }
                }
            }
            else
            {
                LibconfigArray conf = _writeArrays.FirstOrDefault(p => p[_idKey] == sKey);

                if (conf != null)
                {
                    for (int i = 0; i < conf.Length; i++)
                    {
                        _allLines[i + conf.Line - 1] = null;
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void _parse(byte[] buffer)
        {
            try {
                _buffer      = buffer;
                _bufferIndex = 0;

                while (_bufferIndex < buffer.Length)
                {
                    switch ((char)buffer[_bufferIndex])
                    {
                    case '/':
                        if (buffer[_bufferIndex + 1] == '/')
                        {
                            _skipLine();
                            continue;
                        }

                        if (buffer[_bufferIndex + 1] == '*')
                        {
                            _skipCommentBlock();
                            continue;
                        }
                        break;

                    case ':':
                        LibconfigKeyValue keyValue = new LibconfigKeyValue(_word, Line);

                        if (_latest == null && Output != null)
                        {
                            // The file contains multiple arrays
                            LibconfigArray tempList = new LibconfigArray(Line);
                            tempList.AddElement(Output);
                            Output.Parent = tempList;
                            Output        = tempList;
                            _latest       = tempList;
                        }

                        if (_latest == null)
                        {
                            Output  = keyValue;
                            _latest = Output;
                        }
                        else
                        {
                            keyValue.Parent = _latest;

                            switch (_latest.ConfType)
                            {
                            case LibconfigTypes.Array:
                                ((LibconfigArray)_latest).AddElement(keyValue);
                                _latest = keyValue;
                                break;

                            default:
                                throw new Exception("Expected an Array.");
                            }
                        }

                        break;

                    case '<':
                        _readMultilineQuote();

                        if (_latest != null)
                        {
                            switch (_latest.ConfType)
                            {
                            case LibconfigTypes.KeyValue:
                                ((LibconfigKeyValue)_latest).Value = new LibconfigString(_word, Line);
                                _latest.Length = Line - _latest.Line + 1;
                                _latest        = _latest.Parent;
                                break;

                            default:
                                throw new Exception("Expected a KeyValue.");
                            }
                        }
                        break;

                    case '(':
                        LibconfigList list = new LibconfigList(Line);

                        if (_latest == null)
                        {
                            throw new Exception("Trying to open a List type without a parent.");
                        }

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.List:
                            ((LibconfigList)_latest).AddElement(list);
                            list.Parent = _latest;
                            _latest     = list;
                            break;

                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = list;
                            list.Parent = _latest;
                            _latest     = list;
                            break;

                        default:
                            throw new Exception("Expected a KeyValue.");
                        }

                        break;

                    case '{':
                        LibconfigArray array = new LibconfigArray(Line);

                        if (_latest == null)
                        {
                            // Used for copy pasting inputs, create a temporary list
                            Output = new LibconfigKeyValue("copy_paste", Line)
                            {
                                Value = new LibconfigList(Line)
                            };

                            _latest = Output["copy_paste"];
                        }

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.List:
                            ((LibconfigList)_latest).AddElement(array);
                            array.Parent = _latest;
                            _latest      = array;
                            break;

                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = array;
                            array.Parent = _latest;
                            _latest      = array;
                            break;

                        default:
                            throw new Exception("Expected a List.");
                        }

                        break;

                    case '[':
                        LibconfigAggregate aggregate = new LibconfigAggregate(Line);

                        if (_latest == null)
                        {
                            throw new Exception("Trying to open an Aggregate type without a parent.");
                        }

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = aggregate;
                            _latest.Length   = Line - _latest.Line + 1;
                            aggregate.Parent = _latest;
                            _latest          = aggregate;
                            break;

                        default:
                            throw new Exception("Expected a KeyValue.");
                        }

                        break;

                    case ']':
                    case ')':
                    case '}':
                        if (_latest == null)
                        {
                            throw new Exception("Trying to close a statement without knowing its beginning.");
                        }

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.Aggregate:
                        case LibconfigTypes.Array:
                        case LibconfigTypes.List:
                            _latest.Length = Line - _latest.Line + 1;
                            _latest        = _latest.Parent;

                            if (_latest is LibconfigKeyValue)
                            {
                                _latest = _latest.Parent;
                            }
                            break;

                        case LibconfigTypes.KeyValue:
                            _latest        = _latest.Parent;
                            _latest.Length = Line - _latest.Line + 1;
                            break;

                        default:
                            throw new Exception("Expected a KeyValue or an Array.");
                        }

                        break;

                    case '\"':
                        _readQuote();

                        if (_latest == null)
                        {
                            throw new Exception("Trying to read a quote without a parent.");
                        }

                        switch (_latest.ConfType)
                        {
                        case LibconfigTypes.KeyValue:
                            ((LibconfigKeyValue)_latest).Value = new LibconfigString(_word, Line);
                            _latest.Length = Line - _latest.Line + 1;
                            _latest        = _latest.Parent;
                            break;

                        case LibconfigTypes.List:
                            ((LibconfigList)_latest).AddElement(new LibconfigString(_word, Line));
                            break;

                        default:
                            throw new Exception("Expected a KeyValue.");
                        }

                        continue;

                    case ',':
                    case '\t':
                    case ' ':
                    case '\r':
                        break;

                    case '\n':
                        Line++;
                        break;

                    default:
                        _readWord();

                        if (_word == "")
                        {
                            throw new Exception("Null-length word. This is most likely caused by an unexpected character in a string.");
                        }

                        if (_buffer[_bufferIndex] == ':')
                        {
                            continue;
                        }

                        if (_latest != null)
                        {
                            switch (_latest.ConfType)
                            {
                            case LibconfigTypes.KeyValue:
                                ((LibconfigKeyValue)_latest).Value = new LibconfigString(_word, Line);
                                _latest.Length = Line - _latest.Line + 1;
                                _latest        = _latest.Parent;
                                break;

                            case LibconfigTypes.List:
                            case LibconfigTypes.Aggregate:
                                ((LibconfigArrayBase)_latest).AddElement(new LibconfigString(_word, Line));
                                break;

                            default:
                                // It will be handled by the ':' parsing.
                                break;
                            }
                        }

                        continue;
                    }

                    _bufferIndex++;
                }
            }
            catch (Exception err) {
                throw new Exception("Failed to parse " + _file + " at line " + Line + ", position " + LinePosition, err);
            }
        }