]> Dogcows Code - chaz/yoink/blob - src/deserializer.cc
datarootdir autotools fix
[chaz/yoink] / src / 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 "serializable.hh"
35 #include "deserializer.hh"
36
37
38 namespace dc {
39
40
41 class deserializer::deserializer_impl
42 {
43 public:
44 deserializer_impl(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 deserializer_impl(std::istream& input, bool comments = false,
52 bool check = false)
53 {
54 init(input, false, comments, check);
55 }
56
57 ~deserializer_impl()
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* errorMsg = yajl_get_error(hand, 0, 0, 0);
75 deserializer::exception problem((char*)errorMsg);
76 yajl_free_error(hand, errorMsg);
77 throw problem;
78 }
79
80
81 static int parsedNull(void* ctx)
82 {
83 ((deserializer_impl*)ctx)->parsed.push(new null);
84 return 1;
85 }
86
87 static int parsedBoolean(void* ctx, int value)
88 {
89 ((deserializer_impl*)ctx)->parsed.push(new wrapped_boolean(value));
90 return 1;
91 }
92
93 static int parsedInteger(void* ctx, long value)
94 {
95 ((deserializer_impl*)ctx)->parsed.push(new wrapped_integer(value));
96 return 1;
97 }
98
99 static int parsedFloat(void* ctx, double value)
100 {
101 ((deserializer_impl*)ctx)->parsed.push(new wrapped_real(value));
102 return 1;
103 }
104
105 static int parsedString(void* ctx, const unsigned char* value,
106 unsigned length)
107 {
108 wrapped_string* parsed = new wrapped_string(std::string((char*)value,
109 length));
110 ((deserializer_impl*)ctx)->parsed.push(parsed);
111 return 1;
112 }
113
114 static int parsedBeginMap(void* ctx)
115 {
116 ((deserializer_impl*)ctx)->parsed.push(new wrapped_dictionary);
117 return 1;
118 }
119
120 static int parsedMapKey(void* ctx, const unsigned char* value,
121 unsigned length)
122 {
123 return parsedString(ctx, value, length);
124 }
125
126 static int parsedEndMap(void* ctx)
127 {
128 ((deserializer_impl*)ctx)->parsed.push(0);
129 return 1;
130 }
131
132 static int parsedBeginArray(void* ctx)
133 {
134 ((deserializer_impl*)ctx)->parsed.push(new wrapped_array);
135 return 1;
136 }
137
138 static int parsedEndArray(void* ctx)
139 {
140 ((deserializer_impl*)ctx)->parsed.push(0);
141 return 1;
142 }
143
144
145 void parse()
146 {
147 unsigned char buffer[4096];
148
149 yajl_status stat;
150
151 while (parsed.empty() && in->good())
152 {
153 in->read((char*)buffer, sizeof(buffer));
154 unsigned readIn = in->gcount();
155
156 if (readIn > 0)
157 {
158 stat = yajl_parse(hand, buffer, readIn);
159 }
160 else
161 {
162 stat = yajl_parse_complete(hand);
163 }
164
165 if (stat != yajl_status_ok &&
166 stat != yajl_status_insufficient_data)
167 {
168 throwError();
169 }
170 }
171 }
172
173
174 yajl_handle hand;
175
176 std::istream* in;
177 bool deleteWhenDone;
178
179 std::queue<serializable*> parsed;
180
181 private:
182 void init(std::istream& input, bool deleteIn, bool comments, bool check)
183 {
184 const yajl_callbacks callbacks =
185 {
186 deserializer_impl::parsedNull,
187 deserializer_impl::parsedBoolean,
188 deserializer_impl::parsedInteger,
189 deserializer_impl::parsedFloat,
190 0,
191 deserializer_impl::parsedString,
192 deserializer_impl::parsedBeginMap,
193 deserializer_impl::parsedMapKey,
194 deserializer_impl::parsedEndMap,
195 deserializer_impl::parsedBeginArray,
196 deserializer_impl::parsedEndArray
197 };
198
199 in = &input;
200 deleteWhenDone = deleteIn;
201
202 yajl_parser_config config = {comments, check};
203 hand = yajl_alloc(&callbacks, &config, NULL, this);
204 }
205 };
206
207
208 deserializer::deserializer(const std::string& filePath, bool comments,
209 bool check) :
210 // pass through
211 impl(new deserializer::deserializer_impl(filePath, comments, check)) {}
212
213 deserializer::deserializer(std::istream& input, bool comments, bool check) :
214 // pass through
215 impl(new deserializer::deserializer_impl(input, comments, check)) {}
216
217
218 serializable_ptr deserializer::deserialize()
219 {
220 serializable* ptr = pullNext();
221 if (ptr)
222 {
223 ptr->deserialize(*this);
224 }
225 return serializable_ptr(ptr);
226 }
227
228
229 serializable* deserializer::pullNext()
230 {
231 impl->parse();
232 if (!impl->parsed.empty())
233 {
234 serializable* ptr = impl->parsed.front();
235 return ptr;
236 }
237 return 0;
238 }
239
240 void deserializer::pop()
241 {
242 // pass through
243 impl->parsed.pop();
244 }
245
246
247 } // namespace dc
248
249 /** vim: set ts=4 sw=4 tw=80: *************************************************/
250
This page took 0.045162 seconds and 4 git commands to generate.