/// <summary> /// Parses an unsigned integer value, storing it in the smallest available unsigned integer type. /// </summary> /// <remarks> /// This method is called internally by GetTypedValue. /// </remarks> /// <param name="show">Value to parse.</param> /// <param name="typedType">Receives the parsed type.</param> /// <param name="typedValue">Receives the parsed value.</param> /// <param name="field">The field descriptor.</param> private static void ParseUnsigned(string show, ref Type typedType, ref object typedValue, TSharkField field) { if (field.DisplayBase.Equals("BASE_NONE")) { typedType = typeof(string); typedValue = show; return; } // oct values seem to be stored in decimal form...? ulong v; if (field.DisplayBase.StartsWith("BASE_HEX")) { v = BitConverter.ToUInt64(TSharkTypeParser.Reverse(TSharkTypeParser.ParseHexBytes(show, 8)), 0); } /*else if (field.DisplayBase.StartsWith("BASE_OCT")) * { * v = BitConverter.ToUInt64(TSharkTypeParser.Reverse(TSharkTypeParser.ParseOctBytes(show, 8)), 0); * }*/ else //if (field.DisplayBase.StartsWith("BASE_DEC")) { v = ulong.Parse(show); } if (v >= byte.MinValue && v <= byte.MaxValue) { typedType = typeof(byte); typedValue = (byte)v; } else if (v >= ushort.MinValue && v <= ushort.MaxValue) { typedType = typeof(ushort); typedValue = (ushort)v; } else if (v >= uint.MinValue && v <= uint.MaxValue) { typedType = typeof(uint); typedValue = (uint)v; } else //if (v >= ulong.MinValue && v <= ulong.MaxValue) { typedType = typeof(ulong); typedValue = (ulong)v; } }
/// <summary> /// Converts a string value to an instance of a runtime type. /// </summary> /// <param name="state">The parse state.</param> /// <param name="name">Field name.</param> /// <param name="showname">Node's 'showname' attribute.</param> /// <param name="show">Node's 'show' attribute.</param> /// <param name="value">Node's 'value' attribute.</param> /// <param name="typedType">Receives the runtime type of the value.</param> /// <param name="typedValue">Receives the typed value.</param> /// <param name="typedValueString">Receives the string corresponding to the typed value.</param> private static void GetTypedValue(ParseState state, string name, string showname, string show, string value, out Type typedType, out object typedValue, out string typedValueString) { typedType = null; typedValue = null; typedValueString = null; try { TSharkField field; if (state.Schema.Fields.TryGetValue(name, out field)) { typedType = field.Type; if (typedType == typeof(bool)) { bool?t; switch (show) { case "0": t = false; break; case "1": t = true; break; default: t = null; break; } if (t == null) { typedType = typeof(string); typedValue = show; } else { typedValue = (bool)t; } } else if (typedType == typeof(byte[])) { if (value == null) { typedValue = new byte[0]; } else { typedValue = TSharkTypeParser.ParseHexBytes(value); } } else if (typedType == typeof(byte) || typedType == typeof(ushort) || typedType == typeof(uint) || typedType == typeof(ulong)) { ParseUnsigned(show, ref typedType, ref typedValue, field); } else if (typedType == typeof(sbyte) || typedType == typeof(short) || typedType == typeof(int) || typedType == typeof(long)) { ParseSigned(show, ref typedType, ref typedValue, field); } else if (typedType == typeof(float)) { typedValue = float.Parse(show); } else if (typedType == typeof(double)) { typedValue = double.Parse(show); } else if (typedType == typeof(DateTime)) { typedValue = TSharkTypeParser.ParseDateTime(show); } else if (typedType == typeof(TimeSpan)) { // loss of precision here typedValue = TimeSpan.FromSeconds(double.Parse(show)); } else if (typedType == typeof(IPAddress)) { IPAddress ipAddress; if (!IPAddress.TryParse(show, out ipAddress)) { ipAddress = new IPAddress(TSharkTypeParser.ParseHexBytes(value)); } typedValue = ipAddress; } else if (typedType == typeof(Guid)) { Guid t; if (!Guid.TryParse(show, out t)) { throw new Exception(string.Format("Badly formatted Guid value: {0}", show)); } typedValue = t; } else if (typedType == typeof(string) || typedType == null) { typedType = typeof(string); typedValue = show; } //Log.WriteInfo("Type: {0} Value: {1}", typedType, show); } if (field != null) { if (typedValue == null) { Log.WriteWarning("Unable to convert field value.\nName: {0}\nValue: {1}", name, show); } else { typedValueString = field.FindString(typedValue); } } else { //Log.WriteInfo("No field definition for field.\nName: {0}\nValue: {1}", name, show); } } catch (Exception ex) { typedValue = null; Log.WriteWarning("Unable to convert field value.\nName: {0}\nValue: {1}\nError: {2}", name, show, ex.Message); } if (typedValue == null) { typedType = typeof(string); if (show == null || (value != null && name.IndexOf(show, 0, StringComparison.OrdinalIgnoreCase) >= 0)) { typedValue = value; } else { typedValue = show; } typedValueString = null; } }