Ignore:
Timestamp:
Dec 11, 2000, 8:54:20 AM (25 years ago)
Author:
umoeller
Message:

Coupla bugfixes.

File:
1 edited

Legend:

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

    r14 r15  
    186186 *      this can be used instead of xstrInit if you
    187187 *      want to initialize an XSTRING with a copy
    188  *      of an existing string.
     188 *      of an existing string. This is a shortcut
     189 *      for xstrInit() and then xstrcpy().
    189190 *
    190191 *      As opposed to xstrInitSet, this does create
     
    382383 *
    383384 *      Returns the length of the new string (excluding the null
    384  *      terminator), or null upon errors.
     385 *      terminator) if the string was changed, or 0 if nothing
     386 *      happened.
     387 *
     388 *      Note: To append a single character, xstrcatc is faster
     389 *      than xstrcat.
    385390 *
    386391 *      Example:
     
    391396 +          xstrcat(&str, "blup");
    392397 *
    393  *      After this, psz points to a new string containing
     398 *      After this, str.psz points to a new string containing
    394399 *      "blahblup".
    395400 *
     
    399404 *@@changed V0.9.3 (2000-05-11) [umoeller]: returned 0 if pszString was initially empty; fixed
    400405 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
     406 *@@changed V0.9.7 (2000-12-10) [umoeller]: return value was wrong
    401407 */
    402408
     
    452458            // in all cases, set new length
    453459            pxstr->ulLength += ulSourceLength;
    454             ulrc = ulSourceLength;
     460            ulrc = pxstr->ulLength;     // V0.9.7 (2000-12-10) [umoeller]
    455461
    456462        } // end if (ulSourceLength)
     
    458464        // do nothing
    459465    }
     466
     467    return (ulrc);
     468}
     469
     470/*
     471 *@@ xstrcatc:
     472 *      this is similar to xstrcat, except that this is
     473 *      for a single character. This is a bit faster than
     474 *      xstrcat.
     475 *
     476 *      If "c" is \0, nothing happens.
     477 *
     478 *      If pxstr is empty, this behaves just like xstrcpy.
     479 *
     480 *      Returns the length of the new string (excluding the null
     481 *      terminator) if the string was changed, or 0 if nothing
     482 *      happened.
     483 *
     484 *      Example:
     485 *
     486 +          XSTRING str;
     487 +          xstrInit(&str, 0);
     488 +          xstrcpy(&str, "blu");
     489 +          xstrcatc(&str, 'p');
     490 *
     491 *      After this, str.psz points to a new string containing
     492 *      "blup".
     493 *
     494 *@@added V0.9.7 (2000-12-10) [umoeller]
     495 */
     496
     497ULONG xstrcatc(PXSTRING pxstr,     // in/out: string
     498               CHAR c)             // in: character to append, can be \0
     499{
     500    ULONG   ulrc = 0;
     501
     502    if ((pxstr) && (c))
     503    {
     504        // ULONG   ulSourceLength = 1;
     505        // 1) memory management
     506        ULONG   cbNeeded = pxstr->ulLength  // existing length, without null terminator
     507                           + 1      // new character
     508                           + 1;     // null terminator
     509        if (cbNeeded > pxstr->cbAllocated)
     510        {
     511            // we need more memory than we have previously
     512            // allocated:
     513            if (pxstr->cbAllocated)
     514                // appendee already had memory:
     515                // reallocate
     516                pxstr->psz = (PSZ)realloc(pxstr->psz,
     517                                          cbNeeded);
     518            else
     519                // appendee has no memory:
     520                pxstr->psz = (PSZ)malloc(cbNeeded);
     521
     522            pxstr->cbAllocated = cbNeeded;
     523                    // ulLength is unchanged yet
     524        }
     525        // else: we have enough memory, both if appendee
     526        //       is empty or not empty
     527
     528        // now we have:
     529        // -- if appendee (pxstr) had enough memory, no problem
     530        // -- if appendee (pxstr) needed more memory
     531        //      -- and was not empty: pxstr->psz now points to a
     532        //         reallocated copy of the old string
     533        //      -- and was empty: pxstr->psz now points to a
     534        //         new (unitialized) buffer
     535
     536        // 2) append character:
     537        pxstr->psz[pxstr->ulLength] = c;
     538        pxstr->psz[pxstr->ulLength + 1] = '\0';
     539
     540        // in all cases, set new length
     541        pxstr->ulLength++;
     542        ulrc = pxstr->ulLength;
     543
     544    } // end if ((pxstr) && (c))
    460545
    461546    return (ulrc);
Note: See TracChangeset for help on using the changeset viewer.