Changeset 45 for trunk/src/helpers/xstring.c
- Timestamp:
- Mar 10, 2001, 12:58:28 AM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/xstring.c
r43 r45 125 125 * 126 126 * If (ulPreAllocate != 0), memory is pre-allocated 127 * for the string, but the string will be empty. 127 * for the string, but the string will be empty 128 * (its first byte is set to '\0'). In addition, 129 * pxstr->ulDelta will be set to 10% of ulPreAllocate. 130 * 128 131 * This is useful if you plan to add more stuff to 129 132 * the string later so we don't have to reallocate … … 135 138 * 136 139 *@@added V0.9.6 (2000-11-01) [umoeller] 140 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta 137 141 */ 138 142 … … 147 151 // ulLength is still zero 148 152 *(pxstr->psz) = 0; 149 } 153 154 pxstr->ulDelta = ulPreAllocate * 10 / 100; 155 } 156 // else: pxstr->ulDelta is still 0 150 157 } 151 158 … … 170 177 * 171 178 *@@added V0.9.6 (2000-11-01) [umoeller] 179 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta 172 180 */ 173 181 … … 175 183 PSZ pszNew) 176 184 { 177 pxstr->psz = pszNew;178 185 if (!pszNew) 179 186 { 180 pxstr->cbAllocated = 0; 181 pxstr->ulLength = 0; 187 memset(pxstr, 0, sizeof(XSTRING)); 182 188 } 183 189 else 184 190 { 191 pxstr->psz = pszNew; 185 192 pxstr->ulLength = strlen(pszNew); 186 193 pxstr->cbAllocated = pxstr->ulLength + 1; 194 pxstr->ulDelta = pxstr->ulLength * 10 / 100; 187 195 } 188 196 } … … 209 217 *@@added V0.9.6 (2000-11-01) [umoeller] 210 218 *@@changed V0.9.7 (2000-12-31) [umoeller]: added ulExtraAllocate 219 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta 211 220 */ 212 221 … … 229 238 pxstr->psz = (PSZ)malloc(pxstr->cbAllocated); 230 239 strcpy(pxstr->psz, pcszSource); 240 241 pxstr->ulDelta = pxstr->cbAllocated * 10 / 100; 231 242 } 232 243 } … … 265 276 * this function does nothing. 266 277 * 278 * pxstr->ulDelta has no effect here. 279 * 267 280 * The XSTRING must be initialized before the 268 281 * call. … … 271 284 * 272 285 *@@added V0.9.7 (2001-01-07) [umoeller] 286 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using ulDelta 273 287 */ 274 288 … … 282 296 // we need more memory than we have previously 283 297 // allocated: 284 if (pxstr->cbAllocated) 285 // appendee already had memory: 286 // reallocate 287 pxstr->psz = (PSZ)realloc(pxstr->psz, 288 cbNeeded); 298 ULONG cbAllocate; 299 if (pxstr->ulDelta) 300 { 301 // delta specified: allocate in chunks of that 302 // V0.9.9 (2001-03-07) [umoeller] 303 ULONG cbExtra = cbNeeded - pxstr->cbAllocated; 304 cbExtra = ( (cbExtra + pxstr->ulDelta) 305 / pxstr->ulDelta 306 ) 307 * pxstr->ulDelta; 308 // if we need 3 extra bytes and ulDelta is 10, 309 // this gives us 10 extra bytes 310 // if we need 3 extra bytes and ulDelta is 1000, 311 // this gives us 1000 extra bytes 312 cbAllocate = pxstr->cbAllocated + cbExtra; 313 } 289 314 else 290 { 291 // appendee has no memory: 292 pxstr->psz = (PSZ)malloc(cbNeeded); 293 *(pxstr->psz) = 0; 294 } 295 296 pxstr->cbAllocated = cbNeeded; 315 // no delta specified: 316 cbAllocate = cbNeeded; 317 // V0.9.9 (2001-03-05) [umoeller]: use realloc; 318 // this gives the C runtime a chance to expand the 319 // existing block 320 pxstr->psz = (PSZ)realloc(pxstr->psz, cbAllocate); 321 // if pxstr->psz is NULL, realloc behaves like malloc 322 pxstr->cbAllocated = cbAllocate; 297 323 // ulLength is unchanged 298 324 } 325 // else: we have enough memory 299 326 300 327 return (pxstr->cbAllocated); … … 369 396 pxstr->ulLength = strlen(pszNew); 370 397 pxstr->cbAllocated = pxstr->ulLength + 1; 398 399 pxstr->ulDelta = pxstr->cbAllocated * 10 / 100; 371 400 } 372 401 // else null string: cbAllocated and ulLength are 0 already … … 382 411 * If pxstr contains something, its contents are destroyed. 383 412 * 384 * With ulSourceLength, specify the length of pcszSource. 385 * If you specify 0, this function will run strlen(pcszSource) 386 * itself. 387 * 388 * If you already know the length of pcszSource, you can 389 * speed this function up a bit this way. 390 * 391 * You are required to specify ulSourceLength if you only want 392 * to copy a substring, or pcszSource is not zero-terminated. 413 * With ulSourceLength, specify the length of pcszSource 414 * or 0. 415 * 416 * -- If you specify 0, this function will run 417 * strlen(pcszSource) and copy the entire source 418 * string. 419 * 420 * -- If you already know the length of pcszSource, you 421 * can speed this function up by specifying the 422 * length. 423 * 424 * -- You are required to specify ulSourceLength if you 425 * only want to copy a substring, or if pcszSource is 426 * not zero-terminated. 393 427 * 394 428 * Returns the length of the new string (excluding the null … … 413 447 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash 414 448 *@@changed V0.9.9 (2001-02-16) [umoeller]: now supporting non-zero-terminated pcszSource 449 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve 415 450 */ 416 451 … … 419 454 ULONG ulSourceLength) // in: length of pcszSource or 0 420 455 { 421 // xstrClear(pxstr); NOOOO! this frees the string, we want to keep the memory422 423 456 if (!pxstr) 424 457 return (0); // V0.9.9 (2001-02-14) [umoeller] … … 437 470 { 438 471 // we do have a source string: 439 ULONG cbNeeded = ulSourceLength + 1; 440 if (cbNeeded > pxstr->cbAllocated) 441 { 442 // we need more memory than we have previously 443 // allocated: 444 /* if (pxstr->psz) 445 free(pxstr->psz); // V0.9.9 (2001-01-28) [lafaix] 446 pxstr->cbAllocated = cbNeeded; 447 pxstr->psz = (PSZ)malloc(cbNeeded); */ 448 449 // V0.9.9 (2001-03-05) [umoeller]: use realloc; 450 // this gives the C runtime a chance to expand the 451 // existing block 452 pxstr->psz = (PSZ)realloc(pxstr->psz, cbNeeded); 453 // if pxstr->psz is NULL, realloc behaves like malloc 454 pxstr->cbAllocated = cbNeeded; 455 } 456 // else: we have enough memory 472 xstrReserve(pxstr, 473 // required memory: 474 ulSourceLength + 1); 457 475 458 476 memcpy(pxstr->psz, … … 544 562 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength 545 563 *@@changed V0.9.9 (2001-02-16) [umoeller]: now supporting non-zero-terminated pcszSource 564 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve 546 565 */ 547 566 … … 564 583 565 584 // 1) memory management 566 ULONG cbNeeded = pxstr->ulLength + ulSourceLength + 1; 567 if (cbNeeded > pxstr->cbAllocated) 568 { 569 // we need more memory than we have previously 570 // allocated: 571 if (pxstr->cbAllocated) 572 // appendee already had memory: 573 // reallocate 574 pxstr->psz = (PSZ)realloc(pxstr->psz, 575 cbNeeded); 576 else 577 // appendee has no memory: 578 pxstr->psz = (PSZ)malloc(cbNeeded); 579 580 pxstr->cbAllocated = cbNeeded; 581 // ulLength is unchanged yet 582 } 583 // else: we have enough memory, both if appendee 584 // is empty or not empty 585 586 // now we have: 587 // -- if appendee (pxstr) had enough memory, no problem 588 // -- if appendee (pxstr) needed more memory 589 // -- and was not empty: pxstr->psz now points to a 590 // reallocated copy of the old string 591 // -- and was empty: pxstr->psz now points to a 592 // new (unitialized) buffer 585 xstrReserve(pxstr, 586 // required memory: 587 pxstr->ulLength + ulSourceLength + 1); 593 588 594 589 // 2) append source string: … … 645 640 * 646 641 *@@added V0.9.7 (2000-12-10) [umoeller] 642 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve 647 643 */ 648 644 … … 656 652 // ULONG ulSourceLength = 1; 657 653 // 1) memory management 658 ULONG cbNeeded = pxstr->ulLength // existing length, without null terminator 659 + 1 // new character 660 + 1; // null terminator 661 if (cbNeeded > pxstr->cbAllocated) 662 { 663 // we need more memory than we have previously 664 // allocated: 665 if (pxstr->cbAllocated) 666 // appendee already had memory: 667 // reallocate 668 pxstr->psz = (PSZ)realloc(pxstr->psz, 669 cbNeeded); 670 else 671 // appendee has no memory: 672 pxstr->psz = (PSZ)malloc(cbNeeded); 673 674 pxstr->cbAllocated = cbNeeded; 675 // ulLength is unchanged yet 676 } 677 // else: we have enough memory, both if appendee 678 // is empty or not empty 679 680 // now we have: 681 // -- if appendee (pxstr) had enough memory, no problem 682 // -- if appendee (pxstr) needed more memory 683 // -- and was not empty: pxstr->psz now points to a 684 // reallocated copy of the old string 685 // -- and was empty: pxstr->psz now points to a 686 // new (unitialized) buffer 687 654 xstrReserve(pxstr, 655 // required memory: 656 pxstr->ulLength // existing length, without null terminator 657 + 1 // new character 658 + 1); // null terminator 688 659 // 2) append character: 689 660 pxstr->psz[pxstr->ulLength] = c; … … 691 662 692 663 // in all cases, set new length 693 pxstr->ulLength++;664 (pxstr->ulLength)++; 694 665 ulrc = pxstr->ulLength; 695 666 … … 712 683 return (0); 713 684 714 return (xstrcat(pxstr, pcstrSource->psz, pcstrSource->ulLength)); 685 return (xstrcat(pxstr, 686 pcstrSource->psz, 687 pcstrSource->ulLength)); 715 688 } 716 689 … … 753 726 *@@changed V0.9.9 (2001-01-29) [lafaix]: fixed unnecessary allocation when pxstr was big enough 754 727 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash 728 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve 755 729 */ 756 730 … … 772 746 // can be 0! 773 747 774 // length of new string748 // size of new buffer: 775 749 ULONG cbNeeded = pxstr->ulLength 776 750 + cReplaceLen … … 778 752 + 1; // null terminator 779 753 // offset where pszSearch was found 780 // ulFirstReplOfs = pFound - pxstr->psz; now ulFirstReplOfs781 754 PSZ pFound = pxstr->psz + ulFirstReplOfs; 782 755 783 756 // now check if we have enough memory... 784 if ( pxstr->cbAllocated < cbNeeded)757 if (cbNeeded > pxstr->cbAllocated) 785 758 { 786 // no, we need more memory: 759 // we need more memory than we have previously 760 // allocated: 761 // reallocate using ulDelta V0.9.9 (2001-03-07) [umoeller] 762 ULONG cbAllocate; 763 PSZ pszNew; 764 if (pxstr->ulDelta) 765 { 766 // delta specified: allocate in chunks of that 767 // V0.9.9 (2001-03-07) [umoeller] 768 ULONG cbExtra = cbNeeded - pxstr->cbAllocated; 769 cbExtra = ( (cbExtra + pxstr->ulDelta) 770 / pxstr->ulDelta 771 ) 772 * pxstr->ulDelta; 773 // if we need 3 extra bytes and ulDelta is 10, 774 // this gives us 10 extra bytes 775 // if we need 3 extra bytes and ulDelta is 1000, 776 // this gives us 1000 extra bytes 777 cbAllocate = pxstr->cbAllocated + cbExtra; 778 } 779 else 780 // no delta specified: 781 cbAllocate = cbNeeded; 787 782 // allocate new buffer 788 PSZ pszNew = (PSZ)malloc(cbNeeded); 783 pszNew = (PSZ)malloc(cbAllocate); 784 // end V0.9.9 (2001-03-07) [umoeller] 789 785 790 786 if (ulFirstReplOfs) … … 824 820 pxstr->psz = pszNew; 825 821 pxstr->ulLength = cbNeeded - 1; 826 pxstr->cbAllocated = cb Needed;822 pxstr->cbAllocated = cbAllocate; // V0.9.9 (2001-03-07) [umoeller] 827 823 } // end if (pxstr->cbAllocated < cbNeeded) 828 824 else … … 1355 1351 ULONG ulOfs = 0; 1356 1352 1357 xstrInit(&str, 100);1353 xstrInit(&str, 0); 1358 1354 xstrInit(&strFind, 0); 1359 1355 xstrInit(&strReplace, 0); 1356 1357 str.ulDelta = 50; 1360 1358 1361 1359 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0); … … 1363 1361 xstrcpy(&strReplace, "Dummy", 0); 1364 1362 1365 printf("Old string is: \"%s\" (%d/%d )\n", str.psz, str.ulLength, str.cbAllocated);1363 printf("Old string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1366 1364 1367 1365 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); … … 1376 1374 ; 1377 1375 1378 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1376 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1377 1378 printf("Appending \"blah\".\n"); 1379 xstrcat(&str, "blah", 0); 1380 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1379 1381 1380 1382 xstrcpy(&strFind, strReplace.psz, 0); … … 1392 1394 ; 1393 1395 1394 printf("New string is: \"%s\" (%d/%d )\n", str.psz, str.ulLength, str.cbAllocated);1396 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1395 1397 1396 1398 xstrcpy(&strFind, " ", 0); … … 1408 1410 ; 1409 1411 1410 printf("New string is: \"%s\" (%d/%d )\n", str.psz, str.ulLength, str.cbAllocated);1412 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1411 1413 1412 1414 xstrcpy(&strFind, ".", 0); … … 1424 1426 ; 1425 1427 1426 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1427 1428 printf("Reserving extra mem.\n"); 1429 1430 xstrReserve(&str, 6000); 1431 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1428 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1432 1429 1433 1430 xstrcpy(&strFind, "..........", 0); … … 1445 1442 ; 1446 1443 1447 printf("New string is: \"%s\" (%d/%d )\n", str.psz, str.ulLength, str.cbAllocated);1444 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1448 1445 1449 1446 printf("Encoding @* chars.\n"); 1450 1447 xstrEncode(&str, "@*"); 1451 printf("New string is: \"%s\" (%d/%d )\n", str.psz, str.ulLength, str.cbAllocated);1448 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1452 1449 1453 1450 printf("Decoding @* chars.\n"); 1454 1451 xstrDecode(&str); 1455 printf("New string is: \"%s\" (%d/%d )\n", str.psz, str.ulLength, str.cbAllocated);1452 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta); 1456 1453 1457 1454 return (0); 1458 } 1459 1460 */ 1455 } */ 1456 1457
Note:
See TracChangeset
for help on using the changeset viewer.