Ignore:
Timestamp:
Mar 10, 2001, 12:58:28 AM (24 years ago)
Author:
umoeller
Message:

Misc. changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/xstring.c

    r43 r45  
    125125 *
    126126 *      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 *
    128131 *      This is useful if you plan to add more stuff to
    129132 *      the string later so we don't have to reallocate
     
    135138 *
    136139 *@@added V0.9.6 (2000-11-01) [umoeller]
     140 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta
    137141 */
    138142
     
    147151                // ulLength is still zero
    148152        *(pxstr->psz) = 0;
    149     }
     153
     154        pxstr->ulDelta = ulPreAllocate * 10 / 100;
     155    }
     156    // else: pxstr->ulDelta is still 0
    150157}
    151158
     
    170177 *
    171178 *@@added V0.9.6 (2000-11-01) [umoeller]
     179 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta
    172180 */
    173181
     
    175183                 PSZ pszNew)
    176184{
    177     pxstr->psz = pszNew;
    178185    if (!pszNew)
    179186    {
    180         pxstr->cbAllocated = 0;
    181         pxstr->ulLength = 0;
     187        memset(pxstr, 0, sizeof(XSTRING));
    182188    }
    183189    else
    184190    {
     191        pxstr->psz = pszNew;
    185192        pxstr->ulLength = strlen(pszNew);
    186193        pxstr->cbAllocated = pxstr->ulLength + 1;
     194        pxstr->ulDelta = pxstr->ulLength  * 10 / 100;
    187195    }
    188196}
     
    209217 *@@added V0.9.6 (2000-11-01) [umoeller]
    210218 *@@changed V0.9.7 (2000-12-31) [umoeller]: added ulExtraAllocate
     219 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta
    211220 */
    212221
     
    229238                pxstr->psz = (PSZ)malloc(pxstr->cbAllocated);
    230239                strcpy(pxstr->psz, pcszSource);
     240
     241                pxstr->ulDelta = pxstr->cbAllocated * 10 / 100;
    231242            }
    232243        }
     
    265276 *      this function does nothing.
    266277 *
     278 *      pxstr->ulDelta has no effect here.
     279 *
    267280 *      The XSTRING must be initialized before the
    268281 *      call.
     
    271284 *
    272285 *@@added V0.9.7 (2001-01-07) [umoeller]
     286 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using ulDelta
    273287 */
    274288
     
    282296        // we need more memory than we have previously
    283297        // 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        }
    289314        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;
    297323                // ulLength is unchanged
    298324    }
     325    // else: we have enough memory
    299326
    300327    return (pxstr->cbAllocated);
     
    369396        pxstr->ulLength = strlen(pszNew);
    370397        pxstr->cbAllocated = pxstr->ulLength + 1;
     398
     399        pxstr->ulDelta = pxstr->cbAllocated * 10 / 100;
    371400    }
    372401    // else null string: cbAllocated and ulLength are 0 already
     
    382411 *      If pxstr contains something, its contents are destroyed.
    383412 *
    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.
    393427 *
    394428 *      Returns the length of the new string (excluding the null
     
    413447 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
    414448 *@@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
    415450 */
    416451
     
    419454              ULONG ulSourceLength)         // in: length of pcszSource or 0
    420455{
    421     // xstrClear(pxstr);        NOOOO! this frees the string, we want to keep the memory
    422 
    423456    if (!pxstr)
    424457        return (0);         // V0.9.9 (2001-02-14) [umoeller]
     
    437470    {
    438471        // 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);
    457475
    458476        memcpy(pxstr->psz,
     
    544562 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
    545563 *@@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
    546565 */
    547566
     
    564583
    565584                // 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);
    593588
    594589                // 2) append source string:
     
    645640 *
    646641 *@@added V0.9.7 (2000-12-10) [umoeller]
     642 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
    647643 */
    648644
     
    656652        // ULONG   ulSourceLength = 1;
    657653        // 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
    688659        // 2) append character:
    689660        pxstr->psz[pxstr->ulLength] = c;
     
    691662
    692663        // in all cases, set new length
    693         pxstr->ulLength++;
     664        (pxstr->ulLength)++;
    694665        ulrc = pxstr->ulLength;
    695666
     
    712683        return (0);
    713684
    714     return (xstrcat(pxstr, pcstrSource->psz, pcstrSource->ulLength));
     685    return (xstrcat(pxstr,
     686                    pcstrSource->psz,
     687                    pcstrSource->ulLength));
    715688}
    716689
     
    753726 *@@changed V0.9.9 (2001-01-29) [lafaix]: fixed unnecessary allocation when pxstr was big enough
    754727 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
     728 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
    755729 */
    756730
     
    772746                    // can be 0!
    773747
    774         // length of new string
     748        // size of new buffer:
    775749        ULONG   cbNeeded = pxstr->ulLength
    776750                         + cReplaceLen
     
    778752                         + 1;                  // null terminator
    779753        // offset where pszSearch was found
    780                 // ulFirstReplOfs = pFound - pxstr->psz; now ulFirstReplOfs
    781754        PSZ     pFound = pxstr->psz + ulFirstReplOfs;
    782755
    783756        // now check if we have enough memory...
    784         if (pxstr->cbAllocated < cbNeeded)
     757        if (cbNeeded > pxstr->cbAllocated)
    785758        {
    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;
    787782            // allocate new buffer
    788             PSZ pszNew = (PSZ)malloc(cbNeeded);
     783            pszNew = (PSZ)malloc(cbAllocate);
     784            // end V0.9.9 (2001-03-07) [umoeller]
    789785
    790786            if (ulFirstReplOfs)
     
    824820            pxstr->psz = pszNew;
    825821            pxstr->ulLength = cbNeeded - 1;
    826             pxstr->cbAllocated = cbNeeded;
     822            pxstr->cbAllocated = cbAllocate; // V0.9.9 (2001-03-07) [umoeller]
    827823        } // end if (pxstr->cbAllocated < cbNeeded)
    828824        else
     
    13551351    ULONG   ulOfs = 0;
    13561352
    1357     xstrInit(&str, 100);
     1353    xstrInit(&str, 0);
    13581354    xstrInit(&strFind, 0);
    13591355    xstrInit(&strReplace, 0);
     1356
     1357    str.ulDelta = 50;
    13601358
    13611359    xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0);
     
    13631361    xstrcpy(&strReplace, "Dummy", 0);
    13641362
    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);
    13661364
    13671365    printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
     
    13761374        ;
    13771375
    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);
    13791381
    13801382    xstrcpy(&strFind, strReplace.psz, 0);
     
    13921394        ;
    13931395
    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);
    13951397
    13961398    xstrcpy(&strFind, " ", 0);
     
    14081410        ;
    14091411
    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);
    14111413
    14121414    xstrcpy(&strFind, ".", 0);
     
    14241426        ;
    14251427
    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);
    14321429
    14331430    xstrcpy(&strFind, "..........", 0);
     
    14451442        ;
    14461443
    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);
    14481445
    14491446    printf("Encoding @* chars.\n");
    14501447    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);
    14521449
    14531450    printf("Decoding @* chars.\n");
    14541451    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);
    14561453
    14571454    return (0);
    1458 }
    1459 
    1460 */
     1455} */
     1456
     1457
Note: See TracChangeset for help on using the changeset viewer.