示例#1
0
 public bool RemoveKeyName(KeyValuesData keyData)
 {
     if (KeyNameValues.Remove(keyData))
     {
         keyData.Parent = null;
         return(true);
     }
     return(false);
 }
示例#2
0
 public void Set(KeyValuesData kvData)
 {
     if (!kvData.IsValid())
     {
         return;
     }
     kvData.Parent = this;
     KeyNameValues.Add(kvData);
 }
示例#3
0
 public bool RemoveKeyName(int index)
 {
     if (index >= KeyNameValues.Count || index < 0)
     {
         return(false);
     }
     KeyNameValues[index].Parent = null;
     KeyNameValues.RemoveAt(index);
     return(true);
 }
示例#4
0
 public string GetValue(string keyName, string defaultValue)
 {
     if (string.IsNullOrEmpty(keyName))
     {
         return(defaultValue);
     }
     foreach (var t in KeyNameValues.Where(t => t.Key == keyName))
     {
         return(t.Value);
     }
     return(defaultValue);
 }
示例#5
0
        public uint RemoveAllKeyNames(uint startPos)
        {
            uint count = 0;

            for (uint i = startPos; i < KeyNameValues.Count; i++)
            {
                KeyNameValues[(int)i].Parent = null;
                count++;
            }
            KeyNameValues.Clear();
            return(count);
        }
示例#6
0
        /// <summary>
        ///     Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use
        ///     in hashing algorithms and data structures like a hash table.
        /// </summary>
        /// <returns>
        ///     A hash code for the current <see cref="T:System.Object"></see>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            // Getting hash codes from volatile variables doesn't seem a good move... //TODO Find an immutable way?
            var result = Name?.GetHashCode() ?? 0;

            result = (result * 397) ^ (KeyNameValues?.GetHashCode() ?? 0);
            result = (result * 397) ^ (KeyChilds?.GetHashCode() ?? 0);
            result = (result * 397) ^ (FirstParent?.GetHashCode() ?? 0);
            result = (result * 397) ^ (Parent?.GetHashCode() ?? 0);
            result = (result * 397) ^ NextSubKeyIndex.GetHashCode();
            result = (result * 397) ^ NextKeyValueIndex.GetHashCode();
            result = (result * 397) ^ DistanceFromTop.GetHashCode();
            return(result);
        }
示例#7
0
 /// <summary>
 /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use in hashing algorithms and data structures like a hash table.
 /// </summary>
 /// <returns>
 /// A hash code for the current <see cref="T:System.Object"></see>.
 /// </returns>
 /// <filterpriority>2</filterpriority>
 public override int GetHashCode()
 {
     unchecked
     {
         int result = (Name != null ? Name.GetHashCode() : 0);
         result = (result * 397) ^ (KeyNameValues != null ? KeyNameValues.GetHashCode() : 0);
         result = (result * 397) ^ (KeyChilds != null ? KeyChilds.GetHashCode() : 0);
         result = (result * 397) ^ (FirstParent != null ? FirstParent.GetHashCode() : 0);
         result = (result * 397) ^ (Parent != null ? Parent.GetHashCode() : 0);
         result = (result * 397) ^ NextSubKeyIndex.GetHashCode();
         result = (result * 397) ^ NextKeyValueIndex.GetHashCode();
         result = (result * 397) ^ DistanceFromTop.GetHashCode();
         return(result);
     }
 }
示例#8
0
 public string GetComment(string keyName, string defaultValue)
 {
     if (keyName == null)
     {
         throw new ArgumentNullException(nameof(keyName));
     }
     if (string.IsNullOrEmpty(keyName))
     {
         return(defaultValue);
     }
     foreach (var t in KeyNameValues.Where(t => t.Key == keyName))
     {
         return(t.Comment);
     }
     return(defaultValue);
 }
示例#9
0
 public bool RemoveKeyName(string keyName)
 {
     if (string.IsNullOrEmpty(keyName))
     {
         return(false);
     }
     for (int i = 0; i < KeyNameValues.Count; i++)
     {
         if (KeyNameValues[i].Key == keyName)
         {
             KeyNameValues[i].Parent = null;
             KeyNameValues.RemoveAt(i);
             return(true);
         }
     }
     return(true);
 }
示例#10
0
        public void SetString(string keyName, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                value = "";
            }
            for (int i = 0; i < KeyNameValues.Count; i++)
            {
                if (KeyNameValues[i].Key == keyName)
                {
                    KeyNameValues[i].Value = value;
                    return;
                }
            }
            KeyValuesData kvValue = new KeyValuesData(keyName, value, null, this);

            KeyNameValues.Add(kvValue);
        }
示例#11
0
        public void SetString(string keyName, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                value = "";
            }
            foreach (var t in KeyNameValues)
            {
                if (t.Key != keyName)
                {
                    continue;
                }
                t.Value = value;
                return;
            }
            var kvValue = new KeyValuesData(keyName, value, null, this);

            KeyNameValues.Add(kvValue);
        }
