]> Dogcows Code - chaz/yoink/blob - src/Moof/Deserializer.cc
8bf230fe68627054e225fd54aa8b48ad9f15a1aa
[chaz/yoink] / src / Moof / Deserializer.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <fstream>
30 #include <queue>
31
32 #include <yajl/yajl_parse.h>
33
34 #include "Deserializer.hh"
35 #include "Serializable.hh"
36
37
38 namespace Mf {
39
40
41 class Deserializer::DeserializerImpl
42 {
43 public:
44 DeserializerImpl(const std::string& filePath, bool comments = false,
45 bool check = false)
46 {
47 std::ifstream* input = new std::ifstream(filePath.c_str());
48 init(*input, true, comments, check);
49 }
50
51 DeserializerImpl(std::istream& input, bool comments = false,
52 bool check = false)
53 {
54 init(input, false, comments, check);
55 }
56
57 ~DeserializerImpl()
58 {
59 while (!parsed.empty())
60 {
61 delete parsed.front();
62 parsed.pop();
63 }
64
65 if (deleteWhenDone)
66 {
67 delete in;
68 }
69 yajl_free(hand);
70 }
71
72 void throwError()
73 {
74 unsigned char* errorStr = yajl_get_error(hand, 0, 0, 0);
75 Deserializer::Exception exception((char*)errorStr);
76 yajl_free_error(hand, errorStr);
77 throw exception;
78 }
79
80
81 static int parsedNull(void* ctx)
82 {
83 ((DeserializerImpl*)ctx)->parsed.push(new SerializableNull);
84 return 1;
85 }
86
87 static int parsedBoolean(void* ctx, int value)
88 {
89 ((DeserializerImpl*)ctx)->parsed.push(new SerializableBoolean(value));
90 return 1;
91 }
92
93 static int parsedInteger(void* ctx, long value)
94 {
95 ((DeserializerImpl*)ctx)->parsed.push(new SerializableInteger(value));
96 return 1;
97 }
98
99 static int parsedFloat(void* ctx, double value)
100 {
101 ((DeserializerImpl*)ctx)->parsed.push(new SerializableReal(value));
102 return 1;
103 }
104
105 static int parsedString(void* ctx, const unsigned char* value,
106 unsigned length)
107 {
108 SerializableString* parsed =
109 new SerializableString(std::string((char*)value, length));
110 ((DeserializerImpl*)ctx)->parsed.push(parsed);
111 return 1;
112 }
113
114 static int parsedBeginMap(void* ctx)
115 {
116 ((DeserializerImpl*)ctx)->parsed.push(new SerializableMap);
117 return 1;
118 }
119
120 static int parsedMapKey(void* ctx, const unsigned char* value,
121 unsigned length)
122 {
123 // same thing as a string
124 return parsedString(ctx, value, length);
125 }
126
127 static int parsedEndMap(void* ctx)
128 {
129 ((DeserializerImpl*)ctx)->parsed.push(0);
130 return 1;
131 }
132
133 static int parsedBeginArray(void* ctx)
134 {
135 ((DeserializerImpl*)ctx)->parsed.push(new SerializableArray);
136 return 1;
137 }
138
139 static int parsedEndArray(void* ctx)
140 {
141 ((DeserializerImpl*)ctx)->parsed.push(0);
142 return 1;
143 }
144
145
146 void parse()
147 {
148 unsigned char buffer[4096];
149
150 yajl_status stat;
151
152 while (parsed.empty() && in->good())
153 {
154 in->read((char*)buffer, sizeof(buffer));
155 unsigned readIn = in->gcount();
156
157 if (readIn > 0)
158 {
159 stat = yajl_parse(hand, buffer, readIn);
160 }
161 else
162 {
163 stat = yajl_parse_complete(hand);
164 }
165
166 if (stat != yajl_status_ok &&
167 stat != yajl_status_insufficient_data)
168 {
169 throwError();
170 }
171 }
172 }
173
174
175 yajl_handle hand;
176
177 std::istream* in;
178 bool deleteWhenDone;
179
180 std::queue<Serializable*> parsed;
181
182 private:
183 void init(std::istream& input, bool deleteIn, bool comments, bool check)
184 {
185 // this has to be static because yajl actually does not copy it into its
186 // internal data structures but rather keeps a pointer to this
187 static const yajl_callbacks callbacks =
188 {
189 DeserializerImpl::parsedNull,
190 DeserializerImpl::parsedBoolean,
191 DeserializerImpl::parsedInteger,
192 DeserializerImpl::parsedFloat,
193 0,
194 DeserializerImpl::parsedString,
195 DeserializerImpl::parsedBeginMap,
196 DeserializerImpl::parsedMapKey,
197 DeserializerImpl::parsedEndMap,
198 DeserializerImpl::parsedBeginArray,
199 DeserializerImpl::parsedEndArray
200 };
201
202 in = &input;
203 deleteWhenDone = deleteIn;
204
205 yajl_parser_config config = {comments, check};
206 hand = yajl_alloc(&callbacks, &config, NULL, this);
207 }
208 };
209
210
211 Deserializer::Deserializer(const std::string& filePath, bool comments,
212 bool check) :
213 // pass through
214 impl_(new Deserializer::DeserializerImpl(filePath, comments, check)) {}
215
216 Deserializer::Deserializer(std::istream& input, bool comments, bool check) :
217 // pass through
218 impl_(new Deserializer::DeserializerImpl(input, comments, check)) {}
219
220
221 SerializablePtr Deserializer::deserialize()
222 {
223 Serializable* ptr = pullNext();
224 if (ptr)
225 {
226 ptr->deserialize(*this);
227 }
228 return SerializablePtr(ptr);
229 }
230
231
232 Serializable* Deserializer::pullNext()
233 {
234 impl_->parse();
235 if (!impl_->parsed.empty())
236 {
237 Serializable* ptr = impl_->parsed.front();
238 return ptr;
239 }
240 return 0;
241 }
242
243 void Deserializer::pop()
244 {
245 // pass through
246 impl_->parsed.pop();
247 }
248
249
250 } // namespace Mf
251
252 /** vim: set ts=4 sw=4 tw=80: *************************************************/
253
This page took 0.041297 seconds and 3 git commands to generate.