]> Dogcows Code - chaz/yoink/blob - src/Moof/Deserializer.cc
improved new vorbisfile compatibility
[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 "Log.hh"
36 #include "Serializable.hh"
37
38
39 namespace Mf {
40
41
42 class Deserializer::Impl
43 {
44 public:
45 Impl(const std::string& filePath, bool comments = false,
46 bool check = false)
47 {
48 std::ifstream* input = new std::ifstream(filePath.c_str());
49 init(*input, true, comments, check);
50 }
51
52 Impl(std::istream& input, bool comments = false,
53 bool check = false)
54 {
55 init(input, false, comments, check);
56 }
57
58 ~Impl()
59 {
60 while (!parsed.empty())
61 {
62 delete parsed.front();
63 parsed.pop();
64 }
65
66 if (deleteWhenDone)
67 {
68 delete in;
69 }
70 yajl_free(hand);
71 }
72
73 void raise()
74 {
75 unsigned char* errorStr = yajl_get_error(hand, 0, 0, 0);
76 logError("parser error: %s", errorStr);
77 yajl_free_error(hand, errorStr);
78
79 throw Exception(Exception::PARSING_FAILED);
80 }
81
82
83 static int parsedNull(void* ctx)
84 {
85 ((Impl*)ctx)->parsed.push(new SerializableNull);
86 return 1;
87 }
88
89 static int parsedBoolean(void* ctx, int value)
90 {
91 ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Boolean>(value));
92 return 1;
93 }
94
95 static int parsedInteger(void* ctx, long value)
96 {
97 ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Integer>(value));
98 return 1;
99 }
100
101 static int parsedFloat(void* ctx, double value)
102 {
103 ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Float>(value));
104 return 1;
105 }
106
107 static int parsedString(void* ctx, const unsigned char* value,
108 unsigned length)
109 {
110 ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::String>(std::string((char*)value, length)));
111 return 1;
112 }
113
114 static int parsedBeginMap(void* ctx)
115 {
116 ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Map>);
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 // null means the end of a structure
130 ((Impl*)ctx)->parsed.push(0);
131 return 1;
132 }
133
134 static int parsedBeginArray(void* ctx)
135 {
136 ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Array>);
137 return 1;
138 }
139
140 static int parsedEndArray(void* ctx)
141 {
142 // null means the end of a structure
143 ((Impl*)ctx)->parsed.push(0);
144 return 1;
145 }
146
147
148 void parse()
149 {
150 unsigned char buffer[4096];
151
152 yajl_status stat;
153
154 while (parsed.empty() && in->good())
155 {
156 in->read((char*)buffer, sizeof(buffer));
157 unsigned readIn = in->gcount();
158
159 if (readIn > 0)
160 {
161 stat = yajl_parse(hand, buffer, readIn);
162 }
163 else
164 {
165 stat = yajl_parse_complete(hand);
166 }
167
168 if (stat != yajl_status_ok &&
169 stat != yajl_status_insufficient_data)
170 {
171 raise();
172 }
173 }
174 }
175
176
177 yajl_handle hand;
178
179 std::istream* in;
180 bool deleteWhenDone;
181
182 std::queue<Serializable*> parsed;
183
184 private:
185 void init(std::istream& input, bool deleteIn, bool comments, bool check)
186 {
187 // this has to be static because yajl actually does not copy it into its
188 // internal data structures but rather keeps a pointer to this
189 static const yajl_callbacks callbacks =
190 {
191 Impl::parsedNull,
192 Impl::parsedBoolean,
193 Impl::parsedInteger,
194 Impl::parsedFloat,
195 0,
196 Impl::parsedString,
197 Impl::parsedBeginMap,
198 Impl::parsedMapKey,
199 Impl::parsedEndMap,
200 Impl::parsedBeginArray,
201 Impl::parsedEndArray
202 };
203
204 in = &input;
205 deleteWhenDone = deleteIn;
206
207 yajl_parser_config config = {comments, check};
208 hand = yajl_alloc(&callbacks, &config, NULL, this);
209 }
210 };
211
212
213 Deserializer::Deserializer(const std::string& filePath, bool comments,
214 bool check) :
215 // pass through
216 impl_(new Deserializer::Impl(filePath, comments, check)) {}
217
218 Deserializer::Deserializer(std::istream& input, bool comments, bool check) :
219 // pass through
220 impl_(new Deserializer::Impl(input, comments, check)) {}
221
222
223 SerializableP Deserializer::deserialize()
224 {
225 Serializable* ptr = pullNext();
226 if (ptr)
227 {
228 ptr->deserialize(*this);
229 }
230 return SerializableP(ptr);
231 }
232
233
234 Serializable* Deserializer::pullNext()
235 {
236 impl_->parse();
237 if (!impl_->parsed.empty())
238 {
239 Serializable* ptr = impl_->parsed.front();
240 return ptr;
241 }
242 return 0;
243 }
244
245 void Deserializer::pop()
246 {
247 // pass through
248 impl_->parsed.pop();
249 }
250
251
252 } // namespace Mf
253
254 /** vim: set ts=4 sw=4 tw=80: *************************************************/
255
This page took 0.044788 seconds and 4 git commands to generate.