Changeset 1179 for cpp/frams/util
- Timestamp:
- 07/26/22 02:33:45 (2 years ago)
- Location:
- cpp/frams/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/sstringutils.cpp
r1156 r1179 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-202 1Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2022 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 8 8 #include <common/nonstd.h> 9 9 #include <common/Convert.h> 10 #include <common/nonstd_span.h> 10 11 11 12 int loadSString(const char* filename, SString& s, const char* framsgmodule, const char* error, bool remove_cr) … … 30 31 while (!f->Veof()) 31 32 { 32 len = f->Vread(buf, 1, sizeof(buf));33 len = (int)f->Vread(buf, 1, sizeof(buf)); 33 34 s.append(buf, len); 34 35 } … … 49 50 if (r == NULL) break; 50 51 ret = true; 51 int d = strlen(r);52 int d = (int)strlen(r); 52 53 if (d > 0) 53 54 { … … 416 417 } 417 418 } 419 420 static span<char> findEof(span<char> field, const char* pos) // 'eof' includes any number of leading '\'s and is only detected when there is nothing else in the line 421 { 422 const char* eof = strstr(pos, "eof"); 423 if (!eof) 424 return span<char>(); 425 426 if (!(((eof + 3) == field.end()) || (eof[3] == '\n'))) 427 return span<char>(); 428 429 const char* begin = eof - 1; 430 for (; begin >= field.begin(); begin--) 431 { 432 if (*begin == '\n') 433 break; 434 if (*begin != '\\') 435 return span<char>(); 436 } 437 438 return span<char>((char*)(begin + 1), (eof + 3) - (begin + 1)); 439 } 440 441 bool sstringQuoteEOF(SString& target) 442 { 443 span<char> field((char*)target.c_str(), target.size()); 444 const char* pos = field.begin(); 445 446 bool changed = false; 447 448 SString tmp; 449 450 while (true) 451 { 452 auto eof = findEof(field, pos); 453 if (eof.size()) 454 { 455 if (!changed) 456 { 457 tmp.reserve(target.length() + 10); 458 changed = true; 459 } 460 tmp.append(pos, eof.begin() - pos); 461 tmp += "\\"; 462 tmp.append(eof.begin(), (int)eof.size()); 463 pos = eof.end(); 464 } 465 else 466 { 467 if (!changed) 468 return false; 469 tmp.append(pos, field.end() - pos); 470 break; 471 } 472 } 473 474 target = tmp; 475 return true; 476 } 477 478 bool sstringUnquoteEOF(SString& target) 479 { 480 span<char> field((char*)target.c_str(), target.size()); 481 const char* pos = field.begin(); 482 483 bool changed = false; 484 485 SString tmp; 486 487 while (true) 488 { 489 auto eof = findEof(field, pos); 490 if (eof.size() > 3) 491 { 492 if (!changed) 493 { 494 tmp.reserve(target.length()); 495 changed = true; 496 } 497 tmp.append(pos, eof.begin() - pos); 498 tmp.append(eof.begin() + 1, (int)eof.size() - 1); 499 pos = eof.end(); 500 } 501 else 502 { 503 if (eof.size() == 3) //size()==3 means bare 'eof', which cannot be unquoted (and means something went wrong somewhere else) 504 logPrintf("String", "unquoteEof", LOG_WARN, "Unexpected bare \"eof\" line"); 505 if (!changed) 506 return false; 507 tmp.append(pos, field.end() - pos); 508 break; 509 } 510 } 511 512 target = tmp; 513 return true; 514 } -
cpp/frams/util/sstringutils.h
r1156 r1179 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-202 1Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2022 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 25 25 bool sstringURLEncode(SString& target); 26 26 bool sstringURLDecode(SString &target); 27 bool sstringQuoteEOF(SString& target); 28 bool sstringUnquoteEOF(SString& target); 27 29 28 30 int strFindField(const SString& txt, const SString& name, int &end);
Note: See TracChangeset
for help on using the changeset viewer.