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

# User Rev Content
1 root 1.1 /*
2 root 1.21 * 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 root 1.22 * 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 root 1.21 *
11 root 1.22 * 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 root 1.21 *
16 root 1.22 * 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 root 1.21 *
19     * The authors can be reached via e-mail to <crossfire@schmorp.de>
20 root 1.1 */
21    
22 root 1.4 #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
23 root 1.1 #include "logger.h"
24     #include "cfperl.h"
25     #include "kw_hash.h"
26    
27     object_freezer::object_freezer ()
28 root 1.17 : dynbuf_text (128 * 1024, 64 * 1024)
29 root 1.1 {
30     av = newAV ();
31     }
32    
33     object_freezer::~object_freezer ()
34     {
35     SvREFCNT_dec (av);
36     }
37    
38 root 1.4 void
39     object_freezer::put (attachable *ext)
40 root 1.1 {
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 root 1.4 bool
55     object_freezer::save (const char *path)
56 root 1.1 {
57     CALL_BEGIN (3);
58 root 1.4 CALL_ARG_SV (newSVpv (path, 0));
59 root 1.1 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 root 1.13
64     return 1;
65 root 1.1 }
66    
67 root 1.4 char *
68     object_freezer::as_string ()
69 root 1.1 {
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 root 1.3 ? strdup (SvPVX (POPs))
77 root 1.1 : strdup ("[fatal error]");
78    
79     CALL_END;
80    
81     return res;
82     }
83    
84 root 1.20 #if 0
85 root 1.13 void
86 root 1.4 fprintf (object_freezer &freezer, const char *format, ...)
87 root 1.1 {
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 pippijn 1.12 // XXX: function not returning an int
101 root 1.13 void
102 root 1.4 fputs (const char *s, object_freezer &freezer)
103 root 1.1 {
104     freezer.add (s);
105     }
106 root 1.20 #endif
107 root 1.1
108     static const char thawer_eof[] = "\n\n\n\0\0\0";
109    
110 root 1.4 object_thawer::object_thawer (const char *path)
111     : name (strdup (path))
112 root 1.1 {
113     static const char eof[] = "\n\n\n\0\0\0";
114    
115 root 1.10 av = 0;
116     text = 0;
117     line = 0;
118 root 1.8 linenum = 0;
119 root 1.1
120 root 1.10 kw = KW_ERROR;
121     kw_str = 0;
122     value = 0;
123    
124 root 1.4 if (path)
125 root 1.1 {
126     CALL_BEGIN (1);
127 root 1.4 CALL_ARG_SV (newSVpv (path, 0));
128 root 1.1 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 root 1.23 : name (strdup ("(memory stream)"))
159 root 1.1 {
160     av = perlav;
161     text = newSVpv (data, 0);
162     sv_catpv (text, thawer_eof);
163     line = SvPVbyte_nolen (text);
164     }
165    
166 root 1.4 void
167     object_thawer::get (attachable *obj, int oid)
168 root 1.1 {
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 root 1.15 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
177 root 1.1 return;
178     }
179    
180     if (!SvROK (*svp))
181     {
182 root 1.15 LOG (llevError, "deserialised perl object is not an RV\n");
183 root 1.1 return;
184     }
185    
186     HV *hv = (HV *)SvRV (*svp);
187    
188     if (SvTYPE (hv) != SVt_PVHV)
189     {
190 root 1.15 LOG (llevError, "deserialised perl object is not a PVHV\n");
191 root 1.1 return;
192     }
193    
194     if (obj->self)
195     {
196     // the hard way(?)
197    
198 root 1.6 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 root 1.1 }
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 root 1.4
224     free ((void *)name);
225 root 1.1 }
226    
227 root 1.8 //TODO: remove
228 root 1.4 char *
229     fgets (char *s, int n, object_thawer &thawer)
230 root 1.1 {
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 root 1.8 {
246     ++thawer.linenum;
247     break;
248     }
249 root 1.1 }
250    
251     *q = 0;
252     thawer.line = p;
253    
254     return s == q ? 0 : s;
255     }
256    
257 root 1.24 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 root 1.8 bool
268 root 1.10 object_thawer::parse_error (const char *type, const char *name, bool skip)
269 root 1.8 {
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 root 1.10 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
282 root 1.8 this->name, linenum,
283 root 1.10 type, name,
284     kw_str ? kw_str : "<file load>");
285 root 1.8 return false;
286    
287     default:
288 root 1.9 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
289 root 1.8 this->name, linenum,
290 root 1.10 kw_str ? kw_str : "<null>",
291 root 1.24 value ? value : "<null>",
292 root 1.8 type, name,
293     skip ? "skipping line" : "aborting load");
294     return skip;
295     }
296     }
297    
298 root 1.10 void
299 root 1.11 object_thawer::next ()
300 root 1.1 {
301     if (!line)
302 root 1.10 {
303     kw = KW_ERROR;
304     return;
305     }
306 root 1.1
307     for (;;)
308     {
309     char *p = line;
310    
311 root 1.16 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 root 1.1 if (!*p)
321 root 1.10 {
322     kw = KW_EOF;
323     break;
324     }
325 root 1.1
326     // parse keyword
327     while (*p > ' ')
328     p++;
329    
330     int klen = p - line;
331    
332 root 1.19 value_nn = "";
333 root 1.16 value = 0;
334    
335 root 1.1 if (*p++ != '\n')
336     {
337     // parse value
338 root 1.16 while (*(unsigned char *)p <= ' ' && *p != '\n')
339 root 1.1 ++p;
340    
341 root 1.19 value_nn = value = p;
342 root 1.1
343     while (*p != '\n')
344     p++;
345    
346     *p++ = 0;
347     }
348    
349 root 1.8 ++linenum;
350 root 1.1 line [klen] = 0;
351 root 1.10 keyword_idx *kw_idx = kw_lex::match (line, klen);
352 root 1.1
353 root 1.10 kw_str = line;
354 root 1.1 line = p;
355    
356 root 1.10 if (kw_idx)
357     {
358     kw = kw_idx->index;
359     break;
360     }
361     else if (!*kw_str || *kw_str == '#')
362 root 1.1 ; // empty/comment line
363     else
364 root 1.10 {
365     kw = KW_ERROR;
366     break;
367     }
368 root 1.1 }
369     }
370    
371 root 1.4 void
372 root 1.11 object_thawer::skip ()
373 root 1.1 {
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 pippijn 1.14 default: break;
382 root 1.1 }
383 root 1.10
384 root 1.11 next ();
385 root 1.1 }
386    
387 root 1.4 void
388     object_thawer::get_ml (keyword kend, shstr &sh)
389 root 1.1 {
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 root 1.8 ++linenum;
400    
401 root 1.1 // 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 root 1.8 // count line numbers
426     while (line < end)
427     linenum += *line++ == '\n';
428    
429     line += keyword_len [kend];
430 root 1.1
431     while (*line++ != '\n')
432     ;
433 root 1.8
434     ++linenum;
435 root 1.1 }
436     }
437    
438 root 1.4 sint32
439     object_thawer::get_sint32 () const
440 root 1.1 {
441 root 1.19 char *p = value_nn;
442 root 1.1
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