ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/freezethaw.C
(Generate patch)

Comparing deliantra/server/server/freezethaw.C (file contents):
Revision 1.2 by elmex, Sat Dec 30 14:34:10 2006 UTC vs.
Revision 1.15 by root, Thu Apr 5 13:44:31 2007 UTC

20 * You should have received a copy of the GNU General Public License 20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software 21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 * Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */ 23 */
24 24
25#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
25#include "logger.h" 26#include "logger.h"
26#include "cfperl.h" 27#include "cfperl.h"
27#include "kw_hash.h" 28#include "kw_hash.h"
28 29
29object_freezer::object_freezer () 30object_freezer::object_freezer ()
35object_freezer::~object_freezer () 36object_freezer::~object_freezer ()
36{ 37{
37 SvREFCNT_dec (av); 38 SvREFCNT_dec (av);
38} 39}
39 40
41void
40void object_freezer::put (attachable *ext) 42object_freezer::put (attachable *ext)
41{ 43{
42 ext->optimise (); 44 ext->optimise ();
43 45
44 if (ext->self) 46 if (ext->self)
45 { 47 {
50 add ((sint32)idx); 52 add ((sint32)idx);
51 add ('\n'); 53 add ('\n');
52 } 54 }
53} 55}
54 56
57bool
55bool object_freezer::save (const char *filename) 58object_freezer::save (const char *path)
56{ 59{
57 CALL_BEGIN (3); 60 CALL_BEGIN (3);
58 CALL_ARG_SV (newSVpv (filename, 0)); 61 CALL_ARG_SV (newSVpv (path, 0));
59 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 62 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
60 CALL_ARG_SV (newRV_inc ((SV *)av)); 63 CALL_ARG_SV (newRV_inc ((SV *)av));
61 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD); 64 CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
62 CALL_END; 65 CALL_END;
63}
64 66
67 return 1;
68}
69
70char *
65char *object_freezer::as_string () 71object_freezer::as_string ()
66{ 72{
67 CALL_BEGIN (2); 73 CALL_BEGIN (2);
68 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ()))); 74 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
69 CALL_ARG_SV (newRV_inc ((SV *)av)); 75 CALL_ARG_SV (newRV_inc ((SV *)av));
70 CALL_CALL ("cf::object_freezer_as_string", G_SCALAR); 76 CALL_CALL ("cf::object_freezer_as_string", G_SCALAR);
71 77
72 STRLEN len;
73
74 char *res = count > 0 78 char *res = count > 0
75 ? strdup (SvPVx (POPs, len)) 79 ? strdup (SvPVX (POPs))
76 : strdup ("[fatal error]"); 80 : strdup ("[fatal error]");
77 81
78 CALL_END; 82 CALL_END;
79 83
80 return res; 84 return res;
81} 85}
82 86
87void
83int fprintf (object_freezer &freezer, const char *format, ...) 88fprintf (object_freezer &freezer, const char *format, ...)
84{ 89{
85 va_list ap; 90 va_list ap;
86 91
87 va_start (ap, format); 92 va_start (ap, format);
88 93
92 freezer.alloc (len); 97 freezer.alloc (len);
93 98
94 va_end (ap); 99 va_end (ap);
95} 100}
96 101
102// XXX: function not returning an int
103void
97int fputs (const char *s, object_freezer &freezer) 104fputs (const char *s, object_freezer &freezer)
98{ 105{
99 freezer.add (s); 106 freezer.add (s);
100} 107}
101 108
102static const char thawer_eof[] = "\n\n\n\0\0\0"; 109static const char thawer_eof[] = "\n\n\n\0\0\0";
103 110
104object_thawer::object_thawer (const char *filename) 111object_thawer::object_thawer (const char *path)
112: name (strdup (path))
105{ 113{
106 static const char eof[] = "\n\n\n\0\0\0"; 114 static const char eof[] = "\n\n\n\0\0\0";
107 115
108 av = 0; 116 av = 0;
109 text = 0; 117 text = 0;
110 line = 0; 118 line = 0;
119 linenum = 0;
111 120
112 if (filename) 121 kw = KW_ERROR;
122 kw_str = 0;
123 value = 0;
124
125 if (path)
113 { 126 {
114 CALL_BEGIN (1); 127 CALL_BEGIN (1);
115 CALL_ARG_SV (newSVpv (filename, 0)); 128 CALL_ARG_SV (newSVpv (path, 0));
116 CALL_CALL ("cf::object_thawer_load", G_ARRAY); 129 CALL_CALL ("cf::object_thawer_load", G_ARRAY);
117 130
118 if (count == 2) 131 if (count == 2)
119 { 132 {
120 // second value - perl objects 133 // second value - perl objects
141 CALL_END; 154 CALL_END;
142 } 155 }
143} 156}
144 157
145object_thawer::object_thawer (const char *data, AV *perlav) 158object_thawer::object_thawer (const char *data, AV *perlav)
159: name (strdup ("(memory stream"))
146{ 160{
147 av = perlav; 161 av = perlav;
148 text = newSVpv (data, 0); 162 text = newSVpv (data, 0);
149 sv_catpv (text, thawer_eof); 163 sv_catpv (text, thawer_eof);
150 line = SvPVbyte_nolen (text); 164 line = SvPVbyte_nolen (text);
151} 165}
152 166
167void
153void object_thawer::get (attachable *obj, int oid) 168object_thawer::get (attachable *obj, int oid)
154{ 169{
155 if (!av || oid < 0) // this is actually an error of sorts 170 if (!av || oid < 0) // this is actually an error of sorts
156 return; 171 return;
157 172
158 SV **svp = av_fetch ((AV *)av, oid, 0); 173 SV **svp = av_fetch ((AV *)av, oid, 0);
159 174
160 if (!svp || !SvROK (*svp)) 175 if (!svp || !SvROK (*svp))
161 { 176 {
162 printf ("trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid); 177 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
163 return; 178 return;
164 } 179 }
165 180
166 if (!SvROK (*svp)) 181 if (!SvROK (*svp))
167 { 182 {
168 LOG (llevError, "deserialised perl object is not an RV"); 183 LOG (llevError, "deserialised perl object is not an RV\n");
169 return; 184 return;
170 } 185 }
171 186
172 HV *hv = (HV *)SvRV (*svp); 187 HV *hv = (HV *)SvRV (*svp);
173 188
174 if (SvTYPE (hv) != SVt_PVHV) 189 if (SvTYPE (hv) != SVt_PVHV)
175 { 190 {
176 LOG (llevError, "deserialised perl object is not a PVHV"); 191 LOG (llevError, "deserialised perl object is not a PVHV\n");
177 return; 192 return;
178 } 193 }
179 194
180 if (obj->self) 195 if (obj->self)
181 { 196 {
182 // the hard way(?) 197 // the hard way(?)
183 198
184 // the dirty blues: "just" swap the XPVHV's 199 CALL_BEGIN (2);
185 swap (SvANY (obj->self), SvANY (hv)); 200 CALL_ARG_SV (newRV_inc ((SV *)obj->self));
186 201 CALL_ARG_SV (newRV_inc ((SV *)hv));
187 // do not swap magic, though 202 PUTBACK;
188 swap (SvMAGIC (obj->self), SvMAGIC (hv)); 203 call_method ("thawer_merge", G_DISCARD | G_EVAL);
204 SPAGAIN;
205 CALL_END;
189 } 206 }
190 else 207 else
191 { 208 {
192 // the easy way(?) 209 // the easy way(?)
193 210
194 obj->self = hv; 211 obj->self = hv;
195 SvRV_set (*svp, &PL_sv_undef); 212 SvRV_set (*svp, &PL_sv_undef);
196 213
197 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0); 214 sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
198
199 // borrow a refcount for the perl object
200 obj->flags |= attachable::F_BORROWED;
201 obj->refcnt_dec ();
202 } 215 }
203 216
204 obj->reattach (); 217 obj->reattach ();
205} 218}
206 219
207object_thawer::~object_thawer () 220object_thawer::~object_thawer ()
208{ 221{
209 if (text) SvREFCNT_dec (text); 222 if (text) SvREFCNT_dec (text);
210 if (av) SvREFCNT_dec (av); 223 if (av) SvREFCNT_dec (av);
211}
212 224
225 free ((void *)name);
226}
227
228//TODO: remove
229char *
213char *fgets (char *s, int n, object_thawer &thawer) 230fgets (char *s, int n, object_thawer &thawer)
214{ 231{
215 char *p = thawer.line; 232 char *p = thawer.line;
216 char *q = s; 233 char *q = s;
217 234
218 if (!p) 235 if (!p)
224 break; 241 break;
225 242
226 *q++ = *p; 243 *q++ = *p;
227 244
228 if (*p++ == '\n') 245 if (*p++ == '\n')
246 {
247 ++thawer.linenum;
229 break; 248 break;
249 }
230 } 250 }
231 251
232 *q = 0; 252 *q = 0;
233 thawer.line = p; 253 thawer.line = p;
234 254
235 return s == q ? 0 : s; 255 return s == q ? 0 : s;
236} 256}
237 257
238keyword object_thawer::get_kv () 258bool
259object_thawer::parse_error (const char *type, const char *name, bool skip)
260{
261 if (!type) type = "file section";
262 if (!name) name = "generic";
263
264 switch (kw)
265 {
266 case KW_EOF:
267 LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n",
268 this->name, linenum, type, name);
269 return false;
270
271 case KW_ERROR:
272 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
273 this->name, linenum,
274 type, name,
275 kw_str ? kw_str : "<file load>");
276 return false;
277
278 default:
279 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
280 this->name, linenum,
281 kw_str ? kw_str : "<null>",
282 value ? value : "<null>",
283 type, name,
284 skip ? "skipping line" : "aborting load");
285 return skip;
286 }
287}
288
289void
290object_thawer::next ()
239{ 291{
240 if (!line) 292 if (!line)
241 return KW_EOF; 293 {
294 kw = KW_ERROR;
295 return;
296 }
242 297
243 for (;;) 298 for (;;)
244 { 299 {
245 char *p = line; 300 char *p = line;
246 301
247 if (!*p) 302 if (!*p)
248 return KW_EOF; 303 {
304 kw = KW_EOF;
305 break;
306 }
249 307
250 // parse keyword 308 // parse keyword
251 while (*p > ' ') 309 while (*p > ' ')
252 p++; 310 p++;
253 311
257 { 315 {
258 // parse value 316 // parse value
259 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20 317 while (*(unsigned char *)p <= ' ' && *p != '\n') // skip 0x01 .. 0x20
260 ++p; 318 ++p;
261 319
262 last_value = p; 320 value = p;
263 321
264 while (*p != '\n') 322 while (*p != '\n')
265 p++; 323 p++;
266 324
267 *p++ = 0; 325 *p++ = 0;
268 } 326 }
269 else 327 else
270 last_value = 0; 328 value = 0;
271 329
330 ++linenum;
272 line [klen] = 0; 331 line [klen] = 0;
273 keyword_idx *kw = kw_lex::match (line, klen); 332 keyword_idx *kw_idx = kw_lex::match (line, klen);
274 333
275 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, last_value);//D 334 //printf ("KV %d<%s,%s>\n", kw ? kw->index : 0, line, value);//D
276 335
277 last_keyword = line; 336 kw_str = line;
278 line = p; 337 line = p;
279 338
280 if (kw) 339 if (kw_idx)
281 return kw->index; 340 {
341 kw = kw_idx->index;
342 break;
343 }
282 else if (!*last_keyword || *last_keyword == '#') 344 else if (!*kw_str || *kw_str == '#')
283 ; // empty/comment line 345 ; // empty/comment line
284 else 346 else
347 {
285 return KW_ERROR; 348 kw = KW_ERROR;
349 break;
350 }
286 } 351 }
287} 352}
288 353
289void object_thawer::skip_kv (keyword kw) 354void
355object_thawer::skip ()
290{ 356{
291 shstr ml; 357 shstr ml;
292 358
293 switch (kw) 359 switch (kw)
294 { 360 {
295 case KW_msg: get_ml (KW_endmsg , ml); break; 361 case KW_msg: get_ml (KW_endmsg , ml); break;
296 case KW_lore: get_ml (KW_endlore , ml); break; 362 case KW_lore: get_ml (KW_endlore , ml); break;
297 case KW_maplore: get_ml (KW_endmaplore, ml); break; 363 case KW_maplore: get_ml (KW_endmaplore, ml); break;
364 default: break;
298 } 365 }
366
367 next ();
299} 368}
300 369
370void
301void object_thawer::get (shstr &sh) const 371object_thawer::get (shstr &sh) const
302{ 372{
303 if (last_value) 373 if (value)
304 sh = last_value; 374 sh = value;
305 else 375 else
306 { 376 {
307 sh = "<value missing>"; 377 sh = "<value missing>";
308 LOG (llevError, "keyword requires value: <%.320s>\n", line);//TODO: add filename 378 LOG (llevError, "keyword \"%s\" requires value, substituting with <value missing>\n", kw_str);//TODO: add filename
309 } 379 }
310} 380}
311 381
382void
312void object_thawer::get_ml (keyword kend, shstr &sh) 383object_thawer::get_ml (keyword kend, shstr &sh)
313{ 384{
314 char kw[128]; 385 char kw[128];
315 386
316 int klen = keyword_len [kend]; 387 int klen = keyword_len [kend];
317 388
318 kw [0] = '\n'; 389 kw [0] = '\n';
319 memcpy (kw + 1, keyword_str [kend], klen); 390 memcpy (kw + 1, keyword_str [kend], klen);
320 kw [klen + 1] = '\n'; 391 kw [klen + 1] = '\n';
321 kw [klen + 2] = 0; 392 kw [klen + 2] = 0;
322 393
394 ++linenum;
395
323 // first test for completely empty msg... "endXXX\n" 396 // first test for completely empty msg... "endXXX\n"
324 if (!strncmp (line, kw + 1, klen + 1)) 397 if (!strncmp (line, kw + 1, klen + 1))
325 { 398 {
326 sh = 0; 399 sh = 0;
327 400
342 } 415 }
343 416
344 *end = 0; 417 *end = 0;
345 sh = line; 418 sh = line;
346 419
420 // count line numbers
421 while (line < end)
422 linenum += *line++ == '\n';
423
347 line = end + keyword_len [kend] + 1; 424 line += keyword_len [kend];
348 425
349 while (*line++ != '\n') 426 while (*line++ != '\n')
350 ; 427 ;
351 }
352}
353 428
429 ++linenum;
430 }
431}
432
433sint32
354sint32 object_thawer::get_sint32 () const 434object_thawer::get_sint32 () const
355{ 435{
356 char *p = last_value; 436 char *p = value;
357 437
358 if (!p) 438 if (!p)
359 return 0; 439 return 0;
360 440
361 sint32 val = 0; 441 sint32 val = 0;
377 while (*p); 457 while (*p);
378 458
379 return negate ? -val : val; 459 return negate ? -val : val;
380} 460}
381 461
462sint64
382sint64 object_thawer::get_sint64 () const 463object_thawer::get_sint64 () const
383{ 464{
384 return last_value ? atoll (last_value) : 0; 465 return value ? atoll (value) : 0;
385} 466}
386 467
468double
387double object_thawer::get_double () const 469object_thawer::get_double () const
388{ 470{
389 return last_value ? atof (last_value) : 0; 471 return value ? atof (value) : 0;
390} 472}
391 473

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines