/// <summary> /// Searches a <see cref="XElement" /> for the first attribute instance with the /// specified <see cref="XName" /> and then parses and returns its value as a /// hex encoded byte array. /// </summary> /// <param name="element">The <see cref="XElement" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public static byte[] ParseAttribute(this XElement element, XName name, byte[] def) { var attribute = element.Attribute(name); var value = attribute != null ? attribute.Value : null; return(Serialize.Parse(value, def)); }
/// <summary> /// Searches a <see cref="XContainer" /> for the first <see cref="XElement" /> instance with the specified /// specified <see cref="XName" /> and then parses and returns its value as a /// an <see cref="NetworkBinding" />. /// </summary> /// <param name="container">The <see cref="XContainer" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// Network bindings are formatted as <dotted-quad>:<port> or /// <host>:<port> /// </remarks> public static NetworkBinding ParseElement(this XContainer container, XName name, NetworkBinding def) { var element = container.Element(name); var value = element != null ? element.Value : null; return(Serialize.Parse(value, def)); }
/// <summary> /// Searches a <see cref="XContainer" /> for the first <see cref="XElement" /> instance with the specified /// specified <see cref="XName" /> and then parses and returns its value as /// a case insensitive enumeration of type <typeparamref name="TEnum" />. /// </summary> /// <typeparam name="TEnum">The enumeration type being parsed.</typeparam> /// <param name="container">The <see cref="XContainer" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default enumeration value.</param> /// <returns>The value loaded from the configuration or the default value.</returns> public static TEnum ParseElement <TEnum>(this XContainer container, XName name, object def) { var element = container.Element(name); var value = element != null ? element.Value : null; return(Serialize.Parse <TEnum>(value, def)); }
/// <summary> /// Searches a <see cref="XElement" /> for the first attribute instance with the /// specified <see cref="XName" /> and then parses and returns its value as a /// an <see cref="NetworkBinding" />. /// </summary> /// <param name="element">The <see cref="XElement" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// Network bindings are formatted as <dotted-quad>:<port> or /// <host>:<port> /// </remarks> public static NetworkBinding ParseAttribute(this XElement element, XName name, NetworkBinding def) { var attribute = element.Attribute(name); var value = attribute != null ? attribute.Value : null; return(Serialize.Parse(value, def)); }
/// <summary> /// Searches a <see cref="XElement" /> for the first attribute instance with the /// specified <see cref="XName" /> and then parses and returns its value as /// a case insensitive enumeration of type <typeparamref name="TEnum" />. /// </summary> /// <typeparam name="TEnum">The enumeration type being parsed.</typeparam> /// <param name="element">The <see cref="XElement" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default enumeration value.</param> /// <returns>The value loaded from the configuration or the default value.</returns> public static TEnum ParseAttribute <TEnum>(this XElement element, XName name, object def) { var attribute = element.Attribute(name); var value = attribute != null ? attribute.Value : null; return(Serialize.Parse <TEnum>(value, def)); }
/// <summary> /// Searches a <see cref="XContainer" /> for the first <see cref="XElement" /> instance with the specified /// specified <see cref="XName" /> and then parses and returns its value as a /// hex encoded byte array. /// </summary> /// <param name="container">The <see cref="XContainer" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public static byte[] ParseElement(this XContainer container, XName name, byte[] def) { var element = container.Element(name); var value = element != null ? element.Value : null; return(Serialize.Parse(value, def)); }
/// <summary> /// Searches a <see cref="XContainer" /> for the first <see cref="XElement" /> instance with the specified /// specified <see cref="XName" /> and then parses and returns its value as /// a <see cref="Uri" />. /// </summary> /// <param name="container">The <see cref="XContainer" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public static Uri ParseElement(this XContainer container, XName name, Uri def) { var element = container.Element(name); var value = element != null ? element.Value : null; if (value == null) { return(def); } return(Serialize.Parse(value, def)); }
/// <summary> /// Searches a <see cref="XElement" /> for the first attribute instance with the /// specified <see cref="XName" /> and then parses and returns its value as /// a <see cref="Uri" />. /// </summary> /// <param name="element">The <see cref="XElement" /></param> /// <param name="name">The attribute <see cref="XName" />.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public static Uri ParseAttribute(this XElement element, XName name, Uri def) { var attribute = element.Attribute(name); var value = attribute != null ? attribute.Value : null; if (value == null) { return(def); } return(Serialize.Parse(value, def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public Uri Parse(string columnName, Uri def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses an enumeration value where the value is case insenstive. /// </summary> /// <typeparam name="TEnum">The enumeration type being parsed.</typeparam> /// <param name="columnName">The column name.</param> /// <param name="def">The default enumeration value.</param> /// <returns>The value loaded from the configuration or the default value.</returns> public TEnum Parse <TEnum>(string columnName, object def) { return(Serialize.Parse <TEnum>(GetColumn(columnName), def)); }
/// <summary> /// Parses an enumeration value where the value is case insenstive. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="enumType">The enumeration type.</param> /// <param name="def">The default enumeration value.</param> /// <returns>The value loaded from the configuration or the default value.</returns> public object Parse(string columnName, System.Type enumType, object def) { return(Serialize.Parse(GetColumn(columnName), enumType, def)); }
//--------------------------------------------------------------------- // IParsable implementation /// <summary> /// Attempts to parse the configuration value. /// </summary> /// <param name="value">The configuration value.</param> /// <returns><c>true</c> if the value could be parsed, <b></b> if the value is not valid for the type.</returns> public bool TryParse(string value) { string[] fields; double minutes = 0; double seconds = 0; if (value == null) { return(false); } if (value.ToLower().StartsWith("disabled")) { this.type = RecurringTimerType.Disabled; return(true); } fields = value.Split(','); switch (fields[0].Trim().ToLower()) { case "minute": this.type = RecurringTimerType.Minute; this.timeOffset = TimeSpan.Zero; return(true); case "quarterhour": if (fields.Length != 2) { return(false); } fields = fields[1].Split(':'); if (fields.Length > 2) { return(false); } this.type = RecurringTimerType.QuarterHour; try { minutes = double.Parse(fields[0]); if (fields.Length > 1) { seconds = double.Parse(fields[1]); } timeOffset = TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds); if (timeOffset >= TimeSpan.FromMinutes(15)) { timeOffset = TimeSpan.Zero; } } catch { return(false); } return(true); case "hourly": if (fields.Length != 2) { return(false); } fields = fields[1].Split(':'); if (fields.Length > 2) { return(false); } this.type = RecurringTimerType.Hourly; try { minutes = double.Parse(fields[0]); if (fields.Length > 1) { seconds = double.Parse(fields[1]); } timeOffset = TimeSpan.FromMinutes(minutes) + TimeSpan.FromSeconds(seconds); if (timeOffset >= TimeSpan.FromHours(1)) { timeOffset = TimeSpan.Zero; } } catch { return(false); } return(true); case "daily": TimeOfDay timeOfDay; if (fields.Length != 2) { return(false); } if (!TimeOfDay.TryParse(fields[1], out timeOfDay)) { return(false); } this.type = RecurringTimerType.Daily; this.timeOffset = timeOfDay.TimeSpan; if (timeOffset >= TimeSpan.FromDays(1)) { timeOffset = TimeSpan.Zero; } return(true); case "interval": if (fields.Length != 2) { return(false); } timeOffset = Serialize.Parse(fields[1], TimeSpan.FromSeconds(-1)); if (timeOffset < TimeSpan.Zero) { return(false); } this.type = RecurringTimerType.Interval; return(true); default: return(false); } }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public Guid Parse(string columnName, Guid def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// IP addresses are formatted as <dotted-quad> /// </remarks> public IPAddress Parse(string columnName, IPAddress def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// <para> /// Timespan values are encoded as floating point values terminated with /// one of the unit codes: "d" (days), "h" (hours), "m" (minutes), "s" /// (seconds), or "ms" (milliseconds). If the unit code is missing then /// seconds will be assumed. An infinite timespan is encoded using the /// literal "infinite". /// </para> /// <para> /// Timespan values can also be specified as: /// </para> /// <para> /// <c>[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]</c> /// </para> /// <para>where:</para> /// <list type="table"> /// <item> /// <term>ws</term> /// <definition>is whitespace</definition> /// </item> /// <item> /// <term>d</term> /// <definition>specifies days.</definition> /// </item> /// <item> /// <term>hh</term> /// <definition>specifies hours</definition> /// </item> /// <item> /// <term>mm</term> /// <definition>specifies minutes</definition> /// </item> /// <item> /// <term>ss</term> /// <definition>specifies seconds</definition> /// </item> /// <item> /// <term>ff</term> /// <definition>specifies fractional seconds</definition> /// </item> /// </list> /// </remarks> public TimeSpan Parse(string columnName, TimeSpan def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// <para> /// This method recognises the following boolean values: /// </para> /// <code language="none"> /// False Values True Values /// ------------ ----------- /// 0 1 /// no yes /// off on /// low high /// false true /// disable enable /// </code> /// </remarks> public bool Parse(string columnName, bool def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// <note> /// <para> /// This method supports "K", "M", "G", and "T" suffixes /// where "K" multiples the value parsed by 1024, "M multiplies /// the value parsed by 1048576, "G" multiples the value /// parsed by 1073741824, and "T" multiplies the value by /// 1099511627776. /// </para> /// <para> /// The following constant values are also supported: /// </para> /// <list type="table"> /// <item><term><b>short.min</b></term><description>-32768</description></item> /// <item><term><b>short.max</b></term><description>32767</description></item> /// <item><term><b>ushort.max</b></term><description>65533</description></item> /// <item><term><b>int.min</b></term><description>-2147483648</description></item> /// <item><term><b>int.max</b></term><description>2147483647</description></item> /// <item><term><b>uint.max</b></term><description>4294967295</description></item> /// <item><term><b>long.max</b></term><description>2^63 - 1</description></item> /// </list> /// </note> /// </remarks> public double Parse(string columnName, double def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Returns the named value if it exists in the user's profile otherwise /// returns a specified default value. /// </summary> /// <param name="name">The name.</param> /// <param name="def">The default value.</param> /// <returns>The named value from the user's profile if it is present, the default value otherwise.</returns> public DateTime Get(string name, DateTime def) { var value = this[name]; return(value != null?Serialize.Parse(value, def) : def); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// Dates are encoded into strings as described in RFC 1123. /// </remarks> public DateTime Parse(string columnName, DateTime def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// <note> /// <para> /// This method supports "K", "M", and "G" suffixes /// where "K" multiples the value parsed by 1024, "M multiplies /// the value parsed by 1048576, and "G" multiples the value /// parsed by 1073741824. The "T" suffix is not supported /// by this method because it exceeds the capacity of a /// 32-bit integer. /// </para> /// <para> /// The following constant values are also supported: /// </para> /// <list type="table"> /// <item><term><b>short.min</b></term><description>-32768</description></item> /// <item><term><b>short.max</b></term><description>32767</description></item> /// <item><term><b>ushort.max</b></term><description>65533</description></item> /// <item><term><b>int.min</b></term><description>-2147483648</description></item> /// </list> /// </note> /// </remarks> public int Parse(string columnName, int def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// Network bindings are formatted as <dotted-quad>:<port> or /// <host>:<port> /// </remarks> public NetworkBinding Parse(string columnName, NetworkBinding def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the string passed unless the string is <c>null</c> or /// the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> /// <remarks> /// <note> /// <para> /// This method supports "K", "M", "G", and "T" suffixes /// where "K" multiples the value parsed by 1024, "M multiplies /// the value parsed by 1048576, "G" multiples the value /// parsed by 1073741824, and "T" multiplies the value by /// 1099511627776. /// </para> /// <para> /// The following constant values are also supported: /// </para> /// <list type="table"> /// <item><term><b>short.min</b></term><description>-32768</description></item> /// <item><term><b>short.max</b></term><description>32767</description></item> /// <item><term><b>ushort.max</b></term><description>65533</description></item> /// <item><term><b>int.min</b></term><description>-2147483648</description></item> /// <item><term><b>int.max</b></term><description>2147483647</description></item> /// <item><term><b>uint.max</b></term><description>4294967295</description></item> /// <item><term><b>long.max</b></term><description>2^63 - 1</description></item> /// </list> /// </note> /// </remarks> public long Parse(string columnName, long def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses the hex encoded string passed unless the string is <c>null</c> /// or the parse failed, in which case the default value /// will be returned. /// </summary> /// <param name="columnName">The column name.</param> /// <param name="def">The default value.</param> /// <returns>The parsed value on success, the default value on failure.</returns> public byte[] Parse(string columnName, byte[] def) { return(Serialize.Parse(GetColumn(columnName), def)); }
/// <summary> /// Parses a string into a position record. /// </summary> /// <param name="input">The input string in the same format as generated by <see cref="ToString()" />.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="input" /> is <c>null</c>.</exception> /// <exception cref="FormatException">Thrown if the input string is not correctly formatted.</exception> public GeoFix(string input) : this() { if (input == null) { throw new ArgumentNullException("input"); } var fields = input.Split(','); if (fields.Length < 3) { throw new FormatException("Bad GeoFix record: at least [3] comma separated fields are expected."); } try { if (string.IsNullOrWhiteSpace(fields[0])) { TimeUtc = null; } else { TimeUtc = Serialize.Parse(fields[0], DateTime.MinValue); } if (!string.IsNullOrWhiteSpace(fields[1]) && !string.IsNullOrWhiteSpace(fields[2])) { Latitude = double.Parse(fields[1]); Longitude = double.Parse(fields[2]); } if (fields.Length == 3) { return; } if (!string.IsNullOrWhiteSpace(fields[3])) { Altitude = double.Parse(fields[3]); } if (fields.Length == 4) { return; } if (!string.IsNullOrWhiteSpace(fields[4])) { Course = double.Parse(fields[4]); } if (fields.Length == 5) { return; } if (!string.IsNullOrWhiteSpace(fields[5])) { Speed = double.Parse(fields[5]); } if (fields.Length == 6) { return; } if (!string.IsNullOrWhiteSpace(fields[6])) { HorizontalAccuracy = double.Parse(fields[6]); } if (fields.Length == 7) { return; } if (!string.IsNullOrWhiteSpace(fields[7])) { VerticalAccurancy = double.Parse(fields[7]); } if (fields.Length == 8) { return; } if (!string.IsNullOrWhiteSpace(fields[8])) { Technology = Serialize.Parse <GeoFixTechnology>(fields[8], GeoFixTechnology.Unknown); } if (fields.Length == 9) { return; } if (!string.IsNullOrWhiteSpace(fields[9])) { NetworkStatus = Serialize.Parse <NetworkStatus>(fields[9], NetworkStatus.Unknown); } } catch (Exception e) { throw new FormatException(string.Format("Bad GeoFix record: {0}", e.Message)); } }