1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
|
/*---------------------------------------------------------------------------*\
Copyright (C) 2000 AVM GmbH. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
*
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY, without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, and WITHOUT
ANY LIABILITY FOR ANY DAMAGES arising out of or in connection
with the use or performance of this software. See the
GNU General Public License for further details.
\*---------------------------------------------------------------------------*/
#include "CString.h"
#include "aString.h"
#include "aStdLib.h"
#include "aStrPrt.h"
#include "aFlags.h"
#include "dbgSTD.h"
/*---------------------------------------------------------------------------*\
internal defines
\*---------------------------------------------------------------------------*/
#define CSTRING_PRINT_BufferSize 80 // Must be enough to take a int or
// pointer to string conversion string
#define CSTRING_PRINT_MemoryError -1
#define PRINTFLAGS_ACTIVE 0x0001 // Placeholder found
#define PRINTFLAGS_READY 0x0002 // Placeholder fully insert
#define PRINTFLAGS_MINUS 0x0004 // Output left oriented
#define PRINTFLAGS_PLUS 0x0008 // Use sign '+' or '-'
#define PRINTFLAGS_ZERO 0x0010 // Fill with leading zeros
#define PRINTFLAGS_BLANK 0x0020 // Use sign '-' or ' '
#define PRINTFLAGS_HASH 0x0040 // Show 0 or 0x before numbers
#define PRINTFLAGS_LARGE 0x0080 // Placeholder is in LARGE-format
#define PRINTFLAGS_SHORT 0x0100 // Placeholder is in SHORT-format
#define PRINTFLAGS_FAR 0x0200 // Placeholder is a FAR-pointer
#define PRINTFLAGS_NEAR 0x0400 // Placeholder is a NEAR-pointer
#define PRINTFLAGS_QUESTION 0x0800 // Sizeof Char specified before string
#define PRINTFLAGS_STRINGTYPE 0x1000 // Placeholder is a CDynamicString-Pointer
#define PRINTFLAGS_SHELLESCAPE 0x2000 // The placeholder should be filled in with
// a string suitable for passing to /bin/sh
// between double quotes.
/*===========================================================================*\
\*===========================================================================*/
CConstString::CConstString (tString string, tUInt len)
: pntr (string),
detectError (vFalse) {
dhead ("CConstString-Constructor", DCON_CDynamicString);
dassert (!len || pntr);
if (!len) {
len = check_strlen (string);
}
curLen = len;
maxSize = (len) ? len + 1 : 0; // TODO?: Is this correct?
}
/*===========================================================================*\
\*===========================================================================*/
tUInt CConstString::check_strlen (const tChar *string) {
dhead ("CConstString::check_strlen", DCON_CDynamicString);
dassert (string != 0);
tSize s = s_strlen (string);
dassert (s < MAXVAL_tUInt);
return (tUInt)s;
}
/*===========================================================================*\
\*===========================================================================*/
tUInt CConstString::check_strlen (const tWiChar *string) {
dhead ("CConstString::check_strlenW", DCON_CDynamicString);
dassert (string != 0);
tSize s = s_strlen (string);
dassert (s < MAXVAL_tUInt);
return (tUInt)s;
}
/*===========================================================================*\
\*===========================================================================*/
tUInt CConstString::check_strlen (const char *string) {
dhead ("CConstString::check_strlenC", DCON_CDynamicString);
dassert (string != 0);
tSize s = s_strlen (string);
dassert (s < MAXVAL_tUInt);
return (tUInt)s;
}
/*===========================================================================*\
\*===========================================================================*/
CDynamicString::CDynamicString (tFormatChar *string, tUInt len) {
dhead ("CDynamicString-Constructor", DCON_CDynamicString);
dparams ("%s,%x", string, len);
Set (string, len);
}
/*===========================================================================*\
\*===========================================================================*/
CDynamicString::CDynamicString (CConstString *pcstr) {
dhead ("CDynamicString-Constructor", DCON_CDynamicString);
dparams ("%s", pcstr);
Set (pcstr);
}
/*===========================================================================*\
\*===========================================================================*/
CDynamicString::CDynamicString (tUInt startSize) {
dhead ("CDynamicString-Constructor", DCON_CDynamicString);
Resize (startSize);
}
/*===========================================================================*\
\*===========================================================================*/
CDynamicString::~CDynamicString (void) {
dhead ("CDynamicString-Destructor", DCON_CDynamicString);
tString delPntr = pntr;
pntr = 0;
curLen = 0;
maxSize = 0;
detectError = vFalse;
if (delPntr) {
delete [] delPntr;
}
}
/*===========================================================================*\
\*===========================================================================*/
tBool CDynamicString::Change (tString string, tUInt startPos, tUInt len) {
dhead ("CDynamicString::Change", DCON_CDynamicString);
dparams ("%s,%x,%x", string, startPos, len, pntr);
dassert (string != 0);
dassert (startPos <= GetLen());
if (!len) {
len = check_strlen (string);
dprint ("CalcLen=%x ", len);
}
tUInt endPos = len + startPos;
if (endPos >= GetMaxSize()) {
if (DynExpand (endPos + 1) == vFalse) {
return vFalse;
}
}
dassert (endPos < GetMaxSize());
dassert (pntr != 0);
s_strncpy (pntr + startPos, string, len);
if (endPos > GetLen()) {
pntr[endPos] = '\0';
curLen = endPos;
}
return vTrue;
}
/*===========================================================================*\
\*===========================================================================*/
tBool CDynamicString::Change (tFormatChar *string, tUInt startPos, tUInt len) {
dhead ("CDynamicString::Change", DCON_CDynamicString);
dparams ("%s,%x,%x,%s", string, startPos, len, pntr);
dassert (string != 0);
dassert (startPos <= GetLen());
if (!len) {
len = check_strlen (string);
dprint ("CalcLen=%x ", len);
}
tUInt endPos = len + startPos;
if (endPos >= GetMaxSize()) {
if (DynExpand (endPos + 1) == vFalse) {
return vFalse;
}
}
dassert (endPos < GetMaxSize());
dassert (pntr != 0);
s_strncpy (pntr + startPos, string, len);
if (endPos > GetLen()) {
pntr[endPos] = '\0';
curLen = endPos;
}
return vTrue;
}
/*===========================================================================*\
\*===========================================================================*/
tBool CDynamicString::Insert (tString string, tUInt startPos, tUInt len) {
dhead ("CDynamicString::Insert", DCON_CDynamicString);
dassert (string != 0);
dassert (startPos <= GetLen());
if (!len) {
len = check_strlen (string);
}
tUInt endPos = len + GetLen();
if (endPos >= GetMaxSize()) {
if (DynExpand (endPos + 1) == vFalse) {
return vFalse;
}
}
dassert (endPos < GetMaxSize());
dassert (pntr != 0);
if (GetLen() > startPos) {
s_strmov (pntr + startPos, len, GetLen() - startPos);
}
s_strncpy (pntr + startPos, string, len);
pntr[endPos] = '\0';
curLen = endPos;
return vTrue;
}
/*===========================================================================*\
\*===========================================================================*/
tBool CDynamicString::Fill (tStringChar fillChar, tUInt len, tUInt startPos) {
dhead ("CDynamicString::Fill", DCON_CDynamicString);
dparams ("%c,%x,%x,%s", fillChar, len, startPos, pntr);
dassert (startPos <= GetLen());
tUInt endPos = len + startPos;
if (endPos >= GetMaxSize()) {
if (DynExpand (endPos + 1) == vFalse) {
return vFalse;
}
}
dassert (endPos < GetMaxSize());
dassert (pntr != 0);
s_strset (pntr + startPos, fillChar, len);
if (endPos > GetLen()) {
pntr[endPos] = '\0';
curLen = endPos;
}
return vTrue;
}
/*===========================================================================*\
\*===========================================================================*/
tCompare CDynamicString::Compare (tString string, tUInt len) {
dhead ("CDynamicString::Compare", DCON_CDynamicString);
dassert (string != 0);
if (!len) {
len = check_strlen (string);
}
tCompare cmp2;
if (len == GetLen()) {
cmp2 = vEqual;
} else if (len > GetLen()) {
len = GetLen();
cmp2 = vHigher;
} else {
cmp2 = vLower;
}
tCompare fret = s_strncmp (string, pntr, len);
if (fret == vEqual) {
fret = cmp2;
}
RETURN ('x', fret);
}
/*===========================================================================*\
\*===========================================================================*/
void CDynamicString::Get (tString string, tUInt len, tUInt startPos) {
dhead ("CDynamicString::Get", DCON_CDynamicString);
dassert (string != 0);
dassert (startPos <= GetLen());
dassert (len <= GetLen());
dassert (startPos + len <= GetLen());
s_strncpy (string, pntr + startPos, len);
string[len] = '\0';
}
/*===========================================================================*\
\*===========================================================================*/
tUInt CDynamicString::FindChar (tStringChar ch, tUInt startpos) {
dhead ("CDynamicString::FindChar", DCON_CDynamicString);
for (; startpos < GetLen(); startpos++) {
if (pntr[startpos] == ch) {
RETURN ('x', startpos);
}
}
RETURN ('x', MAXVAL_tUInt);
}
/*===========================================================================*\
\*===========================================================================*/
tUInt CDynamicString::FindLastChar (tStringChar ch) {
dhead ("CDynamicString::FindLastChar", DCON_CDynamicString);
tUInt i = GetLen();
while (i > 0) {
if (pntr[--i] == ch) {
RETURN ('x', i);
}
}
RETURN ('x', MAXVAL_tUInt);
}
/*===========================================================================*\
\*===========================================================================*/
void CDynamicString::Remove (tUInt startPos, tUInt len) {
dhead ("CDynamicString::Remove", DCON_CDynamicString);
dparams ("%x,%x,%x", startPos, len, GetLen());
dassert (startPos <= GetLen());
dassert (len <= GetLen());
dassert (startPos + len <= GetLen());
tUInt endPos = startPos + len;
if (endPos >= GetLen()) {
SetLen (startPos);
} else {
s_strmov (pntr + endPos, -(tSLong)len, GetLen() - endPos + 1);
dassert (curLen > len);
curLen -= len;
}
}
/*===========================================================================*\
\*===========================================================================*/
void CDynamicString::RemoveChars (tString RemoveChars, tUInt startPos, tUInt len) {
dhead ("CDynamicString::RemoveChars", DCON_CDynamicString);
if (startPos < GetLen()) {
if (!len || (len + startPos > GetLen())) {
len = GetLen() - startPos;
}
dassert (GetPointer() != 0);
tString lauf = GetPointer() + startPos;
for (; (len > 0) && (s_findfirstchar (*lauf, RemoveChars) == 0); len--, lauf++) {
dassert (*lauf != 0);
}
for (tString current = lauf + 1; len > 0; len--, current++) {
dassert (*current != 0);
if (s_findfirstchar (*current, RemoveChars) == 0) {
*lauf++ = *current;
} else {
curLen--;
}
}
*lauf = '\0';
}
}
/*===========================================================================*\
\*===========================================================================*/
tBool CDynamicString::Resize (tUInt newMaxSize) {
dhead ("CDynamicString::Resize", DCON_CDynamicString);
dassert (GetLen() + 1 <= newMaxSize);
tString delPntr = 0;
tString newPntr = new tStringChar [newMaxSize];
if (!newPntr) {
detectError = vTrue;
return vFalse;
}
if (pntr) {
s_strncpy (newPntr, pntr, GetLen() + 1);
} else {
newPntr[0] = '\0';
}
delPntr = pntr;
pntr = newPntr;
maxSize = newMaxSize;
delete [] delPntr;
return vTrue;
}
/*===========================================================================*\
\*===========================================================================*/
tSInt CDynamicString::i_vPrintAppend (tFormatChar *format, va_list argptr, tUInt tabsize, tStringChar tabchar) {
dhead ("CDynamicString::i_vPrintAppend", DCON_CDynamicString);
dassert (format != 0);
tFormatChar localBuf[CSTRING_PRINT_BufferSize]; // Local internal buffer
tFormatChar *savepos = format; // Position of last "placeholder"
tFormatChar *copyBuf = &localBuf[0]; // Buffer that will be append to String
tUInt copyBufLen = 0; // Size of copyBuf
tUInt PrtFlags = 0; // Possible Flags for "placeholder"
tUInt PrtWidth = 0; // Width of "placeholder"
tSInt PrtPreci = -2; // Precision of "placeholder"
tUInt startLen = GetLen(); // Len of str before Print was executed
while (*format != 0) {
if (PrtFlags) {
/*----- exams placeholders (%...) -----*/
switch (*format) {
/*----- FLAGS -----*/
case '-':
PrtFlags |= PRINTFLAGS_MINUS;
break;
case '+':
PrtFlags |= PRINTFLAGS_PLUS;
break;
case ' ':
PrtFlags |= PRINTFLAGS_BLANK;
break;
case '#':
PrtFlags |= PRINTFLAGS_HASH;
break;
case '?':
PrtFlags |= PRINTFLAGS_QUESTION;
break;
case 'l':
case 'L':
PrtFlags |= PRINTFLAGS_LARGE;
break;
case 'h':
PrtFlags |= PRINTFLAGS_SHORT;
break;
case 'e':
PrtFlags |= PRINTFLAGS_SHELLESCAPE;
break;
/*----- WIDTH or PRECISION-----*/
case '0':
if (PrtPreci == -2) {
PrtFlags |= PRINTFLAGS_ZERO;
}
// no break!
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (PrtPreci == -2) {
PrtWidth = a_strtol (format, &format, 10);
} else {
PrtPreci = a_strtol (format, &format, 10);
}
format--;
break;
case '*':
if (PrtPreci == -2) {
PrtWidth = va_arg (argptr, tUInt);
} else {
PrtPreci = va_arg (argptr, tUInt);
}
break;
/*----- TYPE -----*/
case 'b':
if ((tUByte)va_arg (argptr, int)) {
s_strncpy (localBuf, "TRUE", 5);
copyBufLen = 4;
} else {
s_strncpy (localBuf, "FALSE", 6);
copyBufLen = 5;
}
if (PrtPreci == 1) {
copyBufLen = 1;
}
PrtFlags &= ~(PRINTFLAGS_LARGE | PRINTFLAGS_SHORT);
PrtFlags |= PRINTFLAGS_READY;
break;
case 't':
tabsize = PrtWidth;
s_charcpy (&tabchar, (tFormatChar)va_arg (argptr, int));
copyBufLen = 0;
PrtFlags |= PRINTFLAGS_READY;
break;
case 'd':
case 'i':
copyBufLen = i_Print_ltoa (©Buf,
(CheckBitsOne (PrtFlags, PRINTFLAGS_LARGE) ? va_arg (argptr, tUInt64) :
(CheckBitsOne (PrtFlags, PRINTFLAGS_SHORT) ? (tUInt16)va_arg (argptr, int) :
va_arg (argptr, tUInt))),
-10, &PrtWidth, PrtFlags);
if (!copyBufLen) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
PrtFlags &= ~(PRINTFLAGS_LARGE | PRINTFLAGS_SHORT);
PrtFlags |= PRINTFLAGS_READY;
break;
case 'u':
copyBufLen = i_Print_ltoa (©Buf,
(CheckBitsOne (PrtFlags, PRINTFLAGS_LARGE) ? va_arg (argptr, tUInt64) :
(CheckBitsOne (PrtFlags, PRINTFLAGS_SHORT) ? (tUInt16)va_arg (argptr, int) :
va_arg (argptr, tUInt))),
10, &PrtWidth, PrtFlags);
if (!copyBufLen) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
PrtFlags &= ~(PRINTFLAGS_LARGE | PRINTFLAGS_SHORT);
PrtFlags |= PRINTFLAGS_READY;
break;
case 'o':
copyBufLen = i_Print_ltoa (©Buf,
(CheckBitsOne (PrtFlags, PRINTFLAGS_LARGE) ? va_arg (argptr, tUInt64) :
(CheckBitsOne (PrtFlags, PRINTFLAGS_SHORT) ? (tUInt16)va_arg (argptr, int) :
va_arg (argptr, tUInt))),
8, &PrtWidth, PrtFlags);
if (!copyBufLen) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
PrtFlags &= ~(PRINTFLAGS_LARGE | PRINTFLAGS_SHORT);
PrtFlags |= PRINTFLAGS_READY;
break;
case 'x':
copyBufLen = i_Print_ltoa (©Buf,
(CheckBitsOne (PrtFlags, PRINTFLAGS_LARGE) ? va_arg (argptr, tUInt64) :
(CheckBitsOne (PrtFlags, PRINTFLAGS_SHORT) ? (tUInt16)va_arg (argptr, int) :
va_arg (argptr, tUInt))),
0x10, &PrtWidth, PrtFlags);
if (!copyBufLen) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
PrtFlags &= ~(PRINTFLAGS_SHORT | PRINTFLAGS_LARGE);
PrtFlags |= PRINTFLAGS_READY;
break;
case 'X':
copyBufLen = i_Print_ltoa (©Buf,
(CheckBitsOne (PrtFlags, PRINTFLAGS_LARGE) ? va_arg (argptr, tUInt64) :
(CheckBitsOne (PrtFlags, PRINTFLAGS_SHORT) ? (tUInt16)va_arg (argptr, int) :
va_arg (argptr, tUInt))),
- 0x10, &PrtWidth, PrtFlags);
if (!copyBufLen) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
PrtFlags &= ~(PRINTFLAGS_SHORT | PRINTFLAGS_LARGE);
PrtFlags |= PRINTFLAGS_READY;
break;
case 'c':
if ((PrtFlags & PRINTFLAGS_QUESTION) && (va_arg (argptr, tUInt) > 1)) {
PrtFlags |= PRINTFLAGS_LARGE;
}
if (PrtFlags & PRINTFLAGS_LARGE) {
*((tUWiChar *)copyBuf) = (tUWiChar)va_arg (argptr, int);
} else if (PrtFlags & PRINTFLAGS_SHORT) {
*((tUChar *)copyBuf) = (tUChar)va_arg (argptr, int);
} else {
copyBuf[0] = (tFormatChar)va_arg (argptr, int);
}
copyBufLen = 1;
PrtFlags |= PRINTFLAGS_READY;
break;
case 's':
if ((PrtFlags & PRINTFLAGS_QUESTION) && (va_arg (argptr, tUInt) > 1)) {
PrtFlags |= PRINTFLAGS_LARGE;
}
copyBuf = va_arg (argptr, tFormatChar *);
if (copyBuf) {
if (PrtFlags & PRINTFLAGS_LARGE) {
copyBufLen = check_strlen ((tUWiChar *)copyBuf);
} else if (PrtFlags & PRINTFLAGS_SHORT) {
copyBufLen = check_strlen ((tUChar *)copyBuf);
} else {
copyBufLen = check_strlen (copyBuf);
}
} else {
s_strncpy (localBuf, "[NULL]", 7);
copyBufLen = 6;
}
PrtFlags |= PRINTFLAGS_READY;
break;
case 'S':
{
CConstString *pStr = va_arg (argptr, CConstString *);
if (pStr) {
copyBuf = (tFormatChar *)pStr->GetPointer();
copyBufLen = pStr->GetLen();
PrtFlags |= PRINTFLAGS_STRINGTYPE;
} else {
s_strncpy (localBuf, "[NULL]", 7);
copyBufLen = 6;
}
PrtFlags |= PRINTFLAGS_READY;
}
break;
case 'p':
copyBufLen = a_pointer2string (va_arg (argptr, void *), copyBuf);
PrtFlags |= PRINTFLAGS_READY;
break;
/*----- Found PRECISION -----*/
case '.':
if (PrtPreci == -2) {
PrtPreci = -1;
break;
}
/*----- no Break! -----*/
/*----- OTHERS: wrong placeholder -----*/
default:
copyBuf = savepos;
copyBufLen = (tUInt)(format - savepos + 1);
PrtFlags |= PRINTFLAGS_READY;
break;
} //end: switch (*format)
if (PrtFlags & PRINTFLAGS_READY) {
// placeholder found
if (copyBufLen > 0) {
if (!(PrtFlags & PRINTFLAGS_MINUS) && (PrtWidth > copyBufLen)) {
PrtWidth -= copyBufLen;
if (FillAppend ((PrtFlags & PRINTFLAGS_ZERO) ? '0' : ' ', PrtWidth) == vFalse) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
PrtWidth = 0;
}
tUInt endPos = curLen + copyBufLen;
const tUInt max_space_needed = endPos + ((PrtFlags & PRINTFLAGS_SHELLESCAPE) ? copyBufLen : 0);
if (max_space_needed >= GetMaxSize()) {
if (DynExpand (max_space_needed + 1) == vFalse) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
}
dassert (max_space_needed < GetMaxSize());
dassert (pntr != 0);
if (PrtFlags & PRINTFLAGS_SHELLESCAPE) {
const tChar * const c_bs = (tChar*) "\\";
const tChar * const c_0 = (tChar*) "\0";
const tChar * const c_dq = (tChar*) "\"";
const tChar * const c_d = (tChar*) "$";
const tChar * const c_bq = (tChar*) "`";
const tChar * const c_qm = (tChar*) "?";
// Note that in this case, PrtWidth refers to the string _after_
// interpretation by /bin/sh, i.e. after the escaping is removed.
if (PrtFlags & PRINTFLAGS_STRINGTYPE) {
const tStringChar *p = (tStringChar*)copyBuf;
const tStringChar * const p_end = p + copyBufLen;
for (;p < p_end; ++p) {
if (!s_strncmp(p,c_0,1)) {
s_strncpy (pntr + curLen, c_qm, 1);
} else {
if (!(s_strncmp(p,c_dq,1) &&
s_strncmp(p,c_bs,1) &&
s_strncmp(p,c_d ,1) &&
s_strncmp(p,c_bq,1))) {
s_strncpy (pntr + curLen, c_bs, 1);
++curLen;
++endPos;
}
s_strncpy (pntr + curLen, p, 1);
}
++curLen;
}
} else if (PrtFlags & PRINTFLAGS_LARGE) {
const tUWiChar *p = (tUWiChar*)copyBuf;
const tUWiChar * const p_end = p + copyBufLen;
for (;p < p_end; ++p) {
if (!s_strncmp(p,c_0,1)) {
s_strncpy (pntr + curLen, c_qm, 1);
} else {
if (!(s_strncmp(p,c_dq,1) &&
s_strncmp(p,c_bs,1) &&
s_strncmp(p,c_d,1) &&
s_strncmp(p,c_bq,1))) {
s_strncpy (pntr + curLen, c_bs, 1);
++curLen;
++endPos;
}
s_strncpy (pntr + curLen, p, 1);
}
++curLen;
}
} else if (PrtFlags & PRINTFLAGS_SHORT) {
const tUChar *p = (tUChar*)copyBuf;
const tUChar * const p_end = p + copyBufLen;
for (;p < p_end; ++p) {
if (!s_strncmp(p,c_0,1)) {
s_strncpy (pntr + curLen, c_qm, 1);
} else {
if (!(s_strncmp(p,c_dq,1) &&
s_strncmp(p,c_bs,1) &&
s_strncmp(p,c_d,1) &&
s_strncmp(p,c_bq,1))) {
s_strncpy (pntr + curLen, c_bs, 1);
++curLen;
++endPos;
}
s_strncpy (pntr + curLen, p, 1);
}
++curLen;
}
} else {
dassert((sizeof tChar)== (sizeof tFormatChar));
const tChar *p = (tChar*)copyBuf;
const tChar * const p_end = p + copyBufLen;
for (;p < p_end; ++p) {
if (!s_strncmp(p,c_0,1)) {
s_strncpy (pntr + curLen, c_qm, 1);
} else {
if (!(s_strncmp(p,c_dq,1) &&
s_strncmp(p,c_bs,1) &&
s_strncmp(p,c_d,1) &&
s_strncmp(p,c_bq,1))) {
s_strncpy (pntr + curLen, c_bs, 1);
++curLen;
++endPos;
}
s_strncpy (pntr + curLen, p, 1);
}
++curLen;
}
}
} else {
if (PrtFlags & PRINTFLAGS_STRINGTYPE) {
s_strncpy (pntr + curLen, (tStringChar *)copyBuf, copyBufLen);
} else if (PrtFlags & PRINTFLAGS_LARGE) {
s_strncpy (pntr + curLen, (tUWiChar *)copyBuf, copyBufLen);
} else if (PrtFlags & PRINTFLAGS_SHORT) {
s_strncpy (pntr + curLen, (tUChar *)copyBuf, copyBufLen);
} else {
s_strncpy (pntr + curLen, copyBuf, copyBufLen);
}
}
pntr[endPos] = '\0';
curLen = endPos;
if ((PrtWidth > copyBufLen) && (FillAppend (' ', PrtWidth - copyBufLen) == vFalse)) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
}
PrtFlags = 0;
PrtWidth = 0;
PrtPreci = -2;
copyBuf = &localBuf[0];
copyBufLen = 0;
savepos = format + 1;
}
} else {
switch (*format) {
case '%':
//ASSERT (PrtFlags == 0);
if ((format > savepos) && (Append (savepos, (tUInt)(format - savepos)) == vFalse)) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
if (format[1] == '%') {
// found %% => eliminate one %
format++;
savepos = format;
} else {
savepos = format;
PrtFlags = PRINTFLAGS_ACTIVE;
}
break;
case '\n':
if ((tabsize > 0) && (tabchar > 0)) {
if ( ( (format > savepos)
&& (Append (savepos, (tUInt)(format - savepos)) == vFalse))
|| (FillAppend (tabchar, tabsize) == vFalse)) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
savepos = format + 1;
}
break;
}
}
format++;
} //end: while
if ((format > savepos) && (Append (savepos, (tUInt)(format - savepos)) == vFalse)) {
RETURN ('x', CSTRING_PRINT_MemoryError);
}
dassert (GetLen() >= startLen);
RETURN ('x', GetLen() - startLen);
}
/*===========================================================================*\
\*===========================================================================*/
tUInt CDynamicString::i_Print_ltoa (tFormatChar **pBuffer, tUInt64 value, tSByte radix, tUInt *pWidth, tUInt Flags) {
dhead ("CDynamicString::i_Print_ltoa", DCON_CDynamicString);
dparams ("r=%x,w=%x,f=%x", radix, *pWidth, Flags);
dassert (pBuffer != 0);
dassert (*pBuffer != 0);
dassert (pWidth != 0);
dassert (radix <= 16);
dassert (radix >= -16);
dassert (CSTRING_PRINT_BufferSize > sizeof (tUInt64) * 8 + 3);
static tFormatChar LowDigitForStrings[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'x'
};
static tFormatChar HighDigitForStrings[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'X'
};
tFormatChar *DigitForStrings = &LowDigitForStrings[0];
tFormatChar *buffer = *pBuffer + CSTRING_PRINT_BufferSize - 1;
tFormatChar prechar = 0;
tUInt prelen = 0;
tUInt bufpos = 0;
if (Flags & PRINTFLAGS_PLUS) {
prechar = '+';
} else if (Flags & PRINTFLAGS_BLANK) {
prechar = ' ';
}
switch (radix) {
case -10:
if ((tSInt64)value < 0) {
prechar = '-';
value *= -1;
}
radix *= -1;
break;
case -0x10:
DigitForStrings = &HighDigitForStrings[0];
radix *= -1;
break;
}
dassert (radix > 1);
tUInt64 orgval;
while (value >= (tUInt64) radix) {
orgval = value;
//value /= radix;
Special64BitDiv (&value, (tUByte)radix);
dassert ((tUInt)(orgval - value * radix) < 0x10);
*buffer-- = DigitForStrings[(tUByte)(orgval - value * radix)];
bufpos++;
}
bufpos++;
*buffer = DigitForStrings[value];
if (Flags & PRINTFLAGS_HASH) {
switch (radix) {
case 16:
*--buffer = DigitForStrings[0x10]; // x or X
prelen++;
// no break!
case 8:
*--buffer = '0';
prelen++;
break;
}
}
if (prechar) {
*--buffer = prechar;
prelen++;
}
bufpos += prelen;
if (prelen && ((Flags & (PRINTFLAGS_MINUS | PRINTFLAGS_ZERO)) == PRINTFLAGS_ZERO) && (*pWidth > bufpos)) {
// PRINTFLAGS_MINUS is not set and PRINTFLAGS_ZERO ist set
if (Append (buffer, prelen) == vTrue) {
buffer += prelen;
bufpos -= prelen;
*pWidth -= prelen;
} else {
bufpos = 0;
}
prelen = 0;
}
*pBuffer = buffer;
return bufpos;
}
/*===========================================================================*\
\*===========================================================================*/
|