Changeset 12 for trunk/src/helpers/stringh.c
- Timestamp:
- Nov 4, 2000, 8:55:45 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/stringh.c
r8 r12 363 363 } 364 364 } 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 384 PSZ 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 432 ULONG 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 365 453 return (ulrc); 366 454 } … … 935 1023 * of a line. Spaces before the key are tolerated. 936 1024 * Returns NULL if the key was not found. 1025 * 937 1026 * Used by strhGetParameter/strhSetParameter; useful 938 1027 * for analyzing CONFIG.SYS settings. … … 942 1031 */ 943 1032 944 PSZ strhFindKey( PSZ pszSearchIn,// in: text buffer to search945 PSZ pszKey,// in: key to search for1033 PSZ strhFindKey(const char *pcszSearchIn, // in: text buffer to search 1034 const char *pcszKey, // in: key to search for 946 1035 PBOOL pfIsAllUpperCase) // out: TRUE if key is completely in upper case; 947 1036 // can be NULL if not needed 948 1037 { 949 PSZ p = NULL,950 pReturn = NULL;1038 const char *p = NULL; 1039 PSZ pReturn = NULL; 951 1040 // BOOL fFound = FALSE; 952 1041 953 p = p szSearchIn;1042 p = pcszSearchIn; 954 1043 do { 955 p = strhistr(p, p szKey);956 957 if ((p) && (p >= p szSearchIn))1044 p = strhistr(p, pcszKey); 1045 1046 if ((p) && (p >= pcszSearchIn)) 958 1047 { 959 1048 // make sure the key is at the beginning of a line 960 1049 // by going backwards until we find a char != " " 961 PSZp2 = p;1050 const char *p2 = p; 962 1051 while ( (*p2 == ' ') 963 && (p2 > p szSearchIn)1052 && (p2 > pcszSearchIn) 964 1053 ) 965 1054 p2--; 966 1055 967 1056 // if previous char is an EOL sign, go on 968 if ( (p2 == p szSearchIn) // order fixed V0.9.0, Rdiger Ihle1057 if ( (p2 == pcszSearchIn) // order fixed V0.9.0, Rdiger Ihle 969 1058 || (*(p2-1) == '\r') 970 1059 || (*(p2-1) == '\n') … … 983 1072 { 984 1073 // found: 985 pReturn = p; // go on, p contains found key1074 pReturn = (PSZ)p; // go on, p contains found key 986 1075 987 1076 // test for all upper case? 988 1077 if (pfIsAllUpperCase) 989 1078 { 990 ULONG cbKey2 = strlen(p szKey),1079 ULONG cbKey2 = strlen(pcszKey), 991 1080 ul = 0; 992 1081 *pfIsAllUpperCase = TRUE; … … 1005 1094 p++; // search on after this key 1006 1095 } 1007 } while ((!pReturn) && (p != NULL) && (p != p szSearchIn));1096 } while ((!pReturn) && (p != NULL) && (p != pcszSearchIn)); 1008 1097 1009 1098 return (pReturn); … … 1024 1113 */ 1025 1114 1026 PSZ strhGetParameter( PSZ pszSearchIn,// in: text buffer to search1027 PSZ pszKey,// in: key to search for1028 PSZ pszCopyTo, // out: key value1029 ULONG cbCopyTo) // out: sizeof(*pszCopyTo)1030 { 1031 PSZ p = strhFindKey(p szSearchIn, pszKey, NULL),1115 PSZ 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), 1032 1121 prc = NULL; 1033 1122 if (p) 1034 1123 { 1035 prc = p + strlen(p szKey);1124 prc = p + strlen(pcszKey); 1036 1125 if (pszCopyTo) 1037 1126 // copy to pszCopyTo … … 1053 1142 /* 1054 1143 *@@ strhSetParameter: 1055 * searches *ppsz SearchInfor the key pszKey; if found, it1144 * searches *ppszBuf for the key pszKey; if found, it 1056 1145 * replaces the characters following this key up to the 1057 1146 * 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, *ppsz SearchInis 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. 1061 1150 * 1062 1151 * This function searches w/out case sensitivity. … … 1067 1156 */ 1068 1157 1069 PSZ strhSetParameter(PSZ* ppsz SearchIn, // in: text buffer to search1070 PSZ pszKey,// in: key to search for1071 PSZ pszNewParam, 1158 PSZ 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 1072 1161 BOOL fRespectCase) // in: if TRUE, pszNewParam will 1073 1162 // be converted to upper case if the found key is … … 1076 1165 { 1077 1166 BOOL fIsAllUpperCase = FALSE; 1078 PSZ pKey = strhFindKey(*ppsz SearchIn, pszKey, &fIsAllUpperCase),1167 PSZ pKey = strhFindKey(*ppszBuf, pcszKey, &fIsAllUpperCase), 1079 1168 prc = NULL; 1080 1169 … … 1083 1172 // key found in file: 1084 1173 // replace existing parameter 1085 PSZ pOldParam = pKey + strlen(pszKey); 1174 PSZ pOldParam = pKey + strlen(pcszKey); 1175 1086 1176 prc = pOldParam; 1087 1177 // pOldParam now has the old parameter, which we … … 1096 1186 if (pEOL) 1097 1187 { 1188 XSTRING strBuf, 1189 strFind, 1190 strReplace; 1191 1098 1192 PSZ pszOldCopy = (PSZ)malloc(cbOldParam+1); 1099 1193 strncpy(pszOldCopy, pOldParam, cbOldParam); 1100 1194 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! 1101 1202 1102 1203 // check for upper case desired? … … 1105 1206 strupr(pszNewParam); 1106 1207 1107 xstrrpl( ppszSearchIn, 0, pszOldCopy, pszNewParam, NULL);1208 xstrrpl(&strBuf, 0, &strFind, &strReplace, NULL); 1108 1209 1109 1210 free(pszOldCopy); 1211 1212 *ppszBuf = strBuf.psz; 1110 1213 } 1111 1214 } … … 1113 1216 else 1114 1217 { 1115 PSZ pszNew = (PSZ)malloc(strlen(*ppsz SearchIn)1116 + strlen(p szKey)1218 PSZ pszNew = (PSZ)malloc(strlen(*ppszBuf) 1219 + strlen(pcszKey) 1117 1220 + strlen(pszNewParam) 1118 1221 + 5); // 2 * \r\n + null byte 1119 1222 // key not found: append to end of file 1120 1223 sprintf(pszNew, "%s\r\n%s%s\r\n", 1121 *ppsz SearchIn, pszKey, pszNewParam);1122 free(*ppsz SearchIn);1123 *ppsz SearchIn= pszNew;1224 *ppszBuf, pcszKey, pszNewParam); 1225 free(*ppszBuf); 1226 *ppszBuf = pszNew; 1124 1227 } 1125 1228 … … 1673 1776 ULONG ulIndent) // in: indentation of every line 1674 1777 { 1675 PSZ pszReturn = NULL; 1778 PSZ pszReturn = 0; 1779 XSTRING strReturn; 1676 1780 CHAR szTemp[1000]; 1677 1781 … … 1683 1787 PSZ pszLine = szLine, 1684 1788 pszAscii = szAscii; 1789 1790 xstrInit(&strReturn, (ulSize * 30) + ulIndent); 1685 1791 1686 1792 for (pbCurrent = pb; … … 1719 1825 szLine, // bytes string 1720 1826 szAscii); // ASCII string 1721 xstrcat(& pszReturn, szTemp);1827 xstrcat(&strReturn, szTemp); 1722 1828 1723 1829 // restart line buffer … … 1732 1838 } 1733 1839 } 1840 1841 if (strReturn.cbAllocated) 1842 pszReturn = strReturn.psz; 1734 1843 1735 1844 return (pszReturn);
Note:
See TracChangeset
for help on using the changeset viewer.