------------------------------------------------------------------------------------------------
| 468 | |
| 469 | // ------------------------------------------------------------------------------------------------ |
| 470 | std::string ParseTokenAsString(const Token& t, const char*& err_out) |
| 471 | { |
| 472 | err_out = nullptr; |
| 473 | |
| 474 | if (t.Type() != TokenType_DATA) { |
| 475 | err_out = "expected TOK_DATA token"; |
| 476 | return std::string(); |
| 477 | } |
| 478 | |
| 479 | if(t.IsBinary()) |
| 480 | { |
| 481 | const char* data = t.begin(); |
| 482 | if (data[0] != 'S') { |
| 483 | err_out = "failed to parse S(tring), unexpected data type (binary)"; |
| 484 | return std::string(); |
| 485 | } |
| 486 | |
| 487 | // read string length |
| 488 | BE_NCONST int32_t len = SafeParse<int32_t>(data+1, t.end()); |
| 489 | AI_SWAP4(len); |
| 490 | |
| 491 | ai_assert(t.end() - data == 5 + len); |
| 492 | return std::string(data + 5, len); |
| 493 | } |
| 494 | |
| 495 | const size_t length = static_cast<size_t>(t.end() - t.begin()); |
| 496 | if(length < 2) { |
| 497 | err_out = "token is too short to hold a string"; |
| 498 | return std::string(); |
| 499 | } |
| 500 | |
| 501 | const char* s = t.begin(), *e = t.end() - 1; |
| 502 | if (*s != '\"' || *e != '\"') { |
| 503 | err_out = "expected double quoted string"; |
| 504 | return std::string(); |
| 505 | } |
| 506 | |
| 507 | return std::string(s+1,length-2); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | namespace { |
no test coverage detected