ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/freezethaw.C
Revision: 1.24
Committed: Wed Aug 1 20:07:06 2007 UTC (16 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.23: +11 -1 lines
Log Message:
parser warnings

File Contents

# Content
1 /*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3 *
4 * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 *
6 * Crossfire TRT is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * The authors can be reached via e-mail to <crossfire@schmorp.de>
20 */
21
22 #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
23 #include "logger.h"
24 #include "cfperl.h"
25 #include "kw_hash.h"
26
27 object_freezer::object_freezer ()
28 : dynbuf_text (128 * 1024, 64 * 1024)
29 {
30 av = newAV ();
31 }
32
33 object_freezer::~object_freezer ()
34 {
35 SvREFCNT_dec (av);
36 }
37
38 void
39 object_freezer::put (attachable *ext)
40 {
41 ext->optimise ();
42
43 if (ext->self)
44 {
45 int idx = AvFILLp ((AV *)av) + 1;
46 av_store (av, idx, newRV_inc ((SV *)ext->self));
47
48 add ((void *)"oid ", 4);
49 add ((sint32)idx);
50 add ('\n');
51 }
52 }
53
54 bool
55 object_freezer::save (const char *path)
56 {
57 CALL_BEGIN (3);
58 CALL_ARG_SV (newSVpv (path, 0));
59 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
60 CALL_ARG_SV (newRV_inc ((SV *)av));
61 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
62 CALL_END;
63
64 return 1;
65 }
66
67 char *
68 object_freezer::as_string ()
69 {
70 CALL_BEGIN (2);
71 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
72 CALL_ARG_SV (newRV_inc ((SV *)av));
73 CALL_CALL ("cf::object_freezer_as_string", G_SCALAR);
74
75 char *res = count > 0
76 ? strdup (SvPVX (POPs))
77 : strdup ("[fatal error]");
78
79 CALL_END;
80
81 return res;
82 }
83
84 #if 0
85 void
86 fprintf (object_freezer &freezer, const char *format, ...)
87 {
88 va_list ap;
89
90 va_start (ap, format);
91
92 int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap);
93
94 if (len >= 0)
95 freezer.alloc (len);
96
97 va_end (ap);
98 }
99
100 // XXX: function not returning an int
101 void
102 fputs (const char *s, object_freezer &freezer)
103 {
104 freezer.add (s);
105 }
106 #endif
107
108 static const char thawer_eof[] = "\n\n\n\0\0\0";
109
110 object_thawer::object_thawer (const char *path)
111 : name (strdup (path))
112 {
113 static const char eof[] = "\n\n\n\0\0\0";
114
115 av = 0;
116 text = 0;
117 line = 0;
118 linenum = 0;
119
120 kw = KW_ERROR;
121 kw_str = 0;
122 value = 0;
123
124 if (path)
125 {
126 CALL_BEGIN (1);
127 CALL_ARG_SV (newSVpv (path, 0));
128 CALL_CALL ("cf::object_thawer_load", G_ARRAY);
129
130 if (count == 2)
131 {
132 // second value - perl objects
133 {
134 SV *sv = POPs;
135 if (SvROK (sv))
136 av = (AV *)SvREFCNT_inc (SvRV (sv));
137 }
138
139 // first value - text part, pad with 3 zeroes
140 {
141 SV *sv = POPs;
142 STRLEN len;
143 char *sv_ = SvPVbyte (sv, len);
144 text = newSV (len + sizeof (eof));
145 SvCUR_set (text, len);
146 memcpy (SvPVX (text), sv_, len);
147 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure
148
149 line = SvPVX (text);
150 }
151 }
152
153 CALL_END;
154 }
155 }
156
157 object_thawer::object_thawer (const char *data, AV *perlav)
158 : name (strdup ("(memory stream)"))
159 {
160 av = perlav;
161 text = newSVpv (data, 0);
162 sv_catpv (text, thawer_eof);
163 line = SvPVbyte_nolen (text);
164 }
165
166 void
167 object_thawer::get (attachable *obj, int oid)
168 {
169 if (!av || oid < 0) // this is actually an error of sorts
170 return;
171
172 SV **svp = av_fetch ((AV *)av, oid, 0);
173
174 if (!svp || !SvROK (*svp))
175 {
176 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
177 return;
178 }
179
180 if (!SvROK (*svp))
181 {
182 LOG (llevError, "deserialised perl object is not an RV\n");
183 return;
184 }
185
186 HV *hv = (HV *)SvRV (*svp);
187
188 if (SvTYPE (hv) != SVt_PVHV)
189 {
190 LOG (llevError, "deserialised perl object is not a PVHV\n");
191 return;
192 }
193
194 if (obj->self)
195 {
196 // the hard way(?)
197
198 CALL_BEGIN (2);
199 CALL_ARG_SV (newRV_inc ((SV *)obj->self));
200 CALL_ARG_SV (newRV_inc ((SV *)hv));
201 PUTBACK;
202 call_method ("thawer_merge", G_DISCARD | G_EVAL);
203 SPAGAIN;
204 CALL_END;
205 }
206 else
207 {
208 // the easy way(?)
209
210 obj->self = hv;
211 SvRV_set (*svp, &PL_sv_undef);
212
213 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
214 }
215
216 obj->reattach ();
217 }
218
219 object_thawer::~object_thawer ()
220 {
221 if (text) SvREFCNT_dec (text);
222 if (av) SvREFCNT_dec (av);
223
224 free ((void *)name);
225 }
226
227 //TODO: remove
228 char *
229 fgets (char *s, int n, object_thawer &thawer)
230 {
231 char *p = thawer.line;
232 char *q = s;
233
234 if (!p)
235 return 0;
236
237 while (--n)
238 {
239 if (!*p)
240 break;
241
242 *q++ = *p;
243
244 if (*p++ == '\n')
245 {
246 ++thawer.linenum;
247 break;
248 }
249 }
250
251 *q = 0;
252 thawer.line = p;
253
254 return s == q ? 0 : s;
255 }
256
257 void
258 object_thawer::parse_warn (const char *msg)
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)
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 return false;
279
280 case KW_ERROR:
281 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
282 this->name, linenum,
283 type, name,
284 kw_str ? kw_str : "<file load>");
285 return false;
286
287 default:
288 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
289 this->name, linenum,
290 kw_str ? kw_str : "<null>",
291 value ? value : "<null>",
292 type, name,
293 skip ? "skipping line" : "aborting load");
294 return skip;
295 }
296 }
297
298 void
299 object_thawer::next ()
300 {
301 if (!line)
302 {
303 kw = KW_ERROR;
304 return;
305 }
306
307 for (;;)
308 {
309 char *p = line;
310
311 if (*p <= ' ')
312 {
313 // skip whitespace (only some files need this)
314 while (*p == ' ' || *p == '\t')
315 p++;
316
317 line = p;
318 }
319
320 if (!*p)
321 {
322 kw = KW_EOF;
323 break;
324 }
325
326 // parse keyword
327 while (*p > ' ')
328 p++;
329
330 int klen = p - line;
331
332 value_nn = "";
333 value = 0;
334
335 if (*p++ != '\n')
336 {
337 // parse value
338 while (*(unsigned char *)p <= ' ' && *p != '\n')
339 ++p;
340
341 value_nn = value = p;
342
343 while (*p != '\n')
344 p++;
345
346 *p++ = 0;
347 }
348
349 ++linenum;
350 line [klen] = 0;
351 keyword_idx *kw_idx = kw_lex::match (line, klen);
352
353 kw_str = line;
354 line = p;
355
356 if (kw_idx)
357 {
358 kw = kw_idx->index;
359 break;
360 }
361 else if (!*kw_str || *kw_str == '#')
362 ; // empty/comment line
363 else
364 {
365 kw = KW_ERROR;
366 break;
367 }
368 }
369 }
370
371 void
372 object_thawer::skip ()
373 {
374 shstr ml;
375
376 switch (kw)
377 {
378 case KW_msg: get_ml (KW_endmsg , ml); break;
379 case KW_lore: get_ml (KW_endlore , ml); break;
380 case KW_maplore: get_ml (KW_endmaplore, ml); break;
381 default: break;
382 }
383
384 next ();
385 }
386
387 void
388 object_thawer::get_ml (keyword kend, shstr &sh)
389 {
390 char kw[128];
391
392 int klen = keyword_len [kend];
393
394 kw [0] = '\n';
395 memcpy (kw + 1, keyword_str [kend], klen);
396 kw [klen + 1] = '\n';
397 kw [klen + 2] = 0;
398
399 ++linenum;
400
401 // first test for completely empty msg... "endXXX\n"
402 if (!strncmp (line, kw + 1, klen + 1))
403 {
404 sh = 0;
405
406 line += klen + 1;
407
408 return;
409 }
410 else
411 {
412 // multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL)
413
414 char *end = strstr (line, kw);
415
416 if (!end)
417 {
418 sh = 0;
419 return;
420 }
421
422 *end = 0;
423 sh = line;
424
425 // count line numbers
426 while (line < end)
427 linenum += *line++ == '\n';
428
429 line += keyword_len [kend];
430
431 while (*line++ != '\n')
432 ;
433
434 ++linenum;
435 }
436 }
437
438 sint32
439 object_thawer::get_sint32 () const
440 {
441 char *p = value_nn;
442
443 sint32 val = 0;
444 bool negate;
445
446 if (*p == '-')
447 {
448 negate = true;
449 ++p;
450 }
451 else
452 negate = false;
453
454 do
455 {
456 val *= 10;
457 val += *p++ - '0';
458 }
459 while (*p);
460
461 return negate ? -val : val;
462 }
463