1 | /*****************************************************************************/ |
|
|
2 | /* CrossFire, A roguelike realtime multiplayer game */ |
|
|
3 | /*****************************************************************************/ |
|
|
4 | |
|
|
5 | /* |
1 | /* |
6 | * This code is placed under the GNU General Public Licence (GPL) |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
7 | * |
3 | * |
8 | * Copyright (C) 2006 by Marc Lehmann <crossfire@schmorp.de> |
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
9 | * |
5 | * Copyright (©) 2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
6 | * |
10 | * This program is free software; you can redistribute it and/or modify |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
11 | * it under the terms of the GNU General Public License as published by |
8 | * the terms of the Affero GNU General Public License as published by the |
12 | * the Free Software Foundation; either version 2 of the License, or |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
13 | * (at your option) any later version. |
10 | * option) any later version. |
14 | * |
11 | * |
15 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
19 | * |
16 | * |
20 | * You should have received a copy of the GNU General Public License |
17 | * You should have received a copy of the Affero GNU General Public License |
21 | * along with this program; if not, write to the Free Software |
18 | * and the GNU General Public License along with this program. If not, see |
22 | * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | * <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
24 | #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff |
26 | #include "logger.h" |
25 | #include "logger.h" |
27 | #include "cfperl.h" |
26 | #include "cfperl.h" |
28 | #include "kw_hash.h" |
27 | #include "kw_hash.h" |
29 | |
28 | |
30 | object_freezer::object_freezer () |
29 | object_freezer::object_freezer () |
31 | : dynbuf (128 * 1024, 64 * 1024) |
30 | : dynbuf_text (128 * 1024, 64 * 1024) |
32 | { |
31 | { |
33 | av = newAV (); |
32 | av = newAV (); |
34 | } |
33 | } |
35 | |
34 | |
36 | object_freezer::~object_freezer () |
35 | object_freezer::~object_freezer () |
37 | { |
36 | { |
38 | SvREFCNT_dec (av); |
37 | SvREFCNT_dec (av); |
39 | } |
38 | } |
40 | |
39 | |
41 | void |
40 | void |
42 | object_freezer::put (attachable *ext) |
41 | object_freezer::put_ (attachable *ext) |
43 | { |
42 | { |
44 | ext->optimise (); |
43 | ext->optimise (); |
45 | |
44 | |
46 | if (ext->self) |
45 | if (ext->self) |
47 | { |
46 | { |
48 | int idx = AvFILLp ((AV *)av) + 1; |
47 | int idx = AvFILLp ((AV *)av) + 1; |
49 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
48 | av_store (av, idx, newRV_inc ((SV *)ext->self)); |
50 | |
49 | |
51 | add ((void *)"oid ", 4); |
50 | put (KW(oid), sint32(idx)); |
52 | add ((sint32)idx); |
|
|
53 | add ('\n'); |
|
|
54 | } |
51 | } |
55 | } |
52 | } |
56 | |
53 | |
57 | bool |
54 | bool |
58 | object_freezer::save (const char *path) |
55 | object_freezer::save (const char *path) |
… | |
… | |
61 | CALL_ARG_SV (newSVpv (path, 0)); |
58 | CALL_ARG_SV (newSVpv (path, 0)); |
62 | CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
59 | CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); |
63 | CALL_ARG_SV (newRV_inc ((SV *)av)); |
60 | CALL_ARG_SV (newRV_inc ((SV *)av)); |
64 | CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
61 | CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); |
65 | CALL_END; |
62 | CALL_END; |
|
|
63 | |
|
|
64 | return 1; |
66 | } |
65 | } |
67 | |
66 | |
68 | char * |
67 | char * |
69 | object_freezer::as_string () |
68 | object_freezer::as_string () |
70 | { |
69 | { |
… | |
… | |
80 | CALL_END; |
79 | CALL_END; |
81 | |
80 | |
82 | return res; |
81 | return res; |
83 | } |
82 | } |
84 | |
83 | |
85 | int |
84 | #if 0 |
|
|
85 | void |
86 | fprintf (object_freezer &freezer, const char *format, ...) |
86 | fprintf (object_freezer &freezer, const char *format, ...) |
87 | { |
87 | { |
88 | va_list ap; |
88 | va_list ap; |
89 | |
89 | |
90 | va_start (ap, format); |
90 | va_start (ap, format); |
… | |
… | |
95 | freezer.alloc (len); |
95 | freezer.alloc (len); |
96 | |
96 | |
97 | va_end (ap); |
97 | va_end (ap); |
98 | } |
98 | } |
99 | |
99 | |
100 | int |
100 | // XXX: function not returning an int |
|
|
101 | void |
101 | fputs (const char *s, object_freezer &freezer) |
102 | fputs (const char *s, object_freezer &freezer) |
102 | { |
103 | { |
103 | freezer.add (s); |
104 | freezer.add (s); |
104 | } |
105 | } |
|
|
106 | #endif |
105 | |
107 | |
106 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
108 | static const char thawer_eof[] = "\n\n\n\0\0\0"; |
107 | |
109 | |
108 | object_thawer::object_thawer (const char *path) |
110 | bool object_thawer::errors_are_fatal = true; |
109 | : name (strdup (path)) |
|
|
110 | { |
|
|
111 | static const char eof[] = "\n\n\n\0\0\0"; |
|
|
112 | |
|
|
113 | av = 0; |
|
|
114 | text = 0; |
|
|
115 | line = 0; |
|
|
116 | |
|
|
117 | if (path) |
|
|
118 | { |
|
|
119 | CALL_BEGIN (1); |
|
|
120 | CALL_ARG_SV (newSVpv (path, 0)); |
|
|
121 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
|
|
122 | |
|
|
123 | if (count == 2) |
|
|
124 | { |
|
|
125 | // second value - perl objects |
|
|
126 | { |
|
|
127 | SV *sv = POPs; |
|
|
128 | if (SvROK (sv)) |
|
|
129 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
|
|
130 | } |
|
|
131 | |
|
|
132 | // first value - text part, pad with 3 zeroes |
|
|
133 | { |
|
|
134 | SV *sv = POPs; |
|
|
135 | STRLEN len; |
|
|
136 | char *sv_ = SvPVbyte (sv, len); |
|
|
137 | text = newSV (len + sizeof (eof)); |
|
|
138 | SvCUR_set (text, len); |
|
|
139 | memcpy (SvPVX (text), sv_, len); |
|
|
140 | memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure |
|
|
141 | |
|
|
142 | line = SvPVX (text); |
|
|
143 | } |
|
|
144 | } |
|
|
145 | |
|
|
146 | CALL_END; |
|
|
147 | } |
|
|
148 | } |
|
|
149 | |
111 | |
150 | object_thawer::object_thawer (const char *data, AV *perlav) |
112 | object_thawer::object_thawer (const char *data, AV *perlav) |
151 | : name (strdup ("(memory stream")) |
113 | : name (strdup ("(memory stream)")) |
152 | { |
114 | { |
|
|
115 | init ("(memory stream)"); |
|
|
116 | |
153 | av = perlav; |
117 | av = perlav; |
154 | text = newSVpv (data, 0); |
118 | text = newSVpv (data, 0); sv_catpv (text, thawer_eof); |
155 | sv_catpv (text, thawer_eof); |
|
|
156 | line = SvPVbyte_nolen (text); |
119 | line = SvPVbyte_nolen (text); |
|
|
120 | next (); |
|
|
121 | } |
|
|
122 | |
|
|
123 | object_thawer::object_thawer (const_utf8_string path) |
|
|
124 | { |
|
|
125 | init_from_file (path); |
|
|
126 | } |
|
|
127 | |
|
|
128 | // convenience constructor |
|
|
129 | object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file) |
|
|
130 | { |
|
|
131 | init_from_file (format ("%s/%s", dir, file)); |
|
|
132 | } |
|
|
133 | |
|
|
134 | void |
|
|
135 | object_thawer::init (const_utf8_string path) |
|
|
136 | { |
|
|
137 | name = strdup (path); |
|
|
138 | av = 0; |
|
|
139 | text = 0; |
|
|
140 | line = 0; |
|
|
141 | linenum = 0; |
|
|
142 | |
|
|
143 | kw = KW_ERROR; |
|
|
144 | kw_str = 0; |
|
|
145 | value = 0; |
|
|
146 | } |
|
|
147 | |
|
|
148 | void |
|
|
149 | object_thawer::init_from_file (const_utf8_string path) |
|
|
150 | { |
|
|
151 | init (path); |
|
|
152 | |
|
|
153 | CALL_BEGIN (1); |
|
|
154 | CALL_ARG_SV (newSVpv (path, 0)); |
|
|
155 | CALL_CALL ("cf::object_thawer_load", G_ARRAY); |
|
|
156 | |
|
|
157 | if (count == 2) |
|
|
158 | { |
|
|
159 | // second value - perl objects |
|
|
160 | { |
|
|
161 | SV *sv = POPs; |
|
|
162 | if (SvROK (sv)) |
|
|
163 | av = (AV *)SvREFCNT_inc (SvRV (sv)); |
|
|
164 | } |
|
|
165 | |
|
|
166 | // first value - text part, pad with 3 zeroes |
|
|
167 | { |
|
|
168 | SV *sv = POPs; |
|
|
169 | STRLEN len; |
|
|
170 | char *sv_ = SvPVbyte (sv, len); |
|
|
171 | text = newSV (len + sizeof (thawer_eof)); |
|
|
172 | SvCUR_set (text, len + sizeof (thawer_eof)); |
|
|
173 | memcpy (SvPVX (text), sv_, len); |
|
|
174 | memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof)); |
|
|
175 | |
|
|
176 | line = SvPVX (text); |
|
|
177 | next (); |
|
|
178 | } |
|
|
179 | } |
|
|
180 | |
|
|
181 | CALL_END; |
157 | } |
182 | } |
158 | |
183 | |
159 | void |
184 | void |
160 | object_thawer::get (attachable *obj, int oid) |
185 | object_thawer::get (attachable *obj, int oid) |
161 | { |
186 | { |
… | |
… | |
164 | |
189 | |
165 | SV **svp = av_fetch ((AV *)av, oid, 0); |
190 | SV **svp = av_fetch ((AV *)av, oid, 0); |
166 | |
191 | |
167 | if (!svp || !SvROK (*svp)) |
192 | if (!svp || !SvROK (*svp)) |
168 | { |
193 | { |
169 | printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
194 | LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); |
170 | return; |
195 | return; |
171 | } |
196 | } |
172 | |
197 | |
173 | if (!SvROK (*svp)) |
198 | if (!SvROK (*svp)) |
174 | { |
199 | { |
175 | LOG (llevError, "deserialised perl object is not an RV"); |
200 | LOG (llevError, "deserialised perl object is not an RV\n"); |
176 | return; |
201 | return; |
177 | } |
202 | } |
178 | |
203 | |
179 | HV *hv = (HV *)SvRV (*svp); |
204 | HV *hv = (HV *)SvRV (*svp); |
180 | |
205 | |
181 | if (SvTYPE (hv) != SVt_PVHV) |
206 | if (SvTYPE (hv) != SVt_PVHV) |
182 | { |
207 | { |
183 | LOG (llevError, "deserialised perl object is not a PVHV"); |
208 | LOG (llevError, "deserialised perl object is not a PVHV\n"); |
184 | return; |
209 | return; |
185 | } |
210 | } |
186 | |
211 | |
187 | if (obj->self) |
212 | if (obj->self) |
188 | { |
213 | { |
189 | // the hard way(?) |
214 | // the hard way(?) |
190 | |
215 | |
191 | // the dirty blues: "just" swap the XPVHV's |
216 | CALL_BEGIN (2); |
192 | swap (SvANY (obj->self), SvANY (hv)); |
217 | CALL_ARG_SV (newRV_inc ((SV *)obj->self)); |
193 | |
218 | CALL_ARG_SV (newRV_inc ((SV *)hv)); |
194 | // do not swap magic, though |
219 | PUTBACK; |
195 | swap (SvMAGIC (obj->self), SvMAGIC (hv)); |
220 | call_method ("thawer_merge", G_DISCARD | G_EVAL); |
|
|
221 | SPAGAIN; |
|
|
222 | CALL_END; |
196 | } |
223 | } |
197 | else |
224 | else |
198 | { |
225 | { |
199 | // the easy way(?) |
226 | // the easy way(?) |
200 | |
227 | |
201 | obj->self = hv; |
228 | obj->self = hv; |
202 | SvRV_set (*svp, &PL_sv_undef); |
229 | SvRV_set (*svp, &PL_sv_undef); |
203 | |
230 | |
204 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
231 | sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); |
205 | |
|
|
206 | // borrow a refcount for the perl object |
|
|
207 | obj->flags |= attachable::F_BORROWED; |
|
|
208 | obj->refcnt_dec (); |
|
|
209 | } |
232 | } |
210 | |
233 | |
211 | obj->reattach (); |
234 | obj->reattach (); |
212 | } |
235 | } |
213 | |
236 | |
214 | object_thawer::~object_thawer () |
237 | object_thawer::~object_thawer () |
215 | { |
238 | { |
216 | if (text) SvREFCNT_dec (text); |
239 | if (text) SvREFCNT_dec (text); |
217 | if (av) SvREFCNT_dec (av); |
240 | if (av) SvREFCNT_dec (av); |
218 | |
241 | |
|
|
242 | resolve_delayed_derefs (false); |
|
|
243 | |
219 | free ((void *)name); |
244 | free ((void *)name); |
220 | } |
245 | } |
221 | |
246 | |
222 | char * |
247 | static void |
223 | fgets (char *s, int n, object_thawer &thawer) |
248 | error_out () |
224 | { |
249 | { |
225 | char *p = thawer.line; |
250 | if (object_thawer::errors_are_fatal) |
226 | char *q = s; |
251 | { |
|
|
252 | LOG (llevError, "(parse errors at this time are fatal, exiting)"); |
|
|
253 | exit (1); |
|
|
254 | } |
|
|
255 | } |
227 | |
256 | |
|
|
257 | void |
|
|
258 | object_thawer::parse_warn (const char *msg) const |
|
|
259 | { |
|
|
260 | LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", |
|
|
261 | this->name, linenum, |
|
|
262 | kw_str ? kw_str : "<null>", |
|
|
263 | value ? value : "<null>", |
|
|
264 | msg); |
|
|
265 | } |
|
|
266 | |
|
|
267 | bool |
|
|
268 | object_thawer::parse_error (const char *type, const char *name, bool skip) const |
|
|
269 | { |
|
|
270 | if (!type) type = "file section"; |
|
|
271 | if (!name) name = "generic"; |
|
|
272 | |
|
|
273 | switch (kw) |
|
|
274 | { |
|
|
275 | case KW_EOF: |
|
|
276 | LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", |
|
|
277 | this->name, linenum, type, name); |
|
|
278 | error_out (); |
|
|
279 | return false; |
|
|
280 | |
|
|
281 | case KW_ERROR: |
|
|
282 | LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", |
|
|
283 | this->name, linenum, |
|
|
284 | type, name, |
|
|
285 | kw_str ? kw_str : "<file load>"); |
|
|
286 | error_out (); |
|
|
287 | return false; |
|
|
288 | |
|
|
289 | default: |
|
|
290 | LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", |
|
|
291 | this->name, linenum, |
|
|
292 | kw_str ? kw_str : "<null>", |
|
|
293 | value ? value : "<null>", |
|
|
294 | type, name, |
|
|
295 | skip ? "skipping line" : "aborting load"); |
|
|
296 | error_out (); |
|
|
297 | return skip; |
|
|
298 | } |
|
|
299 | } |
|
|
300 | |
|
|
301 | void |
|
|
302 | object_thawer::next () |
|
|
303 | { |
228 | if (!p) |
304 | if (!line) |
|
|
305 | { |
|
|
306 | kw = KW_ERROR; |
229 | return 0; |
307 | return; |
230 | |
|
|
231 | while (--n) |
|
|
232 | { |
308 | } |
|
|
309 | |
|
|
310 | for (;;) |
|
|
311 | { |
|
|
312 | char *p = line; |
|
|
313 | |
|
|
314 | if (ecb_expect_false (*p <= ' ')) |
|
|
315 | { |
|
|
316 | // skip whitespace (only some files need this) |
|
|
317 | while (*p == ' ' || *p == '\t') |
|
|
318 | p++; |
|
|
319 | |
|
|
320 | line = p; |
|
|
321 | } |
|
|
322 | |
233 | if (!*p) |
323 | if (!*p) |
|
|
324 | { |
|
|
325 | kw = KW_EOF; |
234 | break; |
326 | break; |
235 | |
327 | } |
236 | *q++ = *p; |
|
|
237 | |
|
|
238 | if (*p++ == '\n') |
|
|
239 | break; |
|
|
240 | } |
|
|
241 | |
|
|
242 | *q = 0; |
|
|
243 | thawer.line = p; |
|
|
244 | |
|
|
245 | return s == q ? 0 : s; |
|
|
246 | } |
|
|
247 | |
|
|
248 | keyword |
|
|
249 | object_thawer::get_kv () |
|
|
250 | { |
|
|
251 | if (!line) |
|
|
252 | return KW_EOF; |
|
|
253 | |
|
|
254 | for (;;) |
|
|
255 | { |
|
|
256 | char *p = line; |
|
|
257 | |
|
|
258 | if (!*p) |
|
|
259 | return KW_EOF; |
|
|
260 | |
328 | |
261 | // parse keyword |
329 | // parse keyword |
262 | while (*p > ' ') |
330 | while (*p > ' ') |
263 | p++; |
331 | p++; |
264 | |
332 | |
265 | int klen = p - line; |
333 | int klen = p - line; |
266 | |
334 | |
|
|
335 | value_nn = ""; |
|
|
336 | value = 0; |
|
|
337 | |
267 | if (*p++ != '\n') |
338 | if (*p++ != '\n') |
268 | { |
339 | { |
269 | // parse value |
340 | // parse value |
270 | while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 |
341 | while (*(unsigned char *)p <= ' ' && *p != '\n') |
271 | ++p; |
342 | ++p; |
272 | |
343 | |
273 | last_value = p; |
344 | value_nn = value = p; |
274 | |
345 | |
275 | while (*p != '\n') |
346 | while (*p != '\n') |
276 | p++; |
347 | p++; |
277 | |
348 | |
278 | *p++ = 0; |
349 | *p++ = 0; |
279 | } |
350 | } |
280 | else |
|
|
281 | last_value = 0; |
|
|
282 | |
351 | |
|
|
352 | ++linenum; |
283 | line [klen] = 0; |
353 | line [klen] = 0; |
284 | keyword_idx *kw = kw_lex::match (line, klen); |
354 | keyword_idx *kw_idx = kw_lex::match (line, klen); |
285 | |
355 | |
286 | //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D |
356 | kw_str = line; |
287 | |
|
|
288 | last_keyword = line; |
|
|
289 | line = p; |
357 | line = p; |
290 | |
358 | |
291 | if (kw) |
359 | if (kw_idx) |
292 | return kw->index; |
360 | { |
|
|
361 | kw = kw_idx->index; |
|
|
362 | break; |
|
|
363 | } |
293 | else if (!*last_keyword || *last_keyword == '#') |
364 | else if (!*kw_str || *kw_str == '#') |
294 | ; // empty/comment line |
365 | ; // empty/comment line |
295 | else |
366 | else |
|
|
367 | { |
296 | return KW_ERROR; |
368 | kw = KW_ERROR; |
|
|
369 | break; |
|
|
370 | } |
|
|
371 | } |
|
|
372 | } |
|
|
373 | |
|
|
374 | bool |
|
|
375 | object_thawer::next_line () |
|
|
376 | { |
|
|
377 | if (!line) |
297 | } |
378 | { |
298 | } |
379 | kw = KW_ERROR; |
|
|
380 | return 0; |
|
|
381 | } |
299 | |
382 | |
|
|
383 | for (;;) |
|
|
384 | { |
|
|
385 | char *p = line; |
|
|
386 | |
|
|
387 | if (ecb_expect_false (*p <= ' ')) |
|
|
388 | { |
|
|
389 | // skip whitespace (only some files need this) |
|
|
390 | while (*p == ' ' || *p == '\t') |
|
|
391 | p++; |
|
|
392 | |
|
|
393 | line = p; |
|
|
394 | } |
|
|
395 | |
|
|
396 | if (!*p) |
|
|
397 | { |
|
|
398 | kw = KW_EOF; |
|
|
399 | return 0; |
|
|
400 | } |
|
|
401 | |
|
|
402 | kw = KW_value; |
|
|
403 | kw_str = p; |
|
|
404 | value_nn = p; |
|
|
405 | value = p; |
|
|
406 | |
|
|
407 | // parse till newline |
|
|
408 | while (*p > '\n') |
|
|
409 | p++; |
|
|
410 | |
|
|
411 | if (*p == '\n') |
|
|
412 | *p++ = 0; |
|
|
413 | |
|
|
414 | ++linenum; |
|
|
415 | |
|
|
416 | line = p; |
|
|
417 | |
|
|
418 | if (*kw_str && *kw_str != '#') |
|
|
419 | break; |
|
|
420 | |
|
|
421 | // empty/comment line -> skip it |
|
|
422 | } |
|
|
423 | |
|
|
424 | return 1; |
|
|
425 | } |
|
|
426 | |
300 | void |
427 | void |
301 | object_thawer::skip_kv (keyword kw) |
428 | object_thawer::skip () |
302 | { |
429 | { |
303 | shstr ml; |
430 | shstr ml; |
304 | |
431 | |
305 | switch (kw) |
432 | switch (kw) |
306 | { |
433 | { |
307 | case KW_msg: get_ml (KW_endmsg , ml); break; |
434 | case KW_msg: get_ml (KW_endmsg , ml); break; |
308 | case KW_lore: get_ml (KW_endlore , ml); break; |
435 | case KW_lore: get_ml (KW_endlore , ml); break; |
309 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
436 | case KW_maplore: get_ml (KW_endmaplore, ml); break; |
|
|
437 | default: break; |
310 | } |
438 | } |
|
|
439 | |
|
|
440 | next (); |
311 | } |
441 | } |
312 | |
442 | |
313 | void |
443 | void |
314 | object_thawer::get (shstr &sh) const |
444 | object_thawer::skip_block () |
315 | { |
445 | { |
316 | if (last_value) |
446 | // must not stop at KW_ERROR, as those signify custom keys |
317 | sh = last_value; |
447 | while (kw != KW_EOF) |
318 | else |
|
|
319 | { |
448 | { |
320 | sh = "<value missing>"; |
449 | keyword w = kw; |
321 | LOG (llevError, "keyword requires value: <%.320s>\n", line);//TODO: add filename |
450 | skip (); |
|
|
451 | |
|
|
452 | if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region)) |
|
|
453 | skip_block (); |
|
|
454 | else if (w == KW_end) |
|
|
455 | break; |
322 | } |
456 | } |
323 | } |
457 | } |
324 | |
458 | |
325 | void |
459 | void |
326 | object_thawer::get_ml (keyword kend, shstr &sh) |
460 | object_thawer::get_ml (keyword kend, shstr &sh) |
… | |
… | |
332 | kw [0] = '\n'; |
466 | kw [0] = '\n'; |
333 | memcpy (kw + 1, keyword_str [kend], klen); |
467 | memcpy (kw + 1, keyword_str [kend], klen); |
334 | kw [klen + 1] = '\n'; |
468 | kw [klen + 1] = '\n'; |
335 | kw [klen + 2] = 0; |
469 | kw [klen + 2] = 0; |
336 | |
470 | |
|
|
471 | ++linenum; |
|
|
472 | |
337 | // first test for completely empty msg... "endXXX\n" |
473 | // first test for completely empty msg... "endXXX\n" |
338 | if (!strncmp (line, kw + 1, klen + 1)) |
474 | if (!strncmp (line, kw + 1, klen + 1)) |
339 | { |
475 | { |
340 | sh = 0; |
476 | sh = 0; |
341 | |
477 | |
… | |
… | |
356 | } |
492 | } |
357 | |
493 | |
358 | *end = 0; |
494 | *end = 0; |
359 | sh = line; |
495 | sh = line; |
360 | |
496 | |
|
|
497 | // count line numbers |
|
|
498 | while (line < end) |
|
|
499 | linenum += *line++ == '\n'; |
|
|
500 | |
361 | line = end + keyword_len [kend] + 1; |
501 | line += keyword_len [kend]; |
362 | |
502 | |
363 | while (*line++ != '\n') |
503 | while (*line++ != '\n') |
364 | ; |
504 | ; |
|
|
505 | |
|
|
506 | ++linenum; |
365 | } |
507 | } |
366 | } |
508 | } |
367 | |
509 | |
368 | sint32 |
510 | sint32 |
369 | object_thawer::get_sint32 () const |
511 | object_thawer::get_sint32 () const |
370 | { |
512 | { |
371 | char *p = last_value; |
513 | const char *p = value_nn; |
372 | |
|
|
373 | if (!p) |
|
|
374 | return 0; |
|
|
375 | |
514 | |
376 | sint32 val = 0; |
515 | sint32 val = 0; |
377 | bool negate; |
516 | bool negate; |
378 | |
517 | |
379 | if (*p == '-') |
518 | if (*p == '-') |
… | |
… | |
392 | while (*p); |
531 | while (*p); |
393 | |
532 | |
394 | return negate ? -val : val; |
533 | return negate ? -val : val; |
395 | } |
534 | } |
396 | |
535 | |
397 | sint64 |
536 | void |
398 | object_thawer::get_sint64 () const |
537 | object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref) |
399 | { |
538 | { |
400 | return last_value ? atoll (last_value) : 0; |
539 | op->refcnt_inc (); |
|
|
540 | delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 }; |
|
|
541 | delrefs.push_back (r); |
|
|
542 | ptr = 0; |
401 | } |
543 | } |
402 | |
544 | |
403 | double |
545 | void |
404 | object_thawer::get_double () const |
546 | object_thawer::resolve_delayed_derefs (bool deref) |
405 | { |
547 | { |
406 | return last_value ? atof (last_value) : 0; |
548 | while (!delrefs.empty ()) |
407 | } |
549 | { |
|
|
550 | delayed_ref r = delrefs.back (); |
|
|
551 | delrefs.pop_back (); |
408 | |
552 | |
|
|
553 | if (deref) |
|
|
554 | *r.ptr = object::deref (r.ref); |
|
|
555 | |
|
|
556 | free ((void *)r.ref); |
|
|
557 | r.op->refcnt_dec (); |
|
|
558 | } |
|
|
559 | } |