Ignore:
Timestamp:
Jan 16, 2001, 8:49:10 PM (25 years ago)
Author:
umoeller
Message:

Fixes for V0.9.7.

File:
1 edited

Legend:

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

    r21 r23  
    432432
    433433/*
    434  *@@ strhrpl:
    435  *      wrapper around xstrrpl to work with C strings.
     434 *@@ strhFindReplace:
     435 *      wrapper around xstrFindReplace to work with C strings.
    436436 *      Note that *ppszBuf can get reallocated and must
    437437 *      be free()'able.
    438438 *
    439439 *      Repetitive use of this wrapper is not recommended
    440  *      because it is considerably slower than xstrrpl.
     440 *      because it is considerably slower than xstrFindReplace.
    441441 *
    442442 *@@added V0.9.6 (2000-11-01) [umoeller]
    443  */
    444 
    445 ULONG strhrpl(PSZ *ppszBuf,                // in/out: string
    446               PULONG pulOfs,               // in: where to begin search (0 = start);
    447                                            // out: ofs of first char after replacement string
    448               const char *pcszSearch,      // in: search string; cannot be NULL
    449               const char *pcszReplace)     // in: replacement string; cannot be NULL
     443 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from strhrpl
     444 */
     445
     446ULONG strhFindReplace(PSZ *ppszBuf,                // in/out: string
     447                      PULONG pulOfs,               // in: where to begin search (0 = start);
     448                                                   // out: ofs of first char after replacement string
     449                      const char *pcszSearch,      // in: search string; cannot be NULL
     450                      const char *pcszReplace)     // in: replacement string; cannot be NULL
    450451{
    451452    ULONG   ulrc = 0;
     
    455456    size_t  ShiftTable[256];
    456457    BOOL    fRepeat = FALSE;
    457     xstrInit(&xstrBuf, 0);
    458     xstrset(&xstrBuf, *ppszBuf);
    459     xstrInit(&xstrFind, 0);
    460     xstrset(&xstrFind, (PSZ)pcszSearch);
    461     xstrInit(&xstrReplace, 0);
    462     xstrset(&xstrReplace, (PSZ)pcszReplace);
    463 
    464     if ((ulrc = xstrrpl(&xstrBuf,
    465                         pulOfs,
    466                         &xstrFind,
    467                         &xstrReplace,
    468                         ShiftTable,
    469                         &fRepeat)))
     458    xstrInitSet(&xstrBuf, *ppszBuf);
     459                // reallocated and returned, so we're safe
     460    xstrInitSet(&xstrFind, (PSZ)pcszSearch);
     461    xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
     462                // these two are never freed, so we're safe too
     463
     464    if ((ulrc = xstrFindReplace(&xstrBuf,
     465                                pulOfs,
     466                                &xstrFind,
     467                                &xstrReplace,
     468                                ShiftTable,
     469                                &fRepeat)))
    470470        // replaced:
    471471        *ppszBuf = xstrBuf.psz;
     
    10091009    }
    10101010
    1011     if (pulOffset)
     1011    if ((pulOffset) && (prc))
    10121012        *pulOffset = prc - pcszSearchIn;
    10131013
     
    10351035        *pulOffset = pNextLine - pszSearchIn;
    10361036    return (pNextLine);
    1037 }
    1038 
    1039 /*
    1040  *@@ strhFindKey:
    1041  *      finds pszKey in pszSearchIn; similar to strhistr,
    1042  *      but this one makes sure the key is at the beginning
    1043  *      of a line. Spaces before the key are tolerated.
    1044  *      Returns NULL if the key was not found.
    1045  *
    1046  *      Used by strhGetParameter/strhSetParameter; useful
    1047  *      for analyzing CONFIG.SYS settings.
    1048  *
    1049  *@@changed V0.9.0 [umoeller]: fixed bug in that this would also return something if only the first chars matched
    1050  *@@changed V0.9.0 [umoeller]: fixed bug which could cause character before pszSearchIn to be examined
    1051  */
    1052 
    1053 PSZ strhFindKey(const char *pcszSearchIn,   // in: text buffer to search
    1054                 const char *pcszKey,        // in: key to search for
    1055                 PBOOL pfIsAllUpperCase) // out: TRUE if key is completely in upper case;
    1056                                      // can be NULL if not needed
    1057 {
    1058     const char  *p = NULL;
    1059     PSZ         pReturn = NULL;
    1060     // BOOL    fFound = FALSE;
    1061 
    1062     p = pcszSearchIn;
    1063     do {
    1064         p = strhistr(p, pcszKey);
    1065 
    1066         if ((p) && (p >= pcszSearchIn))
    1067         {
    1068             // make sure the key is at the beginning of a line
    1069             // by going backwards until we find a char != " "
    1070             const char *p2 = p;
    1071             while (     (*p2 == ' ')
    1072                      && (p2 > pcszSearchIn)
    1073                   )
    1074                 p2--;
    1075 
    1076             // if previous char is an EOL sign, go on
    1077             if (    (p2 == pcszSearchIn)     // order fixed V0.9.0, Rdiger Ihle
    1078                  || (*(p2-1) == '\r')
    1079                  || (*(p2-1) == '\n')
    1080                )
    1081             {
    1082                 // now check whether the char after the search
    1083                 // is a "=" char
    1084                 // ULONG cbKey = strlen(pszKey);
    1085 
    1086                 // tolerate spaces before "="
    1087                 /* PSZ p3 = p;
    1088                 while (*(p3+cbKey) == ' ')
    1089                     p3++;
    1090 
    1091                 if (*(p3+cbKey) == '=') */
    1092                 {
    1093                     // found:
    1094                     pReturn = (PSZ)p; // go on, p contains found key
    1095 
    1096                     // test for all upper case?
    1097                     if (pfIsAllUpperCase)
    1098                     {
    1099                         ULONG cbKey2 = strlen(pcszKey),
    1100                               ul = 0;
    1101                         *pfIsAllUpperCase = TRUE;
    1102                         for (ul = 0; ul < cbKey2; ul++)
    1103                             if (islower(*(p+ul)))
    1104                             {
    1105                                 *pfIsAllUpperCase = FALSE;
    1106                                 break; // for
    1107                             }
    1108                     }
    1109 
    1110                     break; // do
    1111                 }
    1112             } // else search next key
    1113 
    1114             p++; // search on after this key
    1115         }
    1116     } while ((!pReturn) && (p != NULL) && (p != pcszSearchIn));
    1117 
    1118     return (pReturn);
    1119 }
    1120 
    1121 /*
    1122  *@@ strhGetParameter:
    1123  *      searches pszSearchIn for the key pszKey; if found, it
    1124  *      returns a pointer to the following characters in pszSearchIn
    1125  *      and, if pszCopyTo != NULL, copies the rest of the line to
    1126  *      that buffer, of which cbCopyTo specified the size.
    1127  *
    1128  *      If the key is not found, NULL is returned.
    1129  *      String search is done by calling strhFindKey.
    1130  *      This is useful for querying CONFIG.SYS settings.
    1131  *
    1132  *      <B>Example:</B>
    1133  *
    1134  *      this would return "YES" if you searched for "PAUSEONERROR=",
    1135  *      and "PAUSEONERROR=YES" existed in pszSearchIn.
    1136  */
    1137 
    1138 PSZ strhGetParameter(const char *pcszSearchIn,  // in: text buffer to search
    1139                      const char *pcszKey,       // in: key to search for
    1140                      PSZ pszCopyTo,             // out: key value
    1141                      ULONG cbCopyTo)            // out: sizeof(*pszCopyTo)
    1142 {
    1143     PSZ p = strhFindKey(pcszSearchIn, pcszKey, NULL),
    1144         prc = NULL;
    1145     if (p)
    1146     {
    1147         prc = p + strlen(pcszKey);
    1148         if (pszCopyTo)
    1149         // copy to pszCopyTo
    1150         {
    1151             ULONG cb;
    1152             PSZ pEOL = strhFindEOL(prc, &cb);
    1153             if (pEOL)
    1154             {
    1155                 if (cb > cbCopyTo)
    1156                     cb = cbCopyTo-1;
    1157                 strhncpy0(pszCopyTo, prc, cb);
    1158             }
    1159         }
    1160     }
    1161 
    1162     return (prc);
    1163 }
    1164 
    1165 /*
    1166  *@@ strhSetParameter:
    1167  *      searches *ppszBuf for the key pszKey; if found, it
    1168  *      replaces the characters following this key up to the
    1169  *      end of the line with pszParam. If pszKey is not found in
    1170  *      *ppszBuf, it is appended to the file in a new line.
    1171  *
    1172  *      If any changes are made, *ppszBuf is re-allocated.
    1173  *
    1174  *      This function searches w/out case sensitivity.
    1175  *
    1176  *      Returns a pointer to the new parameter inside the buffer.
    1177  *
    1178  *@@changed V0.9.0 [umoeller]: changed function prototype to PSZ* ppszSearchIn
    1179  */
    1180 
    1181 PSZ strhSetParameter(PSZ* ppszBuf,    // in: text buffer to search
    1182                      const char *pcszKey,   // in: key to search for
    1183                      PSZ pszNewParam, // in: new parameter to set for key
    1184                      BOOL fRespectCase)  // in: if TRUE, pszNewParam will
    1185                              // be converted to upper case if the found key is
    1186                              // in upper case also. pszNewParam should be in
    1187                              // lower case if you use this.
    1188 {
    1189     BOOL fIsAllUpperCase = FALSE;
    1190     PSZ pKey = strhFindKey(*ppszBuf, pcszKey, &fIsAllUpperCase),
    1191         prc = NULL;
    1192 
    1193     if (pKey)
    1194     {
    1195         // key found in file:
    1196         // replace existing parameter
    1197         PSZ pOldParam = pKey + strlen(pcszKey);
    1198 
    1199         prc = pOldParam;
    1200         // pOldParam now has the old parameter, which we
    1201         // will overwrite now
    1202 
    1203         if (pOldParam)
    1204         {
    1205             ULONG cbOldParam;
    1206             PSZ pEOL = strhFindEOL(pOldParam, &cbOldParam);
    1207             // pEOL now has first end-of-line after the parameter
    1208 
    1209             if (pEOL)
    1210             {
    1211                 XSTRING strBuf;
    1212                 ULONG   ulOfs = 0;
    1213 
    1214                 PSZ pszOldCopy = (PSZ)malloc(cbOldParam+1);
    1215                 strncpy(pszOldCopy, pOldParam, cbOldParam);
    1216                 pszOldCopy[cbOldParam] = '\0';
    1217 
    1218                 xstrInit(&strBuf, 0);
    1219                 xstrset(&strBuf, *ppszBuf);         // this must not be freed!
    1220                 /* xstrInit(&strFind, 0);
    1221                 xstrset(&strFind, pszOldCopy);      // this must not be freed!
    1222                 xstrInit(&strReplace, 0);
    1223                 xstrset(&strReplace, pszNewParam);  // this must not be freed!
    1224                    */
    1225 
    1226                 // check for upper case desired?
    1227                 if (fRespectCase)
    1228                     if (fIsAllUpperCase)
    1229                         strupr(pszNewParam);
    1230 
    1231                 xstrcrpl(&strBuf, &ulOfs, pszOldCopy, pszNewParam);
    1232 
    1233                 free(pszOldCopy);
    1234 
    1235                 *ppszBuf = strBuf.psz;
    1236             }
    1237         }
    1238     }
    1239     else
    1240     {
    1241         PSZ pszNew = (PSZ)malloc(strlen(*ppszBuf)
    1242                               + strlen(pcszKey)
    1243                               + strlen(pszNewParam)
    1244                               + 5);     // 2 * \r\n + null byte
    1245         // key not found: append to end of file
    1246         sprintf(pszNew, "%s\r\n%s%s\r\n",
    1247                 *ppszBuf, pcszKey, pszNewParam);
    1248         free(*ppszBuf);
    1249         *ppszBuf = pszNew;
    1250     }
    1251 
    1252     return (prc);
    1253 }
    1254 
    1255 /*
    1256  *@@ strhDeleteLine:
    1257  *      this deletes the line in pszSearchIn which starts with
    1258  *      the key pszKey. Returns TRUE if the line was found and
    1259  *      deleted.
    1260  *
    1261  *      This copies within pszSearchIn.
    1262  */
    1263 
    1264 BOOL strhDeleteLine(PSZ pszSearchIn,        // in: buffer to search
    1265                     PSZ pszKey)             // in: key to find
    1266 {
    1267     BOOL fIsAllUpperCase = FALSE;
    1268     PSZ pKey = strhFindKey(pszSearchIn, pszKey, &fIsAllUpperCase);
    1269     BOOL brc = FALSE;
    1270 
    1271     if (pKey) {
    1272         PSZ pEOL = strhFindEOL(pKey, NULL);
    1273         // pEOL now has first end-of-line after the key
    1274         if (pEOL)
    1275         {
    1276             // delete line by overwriting it with
    1277             // the next line
    1278             strcpy(pKey, pEOL+2);
    1279         }
    1280         else
    1281         {
    1282             // EOL not found: we must be at the end of the file
    1283             *pKey = '\0';
    1284         }
    1285         brc = TRUE;
    1286     }
    1287 
    1288     return (brc);
    12891037}
    12901038
     
    18481596                            szLine,         // bytes string
    18491597                            szAscii);       // ASCII string
    1850             xstrcat(&strReturn, szTemp);
     1598            xstrcat(&strReturn, szTemp, 0);
    18511599
    18521600            // restart line buffer
Note: See TracChangeset for help on using the changeset viewer.