Exemplo n.º 1
0
        internal void MergeKey(CKey inKey)
        {
            AddComments(inKey.Comments);

            TakeAllValues(inKey, false);

            List <CKey> keys = new List <CKey>(inKey._keys);

            for (int i = 0; i < keys.Count; i++)
            {
                var in_sub_key = keys[i];

                CKey child_key = null;

                if (!in_sub_key.IsArray)
                {
                    child_key = FindChildKey(in_sub_key.Name);
                }

                if (child_key != null)
                {
                    child_key.MergeKey(in_sub_key);
                }
                else
                {
                    in_sub_key.SetParent(this);
                }
            }
        }
Exemplo n.º 2
0
        public int BinaryDeserialize(byte[] ioBuffer, int inOffset)
        {
            int offset = inOffset;

            offset = BinarySerializeUtils.Deserialize(ioBuffer, offset, out _name);

            offset  = BinarySerializeUtils.Deserialize(ioBuffer, offset, out bool is_array);
            IsArray = is_array;

            offset = BinarySerializeUtils.Deserialize(ioBuffer, offset, out short size);

            _values.Clear();
            for (int i = 0; i < size; i++)
            {
                offset = AddValue(ioBuffer, offset);
            }

            offset = BinarySerializeUtils.Deserialize(ioBuffer, offset, out size);
            _keys.Clear();

            for (int i = 0; i < size; i++)
            {
                CKey child = CreateChild(this, string.Empty);
                offset = child.BinaryDeserialize(ioBuffer, offset);
            }

            return(offset);
        }
Exemplo n.º 3
0
        internal void CheckOnOneArray()
        {
            if (_keys.Count != 1)
            {
                return;
            }

            if (!_keys[0].IsArray)
            {
                return;
            }

            if (_keys[0].ValuesCount > 0)
            {
                return;
            }

            CKey arr = _keys[0];

            if (!string.IsNullOrEmpty(arr._name) &&
                !string.IsNullOrEmpty(_name))
            {
                return;
            }

            if (!string.IsNullOrEmpty(arr._name))
            {
                _name = arr._name;
            }

            TakeAllElements(arr, false);
            arr.SetParent(null);
        }
Exemplo n.º 4
0
        static void ExecuteCommand_Delete(CKey inParent, CTokenLine line, ITreeBuildSupport inSupport)
        {
            if (line.CommandParams.Length == 0 || string.IsNullOrEmpty(line.CommandParams[0]))
            {
                inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line);
                return;
            }

            string key_path = line.CommandParams[0];

            CKey start_key = inParent;
            int  i         = 0;

            while (i < key_path.Length && key_path[i] == '<' && start_key.Parent != null)
            {
                start_key = start_key.Parent;
                i++;
            }
            key_path = key_path.Substring(i);

            string[] path = key_path.Split(new char[] { '\\', '/' });

            if (!RemoveKeysByPath(start_key, path))
            {
                inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line, $"Start key: {inParent.GetPath()}");
            }
        }
Exemplo n.º 5
0
        public IKey Parse(string inFileName, string inText, ILogPrinter inLogger, object inContextData)
        {
            if (!string.IsNullOrEmpty(inFileName))
            {
                _parsed.RemoveAll(p => string.Equals(p.FileName, inFileName));
            }

            var supporter = new CSupportOwner(this, inLogger, inContextData, inFileName);

            _sentenser.ParseText(inText, supporter.GetLogger());

            List <CTokenLine> lines = new List <CTokenLine>();

            for (int i = 0; i < _sentenser.SentenseCount; i++)
            {
                CSentense  sentense = _sentenser[i];
                CTokenLine tl       = new CTokenLine();
                tl.Init(sentense, supporter.GetLogger());
                lines.Add(tl);
            }

            string root_name = Path.GetFileNameWithoutExtension(inFileName);
            CKey   root      = CTreeBuilder.Build(root_name, lines, supporter);

            _parsed.Add(new CParsed(root, lines, inFileName));

            return(root);
        }
Exemplo n.º 6
0
 public CMemoryString(string value, CKey parent, int line_number, EErrorCode error_code)
 {
     Value        = value;
     Parent       = parent;
     _line_number = line_number;
     _error_code  = error_code;
 }
Exemplo n.º 7
0
        public static IKey CreateKey(byte[] ioBuffer, int inOffset)
        {
            var key = CKey.CreateRoot(string.Empty);

            key.BinaryDeserialize(ioBuffer, inOffset);
            return(key);
        }
Exemplo n.º 8
0
 public static CKey CreateChild(CKey parent, string inName)
 {
     if (!parent.CheckNameForChild(inName))
     {
         return(null);
     }
     return(new CKey(parent, inName, false, SPosition.zero));
 }
