Exemplo n.º 1
0
        internal int exportObject(ClassDescriptor desc, byte[] body, int offs, int indent)
        {
            ClassDescriptor.FieldDescriptor[] all = desc.allFields;

            for (int i = 0, n = all.Length; i < n; i++)
            {
                ClassDescriptor.FieldDescriptor fd = all[i];
                FieldInfo f = fd.field;
                indentation(indent);
                String fieldName = exportIdentifier(fd.fieldName);
                writer.Write("<" + fieldName + ">");
                switch (fd.type)
                {
                case ClassDescriptor.FieldType.tpBoolean:
                    writer.Write(body[offs++] != 0?"1":"0");
                    break;

                case ClassDescriptor.FieldType.tpByte:
                    writer.Write(System.Convert.ToString((byte)body[offs++]));
                    break;

                case ClassDescriptor.FieldType.tpSByte:
                    writer.Write(System.Convert.ToString((sbyte)body[offs++]));
                    break;

                case ClassDescriptor.FieldType.tpChar:
                    writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
                    offs += 2;
                    break;

                case ClassDescriptor.FieldType.tpShort:
                    writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
                    offs += 2;
                    break;

                case ClassDescriptor.FieldType.tpUShort:
                    writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
                    offs += 2;
                    break;

                case ClassDescriptor.FieldType.tpInt:
                    writer.Write(System.Convert.ToString(Bytes.unpack4(body, offs)));
                    offs += 4;
                    break;

                case ClassDescriptor.FieldType.tpEnum:
                    writer.Write(Enum.ToObject(f.FieldType, Bytes.unpack4(body, offs)));
                    offs += 4;
                    break;

                case ClassDescriptor.FieldType.tpUInt:
                    writer.Write(System.Convert.ToString((uint)Bytes.unpack4(body, offs)));
                    offs += 4;
                    break;

                case ClassDescriptor.FieldType.tpLong:
                    writer.Write(System.Convert.ToString(Bytes.unpack8(body, offs)));
                    offs += 8;
                    break;

                case ClassDescriptor.FieldType.tpULong:
                    writer.Write(System.Convert.ToString((ulong)Bytes.unpack8(body, offs)));
                    offs += 8;
                    break;

                case ClassDescriptor.FieldType.tpFloat:
                    writer.Write(System.Convert.ToString(Bytes.unpackF4(body, offs)));
                    offs += 4;
                    break;

                case ClassDescriptor.FieldType.tpDouble:
                    writer.Write(System.Convert.ToString(Bytes.unpackF8(body, offs)));
                    offs += 8;
                    break;

                case ClassDescriptor.FieldType.tpGuid:
                    writer.Write("\"" + Bytes.unpackGuid(body, offs) + "\"");
                    offs += 16;
                    break;

                case ClassDescriptor.FieldType.tpDecimal:
                    writer.Write("\"" + Bytes.unpackDecimal(body, offs) + "\"");
                    offs += 16;
                    break;

                case ClassDescriptor.FieldType.tpString:
                    offs = exportString(body, offs);
                    break;

                case ClassDescriptor.FieldType.tpDate:
                {
                    long msec = Bytes.unpack8(body, offs);
                    offs += 8;
                    if (msec >= 0)
                    {
                        writer.Write("\"" + new System.DateTime(msec) + "\"");
                    }
                    else
                    {
                        writer.Write("null");
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpObject:
                    exportRef(Bytes.unpack4(body, offs));
                    offs += 4;
                    break;

                case ClassDescriptor.FieldType.tpValue:
                    writer.Write('\n');
                    offs = exportObject(fd.valueDesc, body, offs, indent + 1);
                    indentation(indent);
                    break;

#if SUPPORT_RAW_TYPE
                case ClassDescriptor.FieldType.tpRaw:
#endif
                case ClassDescriptor.FieldType.tpArrayOfByte:
                case ClassDescriptor.FieldType.tpArrayOfSByte:
                    offs = exportBinary(body, offs);
                    break;

                case ClassDescriptor.FieldType.tpArrayOfBoolean:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + (body[offs++] != 0?"1":"0") + "</element>\n");
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfChar:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + (Bytes.unpack2(body, offs) & 0xFFFF) + "</element>\n");
                            offs += 2;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfShort:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + Bytes.unpack2(body, offs) + "</element>\n");
                            offs += 2;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfUShort:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + (ushort)Bytes.unpack2(body, offs) + "</element>\n");
                            offs += 2;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfInt:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + Bytes.unpack4(body, offs) + "</element>\n");
                            offs += 4;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfEnum:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        Type elemType = f.FieldType.GetElementType();
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + Enum.ToObject(elemType, Bytes.unpack4(body, offs)) + "</element>\n");
                            offs += 4;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfUInt:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + (uint)Bytes.unpack4(body, offs) + "</element>\n");
                            offs += 4;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfLong:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + Bytes.unpack8(body, offs) + "</element>\n");
                            offs += 8;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfULong:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + (ulong)Bytes.unpack8(body, offs) + "</element>\n");
                            offs += 8;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfFloat:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + Bytes.unpackF4(body, offs) + "</element>\n");
                            offs += 4;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfDouble:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>" + Bytes.unpackF8(body, offs) + "</element>\n");
                            offs += 8;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfDate:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>\"" + Bytes.unpackDate(body, offs) + "\"</element>\n");
                            offs += 8;
                        }
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfGuid:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            writer.Write("<element>\"" + Bytes.unpackGuid(body, offs) + "\"</element>\n");
                            offs += 16;
                        }
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfDecimal:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            writer.Write("<element>\"" + Bytes.unpackDecimal(body, offs) + "\"</element>\n");
                            offs += 16;
                        }
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfString:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>");
                            offs = exportString(body, offs);
                            writer.Write("</element>\n");
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpLink:
                case ClassDescriptor.FieldType.tpArrayOfObject:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            int oid = Bytes.unpack4(body, offs);
                            if (oid != 0 && (exportedBitmap[oid >> 5] & (1 << (oid & 31))) == 0)
                            {
                                markedBitmap[oid >> 5] |= 1 << (oid & 31);
                            }
                            writer.Write("<element><ref id=\"" + oid + "\"/></element>\n");
                            offs += 4;
                        }
                        indentation(indent);
                    }
                    break;
                }

                case ClassDescriptor.FieldType.tpArrayOfValue:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>\n");
                            offs = exportObject(fd.valueDesc, body, offs, indent + 2);
                            indentation(indent + 1);
                            writer.Write("</element>\n");
                        }
                        indentation(indent);
                    }
                    break;
                }

