/// <summary> /// If the field exists in the config, it replaces it with <paramref name="fieldValue"/>. Otherwise, it is added /// to the config. If you dont want to add it, set the 3rd parameter to false /// </summary> /// <param name="fieldName"></param> /// <param name="fieldValue"></param> public void SetString(string fieldName, string fieldValue, bool addIfNotFound = true) { if (TryGetString(fieldName, out string unused)) { StringValues[fieldName] = fieldValue; } else if (addIfNotFound) { StringValues.Add(fieldName, fieldValue); } }
/// <summary> /// The exact same a <see cref="SetString(string, string, bool)"/> except it uses enums making it easier to use /// </summary> /// <typeparam name="TEnum"></typeparam> /// <param name="fieldName"></param> /// <param name="value"></param> /// <returns></returns> public void SetEnum <TEnum>(string fieldName, TEnum enumValue, bool addIfNotFound = true) where TEnum : struct { if (TryGetString(fieldName, out string unused)) { StringValues[fieldName] = enumValue.ToString(); } else if (addIfNotFound) { StringValues.Add(fieldName, enumValue.ToString()); } }
/// <summary> /// Handles setting of a value in the StringValues Dictionary /// </summary> /// <param name="value">Value to Set</param> /// <param name="name">Compiler Generated Name of the Property</param> public void SetString(string value, [CallerMemberName] string name = null) { if (name == null) { return; } if (StringValues.ContainsKey(name)) { StringValues[name] = value; } else { StringValues.Add(name, value); } }
private T ParseValue(string str) { if (string.IsNullOrWhiteSpace(str)) { str = string.Empty; } double val; bool isok = double.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out val); if (!isok) { DateTime dval; bool dateok = DateTime.TryParseExact(str, DateFormat, null, DateTimeStyles.None, out dval); if (dateok) { DateTime def = new DateTime(); val = (dval - def).TotalDays; } else { if (!StringValues.ContainsKey(str)) { int ival = -1; if (!string.IsNullOrWhiteSpace(str)) { var hash = ComputeMd5Hash(GetBytes(str)); if (!UseLongConverter) { hash = Compute4BytesHash(hash); } ival = (ushort)BitConverter.ToInt32(hash, 0); } StringValues.Add(str, ival); } val = StringValues[str]; } } var fval = (T)Convert.ChangeType(val, typeof(T)); return(fval); }
public bool InsertValue(ASTExpressionBase key, ASTExpressionBase value) { if (key == null) { AutoIntegerValues.Add(AutoIntegerValues.Count, new ASTTableElement(AutoIntegerValues.Count, value)); return(true); } if (key.Type == ExpressionType.NUMBER) { var numberKey = key as ASTNumber; if (IntegerValues.ContainsKey((int)numberKey.Value)) // TODO: Do something to stop this from possibly crashing.. someday. (double to int cast) { return(false); } IntegerValues.Add((int)numberKey.Value, new ASTTableElement((int)numberKey.Value, value)); } else if (key.Type == ExpressionType.STRING) { var stringKey = key as ASTString; if (StringValues.ContainsKey(stringKey.Text)) { return(false); } StringValues.Add(stringKey.Text, new ASTTableElement(stringKey.Text, value)); } else if (key.Type == ExpressionType.IDENTIFIER) { var identifierKey = key as ASTIdentifier; if (StringValues.ContainsKey(identifierKey.Name)) { return(false); } StringValues.Add(identifierKey.Name, new ASTTableElement(identifierKey.Name, value)); } else { return(false); } return(true); }
public bool Load(string fullPath) { if (!File.Exists(fullPath)) { return(false); } StringValues.Clear(); ListValues.Clear(); IsConfigLoaded = false; // YML Parser try { bool isNextLineList = false; string listName = ""; List <string> currentList = new List <string>(); string[] configLines = File.ReadAllLines(fullPath); for (int i = 0; i < configLines.Length; i++) { string line = configLines[i]; // even if in the middle of a list, still allow empty spaces if (line == string.Empty || line.TrimStart()[0] == '#') { continue; } // parse lists. cannot parse lists within a list... not yet atleast ;) if (isNextLineList) { string trimmedListItemStart = line.TrimStart(); if (trimmedListItemStart[0] == '-') { string listItem = trimmedListItemStart.After("-").TrimStart().TrimEnd(); currentList.Add(listItem); // last line if ((i + 1) == configLines.Length) { if (isNextLineList) { isNextLineList = false; ListValues.Add(listName, new List <string>(currentList)); listName = ""; currentList.Clear(); break; } } continue; } else { ListValues.Add(listName, new List <string>(currentList)); listName = ""; currentList.Clear(); isNextLineList = false; } } // parse fields. "string" and "bool" pairs are treaded the same when loading and saving // tho they can be parsed to "true" or "false" as a bool later on... see TryGetBoolean string trimmedStart = line.TrimStart(); string trimmedEnd = line.TrimEnd(); string fieldName = trimmedStart.Before(":"); string fieldValue = trimmedEnd.After(":").TrimStart(); if (fieldName.IsEmpty() || fieldName[0] == ':') { continue; } // this is the only thing that can tell if looking for a field/value // or a field/list. fieldValue will always be empty unless you put a // value after the colon. if you put something after the colon then // add the - listValues... idek what could happen but it could break if (fieldValue.IsEmpty()) { isNextLineList = true; listName = fieldName; } else { StringValues.Add(fieldName, fieldValue); } } ConfigPath = fullPath; IsConfigLoaded = true; return(true); } catch { StringValues.Clear(); ListValues.Clear(); IsConfigLoaded = false; return(false); } }
public virtual void ReadFrom(XElement xE) { FeedAttributeId = null; IntegerValue = null; DoubleValue = null; BooleanValue = null; StringValue = null; IntegerValues = null; DoubleValues = null; BooleanValues = null; StringValues = null; MoneyWithCurrencyValue = null; foreach (var xItem in xE.Elements()) { var localName = xItem.Name.LocalName; if (localName == "feedAttributeId") { FeedAttributeId = long.Parse(xItem.Value); } else if (localName == "integerValue") { IntegerValue = long.Parse(xItem.Value); } else if (localName == "doubleValue") { DoubleValue = double.Parse(xItem.Value); } else if (localName == "booleanValue") { BooleanValue = bool.Parse(xItem.Value); } else if (localName == "stringValue") { StringValue = xItem.Value; } else if (localName == "integerValues") { if (IntegerValues == null) { IntegerValues = new List <long>(); } IntegerValues.Add(long.Parse(xItem.Value)); } else if (localName == "doubleValues") { if (DoubleValues == null) { DoubleValues = new List <double>(); } DoubleValues.Add(double.Parse(xItem.Value)); } else if (localName == "booleanValues") { if (BooleanValues == null) { BooleanValues = new List <bool>(); } BooleanValues.Add(bool.Parse(xItem.Value)); } else if (localName == "stringValues") { if (StringValues == null) { StringValues = new List <string>(); } StringValues.Add(xItem.Value); } else if (localName == "moneyWithCurrencyValue") { MoneyWithCurrencyValue = new MoneyWithCurrency(); MoneyWithCurrencyValue.ReadFrom(xItem); } } }
public ExecutionState LoadData(ByteReader br) { while (br.Avaliable > 0) { var Name = br.ReadString(); if (!Name) { return(ExecutionState.Failed()); } var Type = br.ReadByte(); if (!Type) { return(ExecutionState.Failed()); } if (!DataTypeHelper.TryParse(Type.Result, out DataType TypeResult)) { return(ExecutionState.Failed()); } switch (TypeResult) { case DataType.type_byte: var ValueByte = br.ReadByte(); if (!ValueByte) { return(ExecutionState.Failed()); } if (ByteValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } ByteValues.Add(Name.Result, ValueByte.Result); break; case DataType.type_sbyte: var ValueSByte = br.ReadSByte(); if (!ValueSByte) { return(ExecutionState.Failed()); } if (SByteValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } SByteValues.Add(Name.Result, ValueSByte.Result); break; case DataType.type_short: var ValueShort = br.ReadShort(); if (!ValueShort) { return(ExecutionState.Failed()); } if (ShortValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } ShortValues.Add(Name.Result, ValueShort.Result); break; case DataType.type_ushort: var ValueUShort = br.ReadUShort(); if (!ValueUShort) { return(ExecutionState.Failed()); } if (UShortValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } UShortValues.Add(Name.Result, ValueUShort.Result); break; case DataType.type_int: var ValueInt = br.ReadInt(); if (!ValueInt) { return(ExecutionState.Failed()); } if (IntValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } IntValues.Add(Name.Result, ValueInt.Result); break; case DataType.type_uint: var ValueUInt = br.ReadUInt(); if (!ValueUInt) { return(ExecutionState.Failed()); } if (UIntValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } UIntValues.Add(Name.Result, ValueUInt.Result); break; case DataType.type_long: var ValueLong = br.ReadLong(); if (!ValueLong) { return(ExecutionState.Failed()); } if (LongValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } LongValues.Add(Name.Result, ValueLong.Result); break; case DataType.type_ulong: var ValueULong = br.ReadULong(); if (!ValueULong) { return(ExecutionState.Failed()); } if (ULongValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } ULongValues.Add(Name.Result, ValueULong.Result); break; case DataType.type_single: var ValueSingle = br.ReadSingle(); if (!ValueSingle) { return(ExecutionState.Failed()); } if (SingleValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } SingleValues.Add(Name.Result, ValueSingle.Result); break; case DataType.type_double: var ValueDobule = br.ReadDouble(); if (!ValueDobule) { return(ExecutionState.Failed()); } if (DoubleValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } DoubleValues.Add(Name.Result, ValueDobule.Result); break; case DataType.type_string: var ValueString = br.ReadString(); if (!ValueString) { return(ExecutionState.Failed()); } if (StringValues.ContainsKey(Name.Result)) { return(ExecutionState.Failed()); } StringValues.Add(Name.Result, ValueString.Result); break; } } return(ExecutionState.Succeeded()); }