Ejemplo n.º 1
0
        private StringBuilder ToString(bool summarise, string indent, StringBuilder sb)
        {
            const string tab      = "   ";
            const string noIndent = "";

            sb.Append(indent);

            switch (Type)
            {
            case EnumType.None:
                sb.Append("None");
                break;

            case EnumType.Any:
                sb.Append("Any('" + As <string>() + "')");
                break;

            case EnumType.String:
                sb.Append("'" + As <string>() + "'");
                break;

            case EnumType.Int32:
            case EnumType.UInt32:
            case EnumType.Int64:
            case EnumType.UInt64:
            case EnumType.Double:
            case EnumType.Boolean:
            case EnumType.Time:
            case EnumType.DateTime:
                sb.Append(Value.ToString());
                break;

            case EnumType.List:
            {
                if (summarise)
                {
                    sb.Append("List(size=" + Count.ToString() + ")");
                }
                else
                {
                    sb.Append("[\n");
                    int count = Count;
                    foreach (VariantItem item in this)
                    {
                        item.Value.ToString(false, indent + tab, sb);
                        if (--count > 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append("\n");
                    }
                    sb.Append(indent + "]");
                }
                break;
            }

            case EnumType.Dictionary:
            {
                if (summarise)
                {
                    sb.Append("Dictionary(size=" + Count.ToString() + ")");
                }
                else
                {
                    sb.Append("{\n");
                    int count = Count;
                    foreach (VariantItem item in this)
                    {
                        sb.Append(indent + tab + item.Key + ": ");
                        if (item.Value.Is(EnumType.Primitive | EnumType.None))
                        {
                            item.Value.ToString(false, noIndent, sb);
                        }
                        else
                        {
                            sb.Append("\n");
                            item.Value.ToString(false, indent + tab + tab, sb);
                        }
                        if (--count != 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append("\n");
                    }
                    sb.Append(indent + "}");
                }
                break;
            }

            case EnumType.Bag:
            {
                if (summarise)
                {
                    sb.Append("Bag(size=" + Count + ")");
                }
                else
                {
                    sb.Append("[\n");
                    int count = Count;
                    foreach (VariantItem item in this)
                    {
                        sb.Append(indent + tab + "(" + item.Key + ", ");
                        if (item.Value.Is(EnumType.Primitive))
                        {
                            item.Value.ToString(false, noIndent, sb);
                        }
                        else
                        {
                            sb.Append("\n");
                            item.Value.ToString(false, indent + tab, sb);
                        }
                        sb.Append(")");
                        if (--count != 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append("\n");
                    }
                    sb.Append(indent + "]");
                }
                break;
            }

            case EnumType.TimeSeries:
            {
                if (summarise)
                {
                    sb.Append("TimeSeries(size=" + Count + ")");
                }
                else
                {
                    sb.Append("TimeSeries(\n");
                    int count = Count;
                    foreach (VariantItem item in this)
                    {
                        sb.Append(indent + tab + "(" + item.Time + ", ");
                        if (item.Value.Is(EnumType.Primitive))
                        {
                            item.Value.ToString(false, noIndent, sb);
                        }
                        else
                        {
                            sb.Append("\n");
                            item.Value.ToString(false, indent + tab, sb);
                        }
                        sb.Append(")");
                        if (--count != 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append("\n");
                    }
                    sb.Append(indent + ")");
                }
                break;
            }

            case EnumType.Buffer:
            {
                if (summarise)
                {
                    sb.Append("Buffer(size=").Append(Count).Append(")");
                }
                else
                {
                    sb.Append("Buffer(").Append(Encoding.ASCII.GetString(AsBuffer())).Append(')');
                }
                break;
            }

            case EnumType.Tuple:
            {
                if (summarise)
                {
                    sb.Append("Tuple(size=").Append(Count).Append(")");
                }
                else
                {
                    sb.Append("(").AppendLine();
                    int count = Count;
                    foreach (VariantItem item in this)
                    {
                        item.Value.ToString(false, indent + tab, sb);
                        if (--count != 0)
                        {
                            sb.Append(",");
                        }
                        sb.AppendLine();
                    }
                    sb.Append(indent).Append(")");
                }
                break;
            }

            case EnumType.Exception:
            {
                VariantExceptionInfo x = AsException();

                sb.Append(x.Class).Append("('").Append(x.Message).Append("')");

                if (x.Source.Length > 0)
                {
                    sb.Append(" in: ").Append(x.Source);
                }

                if (x.Stack.Length > 0)
                {
                    sb.AppendLine().Append(x.Stack);
                }
                break;
            }

            case EnumType.Object:
            {
                IVariantObject o = AsObject();

                if (summarise)
                {
                    sb.Append(o.Class).Append("(version=").Append(o.Version).Append(")");
                }
                else
                {
                    sb.Append(o.Class).Append("(version=").Append(o.Version).Append(")(").AppendLine();
                    o.Deflate().ToString(false, indent + tab, sb);
                    sb.AppendLine().Append(indent).Append(")");
                }
                break;
            }

            case EnumType.Array:
            {
                TypedArray a = AsArray();

                if (summarise)
                {
                    sb.Append("Array(size=").Append(a.Count).Append(", type=").Append(a.ElementType).Append(")");
                }
                else
                {
                    sb.Append("Array(");

                    int lastIndex = a.Count - 1;
                    for (int i = 0; i < a.Count; ++i)
                    {
                        sb.Append(a[i].ToString()).Append(i != lastIndex ? "," : ")");
                    }
                }
                break;
            }

            default:
                sb.Append("UNKNOWN<").Append(Type).Append(">");
                break;
            }
            return(sb);
        }
Ejemplo n.º 2
0
 public TypedArray(TypedArray rhs)
 {
     ElementType = rhs.ElementType;
     Value = new Object[rhs.Value.Length];
 }
Ejemplo n.º 3
0
        void WriteElement(Variant element)
        {
            WriteStartTag(m_stack.Peek().m_name);

            switch (element.Type)
            {
            case VariantBase.EnumType.None:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                break;
            }

            case VariantBase.EnumType.Any:
            case VariantBase.EnumType.String:
            case VariantBase.EnumType.Float:
            case VariantBase.EnumType.Double:
            case VariantBase.EnumType.Int32:
            case VariantBase.EnumType.UInt32:
            case VariantBase.EnumType.Int64:
            case VariantBase.EnumType.UInt64:
            case VariantBase.EnumType.Boolean:
            case VariantBase.EnumType.Date:
            case VariantBase.EnumType.Time:
            case VariantBase.EnumType.DateTime:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                WriteText(element);
                break;
            }

            case VariantBase.EnumType.Dictionary:
            case VariantBase.EnumType.Bag:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                if ((m_mode & XmlMode.Preserve) != 0)
                {
                    if (element.ContainsKey(XmlConst.Attributes))
                    {
                        WriteAttributes(element[XmlConst.Attributes]);
                    }

                    foreach (VariantItem item in element)
                    {
                        if (item.Key == XmlConst.Attributes)
                        {
                            continue;
                        }
                        else if (item.Key == XmlConst.Text)
                        {
                            WriteText(item.Value);
                        }
                        else if (item.Key == XmlConst.Instruction)
                        {
                            WriteInstruction(item.Value);
                        }
                        else if (item.Key == XmlConst.Comment)
                        {
                            WriteComment(item.Value);
                        }
                        else
                        {
                            Push(item.Key);
                            WriteElement(item.Value);
                            Pop();
                        }
                    }
                }
                else
                {
                    foreach (VariantItem item in element)
                    {
                        Push(item.Key);
                        WriteVariant(item.Value);
                        Pop();
                    }
                }

                break;
            }

            case VariantBase.EnumType.List:
            {
                WriteAttributes(m_stack.Peek().m_attributes);
                foreach (VariantItem item in element)
                {
                    Push();
                    WriteVariant(item.Value);
                    Pop();
                }

                break;
            }

            case VariantBase.EnumType.Tuple:
            {
                m_stack.Peek().m_attributes.Add("size", new Variant(element.Count));

                WriteAttributes(m_stack.Peek().m_attributes);

                foreach (VariantItem item in element)
                {
                    Push();
                    WriteVariant(item.Value);
                    Pop();
                }

                break;
            }

            case VariantBase.EnumType.TimeSeries:
            {
                WriteAttributes(m_stack.Peek().m_attributes);

                foreach (VariantItem item in element)
                {
                    Push().Add("time", new Variant(item.Time));
                    WriteVariant(item.Value);
                    Pop();
                }

                break;
            }

            case VariantBase.EnumType.Buffer:
            {
                WriteAttributes(m_stack.Peek().m_attributes);

                m_writer.WriteBase64(element.AsBuffer(), 0, element.AsBuffer().Length);
                break;
            }

            case VariantBase.EnumType.Object:
            {
                IVariantObject obj = element.AsObject();

                // write class name
                m_stack.Peek().m_attributes.Add("class", new Variant(obj.Class));
                m_stack.Peek().m_attributes.Add("version", new Variant(obj.Version));

                WriteAttributes(m_stack.Peek().m_attributes);

                // write parameter dictionary
                Push("params");
                WriteVariant(obj.Deflate());
                Pop();

                break;
            }

            case VariantBase.EnumType.Exception:
            {
                WriteAttributes(m_stack.Peek().m_attributes);

                VariantExceptionInfo e = element.AsException();

                Push("type");
                WriteElement(new Variant(e.Class));
                Pop();

                Push("message");
                WriteElement(new Variant(e.Message));
                Pop();

                if (e.Source.Length != 0)
                {
                    Push("source");
                    WriteElement(new Variant(e.Source));
                    Pop();
                }

                if (e.Stack.Length != 0)
                {
                    Push("stack");
                    WriteElement(new Variant(e.Stack));
                    Pop();
                }
                break;
            }

            case VariantBase.EnumType.Array:
            {
                TypedArray array = element.AsArray();

                m_stack.Peek().m_attributes.Add("size", new Variant(array.Count));
                m_stack.Peek().m_attributes.Add("elementType", new Variant(array.ElementType.ToString()));

                WriteAttributes(m_stack.Peek().m_attributes);

                WriteDelegate writer = null;
                switch (array.ElementType)
                {
                case VariantBase.EnumType.Float:
                    writer = o => m_writer.WriteString(VariantBase.ToString((float)o));
                    break;

                case VariantBase.EnumType.Double:
                    writer = o => m_writer.WriteString(VariantBase.ToString((double)o));
                    break;

                case VariantBase.EnumType.String:
                    writer = o => m_writer.WriteString((string)o);
                    break;

                case VariantBase.EnumType.Boolean:
                    writer = o => m_writer.WriteString(VariantBase.ToString((bool)o));
                    break;

                case VariantBase.EnumType.Int32:
                    writer = o => m_writer.WriteString(VariantBase.ToString((int)o));
                    break;

                case VariantBase.EnumType.UInt32:
                    writer = o => m_writer.WriteString(VariantBase.ToString((uint)o));
                    break;

                case VariantBase.EnumType.Int64:
                    writer = o => m_writer.WriteString(VariantBase.ToString((long)o));
                    break;

                case VariantBase.EnumType.UInt64:
                    writer = o => m_writer.WriteString(VariantBase.ToString((ulong)o));
                    break;

                case VariantBase.EnumType.Time:
                    writer = o => m_writer.WriteString(VariantBase.ToString((TimeSpan)o));
                    break;

                case VariantBase.EnumType.DateTime:
                    writer = o => m_writer.WriteString(VariantBase.ToString((DateTime)o));
                    break;

                default:
                    throw new VariantException("Case exhaustion: " + array.ElementType);
                }

                for (int i = 0; i < array.Count; ++i)
                {
                    m_writer.WriteStartElement(XmlConst.Default);
                    writer(array[i]);
                    m_writer.WriteEndElement();
                }
                break;
            }

            case VariantBase.EnumType.DataTable:
            {
#if !DISABLE_DATATABLE
                DataTable dataTable = element.AsDataTable();

                m_stack.Peek().m_attributes.Add("rows", new Variant(dataTable.Rows.Count));
                m_stack.Peek().m_attributes.Add("columns", new Variant(dataTable.Columns.Count));

                WriteAttributes(m_stack.Peek().m_attributes);

                foreach (DataColumn col in dataTable.Columns)
                {
                    Push("Column");
                    m_stack.Peek().m_attributes.Add("name", new Variant(col.ColumnName));
                    m_stack.Peek().m_attributes.Add("type", new Variant(VariantPrimitiveBase.TypeToEnum(col.DataType).ToString()));

                    WriteElement(new Variant());
                    Pop();
                }

                foreach (DataColumn col in dataTable.Columns)
                {
                    Action <object> writer;
                    switch (VariantPrimitiveBase.TypeToEnum(col.DataType))
                    {
                    case VariantBase.EnumType.Float:
                        writer = o => m_writer.WriteString(VariantBase.ToString((float)o));
                        break;

                    case VariantBase.EnumType.Double:
                        writer = o => m_writer.WriteString(VariantBase.ToString((double)o));
                        break;

                    case VariantBase.EnumType.String:
                        writer = o => m_writer.WriteString((string)o);
                        break;

                    case VariantBase.EnumType.Boolean:
                        writer = o => m_writer.WriteString(VariantBase.ToString((bool)o));
                        break;

                    case VariantBase.EnumType.Int32:
                        writer = o => m_writer.WriteString(VariantBase.ToString((int)o));
                        break;

                    case VariantBase.EnumType.UInt32:
                        writer = o => m_writer.WriteString(VariantBase.ToString((uint)o));
                        break;

                    case VariantBase.EnumType.Int64:
                        writer = o => m_writer.WriteString(VariantBase.ToString((long)o));
                        break;

                    case VariantBase.EnumType.UInt64:
                        writer = o => m_writer.WriteString(VariantBase.ToString((ulong)o));
                        break;

                    case VariantBase.EnumType.Time:
                        writer = o => m_writer.WriteString(VariantBase.ToString((TimeSpan)o));
                        break;

                    case VariantBase.EnumType.DateTime:
                        writer = o => m_writer.WriteString(VariantBase.ToString((DateTime)o));
                        break;

                    default:
                        throw new VariantException("Case exhaustion: " + VariantPrimitiveBase.TypeToEnum(col.DataType));
                    }

                    Push("Column").Add("name", new Variant(col.ColumnName));
                    WriteStartTag("s");
                    WriteAttributes(m_stack.Peek().m_attributes);
                    foreach (DataRow row in dataTable.Rows)
                    {
                        m_writer.WriteStartElement(XmlConst.Default);
                        writer(row[col]);
                        m_writer.WriteEndElement();
                    }
                    WriteEndTag();
                    Pop();
                }
                break;
#else
                throw new NotSupportedException("Datatables are not supported on this platform.");
#endif
            }

            default:
                throw new VariantException("Case exhaustion: " + element.Type);
            }

            WriteEndTag();
        }
Ejemplo n.º 4
0
 public Variant(TypedArray arg)
 {
     Value = arg;
 }
Ejemplo n.º 5
0
        public int CompareTo(IVariantData value)
        {
            TypedArray rhsValue = ((TypedArray)value);

            if (Value.Length != rhsValue.Value.Length)
            {
                return(Value.Length.CompareTo(rhsValue.Value.Length));
            }
            else if (ElementType != rhsValue.ElementType)
            {
                return(ElementType.CompareTo(rhsValue.ElementType));
            }
            else
            {
                CompareDelegate comparer = null;

                switch (ElementType)
                {
                case VariantBase.EnumType.Float:
                    comparer = delegate(object lhs, object rhs) { return(((float)lhs).CompareTo((float)rhs)); };
                    break;

                case VariantBase.EnumType.Double:
                    comparer = delegate(object lhs, object rhs) { return(((double)lhs).CompareTo((double)rhs)); };
                    break;

                case VariantBase.EnumType.Boolean:
                    comparer = delegate(object lhs, object rhs) { return(((bool)lhs).CompareTo((bool)rhs)); };
                    break;

                case VariantBase.EnumType.String:
                    comparer = delegate(object lhs, object rhs) { return(((string)lhs).CompareTo((string)rhs)); };
                    break;

                case VariantBase.EnumType.Int32:
                    comparer = delegate(object lhs, object rhs) { return(((Int32)lhs).CompareTo((Int32)rhs)); };
                    break;

                case VariantBase.EnumType.UInt32:
                    comparer = delegate(object lhs, object rhs) { return(((UInt32)lhs).CompareTo((UInt32)rhs)); };
                    break;

                case VariantBase.EnumType.Int64:
                    comparer = delegate(object lhs, object rhs) { return(((Int64)lhs).CompareTo((Int64)rhs)); };
                    break;

                case VariantBase.EnumType.UInt64:
                    comparer = delegate(object lhs, object rhs) { return(((UInt64)lhs).CompareTo((UInt64)rhs)); };
                    break;

                case VariantBase.EnumType.Time:
                    comparer = delegate(object lhs, object rhs) { return(((TimeSpan)lhs).CompareTo((TimeSpan)rhs)); };
                    break;

                case VariantBase.EnumType.DateTime:
                    comparer = delegate(object lhs, object rhs) { return(((DateTime)lhs).CompareTo((DateTime)rhs)); };
                    break;

                default:
                    throw new VariantException("Case exhaustion: " + ElementType);
                }

                for (int i = 0; i < Value.Length; ++i)
                {
                    int cmp = comparer(Value[i], rhsValue.Value[i]);
                    if (cmp != 0)
                    {
                        return(cmp);
                    }
                }
            }
            return(0);
        }
Ejemplo n.º 6
0
 public TypedArray(TypedArray rhs)
 {
     ElementType = rhs.ElementType;
     Value       = new Object[rhs.Value.Length];
 }
Ejemplo n.º 7
0
        private TypedArray ReadArray()
        {
            int size = ReadInt32();
            Variant.EnumType elementType = (Variant.EnumType)ReadInt32();

            TypedArray array = new TypedArray(elementType, size);

            ReadDelegate reader = null;
            switch (elementType)
            {
                case VariantBase.EnumType.Float:
                    reader = () => ReadFloat();
                    break;
                case VariantBase.EnumType.Double:
                    reader = () => ReadDouble();
                    break;
                case VariantBase.EnumType.Boolean:
                    reader = () => ReadBoolean();
                    break;
                case VariantBase.EnumType.String:
                    reader = () => ReadString();
                    break;
                case VariantBase.EnumType.Int32:
                    reader = () => ReadInt32();
                    break;
                case VariantBase.EnumType.UInt32:
                    reader = () => ReadUInt32();
                    break;
                case VariantBase.EnumType.Int64:
                    reader = () => ReadInt64();
                    break;
                case VariantBase.EnumType.UInt64:
                    reader = () => ReadUInt64();
                    break;
                case VariantBase.EnumType.Time:
                    reader = () => ReadTime();
                    break;
                case VariantBase.EnumType.DateTime:
                    reader = () => ReadDateTime();
                    break;
                default:
                    throw new VariantException("Case exhaustion: " + array.ElementType);
            }

            for (int i = 0; i < size; ++i)
            {
                array[i] = reader();
            }

            return array;
        }
Ejemplo n.º 8
0
        private TypedArray ReadArray()
        {
            int size = ReadInt32();

            Variant.EnumType elementType = (Variant.EnumType)ReadInt32();

            TypedArray array = new TypedArray(elementType, size);

            ReadDelegate reader = null;

            switch (elementType)
            {
            case VariantBase.EnumType.Float:
                reader = () => ReadFloat();
                break;

            case VariantBase.EnumType.Double:
                reader = () => ReadDouble();
                break;

            case VariantBase.EnumType.Boolean:
                reader = () => ReadBoolean();
                break;

            case VariantBase.EnumType.String:
                reader = () => ReadString();
                break;

            case VariantBase.EnumType.Int32:
                reader = () => ReadInt32();
                break;

            case VariantBase.EnumType.UInt32:
                reader = () => ReadUInt32();
                break;

            case VariantBase.EnumType.Int64:
                reader = () => ReadInt64();
                break;

            case VariantBase.EnumType.UInt64:
                reader = () => ReadUInt64();
                break;

            case VariantBase.EnumType.Time:
                reader = () => ReadTime();
                break;

            case VariantBase.EnumType.DateTime:
                reader = () => ReadDateTime();
                break;

            default:
                throw new VariantException("Case exhaustion: " + array.ElementType);
            }

            for (int i = 0; i < size; ++i)
            {
                array[i] = reader();
            }

            return(array);
        }
Ejemplo n.º 9
0
        private void Write(TypedArray arg)
        {
            WriteDelegate writer;
            switch (arg.ElementType)
            {
                case VariantBase.EnumType.Float:
                    writer = o => Write((float)o);
                    break;
                case VariantBase.EnumType.Double:
                    writer = o => Write((double)o);
                    break;
                case VariantBase.EnumType.String:
                    writer = o => Write((string)o);
                    break;
                case VariantBase.EnumType.Boolean:
                    writer = o => Write((bool)o);
                    break;
                case VariantBase.EnumType.Int32:
                    writer = o => Write((int)o);
                    break;
                case VariantBase.EnumType.UInt32:
                    writer = o => Write((uint)o);
                    break;
                case VariantBase.EnumType.Int64:
                    writer = o => Write((long)o);
                    break;
                case VariantBase.EnumType.UInt64:
                    writer = o => Write((ulong)o);
                    break;
                case VariantBase.EnumType.Time:
                    writer = o => Write((TimeSpan)o);
                    break;
                case VariantBase.EnumType.DateTime:
                    writer = o => Write((DateTime)o);
                    break;
                default:
                    throw new VariantException("Case exhaustion: " + arg.ElementType);
            }

            Write(arg.Count);
            Write((int)arg.ElementType);

            for (int i = 0; i < arg.Count; ++i)
            {
                writer(arg[i]);
            }
        }