Changeset 720 for cpp/frams/param/param.cpp
- Timestamp:
- 01/14/18 11:24:22 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/param/param.cpp
r704 r720 10 10 #include "common/log.h" 11 11 #include <frams/util/sstringutils.h> 12 #include <common/virtfile/stringfile.h> 12 13 13 14 //#define SAVE_ALL_NAMES … … 356 357 } 357 358 358 void SimpleAbstractParam::save 2(SString& f, void *defdata, bool addcr, bool all_names)359 void SimpleAbstractParam::saveSingleLine(SString& f, void *defdata, bool addcr, bool all_names) 359 360 { // defdata!=NULL -> does not save default values 360 361 const char *p; … … 421 422 } 422 423 423 int ParamInterface::load(VirtFILE* f, bool warn_unknown_fields, bool *abortable, int *linenum) 424 int ParamInterface::load(FileFormat format, VirtFILE* f, LoadOptions *options) 425 { 426 LoadOptions default_options; 427 if (options == NULL) 428 options = &default_options; 429 switch (format) 430 { 431 case FormatMultiLine: 432 return loadMultiLine(f, *options); 433 434 case FormatSingleLine: 435 { 436 StringFILE *sf = dynamic_cast<StringFILE*>(f); 437 SString s; 438 if (sf) 439 { 440 s = sf->getString().c_str(); 441 options->offset += sf->Vtell(); 442 } 443 else 444 { 445 if (!loadSStringLine(f, s)) 446 return -1; 447 } 448 return loadSingleLine(s, *options); 449 } 450 } 451 return -1; 452 } 453 454 int ParamInterface::load(FileFormat format, const SString &s, LoadOptions *options) 455 { 456 LoadOptions default_options; 457 if (options == NULL) 458 options = &default_options; 459 switch (format) 460 { 461 case FormatMultiLine: 462 { 463 string std_string(s.c_str()); 464 StringFILE f(std_string); 465 return loadMultiLine(&f, *options); 466 } 467 468 case FormatSingleLine: 469 return loadSingleLine(s, *options); 470 } 471 return -1; 472 } 473 474 int ParamInterface::loadMultiLine(VirtFILE* f, LoadOptions &options) 424 475 { 425 476 SString buf; … … 434 485 if ((i = findId("beforeLoad")) >= 0) 435 486 call(i, NULL, NULL); 436 while (((! abortable) || (!*abortable)) && loadSStringLine(f, buf))437 { 438 if ( linenum) (*linenum)++;487 while (((!options.abortable) || (!*options.abortable)) && loadSStringLine(f, buf)) 488 { 489 if (options.linenum) (*options.linenum)++; 439 490 const char* t = buf.c_str(); 440 491 p0 = t; while (isblank(*p0)) p0++; … … 448 499 case 0: 449 500 logPrintf("ParamInterface", "load", LOG_WARN, "Ignored unexpected line %s while reading object '%s'", 450 linenum ?451 SString::sprintf("%d", * linenum).c_str()501 options.linenum ? 502 SString::sprintf("%d", *options.linenum).c_str() 452 503 : SString::sprintf("'%s'", p0).c_str(), 453 504 getName()); … … 472 523 { 473 524 fileinfo = SString::sprintf(" while reading from '%s'", fname); 474 if ( linenum)475 fileinfo += SString::sprintf(" (line %d)", * linenum);525 if (options.linenum) 526 fileinfo += SString::sprintf(" (line %d)", *options.linenum); 476 527 } 477 528 logPrintf("ParamInterface", "load", LOG_WARN, "Multiple '%s.%s' fields found%s", getName(), id(i), fileinfo.c_str()); … … 496 547 int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break; 497 548 unquoteTilde(s); 498 if ( linenum && (flags(i)&PARAM_LINECOMMENT))499 s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), * linenum + 1) + s;549 if (options.linenum && (flags(i)&PARAM_LINECOMMENT)) 550 s = SString::sprintf("@file %s\n@line %d\n", f->VgetPath(), *options.linenum + 1) + s; 500 551 set(i, s.c_str()); 501 if ( linenum)502 (* linenum) += lfcount;552 if (options.linenum) 553 (*options.linenum) += lfcount; 503 554 } 504 555 else … … 510 561 } 511 562 } 512 else if ( warn_unknown_fields)563 else if (options.warn_unknown_fields) 513 564 { 514 565 SString name(p0, p_len); … … 521 572 if (!readUntilTilde(f, s)) 522 573 closingTildeError(this, f, -1); 523 if ( linenum)574 if (options.linenum) 524 575 { 525 576 const char* tmp = s.c_str(); … … 530 581 lfcount++; tmp++; 531 582 } 532 (* linenum) += lfcount;583 (*options.linenum) += lfcount; 533 584 } 534 585 int ch; while ((ch = f->Vgetc()) != EOF) if (ch == '\n') break; … … 705 756 { 706 757 const char *t; 707 if (((*(t = type(i))) == 'd') && (strchr(t, '~') !=NULL)) //type is int and contains enum labels758 if (((*(t = type(i))) == 'd') && (strchr(t, '~') != NULL)) //type is int and contains enum labels 708 759 { 709 760 paInt mn, mx, def; … … 1131 1182 } 1132 1183 1133 int ParamInterface::load 2(const SString &s, int &poz)1184 int ParamInterface::loadSingleLine(const SString &s, LoadOptions &options) 1134 1185 { 1135 1186 int i; // the index number of the parameter … … 1145 1196 SString tmpvalue; 1146 1197 bool parse_failed = false; 1147 if ( poz>= s.len()) return fields_loaded;1148 t = s.c_str() + poz;1149 1150 lin = getline(s, poz, len); // all fields must be encoded in a single line1198 if (options.offset >= s.len()) return fields_loaded; 1199 t = s.c_str() + options.offset; 1200 1201 lin = getline(s, options.offset, len); // all fields must be encoded in a single line 1151 1202 if (!len) return fields_loaded; // empty line = end 1152 1203 i = 0; … … 1241 1292 #endif 1242 1293 } 1243 return fields_loaded | (parse_failed ? LOAD2_PARSE_FAILED : 0); 1294 if (parse_failed) options.parse_failed = true; 1295 return fields_loaded; 1244 1296 } 1245 1297
Note: See TracChangeset
for help on using the changeset viewer.