#if SUPPORT_RAW_TYPE
                case ClassDescriptor.FieldType.tpArrayOfRaw:
                {
                    int len = Bytes.unpack4(body, offs);
                    offs += 4;
                    if (len < 0)
                    {
                        writer.Write("null");
                    }
                    else
                    {
                        writer.Write('\n');
                        while (--len >= 0)
                        {
                            indentation(indent + 1);
                            writer.Write("<element>");
                            offs = exportBinary(body, offs);
                            writer.Write("</element>\n");
                        }
                        indentation(indent);
                    }
                    break;
                }
#endif
                }
                writer.Write("</" + fieldName + ">\n");
            }
            return(offs);
        }
Exemplo n.º 2
0
        int exportKey(byte[] body, int offs, int size, ClassDescriptor.FieldType type)
        {
            switch (type)
            {
            case ClassDescriptor.FieldType.tpBoolean:
                writer.Write(body[offs++] != 0?"1":"0");
                break;

            case ClassDescriptor.FieldType.tpByte:
                writer.Write(System.Convert.ToString((byte)body[offs++]));
                break;

            case ClassDescriptor.FieldType.tpSByte:
                writer.Write(System.Convert.ToString((sbyte)body[offs++]));
                break;

            case ClassDescriptor.FieldType.tpChar:
                writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
                offs += 2;
                break;

            case ClassDescriptor.FieldType.tpShort:
                writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
                offs += 2;
                break;

            case ClassDescriptor.FieldType.tpUShort:
                writer.Write(System.Convert.ToString((ushort)Bytes.unpack2(body, offs)));
                offs += 2;
                break;

            case ClassDescriptor.FieldType.tpInt:
                writer.Write(System.Convert.ToString(Bytes.unpack4(body, offs)));
                offs += 4;
                break;

            case ClassDescriptor.FieldType.tpUInt:
            case ClassDescriptor.FieldType.tpObject:
            case ClassDescriptor.FieldType.tpEnum:
                writer.Write(System.Convert.ToString((uint)Bytes.unpack4(body, offs)));
                offs += 4;
                break;

            case ClassDescriptor.FieldType.tpLong:
                writer.Write(System.Convert.ToString(Bytes.unpack8(body, offs)));
                offs += 8;
                break;

            case ClassDescriptor.FieldType.tpULong:
                writer.Write(System.Convert.ToString((ulong)Bytes.unpack8(body, offs)));
                offs += 8;
                break;

            case ClassDescriptor.FieldType.tpFloat:
                writer.Write(System.Convert.ToString(Bytes.unpackF4(body, offs)));
                offs += 4;
                break;

            case ClassDescriptor.FieldType.tpDouble:
                writer.Write(System.Convert.ToString(Bytes.unpackF8(body, offs)));
                offs += 8;
                break;

            case ClassDescriptor.FieldType.tpGuid:
                writer.Write(Bytes.unpackGuid(body, offs).ToString());
                offs += 16;
                break;

            case ClassDescriptor.FieldType.tpDecimal:
                writer.Write(Bytes.unpackDecimal(body, offs).ToString());
                offs += 16;
                break;

            case ClassDescriptor.FieldType.tpString:
                for (int i = 0; i < size; i++)
                {
                    exportChar((char)Bytes.unpack2(body, offs));
                    offs += 2;
                }
                break;

            case ClassDescriptor.FieldType.tpArrayOfByte:
                for (int i = 0; i < size; i++)
                {
                    byte b = body[offs++];
                    writer.Write(hexDigit[(b >> 4) & 0xF]);
                    writer.Write(hexDigit[b & 0xF]);
                }
                break;

            case ClassDescriptor.FieldType.tpDate:
                writer.Write(Bytes.unpackDate(body, offs).ToString());
                offs += 8;
                break;

            default:
                Debug.Assert(false, "Invalid type");
                break;
            }
            return(offs);
        }