Ignore:
Timestamp:
Feb 17, 2001, 3:03:14 PM (25 years ago)
Author:
umoeller
Message:

Updates to XML.

File:
1 edited

Legend:

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

    r32 r38  
    346346 *
    347347 *@@added V0.9.6 (2000-11-01) [umoeller]
     348 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
    348349 */
    349350
     
    351352              PSZ pszNew)                   // in: heap PSZ to use
    352353{
     354    if (!pxstr)
     355        return (0);         // V0.9.9 (2001-02-14) [umoeller]
     356
    353357    xstrClear(pxstr);
    354358    pxstr->psz = pszNew;
     
    391395 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
    392396 *@@changed V0.9.9 (2001-01-28) [lafaix]: fixed memory leak and NULL source behavior
     397 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
    393398 */
    394399
     
    399404    // xstrClear(pxstr);        NOOOO! this frees the string, we want to keep the memory
    400405
    401     if (pxstr)
    402     {
    403         if (pcszSource)
     406    if (!pxstr)
     407        return (0);         // V0.9.9 (2001-02-14) [umoeller]
     408
     409    if (pcszSource)
     410    {
     411        // source specified:
     412        if (ulSourceLength == 0)
     413            // but not length:
     414            ulSourceLength = strlen(pcszSource);
     415    }
     416    else
     417        ulSourceLength = 0;
     418
     419    if (ulSourceLength)
     420    {
     421        // we do have a source string:
     422        ULONG cbNeeded = ulSourceLength + 1;
     423        if (cbNeeded > pxstr->cbAllocated)
    404424        {
    405             // source specified:
    406             if (ulSourceLength == 0)
    407                 // but not length:
    408                 ulSourceLength = strlen(pcszSource);
     425            // we need more memory than we have previously
     426            // allocated:
     427            if (pxstr->psz)
     428                free(pxstr->psz); // V0.9.9 (2001-01-28) [lafaix]
     429            pxstr->cbAllocated = cbNeeded;
     430            pxstr->psz = (PSZ)malloc(cbNeeded);
    409431        }
    410         else
    411             ulSourceLength = 0;
    412 
    413         if (ulSourceLength)
    414         {
    415             // we do have a source string:
    416             ULONG cbNeeded = ulSourceLength + 1;
    417             if (cbNeeded > pxstr->cbAllocated)
    418             {
    419                 // we need more memory than we have previously
    420                 // allocated:
    421                 if (pxstr->psz)
    422                     free(pxstr->psz); // V0.9.9 (2001-01-28) [lafaix]
    423                 pxstr->cbAllocated = cbNeeded;
    424                 pxstr->psz = (PSZ)malloc(cbNeeded);
    425             }
    426             // else: we have enough memory
    427 
    428             // strcpy(pxstr->psz, pcszSource);
    429             memcpy(pxstr->psz,
    430                    pcszSource,
    431                    ulSourceLength + 1); // V0.9.9 (2001-01-31) [umoeller]
    432         }
    433         else
    434         {
    435             // no source specified or source is empty:
    436             if (pxstr->cbAllocated)
    437                 // we did have a string: set to empty,
    438                 // but leave allocated memory intact
    439                 *(pxstr->psz) = 0;
    440             // else
    441                 // we had no string previously: in that case
    442                 // psz and ulLength and cbAllocated are all still NULL
    443         }
    444 
    445         // in all cases, set new length
    446         pxstr->ulLength = ulSourceLength;
    447     }
     432        // else: we have enough memory
     433
     434        // strcpy(pxstr->psz, pcszSource);
     435        memcpy(pxstr->psz,
     436               pcszSource,
     437               ulSourceLength + 1); // V0.9.9 (2001-01-31) [umoeller]
     438    }
     439    else
     440    {
     441        // no source specified or source is empty:
     442        if (pxstr->cbAllocated)
     443            // we did have a string: set to empty,
     444            // but leave allocated memory intact
     445            *(pxstr->psz) = 0;
     446        // else
     447            // we had no string previously: in that case
     448            // psz and ulLength and cbAllocated are all still NULL
     449    }
     450
     451    // in all cases, set new length
     452    pxstr->ulLength = ulSourceLength;
    448453
    449454    return (pxstr->ulLength);
     455}
     456
     457/*
     458 *@@ xstrcpys:
     459 *      shortcut to xstrcpy if the source is an XSTRING also.
     460 *
     461 *@@added V0.9.9 (2001-02-14) [umoeller]
     462 */
     463
     464ULONG xstrcpys(PXSTRING pxstr,
     465               const XSTRING *pcstrSource)
     466{
     467    if (!pcstrSource)
     468        return (0);
     469
     470    return (xstrcpy(pxstr, pcstrSource->psz, pcstrSource->ulLength));
    450471}
    451472
     
    634655
    635656/*
     657 *@@ xstrcats:
     658 *      shortcut to xstrcat if the source is an XSTRING also.
     659 *
     660 *@@added V0.9.9 (2001-02-14) [umoeller]
     661 */
     662
     663ULONG xstrcats(PXSTRING pxstr,
     664               const XSTRING *pcstrSource)
     665{
     666    if (!pcstrSource)
     667        return (0);
     668
     669    return (xstrcat(pxstr, pcstrSource->psz, pcstrSource->ulLength));
     670}
     671
     672/*
    636673 *@@ xstrrpl:
    637674 *      replaces cReplLen characters in pxstr, starting
     
    666703 *@@added V0.9.7 (2001-01-15) [umoeller]
    667704 *@@changed V0.9.9 (2001-01-29) [lafaix]: fixed unnecessary allocation when pxstr was big enough
     705  *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
    668706 */
    669707
     
    676714
    677715    // security checks...
    678     if (    (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
     716    if (    (pxstr)         // V0.9.9 (2001-02-14) [umoeller]
     717         && (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
    679718         && (pstrReplaceWith)
    680719         // && (pstrReplaceWith->ulLength)      no, this can be empty
     
    747786            ULONG   cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen;
    748787
    749             // first, we move the end to its new location (memmove
    750             // handles overlap if needed)
     788            // first, we move the end to its new location
     789            // (memmove handles overlap if needed)
    751790            memmove(pFound + cReplaceLen,
    752791                    pFound + cReplLen,
     
    787826 *
    788827 *@@added V0.9.6 (2000-11-12) [umoeller]
     828 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL string crashs
    789829 */
    790830
     
    798838{
    799839    PSZ     pReturn = 0;
    800     ULONG   ulFoundLen = pstrFind->ulLength;
    801 
    802     if ((pxstr->ulLength) && (ulFoundLen))
    803     {
    804         const char *p = pxstr->psz + ulOfs;
    805 
    806         do  // while p
     840
     841    if (pxstr && pstrFind)      // V0.9.9 (2001-02-14) [umoeller]
     842    {
     843        ULONG   ulFoundLen = pstrFind->ulLength;
     844
     845        if ((pxstr->ulLength) && (ulFoundLen))
    807846        {
    808             // p = strstr(p, pstrFind->psz);
    809             p = (PSZ)strhmemfind(p,         // in: haystack
    810                                  pxstr->ulLength - (p - pxstr->psz),
    811                                             // remaining length of haystack
    812                                  pstrFind->psz,
    813                                  ulFoundLen,
    814                                  pShiftTable,
    815                                  pfRepeatFind);
    816             if (p)
     847            const char *p = pxstr->psz + ulOfs;
     848
     849            do  // while p
    817850            {
    818                 // string found:
    819                 // check if that's a word
    820 
    821                 if (strhIsWord(pxstr->psz,
    822                                p,
    823                                ulFoundLen,
    824                                pcszBeginChars,
    825                                pcszEndChars))
     851                p = (PSZ)strhmemfind(p,         // in: haystack
     852                                     pxstr->ulLength - (p - pxstr->psz),
     853                                                // remaining length of haystack
     854                                     pstrFind->psz,
     855                                     ulFoundLen,
     856                                     pShiftTable,
     857                                     pfRepeatFind);
     858                if (p)
    826859                {
    827                     // valid end char:
    828                     pReturn = (PSZ)p;
    829                     break;
     860                    // string found:
     861                    // check if that's a word
     862
     863                    if (strhIsWord(pxstr->psz,
     864                                   p,
     865                                   ulFoundLen,
     866                                   pcszBeginChars,
     867                                   pcszEndChars))
     868                    {
     869                        // valid end char:
     870                        pReturn = (PSZ)p;
     871                        break;
     872                    }
     873
     874                    p += ulFoundLen;
    830875                }
    831 
    832                 p += ulFoundLen;
    833             }
    834         } while (p);
    835 
    836     }
     876            } while (p);
     877
     878        }
     879    }
     880
    837881    return (pReturn);
    838882}
Note: See TracChangeset for help on using the changeset viewer.