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.26 by root, Wed Sep 5 06:13:08 2007 UTC vs.
Revision 1.42 by root, Thu Apr 29 12:24:04 2010 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Crossfire TRT is free software: you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify it under
7 * it under the terms of the GNU General Public License as published by 7 * the terms of the Affero GNU General Public License as published by the
8 * the Free Software Foundation, either version 3 of the License, or 8 * Free Software Foundation, either version 3 of the License, or (at your
9 * (at your option) any later version. 9 * option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the Affero GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
18 * 19 *
19 * The authors can be reached via e-mail to <crossfire@schmorp.de> 20 * The authors can be reached via e-mail to <support@deliantra.net>
20 */ 21 */
21 22
22#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff 23#include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
23#include "logger.h" 24#include "logger.h"
24#include "cfperl.h" 25#include "cfperl.h"
34{ 35{
35 SvREFCNT_dec (av); 36 SvREFCNT_dec (av);
36} 37}
37 38
38void 39void
39object_freezer::put (attachable *ext) 40object_freezer::put_ (attachable *ext)
40{ 41{
41 ext->optimise (); 42 ext->optimise ();
42 43
43 if (ext->self) 44 if (ext->self)
44 { 45 {
45 int idx = AvFILLp ((AV *)av) + 1; 46 int idx = AvFILLp ((AV *)av) + 1;
46 av_store (av, idx, newRV_inc ((SV *)ext->self)); 47 av_store (av, idx, newRV_inc ((SV *)ext->self));
47 48
48 add ((void *)"oid ", 4); 49 put (KW(oid), sint32(idx));
49 add ((sint32)idx);
50 add ('\n');
51 } 50 }
52} 51}
53 52
54bool 53bool
55object_freezer::save (const char *path) 54object_freezer::save (const char *path)
105} 104}
106#endif 105#endif
107 106
108static const char thawer_eof[] = "\n\n\n\0\0\0"; 107static const char thawer_eof[] = "\n\n\n\0\0\0";
109 108
109bool object_thawer::errors_are_fatal = true;
110
110object_thawer::object_thawer (const char *path) 111object_thawer::object_thawer (const char *data, AV *perlav)
112: name (strdup ("(memory stream)"))
113{
114 av = perlav;
115 text = newSVpv (data, 0);
116 sv_catpv (text, thawer_eof);
117 line = SvPVbyte_nolen (text);
118 next ();
119}
120
121object_thawer::object_thawer (const_utf8_string path)
122{
123 new_from_path (path);
124}
125
126// convenience constructor
127object_thawer::object_thawer (const_utf8_string dir, const_utf8_string file)
128{
129 new_from_path (format ("%s/%s", dir, file));
130}
131
132void
133object_thawer::new_from_path (const_utf8_string path)
134{
111: name (strdup (path)) 135 name = strdup (path);
112{
113 static const char eof[] = "\n\n\n\0\0\0";
114
115 av = 0; 136 av = 0;
116 text = 0; 137 text = 0;
117 line = 0; 138 line = 0;
118 linenum = 0; 139 linenum = 0;
119 140
139 // first value - text part, pad with 3 zeroes 160 // first value - text part, pad with 3 zeroes
140 { 161 {
141 SV *sv = POPs; 162 SV *sv = POPs;
142 STRLEN len; 163 STRLEN len;
143 char *sv_ = SvPVbyte (sv, len); 164 char *sv_ = SvPVbyte (sv, len);
144 text = newSV (len + sizeof (eof)); 165 text = newSV (len + sizeof (thawer_eof));
145 SvCUR_set (text, len); 166 SvCUR_set (text, len + sizeof (thawer_eof));
146 memcpy (SvPVX (text), sv_, len); 167 memcpy (SvPVX (text), sv_, len);
147 memcpy (SvEND (text), eof, sizeof (eof)); // just to be sure 168 memcpy (SvPVX (text) + len, thawer_eof, sizeof (thawer_eof));
148 169
149 line = SvPVX (text); 170 line = SvPVX (text);
150 next (); 171 next ();
151 } 172 }
152 } 173 }
153 174
154 CALL_END; 175 CALL_END;
155 } 176 }
156}
157
158object_thawer::object_thawer (const char *data, AV *perlav)
159: name (strdup ("(memory stream)"))
160{
161 av = perlav;
162 text = newSVpv (data, 0);
163 sv_catpv (text, thawer_eof);
164 line = SvPVbyte_nolen (text);
165 next ();
166} 177}
167 178
168void 179void
169object_thawer::get (attachable *obj, int oid) 180object_thawer::get (attachable *obj, int oid)
170{ 181{
226 resolve_delayed_derefs (false); 237 resolve_delayed_derefs (false);
227 238
228 free ((void *)name); 239 free ((void *)name);
229} 240}
230 241
231//TODO: remove 242static void
232char * 243error_out ()
233fgets (char *s, int n, object_thawer &thawer)
234{ 244{
235 char *p = thawer.line; 245 if (object_thawer::errors_are_fatal)
236 char *q = s;
237
238 if (!p)
239 return 0;
240
241 while (--n)
242 {
243 if (!*p)
244 break;
245
246 *q++ = *p;
247
248 if (*p++ == '\n')
249 {
250 ++thawer.linenum;
251 break;
252 }
253 } 246 {
254 247 LOG (llevError, "(parse errors at this time are fatal, exiting)");
255 *q = 0; 248 exit (1);
256 thawer.line = p; 249 }
257
258 return s == q ? 0 : s;
259} 250}
260 251
261void 252void
262object_thawer::parse_warn (const char *msg) 253object_thawer::parse_warn (const char *msg) const
263{ 254{
264 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n", 255 LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
265 this->name, linenum, 256 this->name, linenum,
266 kw_str ? kw_str : "<null>", 257 kw_str ? kw_str : "<null>",
267 value ? value : "<null>", 258 value ? value : "<null>",
268 msg); 259 msg);
269} 260}
270 261
271bool 262bool
272object_thawer::parse_error (const char *type, const char *name, bool skip) 263object_thawer::parse_error (const char *type, const char *name, bool skip) const
273{ 264{
274 if (!type) type = "file section"; 265 if (!type) type = "file section";
275 if (!name) name = "generic"; 266 if (!name) name = "generic";
276 267
277 switch (kw) 268 switch (kw)
278 { 269 {
279 case KW_EOF: 270 case KW_EOF:
280 LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n", 271 LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n",
281 this->name, linenum, type, name); 272 this->name, linenum, type, name);
273 error_out ();
282 return false; 274 return false;
283 275
284 case KW_ERROR: 276 case KW_ERROR:
285 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n", 277 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
286 this->name, linenum, 278 this->name, linenum,
287 type, name, 279 type, name,
288 kw_str ? kw_str : "<file load>"); 280 kw_str ? kw_str : "<file load>");
281 error_out ();
289 return false; 282 return false;
290 283
291 default: 284 default:
292 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n", 285 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
293 this->name, linenum, 286 this->name, linenum,
294 kw_str ? kw_str : "<null>", 287 kw_str ? kw_str : "<null>",
295 value ? value : "<null>", 288 value ? value : "<null>",
296 type, name, 289 type, name,
297 skip ? "skipping line" : "aborting load"); 290 skip ? "skipping line" : "aborting load");
291 error_out ();
298 return skip; 292 return skip;
299 } 293 }
300} 294}
301 295
302void 296void
310 304
311 for (;;) 305 for (;;)
312 { 306 {
313 char *p = line; 307 char *p = line;
314 308
315 if (*p <= ' ') 309 if (expect_false (*p <= ' '))
316 { 310 {
317 // skip whitespace (only some files need this) 311 // skip whitespace (only some files need this)
318 while (*p == ' ' || *p == '\t') 312 while (*p == ' ' || *p == '\t')
319 p++; 313 p++;
320 314
368 { 362 {
369 kw = KW_ERROR; 363 kw = KW_ERROR;
370 break; 364 break;
371 } 365 }
372 } 366 }
367}
368
369bool
370object_thawer::next_line ()
371{
372 if (!line)
373 {
374 kw = KW_ERROR;
375 return 0;
376 }
377
378 for (;;)
379 {
380 char *p = line;
381
382 if (expect_false (*p <= ' '))
383 {
384 // skip whitespace (only some files need this)
385 while (*p == ' ' || *p == '\t')
386 p++;
387
388 line = p;
389 }
390
391 if (!*p)
392 {
393 kw = KW_EOF;
394 return 0;
395 }
396
397 kw = KW_value;
398 kw_str = p;
399 value_nn = p;
400 value = p;
401
402 // parse till newline
403 while (*p > '\n')
404 p++;
405
406 if (*p == '\n')
407 *p++ = 0;
408
409 ++linenum;
410
411 line = p;
412
413 if (*kw_str && *kw_str != '#')
414 break;
415
416 // empty/comment line -> skip it
417 }
418
419 return 1;
373} 420}
374 421
375void 422void
376object_thawer::skip () 423object_thawer::skip ()
377{ 424{
389} 436}
390 437
391void 438void
392object_thawer::skip_block () 439object_thawer::skip_block ()
393{ 440{
441 // must not stop at KW_ERROR, as those signify custom keys
394 while (kw != KW_EOF) 442 while (kw != KW_EOF)
395 { 443 {
396 keyword w = kw; 444 keyword w = kw;
397 skip (); 445 skip ();
446
447 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
448 skip_block ();
398 if (w == KW_end) 449 else if (w == KW_end)
399 break; 450 break;
400 } 451 }
401} 452}
402 453
403void 454void
452} 503}
453 504
454sint32 505sint32
455object_thawer::get_sint32 () const 506object_thawer::get_sint32 () const
456{ 507{
457 char *p = value_nn; 508 const char *p = value_nn;
458 509
459 sint32 val = 0; 510 sint32 val = 0;
460 bool negate; 511 bool negate;
461 512
462 if (*p == '-') 513 if (*p == '-')

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines