Example #1
0
 /// <summary>
 /// Creates a copy of the key.
 /// </summary>
 /// <param name="key">The Key to be duplicated.</param>
 /// <param name="parent">The Node that will parent the key.</param>
 /// <returns></returns>
 public static VDFKey Duplicate(this VDFKey key, VDFNode parent)
 {
     if (parent == null)
     {
         throw new ArgumentNullException("parent");
     }
     return(new VDFKey(key.Name, key.Value, parent)); //Return a new instance of a key class. Copy the name and value of the original key but set the parent of the clone to the parent argument passed to this method.
 }
Example #2
0
 /// <summary>
 /// Removes key from its parent.
 /// </summary>
 /// <param name="key">The key to be removed.</param>
 /// <param name="throwErrorOnNoParent">Throw an error if the parent property of the key is set to null.</param>
 public static void RemoveKeyFromNode(this VDFKey key, bool throwErrorOnNoParent = false)
 {
     if (key.Parent != null)
     {
         key.Parent.Keys.Remove(key);
         key.Parent = null;
     }
     else if (key.Parent == null && throwErrorOnNoParent)
     {
         throw new NullReferenceException("Key " + key.Name + " parent property is not set!");
     }
 }
Example #3
0
        /// <summary>
        /// Cleanly removes a key from the list of children of the parent node.
        /// </summary>
        /// <param name="keys">The collection of keys to search through.</param>
        /// <param name="Name">The name of the key that the method needs to find.</param>
        /// <param name="CaseSensitive">Indicates if the name argument and the name of the key needs to be an exact match in terms of capitalization.</param>
        /// <param name="throwErrorIfNotFound">Throw an exception if the key could not be found.</param>
        /// <returns></returns>
        public static void CleanRemoveKey(this List <VDFKey> keys, string Name, bool CaseSensitive = false, bool throwErrorIfNotFound = false)
        {
            int i = keys.FindBaseTokenIndex(Name, CaseSensitive, throwErrorIfNotFound); //Find our node from the nodes list by calling the FindBaseTokenIndex method and pass on the arguments.

            if (i == -1)                                                                //The FindBaseTokenIndex method will do the error handling for us. However, if the argument throwErrorIfNotFound is set to false, it wouldn't do that so what'll do is exit from this method if the func returns -1.
            {
                return;
            }
            VDFKey tKey = keys[i];//cache our node.

            tKey.Parent = null;
            keys.RemoveAt(i);
        }
        /// <summary>
        /// Try to create a new token or key from the string in the StringBuilder and PreviousString.
        /// </summary>
        /// <param name="MustHaveNewToken">Indicates if a new token is required to be created using the StringBuilder.</param>
        void TryNewTokenOrKey(bool MustHaveNewToken)
        {
            if (previousString != null)             //If we already have a string before this one that isn't part of another key or isn't the name of another node.
            {
                string newToken = "";               //This will contain the value of our new token.
                if (MustHaveNewToken && sb != null) //If we are required to have a new token and the StringBuilder isn't empty.
                {
                    newToken = sb.ToString();       //Set the newToken to the value of the StringBuilder.
                }
                else if (!MustHaveNewToken)         //If we aren't required to have a new token.
                {
                    if (sb == null)                 //No token was found in between calls of this method.
                    {
                        return;
                    }
                    else
                    {
                        newToken = sb.ToString();
                    }
                }

                //Since we have a unhandled previousString, it means that the current token we are working on and the previous one is a key-value pair. If that's the case, then it needs to be inside a node so we check that.
                if (currentNode == null)
                {
                    throw new VDFStreamException("Key-value pair must be inside a node!", lineCounter, characterCount);
                }
                VDFKey key = new VDFKey(previousString, newToken, currentNode); //key name first (previousString) before its value
                currentNode.Keys.Add(key);
                previousString = null;                                          //We already know that the previous String is the name of the key and it has already been parsed, so set the referencing variable to null.
                sb             = null;                                          //We do the same with stringbuilder.
            }
            else //If we haven't found any string yet or the previous strings have already been parsed.
            {
                if (sb == null)
                {
                    if (MustHaveNewToken) //If we are required to have a new token and the stringbuilder is null.
                    {
                        throw new VDFStreamException("Node name or Key name is set to null!", lineCounter, characterCount);
                    }
                    else
                    {
                        return;
                    }
                }
                SetPreviousStringToStringBuilder(); //We do not know if this string is a name of a node or a name of a key so lets just store it for now and proceed to the next characters in order to find out.
            }
        }
Example #5
0
 /// <summary>
 /// Moves the key to another node while making sure that the Parent property is set correctly and that the key is removed from the previous parent's key list and added to the new parent's key list.
 /// </summary>
 /// <param name="key">The key to be moved.</param>
 /// <param name="newParent">The new parent of the key.</param>
 public static void Migrate(this VDFKey key, VDFNode newParent)
 {
     if (newParent == null)
     {
         throw new ArgumentNullException("parent");
     }
     if (key.Parent == null)
     {
         throw new NullReferenceException("The Parent property of key " + key.Name + " is set to null!");
     }
     key.Parent.Keys.Remove(key);
     key.Parent = newParent;
     if (newParent.Keys == null) //If the newParent node's keys list is not yet created, we will have to instantiate it ourselves.
     {
         newParent.Keys = new List <VDFKey>();
     }
     newParent.Keys.Add(key);
 }