Exemplo n.º 9
0
        static void ExecuteCommand_Insert(CKey arr_key, CTokenLine line, ITreeBuildSupport inSupport)
        {
            string file_name = line.CommandParams["file"];
            string key_path  = line.CommandParams["key"];

            if (string.IsNullOrEmpty(file_name) && string.IsNullOrEmpty(key_path))
            {
                inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line);
            }

            CKey root = null;

            if (!string.IsNullOrEmpty(file_name))
            {
                root = (CKey)inSupport.GetTree(file_name);
            }
            else
            {
                root = arr_key.GetRoot();
            }

            if (root == null)
            {
                inSupport.GetLogger().LogError(EErrorCode.CantFindInsertFile, line);
                return;
            }

            if (root.KeyCount == 1 && root.GetKey(0).IsArrayKey())
            {
                root = root.GetKey(0);
            }

            CKey key = root;

            if (!string.IsNullOrEmpty(key_path))
            {
                key = (CKey)root.FindKey(key_path);
                if (key == null)
                {
                    inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line);
                    return;
                }
            }

            bool insert_parent = line.CommandParams.ContainsKey("parent");
            CKey copy_key      = key.GetCopy() as CKey;

            if (insert_parent)
            {
                copy_key.SetParent(arr_key);
            }
            else
            {
                arr_key.TakeAllElements(copy_key, false);
            }
            arr_key.CheckOnOneArray();
        }
Exemplo n.º 10
0
 static void Swap(List <CKey> keys, int left, int right)
 {
     if (left != right)
     {
         CKey temp = keys[left];
         keys[left]  = keys[right];
         keys[right] = temp;
     }
 }
Exemplo n.º 11
0
        static void OnClosingKey(CKey key, EKeyAddingMode inKeyAddMode, ITreeBuildSupport inSupport)
        {
            if (key == null)
            {
                return;
            }

            if (key.IsEmpty)
            {
                inSupport.GetLogger().LogError(EErrorCode.HeadWithoutValues, key);
                return;
            }

            if (inKeyAddMode == EKeyAddingMode.AddUnique)
            {
                return;
            }

            CKey parent = key.Parent;

            if (parent == null)
            {
                inSupport.GetLogger().LogError(EErrorCode.KeyMustHaveParent, key);
                return;
            }

            key.SetParent(null);

            if (inKeyAddMode == EKeyAddingMode.Override)
            {
                var pathes = new List <List <string> >();
                key.GetTerminalPathes(pathes, new List <string>());

                //for correct deleting array elems
                for (int i = pathes.Count - 1; i >= 0; --i)
                {
                    var path = pathes[i];
                    RemoveKeysByPath(parent, path);
                }

                inKeyAddMode = EKeyAddingMode.Add;
            }

            if (inKeyAddMode == EKeyAddingMode.Add)
            {
                CKey child_key = parent.FindChildKey(key.Name);
                if (child_key != null)
                {
                    child_key.MergeKey(key);
                }
                else
                {
                    key.SetParent(parent);
                }
            }
        }
Exemplo n.º 12
0
        static CKey CreateNewArrayKey(CKey inParent, CTokenLine line, CBuildCommands inCommands)
        {
            var res_arr_key = CKey.CreateArrayKey(inParent, line.Position);

            if (inCommands.IsNextArrayKeyNamePresent)
            {
                res_arr_key.SetName(inCommands.PopNextArrayKeyName(inParent));
            }
            return(res_arr_key);
        }
Exemplo n.º 13
0
        static SAddLineResult AddLine(CKey inParent, CKey inCurrentArrayKey, CTokenLine line, ITreeBuildSupport inSupport, CBuildCommands inCommands)
        {
            SAddLineResult result = new SAddLineResult();

            if (line.IsEmpty())
            {
                if (line.Comments != null)
                {
                    inCommands.SetNextLineComment(line.Comments.Text, line.Position.Line, inParent);
                }
                result.current_array_key = inCurrentArrayKey;
            }
            else if (line.IsRecordDivider())
            {
                result.current_array_key = null;
                result.WasRecordDivider  = true;
            }
            else if (line.IsCommandLine())
            {
                result.current_array_key = ExecuteCommand(inParent, inCurrentArrayKey, line, inSupport, inCommands);
            }
            else if (line.Head != null)
            {
                result.current_array_key = inCurrentArrayKey;
                if (result.current_array_key == null)
                {
                    result.current_array_key = CreateNewArrayKey(inParent, line, inCommands);
                }

                if (line.AdditionMode == EKeyAddingMode.AddUnique && result.current_array_key.IsKeyWithNamePresent(line.Head.Text))
                {
                    inSupport.GetLogger().LogError(EErrorCode.ElementWithNameAlreadyPresent, line);
                }

                result.last_key_add_mode = line.AdditionMode;
                result.last_record_key   = CKey.Create(result.current_array_key, line, inSupport.GetLogger());

                if (inCommands.IsNextLineCommentPresent)
                {
                    result.last_record_key.AddComments(inCommands.PopNextLineComments(inParent));
                }
            }
            else if (!line.IsTailEmpty)
            {
                //if (!line.IsNewArrayLine && line.TailLength == 1 && !inParent.IsArray)
                //    inParent.AddTokenTail(line, true, inSupport.GetLogger());
                //else
                {
                    result.current_array_key = CreateNewArrayKey(inParent, line, inCommands);
                    result.current_array_key.AddTokenTail(line, inSupport.GetLogger());
                }
            }

            return(result);
        }
