Esempio n. 1
0
 /// <summary>This removes a field with given name.</summary>
 public static void RemoveField(UniFields fields, string key)
 {
     if (fields == null || !fields.ContainsKey(key))
     {
         return;
     }
     fields.Remove(key);
 }
Esempio n. 2
0
 /// <summary>This removes fields with given names.</summary>
 public static void RemoveFields(UniFields fields, IEnumerable <string> keys)
 {
     if (fields == null)
     {
         return;
     }
     foreach (string key in keys)
     {
         if (fields.ContainsKey(key))
         {
             fields.Remove(key);
         }
     }
 }
Esempio n. 3
0
 // String
 public static void SetString(UniFields fields, string key, string value, string defaultvalue)
 {
     if (fields == null)
     {
         return;
     }
     if (value != defaultvalue)
     {
         if (!fields.ContainsKey(key))
         {
             fields.Add(key, new UniValue(UniversalType.String, value));
         }
         else
         {
             fields[key].Value = value;
         }
     }
     // Don't save default value
     else if (fields.ContainsKey(key))
     {
         fields.Remove(key);
     }
 }
Esempio n. 4
0
        /// <summary>This compares types and values of given UniFields by key.</summary>
        public static bool ValuesMatch(string key, UniFields fields1, UniFields fields2)
        {
            bool f1 = fields1.ContainsKey(key);
            bool f2 = fields2.ContainsKey(key);

            if (!f1 && !f2)
            {
                return(true);
            }
            if (f1 != f2)
            {
                return(false);
            }
            return(UniValuesMatch(fields1[key], fields2[key]));
        }
Esempio n. 5
0
 /// <summary>This compares all fields.</summary>
 public static bool AllFieldsMatch(UniFields fields1, UniFields fields2)
 {
     if (fields1.Keys.Count != fields2.Keys.Count)
     {
         return(false);
     }
     foreach (KeyValuePair <string, UniValue> group in fields1)
     {
         if (!fields2.ContainsKey(group.Key) || !UniValuesMatch(fields1[group.Key], fields2[group.Key]))
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 6
0
        // ano - for calling from outside, makes sure to make an undo snapshot
        // can throw ArgumentException if the new value's type
        // is not int, float, string, or bool
        // or the value is null
        public void SetField(string key, object value)
        {
            if (key == null)
            {
                return;
            }
            if (IsDisposed)
            {
                return;
            }

            key = UniValue.ValidateName(key);

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

            if (fields.ContainsKey(key))
            {
                // ano - we don't want to record an undo for
                // this element if the values aren't changing
                if (fields[key].Value == value)
                {
                    return;
                }

                BeforeFieldsChange();

                Type new_type = value.GetType();
                if (!fields[key].Value.GetType().IsAssignableFrom(new_type))
                {
                    if (value is float)
                    {
                        fields[key].Type = (int)UniversalType.Float;
                    }
                    else if (value is int)
                    {
                        fields[key].Type = (int)UniversalType.Integer;
                    }
                    else if (value is bool)
                    {
                        fields[key].Type = (int)UniversalType.Boolean;
                    }
                    else if (value is string)
                    {
                        fields[key].Type = (int)UniversalType.String;
                    }

                    // ano - other cases are handled by the argument exception
                }
                // ano - ArgumentException can be thrown here
                fields[key].Value = value;
            }
            else
            {
                UniValue new_field = new UniValue();

                // ano - ArgumentException can be thrown here
                new_field.Value = value;

                // ano - undo snapshot recorded after exception
                // in this case, because we can
                BeforeFieldsChange();

                // ano - other cases are handled by the ArgumentException before
                if (value is float)
                {
                    new_field.Type = (int)UniversalType.Float;
                }
                else if (value is int)
                {
                    new_field.Type = (int)UniversalType.Integer;
                }
                else if (value is bool)
                {
                    new_field.Type = (int)UniversalType.Boolean;
                }
                else if (value is string)
                {
                    new_field.Type = (int)UniversalType.String;
                }

                fields.Add(key, new_field);
            }
        }