aboutsummaryrefslogtreecommitdiff
path: root/src/object.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-05-17 01:24:51 +0000
committerMike Buland <eichlan@xagasoft.com>2011-05-17 01:24:51 +0000
commitefcbdb7a0347b4399cbabacf3cbea432eeafb17b (patch)
treed468a9d8e129cba0cef68a09421c0e21c720ede6 /src/object.cpp
parent02c60c6720f41bcfc367d02ae4c655b651642991 (diff)
downloadlibgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.tar.gz
libgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.tar.bz2
libgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.tar.xz
libgats-efcbdb7a0347b4399cbabacf3cbea432eeafb17b.zip
Gats::Object now has a strToGats function, it's pretty slick, it takes a string
and produces a fully formed gats tree. Also, gatscon now can interact with a server directly.
Diffstat (limited to 'src/object.cpp')
-rw-r--r--src/object.cpp192
1 files changed, 192 insertions, 0 deletions
diff --git a/src/object.cpp b/src/object.cpp
index af81017..da77ff8 100644
--- a/src/object.cpp
+++ b/src/object.cpp
@@ -7,9 +7,14 @@
7#include "gats/list.h" 7#include "gats/list.h"
8#include "gats/dictionary.h" 8#include "gats/dictionary.h"
9 9
10#include <stdlib.h>
11
10#include <bu/formatter.h> 12#include <bu/formatter.h>
11#include <bu/stream.h> 13#include <bu/stream.h>
12 14
15#include <bu/sio.h>
16using namespace Bu;
17
13Gats::Object::Object() 18Gats::Object::Object()
14{ 19{
15} 20}
@@ -63,6 +68,193 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn )
63 return pObj; 68 return pObj;
64} 69}
65 70
71void Gats::Object::skipWs( Bu::String::const_iterator &i )
72{
73 for(; *i == ' ' || *i == '\t' || *i == '\r' || *i == '\n'; i++ ) { }
74}
75
76Bu::String Gats::Object::token( Bu::String::const_iterator &i )
77{
78 Bu::String sRet;
79 if( *i == '\"' )
80 {
81 for( i++; i && *i != '\"' ; i++ )
82 {
83 if( *i == '\\' )
84 i++;
85 sRet += i;
86 }
87 i++;
88 }
89 else
90 {
91 for(; i && *i != ' ' && *i != '\t' && *i != '\r' && *i != '\n' &&
92 *i != ',' && *i != ']' && *i != '}' && *i != '[' &&
93 *i != '{'; i++ )
94 {
95 sRet += i;
96 }
97 }
98
99 return sRet;
100}
101
102Gats::Object *Gats::Object::strToGats( Bu::String::const_iterator &i )
103{
104 skipWs( i );
105
106 switch( *i )
107 {
108 case '[':
109 {
110 Gats::List *pLst = new Gats::List();
111 i++;
112 for(;;)
113 {
114 Gats::Object *pObj = strToGats( i );
115 if( !pObj )
116 break;
117 pLst->append( pObj );
118 skipWs( i );
119 switch( *i )
120 {
121 case ',':
122 i++;
123 break;
124
125 case ']':
126 i++;
127 return pLst;
128
129 default:
130 throw Bu::ExceptionBase("Invalid character found.");
131 }
132 }
133 }
134 break;
135
136 case '{':
137 {
138 Gats::Dictionary *pDict = new Gats::Dictionary();
139 i++;
140 for(;;)
141 {
142 skipWs( i );
143 if( *i != '\"' )
144 throw Bu::ExceptionBase("Keys must be quoted strings.");
145 Bu::String sKey = token( i );
146 skipWs( i );
147 if( *i != ':' )
148 throw Bu::ExceptionBase("Keys and values must be "
149 "seperated with colons.");
150 i++;
151 Gats::Object *pObj = strToGats( i );
152 if( !pObj )
153 throw Bu::ExceptionBase("No value object found.");
154 pDict->insert( sKey, pObj );
155 skipWs( i );
156 switch( *i )
157 {
158 case ',':
159 i++;
160 break;
161
162 case '}':
163 i++;
164 return pDict;
165
166 default:
167 throw Bu::ExceptionBase("Invalid character found.");
168 }
169 }
170 }
171 break;
172
173 case '\"':
174 return new Gats::String( token( i ) );
175 break;
176
177 case '0':
178 case '1':
179 case '2':
180 case '3':
181 case '4':
182 case '5':
183 case '6':
184 case '7':
185 case '8':
186 case '9':
187 case '.':
188 case '+':
189 case '-':
190 {
191 Bu::String s = token( i );
192 int iSize = s.getSize();
193 if( s[iSize-1] == 'i' )
194 {
195 return new Gats::Integer(
196 strtoll( s.getStr(), NULL, 10 )
197 );
198 }
199 else if( s[iSize-1] == 'f' )
200 {
201 return new Gats::Float(
202 strtod( s.getStr(), NULL )
203 );
204 }
205 else
206 {
207 for( Bu::String::iterator i = s.begin(); i; i++ )
208 {
209 if( *i == '.' )
210 return new Gats::Float(
211 strtod( s.getStr(), NULL )
212 );
213 }
214 return new Gats::Integer(
215 strtoll( s.getStr(), NULL, 10 )
216 );
217 }
218 }
219 break;
220
221 default:
222 {
223 Bu::String s = token( i );
224 int iSize = s.getSize();
225 // Test for explicit types first
226 if( iSize > 2 )
227 {
228 if( (s[0] >= '0' && s[0] <= '9') || s[0] == '+' || s[0] == '-' )
229 {
230 }
231 else
232 {
233 Bu::String st = s.toLower();
234 if( st == "true" )
235 {
236 return new Gats::Boolean( true );
237 }
238 else if( st == "false" )
239 {
240 return new Gats::Boolean( false );
241 }
242 }
243 }
244 }
245 break;
246 }
247
248 return NULL;
249}
250
251Gats::Object *Gats::Object::strToGats( const Bu::String &sStr )
252{
253 Bu::String::const_iterator i = sStr.begin();
254
255 return strToGats( i );
256}
257
66Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) 258Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj )
67{ 259{
68 switch( obj.getType() ) 260 switch( obj.getType() )