Exemplo n.º 14
0
 public void SetNextArrayKeyName(string inName, int inLineNumber, CKey inParent)
 {
     if (IsNextArrayKeyNamePresent)
     {
         _logger.LogError(EErrorCode.NextArrayKeyNameAlreadySetted, _next_array_key_name.Value, inLineNumber);
     }
     else
     {
         _next_array_key_name = new CMemoryString(inName, inParent, inLineNumber, EErrorCode.NextArrayKeyNameMissParent);
     }
 }
Exemplo n.º 15
0
 public CKey FindChildKey(string key_name)
 {
     for (int i = 0; i < _keys.Count; ++i)
     {
         CKey k = _keys[i];
         if (string.Equals(k.Name, key_name, StringComparison.InvariantCultureIgnoreCase))
         {
             return(k);
         }
     }
     return(null);
 }
Exemplo n.º 16
0
        public IKey CreateChildKey(string inName)
        {
            string name = Utils.GetStringForSave(inName);

            if (!CheckNameForChild(name))
            {
                return(null);
            }

            CKey child = CreateChild(this, name);

            return(child);
        }
Exemplo n.º 17
0
        public string PopNextArrayKeyName(CKey inParent)
        {
            if (!_next_array_key_name.CheckParent(inParent, _logger))
            {
                _next_array_key_name = null;
                return(string.Empty);
            }

            string t = _next_array_key_name.Value;

            _next_array_key_name = null;
            return(t);
        }
Exemplo n.º 18
0
        public EKeyOpResult AddChild(IKey inNewChild)
        {
            EKeyOpResult res = IsKeyAddable(inNewChild);

            if (res != EKeyOpResult.OK)
            {
                return(res);
            }

            CKey k = inNewChild as CKey;

            k.SetParent(this);
            return(res);
        }
Exemplo n.º 19
0
        void TakeAllValues(CKey other_key, bool inClear)
        {
            List <CBaseValue> lst = new List <CBaseValue>(other_key._values);

            if (inClear)
            {
                _values.Clear();
            }

            for (int i = 0; i < lst.Count; ++i)
            {
                lst[i].SetParent(this);
            }
        }
Exemplo n.º 20
0
        void TakeAllKeys(CKey other_key, bool inClear)
        {
            List <CKey> lst = new List <CKey>(other_key._keys);

            if (inClear)
            {
                _keys.Clear();
            }

            for (int i = 0; i < lst.Count; ++i)
            {
                lst[i].SetParent(this);
            }
        }
Exemplo n.º 21
0
            public bool CheckParent(CKey inParent, ILogger logger)
            {
                if (Parent == inParent)
                {
                    return(true);
                }

                logger.LogError(_error_code,
                                string.Format("Name {0}. Setted inside {1}. Try to use inside {2}",
                                              Value,
                                              Parent.Name,
                                              inParent.Name),
                                _line_number);

                return(false);
            }
Exemplo n.º 22
0
        static void ExecuteCommand_Delete(CKey arr_key, CTokenLine line, ITreeBuildSupport inSupport)
        {
            if (line.CommandParams.Length == 0 || string.IsNullOrEmpty(line.CommandParams[0]))
            {
                inSupport.GetLogger().LogError(EErrorCode.PathEmpty, line);
                return;
            }

            string key_path = line.CommandParams[0];

            string[] path = key_path.Split(new char[] { '\\', '/' });

            if (!RemoveKeysByPath(arr_key, path))
            {
                inSupport.GetLogger().LogError(EErrorCode.CantFindKey, line);
            }
        }
Exemplo n.º 23
0
        public EKeyOpResult RemoveChild(IKey inChild)
        {
            CKey k = inChild as CKey;

            if (k == null)
            {
                return(EKeyOpResult.UnnativeKey);
            }

            if (!_keys.Contains(k))
            {
                return(EKeyOpResult.NotFound);
            }

            k.SetParent(null);
            return(EKeyOpResult.OK);
        }