示例#12
0
        /// <summary>
        /// Create a KeyValue from List wich contains file lines
        /// </summary>
        /// <returns>
        /// true if the sucessfully load (KeyValue is valid); otherwise, false.
        /// </returns>
        /// <param name="stream">List collection wich contain file lines</param>
        /// <param name="startPos">Start parse List in that position</param>
        /// <param name="endPos">Return end position index</param>
        private bool LoadFromList(List <string> stream, uint startPos, ref uint endPos)
        {
            if (stream == null)
            {
                return(false);
            }
            bool   wasQuoted   = false;
            bool   wasComment  = false;
            string lastComment = null;
            bool   wasName     = false;

            endPos = 0;
            for (uint i = startPos; i < stream.Count; i++)
            {
                KeyValuePair <string, string> kvPair = ReadToken(stream[(int)i], ref wasQuoted, ref wasComment);
                if (string.IsNullOrEmpty(kvPair.Key))
                {
                    continue;
                }
                endPos = i;
                // Is the end of KeyValues Class?
                if (kvPair.Key == "}")
                {
                    endPos = i + 1;
                    return(Name == null ? false : true);
                }
                // This line is a comment
                if (wasComment)
                {
                    lastComment = kvPair.Value;
                    continue;
                }
                // KeyValue Name not yet seted!
                if (!wasName)
                {
                    if (!string.IsNullOrEmpty(kvPair.Value))
                    {
                        return(false);
                    }
                    Name = kvPair.Key;
                    if (!string.IsNullOrEmpty(lastComment))
                    {
                        Comment = lastComment;
                    }
                    wasName     = true;
                    lastComment = null;
                    uint beganIndex = RetriveIndex(stream, "{", i + 1);
                    if (beganIndex == 0)
                    {
                        return(false);
                    }
                    i = beganIndex;
                    continue;
                }
                // Begin a new KeyValue Class
                if (kvPair.Key == "{")
                {
                    if (!wasQuoted)
                    {
                        lastComment = null;
                        KeyValues kvChild = new KeyValues("NewName");
                        if (!kvChild.LoadFromList(stream, i - 1, ref endPos))
                        {
                            continue;
                        }
                        Update(kvChild, true);
                        KeyChilds.Add(kvChild);
                        if (endPos >= stream.Count)
                        {
                            return(true);
                        }
                        i = endPos;
                        continue;
                    }
                }
                // Is a KeyValue    "Key"       "Value"
                if (string.IsNullOrEmpty(kvPair.Value))
                {
                    continue;
                }
                KeyValuesData kvData = new KeyValuesData(kvPair.Key, kvPair.Value, lastComment, this);
                KeyNameValues.Add(kvData);
                lastComment = null;
            }
            return(true);
        }
示例#13
0
 /// <summary>
 ///     Check if exists a KeyName under that KeyValues Class
 /// </summary>
 /// <returns>
 ///     true if exists; otherwise, false.
 /// </returns>
 /// <param name="keyName">keyName to check.</param>
 public bool ExistsKey(string keyName)
 {
     return(KeyNameValues.Any(t => t.Key == keyName));
 }
示例#14
0
        /// <summary>
        ///     Create a KeyValue from List wich contains file lines
        /// </summary>
        /// <returns>
        ///     true if the sucessfully load (KeyValue is valid); otherwise, false.
        /// </returns>
        /// <param name="stream">List collection wich contain file lines</param>
        /// <param name="startPos">Start parse List in that position</param>
        /// <param name="endPos">Return end position index</param>
        private bool LoadFromList(List <string> stream, uint startPos, ref uint endPos)
        {
            if (stream == null)
            {
                return(false);
            }
            string lastComment = null;
            var    wasName     = false;

            endPos = 0;
            for (var i = startPos; i < stream.Count; i++)
            {
                bool wasQuoted;
                bool wasComment;
                var  kvPair = ReadToken(stream[(int)i], out wasQuoted, out wasComment);
                if (string.IsNullOrEmpty(kvPair.Key))
                {
                    continue;
                }
                endPos = i;
                // Is the end of KeyValues Class?
                if (kvPair.Key == "}")
                {
                    endPos = i + 1;
                    return(Name != null);
                }
                // This line is a comment
                if (wasComment)
                {
                    lastComment = kvPair.Value;
                    continue;
                }
                // KeyValue Name not yet seted!
                if (!wasName)
                {
                    if (!string.IsNullOrEmpty(kvPair.Value))
                    {
                        return(false);
                    }
                    Name = kvPair.Key;
                    if (!string.IsNullOrEmpty(lastComment))
                    {
                        Comment = lastComment;
                    }
                    wasName     = true;
                    lastComment = null;
                    var beganIndex = RetriveIndex(stream, "{", i + 1);
                    if (beganIndex == 0)
                    {
                        return(false);
                    }
                    i = beganIndex;
                    continue;
                }
                // special include macro (not a key name)
                if (kvPair.Key.StartsWith("#include") && !wasQuoted)
                {
                    lastComment = null;
                    if (string.IsNullOrEmpty(kvPair.Value))
                    {
                        continue;
                    }
                    AddIncludeFile(kvPair.Value, true, true);
                    continue;
                }
                // Begin a new KeyValue Class
                if (kvPair.Key == "{")
                {
                    if (!wasQuoted)
                    {
                        lastComment = null;
                        var kvChild = new KeyValues("NewName");
                        if (!kvChild.LoadFromList(stream, i - 1, ref endPos))
                        {
                            continue;
                        }
                        Update(kvChild, true);
                        KeyChilds.Add(kvChild);
                        if (endPos >= stream.Count)
                        {
                            return(true);
                        }
                        i = endPos;
                        continue;
                    }
                }
                // Is a KeyValue    "Key"       "Value"
                if (string.IsNullOrEmpty(kvPair.Value))
                {
                    continue;
                }
                var kvData = new KeyValuesData(kvPair.Key, kvPair.Value, lastComment, this);
                KeyNameValues.Add(kvData);
                lastComment = null;
            }
            return(true);
        }