Changeset 42


Ignore:
Timestamp:
Mar 5, 2001, 8:50:19 PM (24 years ago)
Author:
umoeller
Message:

Misc. changes.

File:
1 edited

Legend:

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

    r41 r42  
    1212 *
    1313 *      Instead of char* pointers, the functions in this file
    14  *      operate on an XSTRING structure, which contains a char*
    15  *      pointer instead.
     14 *      operate on XSTRING structures, which in turn contain
     15 *      a char* pointer pointing to heap memory, which is managed
     16 *      automatically.
    1617 *
    1718 *      Using these functions has the following advantages:
     
    120121/*
    121122 *@@ xstrInit:
    122  *      initializes an empty XSTRING.
     123 *      initializes a new XSTRING. Always call this before
     124 *      using an XSTRING from the stack.
    123125 *
    124126 *      If (ulPreAllocate != 0), memory is pre-allocated
     
    129131 *
    130132 *      Do not use this on an XSTRING which is already
    131  *      initialized. Use xstrset instead.
     133 *      initialized (this would cause memory leaks).
     134 *      Use xstrcpy or xstrset instead.
    132135 *
    133136 *@@added V0.9.6 (2000-11-01) [umoeller]
     
    158161 *
    159162 *      Do not use this on an XSTRING which is already
    160  *      initialized. Use xstrset instead.
     163 *      initialized (this would cause memory leaks).
     164 *      Use xstrcpy or xstrset instead.
    161165 *
    162166 *      Example:
     
    195199 *
    196200 *      Do not use this on an XSTRING which is already
    197  *      initialized. Use xstrcpy instead.
     201 *      initialized (this would cause memory leaks).
     202 *      Use xstrcpy or xstrset instead.
    198203 *
    199204 *      Example:
     
    215220
    216221        if (pcszSource)
     222        {
    217223            pxstr->ulLength = strlen(pcszSource);
    218224
    219         if (pxstr->ulLength)
    220         {
    221             // we do have a source string:
    222             pxstr->cbAllocated = pxstr->ulLength + 1 + ulExtraAllocate;
    223             pxstr->psz = (PSZ)malloc(pxstr->cbAllocated);
    224             strcpy(pxstr->psz, pcszSource);
     225            if (pxstr->ulLength)
     226            {
     227                // we do have a source string:
     228                pxstr->cbAllocated = pxstr->ulLength + 1 + ulExtraAllocate;
     229                pxstr->psz = (PSZ)malloc(pxstr->cbAllocated);
     230                strcpy(pxstr->psz, pcszSource);
     231            }
    225232        }
    226233    }
     
    396403 *      This sequence can be abbreviated using xstrInitCopy.
    397404 *
     405 *      Memory cost: If there's enough room in pxstr for
     406 *      pcszSource, none. Otherwise pxstr is reallocated
     407 *      to hold enough room for pcszSource.
     408 *
    398409 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy
    399410 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
     
    431442            // we need more memory than we have previously
    432443            // allocated:
    433             if (pxstr->psz)
     444            /* if (pxstr->psz)
    434445                free(pxstr->psz); // V0.9.9 (2001-01-28) [lafaix]
    435446            pxstr->cbAllocated = cbNeeded;
    436             pxstr->psz = (PSZ)malloc(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;
    437455        }
    438456        // else: we have enough memory
     
    513531 *      After this, str.psz points to a new string containing
    514532 *      "blahblup".
     533 *
     534 *      Memory cost: If there's enough room in pxstr for
     535 *      pcszSource, none. Otherwise pxstr is reallocated
     536 *      to hold enough room for pcszSource.
    515537 *
    516538 *@@changed V0.9.1 (99-12-20) [umoeller]: fixed memory leak
     
    617639 *      After this, str.psz points to a new string containing
    618640 *      "blup".
     641 *
     642 *      Memory cost: If there's enough room in pxstr for
     643 *      c, none. Otherwise pxstr is reallocated
     644 *      to hold enough room for c.
    619645 *
    620646 *@@added V0.9.7 (2000-12-10) [umoeller]
     
    720746 *      This would yield "This is a stupid string."
    721747 *
     748 *      Memory cost: If there's enough room in pxstr for
     749 *      the replacement, none. Otherwise pxstr is reallocated
     750 *      to hold enough room for the replacement.
     751 *
    722752 *@@added V0.9.7 (2001-01-15) [umoeller]
    723753 *@@changed V0.9.9 (2001-01-29) [lafaix]: fixed unnecessary allocation when pxstr was big enough
    724   *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
     754 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
    725755 */
    726756
     
    959989 *      would replace all occurences of "Test" in str with
    960990 *      "Dummy".
     991 *
     992 *      Memory cost: Calls xstrrpl if pstrSearch was found.
    961993 *
    962994 *@@changed V0.9.0 [umoeller]: totally rewritten.
     
    10841116 +          S%61mple %63hara%63ters.
    10851117 *
     1118 *      Memory cost: None, except for that of xstrFindReplace.
     1119 *
    10861120 *@@added V0.9.9 (2001-02-28) [umoeller]
    10871121 */
     
    11331167 *
    11341168 *      This simply assumes that all '%' characters in
    1135  *      pxstr contain encodings and the next two characters
     1169 *      pxstr introduce encodings and the next two characters
    11361170 *      after '%' always are a hex character code. This
    1137  *      only recognizes hex in upper case.
    1138  *
    1139  *      Returns the no. of characters replaced.
     1171 *      only recognizes hex in upper case. All this will
     1172 *      work properly with encodings from xstrEncode.
     1173 *
     1174 *      Returns the no. of encodings replaced.
     1175 *
     1176 *      Memory cost: Creates a temporary buffer with the
     1177 *      length of pxstr and replaces pxstr with it, if any
     1178 *      encodings were found. In other words, this is
     1179 *      expensive for very large strings.
    11401180 *
    11411181 *@@added V0.9.9 (2001-02-28) [umoeller]
     
    11671207                if (c == '%')
    11681208                {
    1169                     static char ach[] = "01234567989ABCDEF";
     1209                    static char ach[] = "0123456789ABCDEF";
    11701210
    11711211                    // convert two chars after '%'
    1172                     CHAR        c2,         // first char after '%'
    1173                                 c3;         // second char after '%'
     1212                    CHAR        c2,         // first char after '%'     --> hi-nibble
     1213                                c3;         // second char after '%'    --> lo-nibble
    11741214                    const char  *p2,        // for first char: points into ach or is NULL
    11751215                                *p3;        // for second char: points into ach or is NULL
     
    11811221                    {
    11821222                        // both chars after '%' were valid:
    1183                         *pDest++ =    (p2 - ach) // 0 for '0', 10 for 'A', ...
    1184                                     + ((p3 - ach) << 4);
     1223                        *pDest++ =    // lo-nibble:
     1224                                      (p3 - ach) // 0 for '0', 10 for 'A', ...
     1225                                      // hi-nibble:
     1226                                    + ((p2 - ach) << 4);
    11851227                        // go on after that
    11861228                        pSource += 2;
     
    13661408    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
    13671409
     1410    printf("Encoding @* chars.\n");
     1411    xstrEncode(&str, "@*");
     1412    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
     1413
     1414    printf("Decoding @* chars.\n");
     1415    xstrDecode(&str);
     1416    printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated);
     1417
    13681418    return (0);
    13691419}
     1420
    13701421*/
    1371 
    1372 
Note: See TracChangeset for help on using the changeset viewer.