Exemplo n.º 24
0
        public CKey FindKey(IList <string> path, int index = 0)
        {
            string need_name = path[index];

            CKey key = FindChildKey(need_name);

            if (key == null)
            {
                return(null);
            }

            if (index == path.Count - 1)
            {
                return(key);
            }
            return(key.FindKey(path, index + 1));
        }
Exemplo n.º 25
0
        private CKey(CKey other) : base(other)
        {
            _name = other._name;

            IsArray = other.IsArray;

            foreach (var el in other._values)
            {
                var copy = el.GetCopy();
                copy.SetParent(this);
            }

            foreach (var el in other._keys)
            {
                var copy = el.GetCopy();
                copy.SetParent(this);
            }
        }
Exemplo n.º 26
0
        public EKeyOpResult InsertChild(int inIndexPos, IKey inChild)
        {
            EKeyOpResult res = IsKeyAddable(inChild);

            if (res != EKeyOpResult.OK)
            {
                return(res);
            }

            CKey k = inChild as CKey;

            k.SetParent(this);

            _keys.RemoveAt(_keys.Count - 1);
            _keys.Insert(inIndexPos, k);

            return(EKeyOpResult.OK);
        }
Exemplo n.º 27
0
        //inStartLine - next of parent line
        static void CollectByDivs(CKey inParent, int inParentRank, int inStartLine, int inEndLine, List <CTokenLine> inLines,
                                  ITreeBuildSupport inSupport, CKey inRoot, EKeyAddingMode inKeyAddingMode)
        {
            int curr_rank = inParentRank + 1;
            List <Tuple <int, int> > recs_by_divs = new List <Tuple <int, int> >();

            CollectDividers(inStartLine, inEndLine, curr_rank, inLines, inSupport, recs_by_divs);

            if (recs_by_divs.Count > 1)
            {
                if (inKeyAddingMode == EKeyAddingMode.Override)
                {
                    inParent.ClearAllArrayKeys();
                }

                for (int i = 0; i < recs_by_divs.Count; i++)
                {
                    int first_line       = recs_by_divs[i].Item1;
                    int exlude_last_line = recs_by_divs[i].Item2;
                    if (first_line < exlude_last_line)
                    {
                        CKey arr_key = CKey.CreateArrayKey(inParent, inLines[first_line].Position);

                        if (IsLinePresent(curr_rank, first_line, exlude_last_line, inLines))
                        {
                            Collect(arr_key, curr_rank, first_line, exlude_last_line, inLines, inSupport, inRoot, EKeyAddingMode.AddUnique);
                        }
                        else
                        {
                            CollectByDivs(arr_key, curr_rank, first_line, exlude_last_line, inLines, inSupport, inRoot, EKeyAddingMode.AddUnique);
                        }

                        if (arr_key.IsEmpty)
                        {
                            arr_key.SetParent(null);
                        }
                    }
                }
            }
            else
            {
                Collect(inParent, inParentRank, recs_by_divs[0].Item1, recs_by_divs[0].Item2, inLines, inSupport, inRoot, inKeyAddingMode);
            }
        }
Exemplo n.º 28
0
        public static CKey Build(string inRootName, List <CTokenLine> inLines, ITreeBuildSupport inSupport)
        {
            var root = CKey.CreateRoot(inRootName);

            if (inLines.Count == 0)
            {
                return(root);
            }

            CollectByDivs(root, -1, 0, inLines.Count, inLines, inSupport, root, EKeyAddingMode.AddUnique);

            if (root.KeyCount == 1 && root.GetKey(0).IsArrayKey() && root.GetKey(0).KeyCount == 0)
            {
                root.TakeAllElements(root.GetKey(0), false);
                root.GetKey(0).SetParent(null);
            }

            return(root);
        }
Exemplo n.º 29
0
        public string PopNextLineComments(CKey inParent)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < _next_line_comments.Count; ++i)
            {
                if (_next_line_comments[i].CheckParent(inParent, _logger))
                {
                    if (i != 0)
                    {
                        sb.AppendLine();
                    }
                    sb.Append(_next_line_comments[i].Value);
                }
            }

            _next_line_comments.Clear();
            return(sb.ToString());
        }
Exemplo n.º 30
0
        static bool RemoveKeysByPath(CKey inParent, IList <string> inPath)
        {
            CKey key = inParent.FindKey(inPath);

            if (key == null)
            {
                return(false);
            }

            CKey parent = key.Parent;

            key.SetParent(null);
            while (parent != inParent && parent.IsEmpty)
            {
                CKey prev = parent;
                parent = parent.Parent;
                prev.SetParent(null);
            }
            return(true);
        }