aboutsummaryrefslogtreecommitdiff
path: root/cs-dotnet/src/gatsobject.cs
blob: 99eeec271d98586e7c7c41fe8dece5b2a5820cf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libgats library and is released under the
 * terms of the license contained in the file LICENSE.
 */

using System.IO;

namespace Com.Xagasoft.Gats
{
    /// <summary>
    /// The base class of all GATS Data Type classes.
    /// </summary>
    /// <remarks>
    /// This provides the standard Read and Write methods that are used to do
    /// all type serialization, as well as a handy static Read function that
    /// can be used
    /// to read in any type.  These methods should not be used in normal
    /// programming, instead use the GatsStream class to read and write
    /// complete GATS packets.
    /// </remarks>
    public abstract class GatsObject
    {
        /// <summary>
        /// Read a single object from the provided stream.
        /// </summary>
        /// <remarks>
        /// This method does not read the leading type character.  The static
        /// Read method in GatsObject does this, and passes the type into the
        /// Read method as a parameter in case it's needed by a specific type,
        /// such as GatsBoolean.
        /// </remarks>
        /// <param name="s">The Stream derived class to read from.</param>
        /// <param name="type">The already read type specifier.</param>
        public abstract void Read( Stream s, char type );

        /// <summary>
        /// Write a single object to the provided stream.
        /// </summary>
        /// <remarks>
        /// Unlike the Read method, this does actually write the leading type
        /// specifier.
        /// </remarks>
        /// <param name="s">The Stream derived class to write to.</param>
        public abstract void Write( Stream s );

        /// <summary>
        /// Reads a GatsObject from the provided stream and returns it.
        /// </summary>
        /// <remarks>
        /// This method reads the initial type specifier byte, constructs the
        /// proper object, calls the Read method on that object, and returns
        /// the result.
        /// </remarks>
        /// <returns>
        /// The constructed object, or null if an end type was found.
        /// </returns>
        public static GatsObject Read( Stream s )
        {
            int b = s.ReadByte();
            if( b == -1 )
                throw new GatsException( GatsException.Type.PrematureEnd );
            char type = (char)b;
            GatsObject ret = null;
            switch( type )
            {
                case 'i':
                    ret = new GatsInteger();
                    break;

                case 's':
                    ret = new GatsString();
                    break;

                case '0':
                case '1':
                    ret = new GatsBoolean();
                    break;

                case 'l':
                    ret = new GatsList();
                    break;

                case 'd':
                    ret = new GatsDictionary();
                    break;

                case 'f':
                case 'F':
                    ret = new GatsFloat();
                    break;

                case 'n':
                    ret = new GatsNull();
                    break;

                case 'e':
                    return null;

                default:
                    throw new GatsException( GatsException.Type.InvalidType );
            }

            ret.Read( s, type );

            return ret;
        }
    }
}