Example #1
0
 /// <summary>
 /// Returns argument value or default if missing
 /// </summary>
 /// <param name='name'>
 /// Name of argument
 /// </param>
 /// <param name='def'>
 /// Default value
 /// </param>
 public ZDateTime Or(string name, ZDateTime def)
 {
     if (Contains(name))
     {
         return(new ZDateTime(this[name].Value));
     }
     else
     {
         return(def);
     }
 }
Example #2
0
 /// <summary>
 /// Returns argument value or default if missing
 /// </summary>
 /// <param name='def'>
 /// Default value
 /// </param>
 public ZDateTime Or(ZDateTime def)
 {
     if (_valuelist.Count > 0)
     {
         return(new ZDateTime((string)Value));
     }
     else
     {
         return(def);
     }
 }
Example #3
0
        // Functions


        /// <summary>
        /// Post the msg with formatting to stderr
        /// </summary>
        /// <param name='level'>
        /// Level.
        /// </param>
        /// <param name='msg'>
        /// Message.
        /// </param>
        public void Post(Logger.Report report)
        {
            StringBuilder s = new StringBuilder(report.Message.Length + 64);

            bool skip = false;

            if ((_show & (int)Logger.Show.Severity) != 0)
            {
                s.Append(SeverityOf(report.Level));
                skip = true;
            }

            if ((_show & (int)Logger.Show.Time) != 0)
            {
                if (skip)
                {
                    s.Append(' ');
                }

                ZDateTime time = new ZDateTime(report.Time, ZTimeZone.Local);
                s.Append(string.Format(" [{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}.{6:D3}]",
                                       time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second, time.Millisecond));

                skip = true;
            }

            if ((_show & (int)Logger.Show.Module) != 0)
            {
                if (skip)
                {
                    s.Append(' ');
                }

                s.Append('[');
                s.Append(report.Source.Category);
                s.Append(']');

                skip = true;
            }

            if (skip)
            {
                s.Append(": ");
            }

            s.Append(report.Message);

            Console.Error.WriteLine(s);
        }
Example #4
0
        /// <summary>
        /// Convert arguments to parameter types where they mismatch
        /// </summary>
        /// <param name='ctor'>
        /// Ctor.
        /// </param>
        /// <param name='args'>
        /// Arguments.
        /// </param>
        public static void ConformArguments(ParameterInfo[] paramlist, object[] args)
        {
            int i = -1;

            foreach (var parm in paramlist)
            {
                i++;
                object arg    = args[i];
                Type   pclass = parm.ParameterType;
                Type   aclass = arg != null?arg.GetType() : null;

                var ptype = ValueTypeUtils.TypeOf(pclass);
                var atype = ValueTypeUtils.TypeOf(aclass);

                if ((atype == ptype && atype != VType.Other) || pclass == aclass || pclass.IsAssignableFrom(aclass))
                {
                    continue;
                }

                if (arg == null && pclass.IsValueType)
                {
                    throw new ArgumentException("could not find matching constructor");
                }

                if (arg == null)
                {
                    continue;
                }

                // convert simple value types to conform
                if (ptype != VType.Other)
                {
                    args[i] = ValueTypeUtils.Convert(arg, ptype, pclass);
                }

                // special handling for delegates
                else if (DelegateGenerator.IsDelegate(pclass))
                {
                    args[i] = DelegateGenerator.ConvertToDelegate(pclass, args[i]);
                }

                // special case for ZDateTime, allowing long specifier
                else if (pclass == typeof(ZDateTime))
                {
                    if (aclass == typeof(long))
                    {
                        args[i] = new ZDateTime((long)arg, ZTimeZone.NewYork);
                    }
                    else if (aclass == typeof(string))
                    {
                        args[i] = new ZDateTime((string)arg);
                    }
                    else
                    {
                        throw new ArgumentException("unknown argument pairing");
                    }
                }

                // see if we can instantiate a persitable class
                else if (atype == VType.String && pclass.IsSubclassOf(typeof(IPersist <string>)))
                {
                    var nobj = (IPersist <string>)Activator.CreateInstance(pclass);
                    nobj.State = (string)arg;
                    args[i]    = nobj;
                }

                // the parameter type is an array, but our value is a single value of the same type, convert
                else if (pclass.IsArray && pclass.GetElementType() == aclass)
                {
                    var narray = Array.CreateInstance(pclass.GetElementType(), 1);
                    narray.SetValue(arg, 0);
                    args[i] = narray;
                }

                else if (typeof(Vector <double>).IsAssignableFrom(pclass))
                {
                    if (aclass == typeof(double))
                    {
                        var vec = new DenseVector(1);
                        vec[0]  = ((Double)args[i]);
                        args[i] = vec;
                    }
                    if (aclass == typeof(int))
                    {
                        var vec = new DenseVector(1);
                        vec[0]  = (double)((Int32)args[i]);
                        args[i] = vec;
                    }
                }

                // otherwise try to create a new instance from string
                else if (atype == VType.String)
                {
                    string sval = (string)arg;
                    try
                    {
                        var method = pclass.GetMethod("Parse");
                        if (method != null)
                        {
                            args[i] = method.Invoke(null, new object[] { sval });
                            continue;
                        }
                    }
                    catch
                    { }

                    try
                    {
                        args[i] = Activator.CreateInstance(pclass, sval);
                    }
                    catch
                    { throw new ArgumentException("could not coerce type " + aclass + " to " + pclass); }
                }
            }
        }
Example #5
0
        /**
         * Create time from given info, relative to current date
         */
        private ZDateTime CreateTime(ref DateInfo info, ZTimeZone zone)
        {
            ZDateTime now = ZDateTime.TimeNowFor(zone);

            return(new ZDateTime(now.Year, now.Month, Math.Max(now.Day, 1), info.Hours, info.Minutes, info.Seconds, info.Milliseconds, info.Zone));
        }