Changeset 23 for trunk/src/helpers/stringh.c
- Timestamp:
- Jan 16, 2001, 8:49:10 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/stringh.c
r21 r23 432 432 433 433 /* 434 *@@ strh rpl:435 * wrapper around xstr rplto work with C strings.434 *@@ strhFindReplace: 435 * wrapper around xstrFindReplace to work with C strings. 436 436 * Note that *ppszBuf can get reallocated and must 437 437 * be free()'able. 438 438 * 439 439 * Repetitive use of this wrapper is not recommended 440 * because it is considerably slower than xstr rpl.440 * because it is considerably slower than xstrFindReplace. 441 441 * 442 442 *@@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 446 ULONG 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 450 451 { 451 452 ULONG ulrc = 0; … … 455 456 size_t ShiftTable[256]; 456 457 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))) 470 470 // replaced: 471 471 *ppszBuf = xstrBuf.psz; … … 1009 1009 } 1010 1010 1011 if ( pulOffset)1011 if ((pulOffset) && (prc)) 1012 1012 *pulOffset = prc - pcszSearchIn; 1013 1013 … … 1035 1035 *pulOffset = pNextLine - pszSearchIn; 1036 1036 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 beginning1043 * of a line. Spaces before the key are tolerated.1044 * Returns NULL if the key was not found.1045 *1046 * Used by strhGetParameter/strhSetParameter; useful1047 * 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 matched1050 *@@changed V0.9.0 [umoeller]: fixed bug which could cause character before pszSearchIn to be examined1051 */1052 1053 PSZ strhFindKey(const char *pcszSearchIn, // in: text buffer to search1054 const char *pcszKey, // in: key to search for1055 PBOOL pfIsAllUpperCase) // out: TRUE if key is completely in upper case;1056 // can be NULL if not needed1057 {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 line1069 // 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 on1077 if ( (p2 == pcszSearchIn) // order fixed V0.9.0, Rdiger Ihle1078 || (*(p2-1) == '\r')1079 || (*(p2-1) == '\n')1080 )1081 {1082 // now check whether the char after the search1083 // is a "=" char1084 // 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 key1095 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; // for1107 }1108 }1109 1110 break; // do1111 }1112 } // else search next key1113 1114 p++; // search on after this key1115 }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, it1124 * returns a pointer to the following characters in pszSearchIn1125 * and, if pszCopyTo != NULL, copies the rest of the line to1126 * 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 search1139 const char *pcszKey, // in: key to search for1140 PSZ pszCopyTo, // out: key value1141 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 pszCopyTo1150 {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, it1168 * replaces the characters following this key up to the1169 * end of the line with pszParam. If pszKey is not found in1170 * *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* ppszSearchIn1179 */1180 1181 PSZ strhSetParameter(PSZ* ppszBuf, // in: text buffer to search1182 const char *pcszKey, // in: key to search for1183 PSZ pszNewParam, // in: new parameter to set for key1184 BOOL fRespectCase) // in: if TRUE, pszNewParam will1185 // be converted to upper case if the found key is1186 // in upper case also. pszNewParam should be in1187 // 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 parameter1197 PSZ pOldParam = pKey + strlen(pcszKey);1198 1199 prc = pOldParam;1200 // pOldParam now has the old parameter, which we1201 // will overwrite now1202 1203 if (pOldParam)1204 {1205 ULONG cbOldParam;1206 PSZ pEOL = strhFindEOL(pOldParam, &cbOldParam);1207 // pEOL now has first end-of-line after the parameter1208 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 else1240 {1241 PSZ pszNew = (PSZ)malloc(strlen(*ppszBuf)1242 + strlen(pcszKey)1243 + strlen(pszNewParam)1244 + 5); // 2 * \r\n + null byte1245 // key not found: append to end of file1246 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 with1258 * the key pszKey. Returns TRUE if the line was found and1259 * deleted.1260 *1261 * This copies within pszSearchIn.1262 */1263 1264 BOOL strhDeleteLine(PSZ pszSearchIn, // in: buffer to search1265 PSZ pszKey) // in: key to find1266 {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 key1274 if (pEOL)1275 {1276 // delete line by overwriting it with1277 // the next line1278 strcpy(pKey, pEOL+2);1279 }1280 else1281 {1282 // EOL not found: we must be at the end of the file1283 *pKey = '\0';1284 }1285 brc = TRUE;1286 }1287 1288 return (brc);1289 1037 } 1290 1038 … … 1848 1596 szLine, // bytes string 1849 1597 szAscii); // ASCII string 1850 xstrcat(&strReturn, szTemp );1598 xstrcat(&strReturn, szTemp, 0); 1851 1599 1852 1600 // restart line buffer
Note:
See TracChangeset
for help on using the changeset viewer.