diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-08-14 07:12:29 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-08-14 07:12:29 +0000 |
commit | 1b797548dff7e2475826ba29a71c3f496008988f (patch) | |
tree | 2a81ee2e8fa2f17fd95410aabbf44533d35a727a /src | |
download | libgats-1b797548dff7e2475826ba29a71c3f496008988f.tar.gz libgats-1b797548dff7e2475826ba29a71c3f496008988f.tar.bz2 libgats-1b797548dff7e2475826ba29a71c3f496008988f.tar.xz libgats-1b797548dff7e2475826ba29a71c3f496008988f.zip |
libgats gets it's own repo. The rest of the history is in my misc repo.
Diffstat (limited to '')
-rw-r--r-- | src/boolean.cpp | 42 | ||||
-rw-r--r-- | src/boolean.h | 27 | ||||
-rw-r--r-- | src/dictionary.cpp | 145 | ||||
-rw-r--r-- | src/dictionary.h | 44 | ||||
-rw-r--r-- | src/float.cpp | 0 | ||||
-rw-r--r-- | src/float.h | 0 | ||||
-rw-r--r-- | src/gatsstream.cpp | 29 | ||||
-rw-r--r-- | src/gatsstream.h | 24 | ||||
-rw-r--r-- | src/integer.cpp | 27 | ||||
-rw-r--r-- | src/integer.h | 78 | ||||
-rw-r--r-- | src/list.cpp | 33 | ||||
-rw-r--r-- | src/list.h | 22 | ||||
-rw-r--r-- | src/object.cpp | 55 | ||||
-rw-r--r-- | src/object.h | 39 | ||||
-rw-r--r-- | src/packet.cpp | 10 | ||||
-rw-r--r-- | src/packet.h | 29 | ||||
-rw-r--r-- | src/string.cpp | 38 | ||||
-rw-r--r-- | src/string.h | 26 | ||||
-rw-r--r-- | src/tests/int.cpp | 21 | ||||
-rw-r--r-- | src/unit/basic.unit | 122 |
20 files changed, 811 insertions, 0 deletions
diff --git a/src/boolean.cpp b/src/boolean.cpp new file mode 100644 index 0000000..087845a --- /dev/null +++ b/src/boolean.cpp | |||
@@ -0,0 +1,42 @@ | |||
1 | #include "gats/boolean.h" | ||
2 | |||
3 | #include <bu/stream.h> | ||
4 | |||
5 | Gats::Boolean::Boolean() : | ||
6 | bVal( false ) | ||
7 | { | ||
8 | } | ||
9 | |||
10 | Gats::Boolean::Boolean( bool bVal ) : | ||
11 | bVal( bVal ) | ||
12 | { | ||
13 | } | ||
14 | |||
15 | Gats::Boolean::~Boolean() | ||
16 | { | ||
17 | } | ||
18 | |||
19 | void Gats::Boolean::write( Bu::Stream &rOut ) const | ||
20 | { | ||
21 | if( bVal ) | ||
22 | { | ||
23 | rOut.write("1", 1 ); | ||
24 | } | ||
25 | else | ||
26 | { | ||
27 | rOut.write("0", 1 ); | ||
28 | } | ||
29 | } | ||
30 | |||
31 | void Gats::Boolean::read( Bu::Stream &rIn, char cType ) | ||
32 | { | ||
33 | if( cType == '1' ) | ||
34 | { | ||
35 | bVal = true; | ||
36 | } | ||
37 | else | ||
38 | { | ||
39 | bVal = false; | ||
40 | } | ||
41 | } | ||
42 | |||
diff --git a/src/boolean.h b/src/boolean.h new file mode 100644 index 0000000..3035b32 --- /dev/null +++ b/src/boolean.h | |||
@@ -0,0 +1,27 @@ | |||
1 | #ifndef GATS_BOOLEAN_H | ||
2 | #define GATS_BOOLEAN_H | ||
3 | |||
4 | #include "gats/object.h" | ||
5 | |||
6 | namespace Gats | ||
7 | { | ||
8 | class Boolean : public Gats::Object | ||
9 | { | ||
10 | public: | ||
11 | Boolean(); | ||
12 | Boolean( bool bVal ); | ||
13 | virtual ~Boolean(); | ||
14 | |||
15 | virtual Type getType() const { return typeBoolean; } | ||
16 | bool getValue() const { return bVal; } | ||
17 | void setValue( bool b ) { bVal = b; } | ||
18 | |||
19 | virtual void write( Bu::Stream &rOut ) const; | ||
20 | virtual void read( Bu::Stream &rIn, char cType ); | ||
21 | |||
22 | private: | ||
23 | bool bVal; | ||
24 | }; | ||
25 | }; | ||
26 | |||
27 | #endif | ||
diff --git a/src/dictionary.cpp b/src/dictionary.cpp new file mode 100644 index 0000000..385960f --- /dev/null +++ b/src/dictionary.cpp | |||
@@ -0,0 +1,145 @@ | |||
1 | #include "gats/dictionary.h" | ||
2 | |||
3 | #include "gats/boolean.h" | ||
4 | #include "gats/integer.h" | ||
5 | #include "gats/float.h" | ||
6 | #include "gats/string.h" | ||
7 | #include "gats/list.h" | ||
8 | |||
9 | template<> | ||
10 | uint32_t Bu::__calcHashCode<Gats::String>( const Gats::String &s ) | ||
11 | { | ||
12 | return __calcHashCode( dynamic_cast<const Bu::FString &>(s) ); | ||
13 | } | ||
14 | |||
15 | Gats::Dictionary::Dictionary() | ||
16 | { | ||
17 | } | ||
18 | |||
19 | Gats::Dictionary::~Dictionary() | ||
20 | { | ||
21 | for( iterator i = begin(); i; i++ ) | ||
22 | { | ||
23 | delete *i; | ||
24 | } | ||
25 | } | ||
26 | |||
27 | void Gats::Dictionary::write( Bu::Stream &rOut ) const | ||
28 | { | ||
29 | rOut.write("d", 1 ); | ||
30 | for( const_iterator i= begin(); i; i++ ) | ||
31 | { | ||
32 | i.getKey().write( rOut ); | ||
33 | (*i)->write( rOut ); | ||
34 | } | ||
35 | rOut.write("e", 1 ); | ||
36 | } | ||
37 | |||
38 | void Gats::Dictionary::read( Bu::Stream &rIn, char cType ) | ||
39 | { | ||
40 | for(;;) | ||
41 | { | ||
42 | char cNext; | ||
43 | rIn.read( &cNext, 1 ); | ||
44 | if( cNext == 'e' ) | ||
45 | break; | ||
46 | if( cNext != 's' ) | ||
47 | throw Bu::ExceptionBase("You can only use strings as keys."); | ||
48 | Gats::String sKey; | ||
49 | sKey.read( rIn, cNext ); | ||
50 | |||
51 | ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert( | ||
52 | sKey, Gats::Object::read( rIn ) | ||
53 | ); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | void Gats::Dictionary::insert( const Bu::FString &sKey, int64_t i ) | ||
58 | { | ||
59 | ((Bu::Hash<Gats::String, Gats::Object *> *)this)->insert( | ||
60 | sKey, new Gats::Integer( i ) | ||
61 | ); | ||
62 | } | ||
63 | |||
64 | void Gats::Dictionary::insert( const Bu::FString &sKey, bool b ) | ||
65 | { | ||
66 | Bu::Hash<Gats::String, Gats::Object *>::insert( | ||
67 | sKey, new Gats::Boolean( b ) | ||
68 | ); | ||
69 | } | ||
70 | |||
71 | void Gats::Dictionary::insert( const Bu::FString &sKey, double d ) | ||
72 | { | ||
73 | // Bu::Hash<Gats::String, Gats::Object *>::insert( | ||
74 | // sKey, new Gats::Float( d ) | ||
75 | // ); | ||
76 | } | ||
77 | |||
78 | void Gats::Dictionary::insert( const Bu::FString &sKey, const Bu::FString &s ) | ||
79 | { | ||
80 | Bu::Hash<Gats::String, Gats::Object *>::insert( | ||
81 | sKey, new Gats::String( s ) | ||
82 | ); | ||
83 | } | ||
84 | |||
85 | bool Gats::Dictionary::getBool( const Bu::FString &sKey ) | ||
86 | { | ||
87 | Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) ); | ||
88 | if( pOb ) | ||
89 | throw Bu::ExceptionBase("Cannot cast item '%s' to bool.", | ||
90 | sKey.getStr() ); | ||
91 | |||
92 | return pOb->getValue(); | ||
93 | } | ||
94 | |||
95 | int64_t Gats::Dictionary::getInt( const Bu::FString &sKey ) | ||
96 | { | ||
97 | Gats::Integer *pOb = dynamic_cast<Gats::Integer *>( get( sKey ) ); | ||
98 | if( pOb ) | ||
99 | throw Bu::ExceptionBase("Cannot cast item '%s' to int.", | ||
100 | sKey.getStr() ); | ||
101 | |||
102 | return pOb->getValue(); | ||
103 | } | ||
104 | |||
105 | double Gats::Dictionary::getFloat( const Bu::FString &sKey ) | ||
106 | {/* | ||
107 | Gats::Boolean *pOb = dynamic_cast<Gats::Boolean *>( get( sKey ) ); | ||
108 | if( pOb ) | ||
109 | throw Bu::ExceptionBase("Cannot cast item '%s' to bool.", | ||
110 | sKey.getStr() ); | ||
111 | |||
112 | return pOb->getValue();*/ | ||
113 | return 0.0; | ||
114 | } | ||
115 | |||
116 | Bu::FString Gats::Dictionary::getStr( const Bu::FString &sKey ) | ||
117 | { | ||
118 | Gats::String *pOb = dynamic_cast<Gats::String *>( get( sKey ) ); | ||
119 | if( pOb ) | ||
120 | throw Bu::ExceptionBase("Cannot cast item '%s' to string.", | ||
121 | sKey.getStr() ); | ||
122 | |||
123 | return *pOb; | ||
124 | } | ||
125 | |||
126 | Gats::List *Gats::Dictionary::getList( const Bu::FString &sKey ) | ||
127 | { | ||
128 | Gats::List *pOb = dynamic_cast<Gats::List *>( get( sKey ) ); | ||
129 | if( pOb ) | ||
130 | throw Bu::ExceptionBase("Cannot cast item '%s' to list.", | ||
131 | sKey.getStr() ); | ||
132 | |||
133 | return pOb; | ||
134 | } | ||
135 | |||
136 | Gats::Dictionary *Gats::Dictionary::getDict( const Bu::FString &sKey ) | ||
137 | { | ||
138 | Gats::Dictionary *pOb = dynamic_cast<Gats::Dictionary *>( get( sKey ) ); | ||
139 | if( pOb ) | ||
140 | throw Bu::ExceptionBase("Cannot cast item '%s' to dictionary.", | ||
141 | sKey.getStr() ); | ||
142 | |||
143 | return pOb; | ||
144 | } | ||
145 | |||
diff --git a/src/dictionary.h b/src/dictionary.h new file mode 100644 index 0000000..3bcaec6 --- /dev/null +++ b/src/dictionary.h | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef GATS_DICTIONARY_H | ||
2 | #define GATS_DICTIONARY_H | ||
3 | |||
4 | #include "gats/object.h" | ||
5 | #include "gats/string.h" | ||
6 | #include <bu/hash.h> | ||
7 | |||
8 | namespace Gats | ||
9 | { | ||
10 | class List; | ||
11 | |||
12 | class Dictionary : public Gats::Object, | ||
13 | public Bu::Hash<Gats::String, Gats::Object *> | ||
14 | { | ||
15 | public: | ||
16 | Dictionary(); | ||
17 | virtual ~Dictionary(); | ||
18 | |||
19 | virtual Type getType() const { return typeDictionary; } | ||
20 | virtual void write( Bu::Stream &rOut ) const; | ||
21 | virtual void read( Bu::Stream &rIn, char cType ); | ||
22 | |||
23 | void insert( const Bu::FString &sKey, int64_t i ); | ||
24 | void insert( const Bu::FString &sKey, bool b ); | ||
25 | void insert( const Bu::FString &sKey, double d ); | ||
26 | void insert( const Bu::FString &sKey, const Bu::FString &s ); | ||
27 | using Bu::Hash<Gats::String, Gats::Object *>::insert; | ||
28 | |||
29 | bool getBool( const Bu::FString &sKey ); | ||
30 | int64_t getInt( const Bu::FString &sKey ); | ||
31 | double getFloat( const Bu::FString &sKey ); | ||
32 | Bu::FString getStr( const Bu::FString &sKey ); | ||
33 | Gats::List *getList( const Bu::FString &sKey ); | ||
34 | Gats::Dictionary *getDict( const Bu::FString &sKey ); | ||
35 | }; | ||
36 | }; | ||
37 | |||
38 | namespace Bu | ||
39 | { | ||
40 | template<> | ||
41 | uint32_t __calcHashCode<Gats::String>( const Gats::String &s ); | ||
42 | }; | ||
43 | |||
44 | #endif | ||
diff --git a/src/float.cpp b/src/float.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/float.cpp | |||
diff --git a/src/float.h b/src/float.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/float.h | |||
diff --git a/src/gatsstream.cpp b/src/gatsstream.cpp new file mode 100644 index 0000000..4b9cadf --- /dev/null +++ b/src/gatsstream.cpp | |||
@@ -0,0 +1,29 @@ | |||
1 | #include "gats/gatsstream.h" | ||
2 | #include "gats/object.h" | ||
3 | |||
4 | #include <bu/sio.h> | ||
5 | #include <bu/nullstream.h> | ||
6 | using namespace Bu; | ||
7 | |||
8 | Gats::GatsStream::GatsStream( Bu::Stream &rStream ) : | ||
9 | rStream( rStream ) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | Gats::GatsStream::~GatsStream() | ||
14 | { | ||
15 | } | ||
16 | |||
17 | Gats::Object *Gats::GatsStream::readObject() | ||
18 | { | ||
19 | |||
20 | } | ||
21 | |||
22 | void Gats::GatsStream::writeObject( Gats::Object *pObject ) | ||
23 | { | ||
24 | Bu::NullStream ns; | ||
25 | pObject->write( ns ); | ||
26 | |||
27 | sio << "Object consumed " << ns.tell() << "b." << sio.nl; | ||
28 | } | ||
29 | |||
diff --git a/src/gatsstream.h b/src/gatsstream.h new file mode 100644 index 0000000..d668c28 --- /dev/null +++ b/src/gatsstream.h | |||
@@ -0,0 +1,24 @@ | |||
1 | #ifndef GATS_STREAM_H | ||
2 | #define GATS_STREAM_H | ||
3 | |||
4 | #include <bu/stream.h> | ||
5 | |||
6 | namespace Gats | ||
7 | { | ||
8 | class Object; | ||
9 | |||
10 | class GatsStream | ||
11 | { | ||
12 | public: | ||
13 | GatsStream( Bu::Stream &rStream ); | ||
14 | virtual ~GatsStream(); | ||
15 | |||
16 | Gats::Object *readObject(); | ||
17 | void writeObject( Gats::Object *pObject ); | ||
18 | |||
19 | private: | ||
20 | Bu::Stream &rStream; | ||
21 | }; | ||
22 | }; | ||
23 | |||
24 | #endif | ||
diff --git a/src/integer.cpp b/src/integer.cpp new file mode 100644 index 0000000..ad48eaf --- /dev/null +++ b/src/integer.cpp | |||
@@ -0,0 +1,27 @@ | |||
1 | #include "gats/integer.h" | ||
2 | |||
3 | Gats::Integer::Integer() : | ||
4 | iVal( 0 ) | ||
5 | { | ||
6 | } | ||
7 | |||
8 | Gats::Integer::Integer( int64_t iVal ) : | ||
9 | iVal( iVal ) | ||
10 | { | ||
11 | } | ||
12 | |||
13 | Gats::Integer::~Integer() | ||
14 | { | ||
15 | } | ||
16 | |||
17 | void Gats::Integer::write( Bu::Stream &rOut ) const | ||
18 | { | ||
19 | rOut.write("i", 1 ); | ||
20 | writePackedInt( rOut, iVal ); | ||
21 | } | ||
22 | |||
23 | void Gats::Integer::read( Bu::Stream &rIn, char cType ) | ||
24 | { | ||
25 | readPackedInt( rIn, iVal ); | ||
26 | } | ||
27 | |||
diff --git a/src/integer.h b/src/integer.h new file mode 100644 index 0000000..6ed7c49 --- /dev/null +++ b/src/integer.h | |||
@@ -0,0 +1,78 @@ | |||
1 | #ifndef GATS_INTEGER_H | ||
2 | #define GATS_INTEGER_H | ||
3 | |||
4 | #include "gats/object.h" | ||
5 | |||
6 | #include <stdint.h> | ||
7 | |||
8 | #include <bu/stream.h> | ||
9 | |||
10 | namespace Gats | ||
11 | { | ||
12 | class Integer : public Gats::Object | ||
13 | { | ||
14 | public: | ||
15 | Integer(); | ||
16 | Integer( int64_t iVal ); | ||
17 | virtual ~Integer(); | ||
18 | |||
19 | virtual Type getType() const { return typeInteger; } | ||
20 | int64_t getValue() const { return iVal; } | ||
21 | |||
22 | virtual void write( Bu::Stream &rOut ) const; | ||
23 | virtual void read( Bu::Stream &rIn, char cType ); | ||
24 | |||
25 | template<typename itype> | ||
26 | static void readPackedInt( Bu::Stream &rStream, itype &rOut ) | ||
27 | { | ||
28 | int8_t b; | ||
29 | rOut = 0; | ||
30 | bool bNeg; | ||
31 | |||
32 | rStream.read( &b, 1 ); | ||
33 | bNeg = ( b&0x40 ); | ||
34 | rOut |= (itype(b&0x3F)); | ||
35 | int c = 0; | ||
36 | while( (b&0x80) ) | ||
37 | { | ||
38 | rStream.read( &b, 1 ); | ||
39 | rOut |= (itype(b&0x7F)) << (6+7*(c++)); | ||
40 | } | ||
41 | if( bNeg ) rOut = -rOut; | ||
42 | } | ||
43 | |||
44 | template<typename itype> | ||
45 | static void writePackedInt( Bu::Stream &rStream, itype iIn ) | ||
46 | { | ||
47 | uint8_t b; | ||
48 | |||
49 | if( iIn < 0 ) | ||
50 | { | ||
51 | iIn = -iIn; | ||
52 | b = (iIn&0x3F) | 0x40; | ||
53 | } | ||
54 | else | ||
55 | { | ||
56 | b = (iIn&0x3F); | ||
57 | } | ||
58 | if( iIn > b ) | ||
59 | b |= 0x80; | ||
60 | rStream.write( &b, 1 ); | ||
61 | iIn = iIn >> 6; | ||
62 | |||
63 | while( iIn ) | ||
64 | { | ||
65 | b = (iIn&0x7F); | ||
66 | if( iIn > b ) | ||
67 | b |= 0x80; | ||
68 | rStream.write( &b, 1 ); | ||
69 | iIn = iIn >> 7; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | private: | ||
74 | int64_t iVal; | ||
75 | }; | ||
76 | }; | ||
77 | |||
78 | #endif | ||
diff --git a/src/list.cpp b/src/list.cpp new file mode 100644 index 0000000..995a764 --- /dev/null +++ b/src/list.cpp | |||
@@ -0,0 +1,33 @@ | |||
1 | #include "gats/list.h" | ||
2 | |||
3 | #include <bu/stream.h> | ||
4 | |||
5 | Gats::List::List() | ||
6 | { | ||
7 | } | ||
8 | |||
9 | Gats::List::~List() | ||
10 | { | ||
11 | } | ||
12 | |||
13 | void Gats::List::write( Bu::Stream &rOut ) const | ||
14 | { | ||
15 | rOut.write("l", 1 ); | ||
16 | for( const_iterator i = begin(); i; i++ ) | ||
17 | { | ||
18 | (*i)->write( rOut ); | ||
19 | } | ||
20 | rOut.write("e", 1 ); | ||
21 | } | ||
22 | |||
23 | void Gats::List::read( Bu::Stream &rIn, char cType ) | ||
24 | { | ||
25 | for(;;) | ||
26 | { | ||
27 | Gats::Object *pObj = Gats::Object::read( rIn ); | ||
28 | if( pObj == NULL ) | ||
29 | break; | ||
30 | append( pObj ); | ||
31 | } | ||
32 | } | ||
33 | |||
diff --git a/src/list.h b/src/list.h new file mode 100644 index 0000000..48faf23 --- /dev/null +++ b/src/list.h | |||
@@ -0,0 +1,22 @@ | |||
1 | #ifndef GATS_LIST_H | ||
2 | #define GATS_LIST_H | ||
3 | |||
4 | #include "gats/object.h" | ||
5 | #include <bu/list.h> | ||
6 | |||
7 | namespace Gats | ||
8 | { | ||
9 | class List : public Gats::Object, public Bu::List<Gats::Object *> | ||
10 | { | ||
11 | public: | ||
12 | List(); | ||
13 | virtual ~List(); | ||
14 | |||
15 | virtual Type getType() const { return typeList; } | ||
16 | |||
17 | virtual void write( Bu::Stream &rOut ) const; | ||
18 | virtual void read( Bu::Stream &rIn, char cType ); | ||
19 | }; | ||
20 | }; | ||
21 | |||
22 | #endif | ||
diff --git a/src/object.cpp b/src/object.cpp new file mode 100644 index 0000000..3d7765e --- /dev/null +++ b/src/object.cpp | |||
@@ -0,0 +1,55 @@ | |||
1 | #include "gats/object.h" | ||
2 | |||
3 | #include "gats/integer.h" | ||
4 | #include "gats/float.h" | ||
5 | #include "gats/boolean.h" | ||
6 | #include "gats/string.h" | ||
7 | #include "gats/list.h" | ||
8 | #include "gats/dictionary.h" | ||
9 | |||
10 | #include <bu/stream.h> | ||
11 | |||
12 | Gats::Object::Object() | ||
13 | { | ||
14 | } | ||
15 | |||
16 | Gats::Object::~Object() | ||
17 | { | ||
18 | } | ||
19 | |||
20 | Gats::Object *Gats::Object::read( Bu::Stream &rIn ) | ||
21 | { | ||
22 | char buf; | ||
23 | rIn.read( &buf, 1 ); | ||
24 | Object *pObj = NULL; | ||
25 | switch( buf ) | ||
26 | { | ||
27 | case 'i': | ||
28 | pObj = new Gats::Integer(); | ||
29 | break; | ||
30 | |||
31 | case 's': | ||
32 | pObj = new Gats::String(); | ||
33 | break; | ||
34 | |||
35 | case '0': | ||
36 | case '1': | ||
37 | pObj = new Gats::Boolean(); | ||
38 | break; | ||
39 | |||
40 | case 'l': | ||
41 | pObj = new Gats::List(); | ||
42 | break; | ||
43 | |||
44 | case 'e': | ||
45 | return NULL; | ||
46 | |||
47 | default: | ||
48 | throw Bu::ExceptionBase("Invalid Gats type discovered: %c.", buf ); | ||
49 | } | ||
50 | |||
51 | pObj->read( rIn, buf ); | ||
52 | |||
53 | return pObj; | ||
54 | } | ||
55 | |||
diff --git a/src/object.h b/src/object.h new file mode 100644 index 0000000..10bdc47 --- /dev/null +++ b/src/object.h | |||
@@ -0,0 +1,39 @@ | |||
1 | #ifndef GATS_OBJECT_H | ||
2 | #define GATS_OBJECT_H | ||
3 | |||
4 | namespace Bu | ||
5 | { | ||
6 | class Stream; | ||
7 | }; | ||
8 | |||
9 | namespace Gats | ||
10 | { | ||
11 | enum Type | ||
12 | { | ||
13 | typeDictionary, | ||
14 | typeList, | ||
15 | typeString, | ||
16 | typeInteger, | ||
17 | typeFloat, | ||
18 | typeBoolean | ||
19 | }; | ||
20 | |||
21 | /** | ||
22 | * The baseclass for every type that can be stored in a packet. | ||
23 | */ | ||
24 | class Object | ||
25 | { | ||
26 | public: | ||
27 | Object(); | ||
28 | virtual ~Object(); | ||
29 | |||
30 | virtual Type getType() const =0; | ||
31 | |||
32 | virtual void write( Bu::Stream &rOut ) const=0; | ||
33 | virtual void read( Bu::Stream &rIn, char cType )=0; | ||
34 | |||
35 | static Object *read( Bu::Stream &rIn ); | ||
36 | }; | ||
37 | }; | ||
38 | |||
39 | #endif | ||
diff --git a/src/packet.cpp b/src/packet.cpp new file mode 100644 index 0000000..6283ce5 --- /dev/null +++ b/src/packet.cpp | |||
@@ -0,0 +1,10 @@ | |||
1 | #include "gats/packet.h" | ||
2 | |||
3 | Gats::Packet::Packet() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | Gats::Packet::~Packet() | ||
8 | { | ||
9 | } | ||
10 | |||
diff --git a/src/packet.h b/src/packet.h new file mode 100644 index 0000000..5ddc9ee --- /dev/null +++ b/src/packet.h | |||
@@ -0,0 +1,29 @@ | |||
1 | #ifndef GATS_PACKET_H | ||
2 | #define GATS_PACKET_H | ||
3 | |||
4 | namespace Gats | ||
5 | { | ||
6 | class Object; | ||
7 | |||
8 | /** | ||
9 | * A D-encoded packet is a header and metadata related to encoding as well | ||
10 | * as a single object. The advantage of this method is that the packet | ||
11 | * contains all information related to encoding and is highly portable, it | ||
12 | * also lets the reader know how much data to expect, which makes this | ||
13 | * efficient for use in protocols and embedding in other data streams. | ||
14 | */ | ||
15 | class Packet | ||
16 | { | ||
17 | public: | ||
18 | Packet(); | ||
19 | Packet( Object *pObject ); | ||
20 | virtual ~Packet(); | ||
21 | |||
22 | Object *getObject() { return pRoot; } | ||
23 | |||
24 | private: | ||
25 | Object *pRoot; | ||
26 | }; | ||
27 | }; | ||
28 | |||
29 | #endif | ||
diff --git a/src/string.cpp b/src/string.cpp new file mode 100644 index 0000000..416375d --- /dev/null +++ b/src/string.cpp | |||
@@ -0,0 +1,38 @@ | |||
1 | #include "gats/string.h" | ||
2 | |||
3 | #include "gats/integer.h" | ||
4 | |||
5 | Gats::String::String() | ||
6 | { | ||
7 | } | ||
8 | |||
9 | Gats::String::String( const String &s ) : | ||
10 | Bu::FString( s ) | ||
11 | { | ||
12 | } | ||
13 | |||
14 | Gats::String::String( const Bu::FString &s ) : | ||
15 | Bu::FString( s ) | ||
16 | { | ||
17 | } | ||
18 | |||
19 | Gats::String::~String() | ||
20 | { | ||
21 | } | ||
22 | |||
23 | void Gats::String::write( Bu::Stream &rOut ) const | ||
24 | { | ||
25 | rOut.write("s", 1 ); | ||
26 | uint32_t iSize = getSize(); | ||
27 | Gats::Integer::writePackedInt( rOut, iSize ); | ||
28 | rOut.write( getStr(), iSize ); | ||
29 | } | ||
30 | |||
31 | void Gats::String::read( Bu::Stream &rIn, char cType ) | ||
32 | { | ||
33 | uint32_t iSize; | ||
34 | Gats::Integer::readPackedInt( rIn, iSize ); | ||
35 | setSize( iSize ); | ||
36 | rIn.read( getStr(), iSize ); | ||
37 | } | ||
38 | |||
diff --git a/src/string.h b/src/string.h new file mode 100644 index 0000000..5343ba8 --- /dev/null +++ b/src/string.h | |||
@@ -0,0 +1,26 @@ | |||
1 | #ifndef GATS_STRING_H | ||
2 | #define GATS_STRING_H | ||
3 | |||
4 | #include "gats/object.h" | ||
5 | #include <bu/fstring.h> | ||
6 | |||
7 | namespace Gats | ||
8 | { | ||
9 | class String : public Gats::Object, public Bu::FString | ||
10 | { | ||
11 | public: | ||
12 | String(); | ||
13 | String( const String &s ); | ||
14 | String( const Bu::FString &s ); | ||
15 | virtual ~String(); | ||
16 | |||
17 | virtual Type getType() const { return typeString; } | ||
18 | |||
19 | virtual void write( Bu::Stream &rOut ) const; | ||
20 | virtual void read( Bu::Stream &rIn, char cType ); | ||
21 | |||
22 | private: | ||
23 | }; | ||
24 | }; | ||
25 | |||
26 | #endif | ||
diff --git a/src/tests/int.cpp b/src/tests/int.cpp new file mode 100644 index 0000000..5d5b7d2 --- /dev/null +++ b/src/tests/int.cpp | |||
@@ -0,0 +1,21 @@ | |||
1 | #include "gats/integer.h" | ||
2 | |||
3 | #include <bu/sio.h> | ||
4 | #include <bu/membuf.h> | ||
5 | |||
6 | using namespace Bu; | ||
7 | |||
8 | int main() | ||
9 | { | ||
10 | MemBuf mb; | ||
11 | int64_t i = -53954321995838ll; | ||
12 | |||
13 | sio << "Before: " << i << sio.nl; | ||
14 | Gats::Integer::writePackedInt( mb, i ); | ||
15 | mb.setPos( 0 ); | ||
16 | Gats::Integer::readPackedInt( mb, i ); | ||
17 | sio << "After: " << i << sio.nl; | ||
18 | |||
19 | return 0; | ||
20 | } | ||
21 | |||
diff --git a/src/unit/basic.unit b/src/unit/basic.unit new file mode 100644 index 0000000..9389c70 --- /dev/null +++ b/src/unit/basic.unit | |||
@@ -0,0 +1,122 @@ | |||
1 | // vim: syntax=cpp | ||
2 | /* | ||
3 | * Copyright (C) 2007-2010 Xagasoft, All rights reserved. | ||
4 | * | ||
5 | * This file is part of the libbu++ library and is released under the | ||
6 | * terms of the license contained in the file LICENSE. | ||
7 | */ | ||
8 | |||
9 | #include "gats/dictionary.h" | ||
10 | #include "gats/integer.h" | ||
11 | #include "gats/float.h" | ||
12 | #include "gats/list.h" | ||
13 | #include "gats/boolean.h" | ||
14 | #include "gats/string.h" | ||
15 | |||
16 | #include "bu/membuf.h" | ||
17 | #include "bu/list.h" | ||
18 | #include "bu/sio.h" | ||
19 | |||
20 | #include <stdlib.h> | ||
21 | |||
22 | using namespace Bu; | ||
23 | |||
24 | suite Basic | ||
25 | { | ||
26 | test integer | ||
27 | { | ||
28 | Bu::List<int64_t> lInts; | ||
29 | Bu::MemBuf mb; | ||
30 | |||
31 | int64_t i = 1; | ||
32 | for( int x = 0; x < 256; x++ ) | ||
33 | { | ||
34 | lInts.append( i ); | ||
35 | Gats::Integer( i ).write( mb ); | ||
36 | i = -(i<<1)-i; | ||
37 | } | ||
38 | |||
39 | mb.setPos( 0 ); | ||
40 | |||
41 | for( Bu::List<int64_t>::iterator j = lInts.begin(); j; j++ ) | ||
42 | { | ||
43 | Gats::Object *pObj = Gats::Object::read( mb ); | ||
44 | if( pObj->getType() != Gats::typeInteger ) | ||
45 | unitFailed("Bad type read."); | ||
46 | |||
47 | if( dynamic_cast<Gats::Integer *>(pObj)->getValue() != *j ) | ||
48 | unitFailed("Bad number."); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | test string | ||
53 | { | ||
54 | Bu::List<Bu::FString> lStrs; | ||
55 | Bu::MemBuf mb; | ||
56 | |||
57 | lStrs.append( Bu::FString() ); | ||
58 | Gats::String("").write( mb ); | ||
59 | |||
60 | { | ||
61 | int iMax = 0; | ||
62 | for( int j = 1; j <= (1<<16); j=j<<1 ) | ||
63 | iMax += j; | ||
64 | setStepCount( iMax ); | ||
65 | } | ||
66 | for( int j = 1; j <= (1<<16); j=j<<1 ) | ||
67 | { | ||
68 | Bu::FString s( j ); | ||
69 | for( int x = 0; x < j; x++ ) | ||
70 | { | ||
71 | s[x] = (unsigned char)(random()%256); | ||
72 | } | ||
73 | incProgress( j ); | ||
74 | Gats::String( s ).write( mb ); | ||
75 | lStrs.append( s ); | ||
76 | } | ||
77 | |||
78 | mb.setPos( 0 ); | ||
79 | |||
80 | for( Bu::List<Bu::FString>::iterator i = lStrs.begin(); i; i++ ) | ||
81 | { | ||
82 | Gats::Object *pObj = Gats::Object::read( mb ); | ||
83 | if( pObj->getType() != Gats::typeString ) | ||
84 | unitFailed("Bad type read."); | ||
85 | |||
86 | if( *dynamic_cast<Gats::String *>(pObj) != *i ) | ||
87 | unitFailed("Bad string."); | ||
88 | } | ||
89 | } | ||
90 | |||
91 | test boolean | ||
92 | { | ||
93 | Bu::List<bool> lBs; | ||
94 | Bu::MemBuf mb; | ||
95 | |||
96 | for( int j = 0; j < 1024; j++ ) | ||
97 | { | ||
98 | if( random()%2 == 0 ) | ||
99 | { | ||
100 | lBs.append( true ); | ||
101 | Gats::Boolean( true ).write( mb ); | ||
102 | } | ||
103 | else | ||
104 | { | ||
105 | lBs.append( false ); | ||
106 | Gats::Boolean( false ).write( mb ); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | mb.setPos( 0 ); | ||
111 | |||
112 | for( Bu::List<bool>::iterator i = lBs.begin(); i; i++ ) | ||
113 | { | ||
114 | Gats::Object *pObj = Gats::Object::read( mb ); | ||
115 | if( pObj->getType() != Gats::typeBoolean ) | ||
116 | unitFailed("Bad type read."); | ||
117 | |||
118 | if( dynamic_cast<Gats::Boolean *>(pObj)->getValue() != *i ) | ||
119 | unitFailed("Bad string."); | ||
120 | } | ||
121 | } | ||
122 | } | ||