Ignore:
Timestamp:
Nov 4, 2000, 8:55:45 PM (25 years ago)
Author:
umoeller
Message:

Updated string helpers.

File:
1 edited

Legend:

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

    r8 r12  
    363363        }
    364364    }
     365    return (ulrc);
     366}
     367
     368/*
     369 *@@ strhins:
     370 *      this inserts one string into another.
     371 *
     372 *      pszInsert is inserted into pszBuffer at offset
     373 *      ulInsertOfs (which counts from 0).
     374 *
     375 *      A newly allocated string is returned. pszBuffer is
     376 *      not changed. The new string should be free()'d after
     377 *      use.
     378 *
     379 *      Upon errors, NULL is returned.
     380 *
     381 *@@changed V0.9.0 [umoeller]: completely rewritten.
     382 */
     383
     384PSZ strhins(const char *pcszBuffer,
     385            ULONG ulInsertOfs,
     386            const char *pcszInsert)
     387{
     388    PSZ     pszNew = NULL;
     389
     390    if ((pcszBuffer) && (pcszInsert))
     391    {
     392        do {
     393            ULONG   cbBuffer = strlen(pcszBuffer);
     394            ULONG   cbInsert = strlen(pcszInsert);
     395
     396            // check string length
     397            if (ulInsertOfs > cbBuffer + 1)
     398                break;  // do
     399
     400            // OK, let's go.
     401            pszNew = (PSZ)malloc(cbBuffer + cbInsert + 1);  // additional null terminator
     402
     403            // copy stuff before pInsertPos
     404            memcpy(pszNew,
     405                   pcszBuffer,
     406                   ulInsertOfs);
     407            // copy string to be inserted
     408            memcpy(pszNew + ulInsertOfs,
     409                   pcszInsert,
     410                   cbInsert);
     411            // copy stuff after pInsertPos
     412            strcpy(pszNew + ulInsertOfs + cbInsert,
     413                   pcszBuffer + ulInsertOfs);
     414        } while (FALSE);
     415    }
     416
     417    return (pszNew);
     418}
     419
     420/*
     421 *@@ strhrpl:
     422 *      wrapper around xstrrpl to work with C strings.
     423 *      Note that *ppszBuf can get reallocated and must
     424 *      be free()'able.
     425 *
     426 *      Use of this wrapper is not recommended because
     427 *      it is considerably slower than xstrrpl.
     428 *
     429 *@@added V0.9.6 (2000-11-01) [umoeller]
     430 */
     431
     432ULONG strhrpl(PSZ *ppszBuf,                // in/out: string
     433              ULONG ulOfs,                  // in: where to begin search (0 = start)
     434              const char *pcszSearch,    // in: search string; cannot be NULL
     435              const char *pcszReplace,   // in: replacement string; cannot be NULL
     436              PULONG pulAfterOfs)           // out: offset where found (ptr can be NULL)
     437{
     438    ULONG   ulrc = 0;
     439    XSTRING xstrBuf,
     440            xstrFind,
     441            xstrReplace;
     442    xstrInit(&xstrBuf, 0);
     443    xstrset(&xstrBuf, *ppszBuf);
     444    xstrInit(&xstrFind, 0);
     445    xstrset(&xstrFind, (PSZ)pcszSearch);
     446    xstrInit(&xstrReplace, 0);
     447    xstrset(&xstrReplace, (PSZ)pcszReplace);
     448
     449    if (ulrc = xstrrpl(&xstrBuf, ulOfs, &xstrFind, &xstrReplace, pulAfterOfs))
     450        // replaced:
     451        *ppszBuf = xstrBuf.psz;
     452
    365453    return (ulrc);
    366454}
     
    9351023 *      of a line. Spaces before the key are tolerated.
    9361024 *      Returns NULL if the key was not found.
     1025 *
    9371026 *      Used by strhGetParameter/strhSetParameter; useful
    9381027 *      for analyzing CONFIG.SYS settings.
     
    9421031 */
    9431032
    944 PSZ strhFindKey(PSZ pszSearchIn,     // in: text buffer to search
    945                 PSZ pszKey,          // in: key to search for
     1033PSZ strhFindKey(const char *pcszSearchIn,   // in: text buffer to search
     1034                const char *pcszKey,        // in: key to search for
    9461035                PBOOL pfIsAllUpperCase) // out: TRUE if key is completely in upper case;
    9471036                                     // can be NULL if not needed
    9481037{
    949     PSZ     p = NULL,
    950             pReturn = NULL;
     1038    const char  *p = NULL;
     1039    PSZ         pReturn = NULL;
    9511040    // BOOL    fFound = FALSE;
    9521041
    953     p = pszSearchIn;
     1042    p = pcszSearchIn;
    9541043    do {
    955         p = strhistr(p, pszKey);
    956 
    957         if ((p) && (p >= pszSearchIn))
     1044        p = strhistr(p, pcszKey);
     1045
     1046        if ((p) && (p >= pcszSearchIn))
    9581047        {
    9591048            // make sure the key is at the beginning of a line
    9601049            // by going backwards until we find a char != " "
    961             PSZ p2 = p;
     1050            const char *p2 = p;
    9621051            while (     (*p2 == ' ')
    963                      && (p2 > pszSearchIn)
     1052                     && (p2 > pcszSearchIn)
    9641053                  )
    9651054                p2--;
    9661055
    9671056            // if previous char is an EOL sign, go on
    968             if (    (p2 == pszSearchIn)     // order fixed V0.9.0, Rdiger Ihle
     1057            if (    (p2 == pcszSearchIn)     // order fixed V0.9.0, Rdiger Ihle
    9691058                 || (*(p2-1) == '\r')
    9701059                 || (*(p2-1) == '\n')
     
    9831072                {
    9841073                    // found:
    985                     pReturn = p; // go on, p contains found key
     1074                    pReturn = (PSZ)p; // go on, p contains found key
    9861075
    9871076                    // test for all upper case?
    9881077                    if (pfIsAllUpperCase)
    9891078                    {
    990                         ULONG cbKey2 = strlen(pszKey),
     1079                        ULONG cbKey2 = strlen(pcszKey),
    9911080                              ul = 0;
    9921081                        *pfIsAllUpperCase = TRUE;
     
    10051094            p++; // search on after this key
    10061095        }
    1007     } while ((!pReturn) && (p != NULL) && (p != pszSearchIn));
     1096    } while ((!pReturn) && (p != NULL) && (p != pcszSearchIn));
    10081097
    10091098    return (pReturn);
     
    10241113 */
    10251114
    1026 PSZ strhGetParameter(PSZ pszSearchIn,   // in: text buffer to search
    1027                      PSZ pszKey,        // in: key to search for
    1028                      PSZ pszCopyTo,     // out: key value
    1029                      ULONG cbCopyTo)    // out: sizeof(*pszCopyTo)
    1030 {
    1031     PSZ p = strhFindKey(pszSearchIn, pszKey, NULL),
     1115PSZ strhGetParameter(const char *pcszSearchIn,  // in: text buffer to search
     1116                     const char *pcszKey,       // in: key to search for
     1117                     PSZ pszCopyTo,             // out: key value
     1118                     ULONG cbCopyTo)            // out: sizeof(*pszCopyTo)
     1119{
     1120    PSZ p = strhFindKey(pcszSearchIn, pcszKey, NULL),
    10321121        prc = NULL;
    10331122    if (p)
    10341123    {
    1035         prc = p + strlen(pszKey);
     1124        prc = p + strlen(pcszKey);
    10361125        if (pszCopyTo)
    10371126        // copy to pszCopyTo
     
    10531142/*
    10541143 *@@ strhSetParameter:
    1055  *      searches *ppszSearchIn for the key pszKey; if found, it
     1144 *      searches *ppszBuf for the key pszKey; if found, it
    10561145 *      replaces the characters following this key up to the
    10571146 *      end of the line with pszParam. If pszKey is not found in
    1058  *      pszSearchIn, it is appended to the file in a new line.
    1059  *
    1060  *      If any changes are made, *ppszSearchIn is re-allocated.
     1147 *      *ppszBuf, it is appended to the file in a new line.
     1148 *
     1149 *      If any changes are made, *ppszBuf is re-allocated.
    10611150 *
    10621151 *      This function searches w/out case sensitivity.
     
    10671156 */
    10681157
    1069 PSZ strhSetParameter(PSZ* ppszSearchIn,    // in: text buffer to search
    1070                      PSZ pszKey,         // in: key to search for
    1071                      PSZ pszNewParam,    // in: new parameter to set for key
     1158PSZ strhSetParameter(PSZ* ppszBuf,    // in: text buffer to search
     1159                     const char *pcszKey,   // in: key to search for
     1160                     PSZ pszNewParam, // in: new parameter to set for key
    10721161                     BOOL fRespectCase)  // in: if TRUE, pszNewParam will
    10731162                             // be converted to upper case if the found key is
     
    10761165{
    10771166    BOOL fIsAllUpperCase = FALSE;
    1078     PSZ pKey = strhFindKey(*ppszSearchIn, pszKey, &fIsAllUpperCase),
     1167    PSZ pKey = strhFindKey(*ppszBuf, pcszKey, &fIsAllUpperCase),
    10791168        prc = NULL;
    10801169
     
    10831172        // key found in file:
    10841173        // replace existing parameter
    1085         PSZ pOldParam = pKey + strlen(pszKey);
     1174        PSZ pOldParam = pKey + strlen(pcszKey);
     1175
    10861176        prc = pOldParam;
    10871177        // pOldParam now has the old parameter, which we
     
    10961186            if (pEOL)
    10971187            {
     1188                XSTRING strBuf,
     1189                        strFind,
     1190                        strReplace;
     1191
    10981192                PSZ pszOldCopy = (PSZ)malloc(cbOldParam+1);
    10991193                strncpy(pszOldCopy, pOldParam, cbOldParam);
    11001194                pszOldCopy[cbOldParam] = '\0';
     1195
     1196                xstrInit(&strBuf, 0);
     1197                xstrset(&strBuf, *ppszBuf);         // this must not be freed!
     1198                xstrInit(&strFind, 0);
     1199                xstrset(&strFind, pszOldCopy);      // this must not be freed!
     1200                xstrInit(&strReplace, 0);
     1201                xstrset(&strReplace, pszNewParam);  // this must not be freed!
    11011202
    11021203                // check for upper case desired?
     
    11051206                        strupr(pszNewParam);
    11061207
    1107                 xstrrpl(ppszSearchIn, 0, pszOldCopy, pszNewParam, NULL);
     1208                xstrrpl(&strBuf, 0, &strFind, &strReplace, NULL);
    11081209
    11091210                free(pszOldCopy);
     1211
     1212                *ppszBuf = strBuf.psz;
    11101213            }
    11111214        }
     
    11131216    else
    11141217    {
    1115         PSZ pszNew = (PSZ)malloc(strlen(*ppszSearchIn)
    1116                               + strlen(pszKey)
     1218        PSZ pszNew = (PSZ)malloc(strlen(*ppszBuf)
     1219                              + strlen(pcszKey)
    11171220                              + strlen(pszNewParam)
    11181221                              + 5);     // 2 * \r\n + null byte
    11191222        // key not found: append to end of file
    11201223        sprintf(pszNew, "%s\r\n%s%s\r\n",
    1121                 *ppszSearchIn, pszKey, pszNewParam);
    1122         free(*ppszSearchIn);
    1123         *ppszSearchIn = pszNew;
     1224                *ppszBuf, pcszKey, pszNewParam);
     1225        free(*ppszBuf);
     1226        *ppszBuf = pszNew;
    11241227    }
    11251228
     
    16731776                   ULONG ulIndent)      // in: indentation of every line
    16741777{
    1675     PSZ     pszReturn = NULL;
     1778    PSZ     pszReturn = 0;
     1779    XSTRING strReturn;
    16761780    CHAR    szTemp[1000];
    16771781
     
    16831787    PSZ     pszLine = szLine,
    16841788            pszAscii = szAscii;
     1789
     1790    xstrInit(&strReturn, (ulSize * 30) + ulIndent);
    16851791
    16861792    for (pbCurrent = pb;
     
    17191825                            szLine,         // bytes string
    17201826                            szAscii);       // ASCII string
    1721             xstrcat(&pszReturn, szTemp);
     1827            xstrcat(&strReturn, szTemp);
    17221828
    17231829            // restart line buffer
     
    17321838        }
    17331839    }
     1840
     1841    if (strReturn.cbAllocated)
     1842        pszReturn = strReturn.psz;
    17341843
    17351844    return (pszReturn);
Note: See TracChangeset for help on using the changeset viewer.