Changeset 23
- Timestamp:
- Jan 16, 2001, 8:49:10 PM (25 years ago)
- Location:
- trunk
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/helpers/dosh.h
r18 r23 290 290 ULONG ulAttr); 291 291 292 APIRET dosh ReadTextFile(PSZ pszFile,292 APIRET doshLoadTextFile(const char *pcszFile, 293 293 PSZ* ppszContent); 294 294 -
trunk/include/helpers/gpih.h
r18 r23 72 72 BYTE bDivisor); 73 73 74 #ifdef INCL_GPILOGCOLORTABLE 75 76 /* 77 *@@ gpihSwitchToRGB: 78 * this switches the given HPS into RGB mode. 79 * Requires INCL_GPILOGCOLORTABLE. 80 */ 81 82 #define gpihSwitchToRGB(hps) \ 83 GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL); 84 85 #endif 74 BOOL gpihSwitchToRGB(HPS hps); 75 typedef BOOL XWPENTRY GPIHSWITCHTORGB(HPS hps); 76 typedef GPIHSWITCHTORGB *PGPIHSWITCHTORGB; 86 77 87 78 /* ****************************************************************** … … 108 99 ULONG ulWidth); 109 100 110 VOID APIENTRY gpihDraw3DFrame(HPS hps,101 VOID XWPENTRY gpihDraw3DFrame(HPS hps, 111 102 PRECTL prcl, 112 103 USHORT usWidth, 113 104 LONG lColorLeft, 114 105 LONG lColorRight); 115 typedef VOID APIENTRY GPIHDRAW3DFRAME(HPS hps,106 typedef VOID XWPENTRY GPIHDRAW3DFRAME(HPS hps, 116 107 PRECTL prcl, 117 108 USHORT usWidth, -
trunk/include/helpers/stringh.h
r21 r23 63 63 const char *pcszInsert); 64 64 65 ULONG strh rpl(PSZ *ppszBuf,66 PULONG pulOfs,67 const char *pcszSearch,68 const char *pcszReplace);65 ULONG strhFindReplace(PSZ *ppszBuf, 66 PULONG pulOfs, 67 const char *pcszSearch, 68 const char *pcszReplace); 69 69 70 70 ULONG strhWords(PSZ psz); … … 129 129 PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset); 130 130 131 PSZ strhFindKey(const char *pcszSearchIn,132 const char *pcszKey,133 PBOOL pfIsAllUpperCase);134 135 PSZ strhGetParameter(const char *pcszSearchIn,136 const char *pcszKey,137 PSZ pszCopyTo,138 ULONG cbCopyTo);139 140 PSZ strhSetParameter(PSZ* ppszBuf,141 const char *pcszKey,142 PSZ pszNewParam,143 BOOL fRespectCase);144 145 BOOL strhDeleteLine(PSZ pszSearchIn, PSZ pszKey);146 147 131 BOOL strhBeautifyTitle(PSZ psz); 148 132 -
trunk/include/helpers/xstring.h
r21 r23 82 82 typedef XSTRSET *PXSTRSET; 83 83 84 ULONG XWPENTRY xstrcpy(PXSTRING pxstr, const char *pcszSource );85 typedef ULONG XWPENTRY XSTRCPY(PXSTRING pxstr, const char *pcszSource );84 ULONG XWPENTRY xstrcpy(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength); 85 typedef ULONG XWPENTRY XSTRCPY(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength); 86 86 typedef XSTRCPY *PXSTRCPY; 87 87 88 ULONG XWPENTRY xstrcat(PXSTRING pxstr, const char *pcszSource );89 typedef ULONG XWPENTRY XSTRCAT(PXSTRING pxstr, const char *pcszSource );88 ULONG XWPENTRY xstrcat(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength); 89 typedef ULONG XWPENTRY XSTRCAT(PXSTRING pxstr, const char *pcszSource, ULONG ulSourceLength); 90 90 typedef XSTRCAT *PXSTRCAT; 91 91 … … 102 102 103 103 #define xstrIsString(psz) ( (psz != 0) && (*(psz) != 0) ) 104 105 ULONG XWPENTRY xstrrpl(PXSTRING pxstr, 106 ULONG ulFirstReplOfs, 107 ULONG cReplLen, 108 const XSTRING *pstrReplaceWith); 109 typedef ULONG XWPENTRY XSTRRPL(PXSTRING pxstr, 110 ULONG ulFirstReplOfs, 111 ULONG cReplLen, 112 const XSTRING *pstrReplaceWith); 113 typedef XSTRRPL *PXSTRRPL; 104 114 105 115 PSZ XWPENTRY xstrFindWord(const XSTRING *pxstr, … … 119 129 typedef XSTRFINDWORD *PXSTRFINDWORD; 120 130 121 ULONG XWPENTRY xstrrpl(PXSTRING pxstr, 122 PULONG pulOfs, 123 const XSTRING *pstrSearch, 124 const XSTRING *pstrReplace, 125 size_t *pShiftTable, 126 PBOOL pfRepeatFind); 127 typedef ULONG XWPENTRY XSTRRPL(PXSTRING pxstr, 131 ULONG XWPENTRY xstrFindReplace(PXSTRING pxstr, 128 132 PULONG pulOfs, 129 133 const XSTRING *pstrSearch, … … 131 135 size_t *pShiftTable, 132 136 PBOOL pfRepeatFind); 133 typedef XSTRRPL *PXSTRRPL; 137 typedef ULONG XWPENTRY XSTRFINDREPLACE(PXSTRING pxstr, 138 PULONG pulOfs, 139 const XSTRING *pstrSearch, 140 const XSTRING *pstrReplace, 141 size_t *pShiftTable, 142 PBOOL pfRepeatFind); 143 typedef XSTRFINDREPLACE *PXSTRFINDREPLACE; 134 144 135 ULONG XWPENTRY xstrcrpl(PXSTRING pxstr, 136 PULONG pulOfs, 137 const char *pcszSearch, 138 const char *pcszReplace); 139 typedef ULONG XWPENTRY XSTRCRPL(PXSTRING pxstr, 145 ULONG XWPENTRY xstrFindReplaceC(PXSTRING pxstr, 140 146 PULONG pulOfs, 141 147 const char *pcszSearch, 142 148 const char *pcszReplace); 143 typedef XSTRCRPL *PXSTRCRPL; 149 typedef ULONG XWPENTRY XSTRFINDREPLACEC(PXSTRING pxstr, 150 PULONG pulOfs, 151 const char *pcszSearch, 152 const char *pcszReplace); 153 typedef XSTRFINDREPLACEC *PXSTRFINDREPLACEC; 154 155 VOID XWPENTRY xstrConvertLineFormat(PXSTRING pxstr, BOOL fToCFormat); 156 typedef VOID XWPENTRY XSTRCONVERTLINEFORMAT(PXSTRING pxstr, BOOL fToCFormat); 157 typedef XSTRCONVERTLINEFORMAT *PXSTRCONVERTLINEFORMAT; 158 144 159 #endif 145 160 -
trunk/src/helpers/dosh.c
r22 r23 1065 1065 1066 1066 /* 1067 *@@ dosh ReadTextFile:1067 *@@ doshLoadTextFile: 1068 1068 * reads a text file from disk, allocates memory 1069 1069 * via malloc() and sets a pointer to this … … 1074 1074 * Otherwise, you should free() the buffer when 1075 1075 * no longer needed. 1076 */ 1077 1078 APIRET doshReadTextFile(PSZ pszFile, // in: file name to read 1079 PSZ* ppszContent) // out: newly allocated buffer with file's content 1076 * 1077 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from doshReadTextFile 1078 */ 1079 1080 APIRET doshLoadTextFile(const char *pcszFile, // in: file name to read 1081 PSZ* ppszContent) // out: newly allocated buffer with file's content 1080 1082 { 1081 1083 ULONG ulSize, … … 1085 1087 PSZ pszContent = NULL; 1086 1088 1087 APIRET arc = DosOpen( pszFile,1089 APIRET arc = DosOpen((PSZ)pcszFile, 1088 1090 &hFile, 1089 1091 &ulAction, // action taken -
trunk/src/helpers/gpih.c
r21 r23 152 152 *pb++ = (BYTE)((LONG)(*pb) * bMultiplier / bDivisor); // green 153 153 *pb++ = (BYTE)((LONG)(*pb) * bMultiplier / bDivisor); // red 154 } 155 156 /* 157 *@@ gpihSwitchToRGB: 158 * this switches the given HPS into RGB mode. You should 159 * always use this if you are operating with RGB colors. 160 * 161 * This is just a shortcut to calling 162 * 163 + GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL); 164 * 165 *@@changed V0.9.7 (2001-01-15) [umoeller]: turned macro into function 166 */ 167 168 BOOL gpihSwitchToRGB(HPS hps) 169 { 170 return (GpiCreateLogColorTable(hps, 0, LCOLF_RGB, 0, 0, NULL)); 154 171 } 155 172 -
trunk/src/helpers/helpers_post.in
r15 r23 71 71 $(HLPINC)\linklist.h $(HLPINC)\winh.h 72 72 73 $(OUTPUTDIR)\cctl_tooltip.obj: $(@B).c $(HLPINC)\comctl.h \73 $(OUTPUTDIR)\cctl_tooltip.obj: $(@B).c $(HLPINC)\comctl.h \ 74 74 $(PROJECTINC)\setup.h \ 75 75 $(HLPINC)\cnrh.h $(HLPINC)\except.h $(HLPINC)\gpih.h \ 76 76 $(HLPINC)\linklist.h $(HLPINC)\winh.h 77 77 78 $(OUTPUTDIR)\comctl.obj: $(@B).c $(HLPINC)\$(@B).h \78 $(OUTPUTDIR)\comctl.obj: $(@B).c $(HLPINC)\$(@B).h \ 79 79 $(PROJECTINC)\setup.h \ 80 80 $(HLPINC)\cnrh.h $(HLPINC)\except.h $(HLPINC)\gpih.h \ 81 81 $(HLPINC)\linklist.h $(HLPINC)\winh.h 82 82 83 $(OUTPUTDIR)\datetime.obj: $(@B).c $(HLPINC)\$(@B).h \ 83 $(OUTPUTDIR)\configsys.obj: $(@B).c $(HLPINC)\$(@B).h \ 84 $(PROJECTINC)\setup.h \ 85 $(HLPINC)\dosh.h $(HLPINC)\stringh.h $(HLPINC)\xstring.h 86 87 $(OUTPUTDIR)\datetime.obj: $(@B).c $(HLPINC)\$(@B).h \ 84 88 $(PROJECTINC)\setup.h 85 89 -
trunk/src/helpers/helpers_pre.in
r14 r23 72 72 $(OUTPUTDIR)\cctl_tooltip.obj \ 73 73 $(OUTPUTDIR)\comctl.obj \ 74 $(OUTPUTDIR)\configsys.obj \ 74 75 $(OUTPUTDIR)\cnrh.obj \ 75 76 $(OUTPUTDIR)\gpih.obj \ -
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 -
trunk/src/helpers/textview.c
r18 r23 2396 2396 if (pwndParams->fsStatus & WPM_TEXT) 2397 2397 { 2398 xstrcpy(&ptxvd->xfd.strViewText, pwndParams->pszText); 2398 xstrcpy(&ptxvd->xfd.strViewText, 2399 pwndParams->pszText, 2400 0); 2399 2401 ptxvd->lViewXOfs = 0; 2400 2402 ptxvd->lViewYOfs = 0; … … 3473 3475 3474 3476 // use text from window 3475 xstrcpy(&xfd.strViewText, pszViewText );3477 xstrcpy(&xfd.strViewText, pszViewText, 0); 3476 3478 3477 3479 // setup page -
trunk/src/helpers/winh.c
r21 r23 2432 2432 * 2433 2433 *@@added V0.9.6 (2000-10-16) [umoeller] 2434 *@@changed V0.9.7 (2001-01-15) [umoeller]: now using XSTRING 2434 2435 */ 2435 2436 2436 2437 VOID CallBatchCorrectly(PPROGDETAILS pProgDetails, 2437 P SZ *ppszParams,// in/out: modified parameters (reallocated)2438 PXSTRING pstrParams, // in/out: modified parameters (reallocated) 2438 2439 const char *pcszEnvVar, // in: env var spec'g command proc 2439 2440 // (e.g. "OS2_SHELL"); can be NULL … … 2442 2443 // XXX.CMD file as executable: 2443 2444 // fix args to /C XXX.CMD 2444 XSTRING strNewParams; 2445 xstrInit(&strNewParams, 200); 2446 xstrcpy(&strNewParams, "/C "); 2447 xstrcat(&strNewParams, pProgDetails->pszExecutable); 2448 if (*ppszParams) 2449 { 2450 // append old params 2451 xstrcat(&strNewParams, " "); 2452 xstrcat(&strNewParams, *ppszParams); 2453 free(*ppszParams); 2454 } 2455 *ppszParams = strNewParams.psz; 2456 // freed by caller 2445 2446 PSZ pszOldParams = NULL; 2447 ULONG ulOldParamsLength = pstrParams->ulLength; 2448 if (ulOldParamsLength) 2449 // we have parameters already: 2450 // make a backup... we'll append that later 2451 pszOldParams = strdup(pstrParams->psz); 2452 2453 // set new params to "/C filename.cmd" 2454 xstrcpy(pstrParams, "/C ", 0); 2455 xstrcat(pstrParams, 2456 pProgDetails->pszExecutable, 2457 0); 2458 2459 if (pszOldParams) 2460 { 2461 // .cmd had params: 2462 // append space and old params 2463 xstrcatc(pstrParams, ' '); 2464 xstrcat(pstrParams, 2465 pszOldParams, 2466 ulOldParamsLength); 2467 free(pszOldParams); 2468 } 2457 2469 2458 2470 // set executable to $(OS2_SHELL) … … 2508 2520 { 2509 2521 HAPP happ = NULLHANDLE; 2510 PSZ pszParamsPatched = NULL;2522 XSTRING strParamsPatched; 2511 2523 BOOL fIsWindowsApp = FALSE, 2512 2524 fIsWindowsEnhApp = FALSE; … … 2527 2539 // we need this for string manipulations below... 2528 2540 if (ProgDetails.pszParameters) 2529 pszParamsPatched = strdup(ProgDetails.pszParameters); 2541 xstrInitCopy(&strParamsPatched, 2542 ProgDetails.pszParameters, 2543 100); 2544 else 2545 // no old params: 2546 xstrInit(&strParamsPatched, 100); 2530 2547 2531 2548 // _Pmpf((__FUNCTION__ ": old progc: 0x%lX", pcProgDetails->progt.progc)); … … 2587 2604 { 2588 2605 // enhanced Win-OS/2 session: 2589 XSTRING str2; 2590 xstrInit(&str2, 200); 2591 xstrcpy(&str2, "/3 "); 2592 if (pszParamsPatched) 2606 PSZ psz = NULL; 2607 if (strParamsPatched.ulLength) 2608 // "/3 " + existing params 2609 psz = strdup(strParamsPatched.psz); 2610 2611 xstrcpy(&strParamsPatched, "/3 ", 0); 2612 2613 if (psz) 2593 2614 { 2594 // append existing params 2595 xstrcat(&str2, pszParamsPatched); 2596 free(pszParamsPatched); 2615 xstrcat(&strParamsPatched, psz, 0); 2616 free(psz); 2597 2617 } 2598 2599 pszParamsPatched = str2.psz;2600 2618 } 2601 2619 … … 2633 2651 { 2634 2652 CallBatchCorrectly(&ProgDetails, 2635 & pszParamsPatched,2653 &strParamsPatched, 2636 2654 "OS2_SHELL", 2637 2655 "CMD.EXE"); … … 2649 2667 { 2650 2668 CallBatchCorrectly(&ProgDetails, 2651 & pszParamsPatched,2669 &strParamsPatched, 2652 2670 NULL, 2653 2671 "COMMAND.COM"); … … 2716 2734 // _Pmpf((" new progc: 0x%lX", ProgDetails.progt.progc)); 2717 2735 2718 ProgDetails.pszParameters = pszParamsPatched;2736 ProgDetails.pszParameters = strParamsPatched.psz; 2719 2737 2720 2738 happ = WinStartApp(hwndNotify, 2721 2739 // receives WM_APPTERMINATENOTIFY 2722 2740 &ProgDetails, 2723 pszParamsPatched,2741 strParamsPatched.psz, 2724 2742 NULL, // "reserved", PMREF says... 2725 2743 SAF_INSTALLEDCMDLINE); … … 2734 2752 // _Pmpf((__FUNCTION__ ": got happ 0x%lX", happ)); 2735 2753 2736 if (pszParamsPatched) 2737 free(pszParamsPatched); 2754 xstrClear(&strParamsPatched); 2738 2755 if (pszWinOS2Env) 2739 2756 free(pszWinOS2Env); … … 3068 3085 *@@ winhReplaceWindowText: 3069 3086 * this is a combination of winhQueryWindowText 3070 * and xstrrpl (stringh.c) to replace substrings 3071 * in a window. 3087 * and strhFindReplace to replace substrings in a window. 3072 3088 * 3073 3089 * This is useful for filling in placeholders … … 3093 3109 { 3094 3110 ULONG ulOfs = 0; 3095 if (strh rpl(&pszText, &ulOfs, pszSearch, pszReplaceWith) > 0)3111 if (strhFindReplace(&pszText, &ulOfs, pszSearch, pszReplaceWith) > 0) 3096 3112 { 3097 3113 WinSetWindowText(hwnd, pszText); -
trunk/src/helpers/xstring.c
r21 r23 370 370 * If pxstr contains something, its contents are destroyed. 371 371 * 372 * With ulSourceLength, specify the length of pcszSource. 373 * If you specify 0, this function will run strlen(pcszSource) 374 * itself. If you already know the length of pcszSource (or 375 * only want to copy a substring), you can speed this function 376 * up a bit this way. 377 * 372 378 * Returns the length of the new string (excluding the null 373 379 * terminator), or null upon errors. … … 377 383 + XSTRING str; 378 384 + xstrInit(&str, 0); 379 + xstrcpy(&str, "blah" );385 + xstrcpy(&str, "blah", 0); 380 386 * 381 387 * This sequence can be abbreviated using xstrInitCopy. … … 383 389 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy 384 390 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 391 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength 385 392 */ 386 393 387 394 ULONG xstrcpy(PXSTRING pxstr, // in/out: string 388 const char *pcszSource) // in: source, can be NULL 389 { 390 xstrClear(pxstr); 395 const char *pcszSource, // in: source, can be NULL 396 ULONG ulSourceLength) // in: length of pcszSource or 0 397 { 398 // xstrClear(pxstr); NOOOO! this frees the string, we want to keep the memory 391 399 392 400 if (pxstr) 393 401 { 394 ULONG ulSourceLength = 0;395 402 if (pcszSource) 396 ulSourceLength = strlen(pcszSource);397 398 if (ulSourceLength)399 403 { 400 // we do have a source string: 401 ULONG cbNeeded = ulSourceLength + 1; 402 if (cbNeeded > pxstr->cbAllocated) 404 if (ulSourceLength == 0) 405 ulSourceLength = strlen(pcszSource); 406 407 if (ulSourceLength) 403 408 { 404 // we need more memory than we have previously 405 // allocated: 406 pxstr->cbAllocated = cbNeeded; 407 pxstr->psz = (PSZ)malloc(cbNeeded); 409 // we do have a source string: 410 ULONG cbNeeded = ulSourceLength + 1; 411 if (cbNeeded > pxstr->cbAllocated) 412 { 413 // we need more memory than we have previously 414 // allocated: 415 pxstr->cbAllocated = cbNeeded; 416 pxstr->psz = (PSZ)malloc(cbNeeded); 417 } 418 // else: we have enough memory 419 420 // strcpy(pxstr->psz, pcszSource); 421 memcpy(pxstr->psz, 422 pcszSource, 423 ulSourceLength); 424 *(pxstr->psz + ulSourceLength) = 0; 408 425 } 409 // else: we have enough memory 410 411 strcpy(pxstr->psz, pcszSource); 426 else 427 { 428 // no source specified or source is empty: 429 if (pxstr->cbAllocated) 430 // we did have a string: set to empty, 431 // but leave allocated memory intact 432 *(pxstr->psz) = 0; 433 // else: pxstr->psz is still NULL 434 } 435 436 // in all cases, set new length 437 pxstr->ulLength = ulSourceLength; 412 438 } 413 else414 {415 // no source specified or source is empty:416 if (pxstr->cbAllocated)417 // we did have a string: set to empty,418 // but leave allocated memory intact419 *(pxstr->psz) = 0;420 // else: pxstr->psz is still NULL421 }422 423 // in all cases, set new length424 pxstr->ulLength = ulSourceLength;425 439 } 426 440 … … 435 449 * If pxstr is empty, this behaves just like xstrcpy. 436 450 * 451 * With ulSourceLength, specify the length of pcszSource. 452 * If you specify 0, this function will run strlen(pcszSource) 453 * itself. If you already know the length of pcszSource (or 454 * only want to copy a substring), you can speed this function 455 * up a bit this way. 456 * 437 457 * Returns the length of the new string (excluding the null 438 458 * terminator) if the string was changed, or 0 if nothing … … 446 466 + XSTRING str; 447 467 + xstrInit(&str, 0); 448 + xstrcpy(&str, "blah" );449 + xstrcat(&str, "blup" );468 + xstrcpy(&str, "blah", 0); 469 + xstrcat(&str, "blup", 0); 450 470 * 451 471 * After this, str.psz points to a new string containing … … 458 478 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 459 479 *@@changed V0.9.7 (2000-12-10) [umoeller]: return value was wrong 480 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength 460 481 */ 461 482 462 483 ULONG xstrcat(PXSTRING pxstr, // in/out: string 463 const char *pcszSource) // in: source, can be NULL 484 const char *pcszSource, // in: source, can be NULL 485 ULONG ulSourceLength) // in: length of pcszSource or 0 464 486 { 465 487 ULONG ulrc = 0; … … 467 489 if (pxstr) 468 490 { 469 ULONG ulSourceLength = 0;470 491 if (pcszSource) 471 ulSourceLength = strlen(pcszSource);472 473 if (ulSourceLength)474 492 { 475 // we do have a source string: 476 477 // 1) memory management 478 ULONG cbNeeded = pxstr->ulLength + ulSourceLength + 1; 479 if (cbNeeded > pxstr->cbAllocated) 493 if (ulSourceLength == 0) 494 ulSourceLength = strlen(pcszSource); 495 496 if (ulSourceLength) 480 497 { 481 // we need more memory than we have previously 482 // allocated: 483 if (pxstr->cbAllocated) 484 // appendee already had memory: 485 // reallocate 486 pxstr->psz = (PSZ)realloc(pxstr->psz, 487 cbNeeded); 488 else 489 // appendee has no memory: 490 pxstr->psz = (PSZ)malloc(cbNeeded); 491 492 pxstr->cbAllocated = cbNeeded; 493 // ulLength is unchanged yet 494 } 495 // else: we have enough memory, both if appendee 496 // is empty or not empty 497 498 // now we have: 499 // -- if appendee (pxstr) had enough memory, no problem 500 // -- if appendee (pxstr) needed more memory 501 // -- and was not empty: pxstr->psz now points to a 502 // reallocated copy of the old string 503 // -- and was empty: pxstr->psz now points to a 504 // new (unitialized) buffer 505 506 // 2) append source string: 507 memcpy(pxstr->psz + pxstr->ulLength, 508 pcszSource, 509 ulSourceLength + 1); // null terminator 510 511 // in all cases, set new length 512 pxstr->ulLength += ulSourceLength; 513 ulrc = pxstr->ulLength; // V0.9.7 (2000-12-10) [umoeller] 514 515 } // end if (ulSourceLength) 498 // we do have a source string: 499 500 // 1) memory management 501 ULONG cbNeeded = pxstr->ulLength + ulSourceLength + 1; 502 if (cbNeeded > pxstr->cbAllocated) 503 { 504 // we need more memory than we have previously 505 // allocated: 506 if (pxstr->cbAllocated) 507 // appendee already had memory: 508 // reallocate 509 pxstr->psz = (PSZ)realloc(pxstr->psz, 510 cbNeeded); 511 else 512 // appendee has no memory: 513 pxstr->psz = (PSZ)malloc(cbNeeded); 514 515 pxstr->cbAllocated = cbNeeded; 516 // ulLength is unchanged yet 517 } 518 // else: we have enough memory, both if appendee 519 // is empty or not empty 520 521 // now we have: 522 // -- if appendee (pxstr) had enough memory, no problem 523 // -- if appendee (pxstr) needed more memory 524 // -- and was not empty: pxstr->psz now points to a 525 // reallocated copy of the old string 526 // -- and was empty: pxstr->psz now points to a 527 // new (unitialized) buffer 528 529 // 2) append source string: 530 memcpy(pxstr->psz + pxstr->ulLength, 531 pcszSource, 532 ulSourceLength + 1); // null terminator 533 534 // in all cases, set new length 535 pxstr->ulLength += ulSourceLength; 536 ulrc = pxstr->ulLength; // V0.9.7 (2000-12-10) [umoeller] 537 538 } // end if (ulSourceLength) 539 } 540 516 541 // else no source specified or source is empty: 517 542 // do nothing … … 601 626 602 627 /* 628 *@@ xstrrpl: 629 * replaces cSearchLen characters in pxstr, starting 630 * at the position ulStart, with the string 631 * in pxstrReplaceWith. 632 * 633 * Returns the new length of the string, excluding 634 * the null terminator, or 0 if the replacement failed 635 * (e.g. because the offsets were too large). 636 * 637 * This has been extracted from xstrFindReplace because 638 * if you already know the position of a substring, 639 * you can now call this directly. This properly 640 * reallocates the string if more memory is needed. 641 * 642 * Example: 643 * 644 + XSTRING xstr, xstrReplacement; 645 + xstrInitCopy(&xstr, "This is a test string."); 646 + // positions: 0123456789012345678901 647 + // 1 2 648 + 649 + xstrInitCopy(&xstrReplacement, "stupid"); 650 + 651 + xstrrpl(&xstr, 652 + 10, // position of "test" 653 + 4, // length of "test" 654 + &xstrReplacement); 655 * 656 * This would yield "This is a stupid string." 657 * 658 *@@added V0.9.7 (2001-01-15) [umoeller] 659 */ 660 661 ULONG xstrrpl(PXSTRING pxstr, // in/out: string 662 ULONG ulFirstReplOfs, // in: ofs of first char to replace 663 ULONG cReplLen, // in: no. of chars to replace 664 const XSTRING *pstrReplaceWith) // in: string to replace chars with 665 { 666 ULONG ulrc = 0; 667 668 // security checks... 669 if ( (ulFirstReplOfs + cReplLen <= pxstr->ulLength) 670 && (pstrReplaceWith) 671 // && (pstrReplaceWith->ulLength) no, this can be empty 672 ) 673 { 674 ULONG cReplaceLen = pstrReplaceWith->ulLength; 675 // can be 0! 676 677 // length of new string 678 ULONG cbNeeded = pxstr->ulLength 679 + cReplaceLen 680 - cReplLen 681 + 1; // null terminator 682 // offset where pszSearch was found 683 // ulFirstReplOfs = pFound - pxstr->psz; now ulFirstReplOfs 684 PSZ pFound = pxstr->psz + ulFirstReplOfs; 685 686 // now check if we have enough memory... 687 if (pxstr->cbAllocated < cbNeeded) 688 { 689 // no, we need more memory: 690 // allocate new buffer 691 PSZ pszNew = (PSZ)malloc(cbNeeded); 692 693 if (ulFirstReplOfs) 694 // "found" was not at the beginning: 695 // copy from beginning up to found-offset 696 memcpy(pszNew, 697 pxstr->psz, 698 ulFirstReplOfs); // up to "found" 699 700 if (cReplaceLen) 701 { 702 // we have a replacement: 703 // insert it next 704 memcpy(pszNew + ulFirstReplOfs, 705 pstrReplaceWith->psz, 706 cReplaceLen + 1); // include null terminator 707 } 708 709 // copy rest: 710 // pxstr frontFOUNDtail 711 // 0 1 712 // 01234567890123 713 // ³ ³ ³ ³ 714 // ³ ³ ÀÄ ulFirstReplOfs + cReplLen = 10 715 // ³ ³ ³ 716 // ³ ÀÄ ulFirstReplOfs = 5 717 // ³ ³ 718 // pxstr->ulLength = 14 719 memcpy(pszNew + ulFirstReplOfs + cReplaceLen, 720 pFound + cReplLen, 721 // remaining bytes: 722 pxstr->ulLength - ulFirstReplOfs - cReplLen // 9 723 + 1); // null terminator 724 725 // replace old buffer with new one 726 free(pxstr->psz); 727 pxstr->psz = pszNew; 728 pxstr->ulLength = cbNeeded - 1; 729 pxstr->cbAllocated = cbNeeded; 730 } // end if (pxstr->cbAllocated < cbNeeded) 731 else 732 { 733 // we have enough memory left, 734 // we can just overwrite in the middle... 735 736 PSZ pszAfterFoundBackup = 0; 737 // calc length of string after "found" 738 ULONG cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen; 739 740 // if "replace" is longer than "found", 741 // make a backup of the stuff after "found", 742 // or this would get overwritten 743 if (cReplaceLen > cReplLen) 744 { 745 pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1); 746 memcpy(pszAfterFoundBackup, 747 pFound + cReplLen, 748 cTailLength + 1); 749 } 750 751 // now overwrite "found" in the middle 752 if (cReplaceLen) 753 { 754 memcpy(pxstr->psz + ulFirstReplOfs, 755 pstrReplaceWith->psz, 756 cReplaceLen); // no null terminator 757 } 758 759 // now append tail (stuff after "found") again... 760 if (pszAfterFoundBackup) 761 { 762 // we made a backup above: 763 memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen, 764 pszAfterFoundBackup, 765 cTailLength + 1); 766 free(pszAfterFoundBackup); 767 // done! 768 } 769 else 770 // no backup: 771 if (cReplaceLen < cReplLen) 772 // "replace" is shorter than "found: 773 memcpy(pxstr->psz + ulFirstReplOfs + cReplaceLen, 774 pFound + cReplLen, 775 cTailLength + 1); 776 // else (cReplaceLen == cReplLen): 777 // we can leave the tail as it is 778 779 pxstr->ulLength = cbNeeded - 1; 780 } 781 782 ulrc = cbNeeded - 1; 783 } // end checks 784 785 return (ulrc); 786 } 787 788 /* 603 789 *@@ xstrFindWord: 604 790 * searches for pstrFind in pxstr, starting at ulOfs. … … 668 854 669 855 /* 670 *@@ xstr rpl:856 *@@ xstrFindReplace: 671 857 * replaces the first occurence of pstrSearch with 672 858 * pstrReplace in pxstr. … … 688 874 * (*pulOffset == 0), this starts from the beginning 689 875 * of pxstr. 876 * 690 877 * If the string was found, *pulOffset will be set to the 691 878 * first character after the new replacement string. This … … 697 884 * speed advantage): 698 885 * 699 * -- strh rploperates on C strings only;700 * 701 * -- xstr crpluses C strings for the search and replace886 * -- strhFindReplace operates on C strings only; 887 * 888 * -- xstrFindReplaceC uses C strings for the search and replace 702 889 * parameters. 703 890 * 704 891 * <B>Example usage:</B> 705 892 * 706 + XSTRING str; 707 + ULONG ulOffset = 0; 708 + xstrInit(&str, 0); 709 + xstrcpy(&str, "Test phrase 1. Test phrase 2."); 710 + while (xstrrpl(&str, 711 + &ulPos, // in/out: offset 712 + "Test", // search 713 + "Dummy") // replace 893 + XSTRING strBuf, 894 + strFind, 895 + strRepl; 896 + size_t ShiftTable[256]; 897 + BOOL fRepeat = FALSE; 898 + ULONG ulOffset = 0; 899 + 900 + xstrInitCopy(&strBuf, "Test phrase 1. Test phrase 2.", 0); 901 + xstrInitSet(&strFind, "Test"); 902 + xstrInitSet(&strRepl, "Dummy"); 903 + while (xstrFindReplace(&str, 904 + &ulPos, // in/out: offset 905 + &strFind, // search 906 + &strRepl, // replace 907 + ShiftTable, 908 + &fRepeat)) 714 909 + ; 715 910 * … … 722 917 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 723 918 *@@changed V0.9.6 (2000-11-12) [umoeller]: now using strhmemfind 724 */ 725 726 ULONG xstrrpl(PXSTRING pxstr, // in/out: string 727 PULONG pulOfs, // in: where to begin search (0 = start); 728 // out: ofs of first char after replacement string 729 const XSTRING *pstrSearch, // in: search string; cannot be NULL 730 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL 731 size_t *pShiftTable, // in: shift table (see strhmemfind) 732 PBOOL pfRepeatFind) // in: repeat find? (see strhmemfind) 919 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrrpl; extracted new xstrrpl 920 */ 921 922 ULONG xstrFindReplace(PXSTRING pxstr, // in/out: string 923 PULONG pulOfs, // in: where to begin search (0 = start); 924 // out: ofs of first char after replacement string 925 const XSTRING *pstrSearch, // in: search string; cannot be NULL 926 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL 927 size_t *pShiftTable, // in: shift table (see strhmemfind) 928 PBOOL pfRepeatFind) // in: repeat find? (see strhmemfind) 733 929 { 734 930 ULONG ulrc = 0; // default: not found … … 744 940 { 745 941 // yes: 746 /* PSZ pFound = strstr(pxstr->psz + *pulOfs,747 pstrSearch->psz); */748 PSZ pFound = (PSZ)strhmemfind(pxstr->psz + *pulOfs, // in: haystack749 pxstr->ulLength - *pulOfs,750 pstrSearch->psz,751 cSearchLen,752 pShiftTable,753 pfRepeatFind);942 ULONG ulOfs = *pulOfs; 943 const char *pFound 944 = (const char *)strhmemfind(pxstr->psz + ulOfs, // in: haystack 945 pxstr->ulLength - ulOfs, 946 pstrSearch->psz, 947 cSearchLen, 948 pShiftTable, 949 pfRepeatFind); 754 950 if (pFound) 755 951 { 952 ULONG ulFirstReplOfs = pFound - pxstr->psz; 756 953 // found in buffer from ofs: 757 ULONG cReplaceLen = pstrReplace->ulLength; 758 // can be 0! 759 760 // length of new string 761 ULONG cbNeeded = pxstr->ulLength 762 + cReplaceLen 763 - cSearchLen 764 + 1, // null terminator 765 // offset where pszSearch was found 766 ulFoundOfs = pFound - pxstr->psz; 767 768 // now check if we have enough memory... 769 if (pxstr->cbAllocated < cbNeeded) 770 { 771 // no, we need more memory: 772 // allocate new buffer 773 PSZ pszNew = (PSZ)malloc(cbNeeded); 774 775 if (ulFoundOfs) 776 // "found" was not at the beginning: 777 // copy from beginning up to found-offset 778 memcpy(pszNew, 779 pxstr->psz, 780 ulFoundOfs); // up to "found" 781 782 if (cReplaceLen) 783 { 784 // we have a replacement: 785 // insert it next 786 memcpy(pszNew + ulFoundOfs, 787 pstrReplace->psz, 788 cReplaceLen + 1); // include null terminator 789 } 790 791 // copy rest: 792 // pxstr frontFOUNDtail 793 // 0 1 794 // 01234567890123 795 // ³ ³ ³ ³ 796 // ³ ³ ÀÄ ulFoundOfs + cSearchLen = 10 797 // ³ ³ ³ 798 // ³ ÀÄ ulFoundOfs = 5 799 // ³ ³ 800 // pxstr->ulLength = 14 801 memcpy(pszNew + ulFoundOfs + cReplaceLen, 802 pFound + cSearchLen, 803 // remaining bytes: 804 pxstr->ulLength - ulFoundOfs - cSearchLen // 9 805 + 1); // null terminator 806 807 // replace old buffer with new one 808 free(pxstr->psz); 809 pxstr->psz = pszNew; 810 pxstr->ulLength = cbNeeded - 1; 811 pxstr->cbAllocated = cbNeeded; 812 } // end if (pxstr->cbAllocated < cbNeeded) 813 else 814 { 815 // we have enough memory left, 816 // we can just overwrite in the middle... 817 818 PSZ pszAfterFoundBackup = 0; 819 // calc length of string after "found" 820 ULONG cTailLength = pxstr->ulLength - ulFoundOfs - cSearchLen; 821 822 // if "replace" is longer than "found", 823 // make a backup of the stuff after "found", 824 // or this would get overwritten 825 if (cReplaceLen > cSearchLen) 826 { 827 pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1); 828 memcpy(pszAfterFoundBackup, 829 pFound + cSearchLen, 830 cTailLength + 1); 831 } 832 833 // now overwrite "found" in the middle 834 if (cReplaceLen) 835 { 836 memcpy(pxstr->psz + ulFoundOfs, 837 pstrReplace->psz, 838 cReplaceLen); // no null terminator 839 } 840 841 // now append tail (stuff after "found") again... 842 if (pszAfterFoundBackup) 843 { 844 // we made a backup above: 845 memcpy(pxstr->psz + ulFoundOfs + cReplaceLen, 846 pszAfterFoundBackup, 847 cTailLength + 1); 848 free(pszAfterFoundBackup); 849 // done! 850 } 851 else 852 // no backup: 853 if (cReplaceLen < cSearchLen) 854 // "replace" is shorter than "found: 855 memcpy(pxstr->psz + ulFoundOfs + cReplaceLen, 856 pFound + cSearchLen, 857 cTailLength + 1); 858 // else (cReplaceLen == cSearchLen): 859 // we can leave the tail as it is 860 861 pxstr->ulLength = cbNeeded - 1; 862 } 954 // replace pFound with pstrReplace 955 ulrc = xstrrpl(pxstr, 956 ulFirstReplOfs, // where to start 957 cSearchLen, // chars to replace 958 pstrReplace); 863 959 864 960 // return new length 865 ulrc = cbNeeded - 1; 866 *pulOfs = ulFoundOfs + cReplaceLen; 961 *pulOfs = ulFirstReplOfs + pstrReplace->ulLength; 867 962 } // end if (pFound) 868 963 } // end if ( (*pulOfs < pxstr->ulLength) ... … … 873 968 874 969 /* 875 *@@ xstr crpl:876 * wrapper around xstr rpl() which allows using C strings877 * for the find and replace parameters.970 *@@ xstrFindReplaceC: 971 * wrapper around xstrFindReplace() which allows using 972 * C strings for the find and replace parameters. 878 973 * 879 974 * This creates two temporary XSTRING's for pcszSearch 880 * pcszReplace. As a result, this is slower than xstrrpl. 975 * and pcszReplace and thus cannot use the shift table 976 * for repetitive searches. As a result, this is slower 977 * than xstrFindReplace. 978 * 881 979 * If you search with the same strings several times, 882 * you'll be better off using xstr rpl() directly.980 * you'll be better off using xstrFindReplace() directly. 883 981 * 884 982 *@@added V0.9.6 (2000-11-01) [umoeller] 885 * /886 887 ULONG xstrcrpl(PXSTRING pxstr, // in/out: string 888 PULONG pulOfs, // in: where to begin search (0 = start); 889 // out: ofs of first char after replacement string890 const char *pcszSearch, // in: search string; cannot be NULL891 const char *pcszReplace) // in: replacementstring; cannot be NULL892 { 893 // ULONG ulrc = 0; 983 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrcrpl 984 */ 985 986 ULONG xstrFindReplaceC(PXSTRING pxstr, // in/out: string 987 PULONG pulOfs, // in: where to begin search (0 = start); 988 // out: ofs of first char after replacement string 989 const char *pcszSearch, // in: search string; cannot be NULL 990 const char *pcszReplace) // in: replacement string; cannot be NULL 991 { 894 992 XSTRING xstrFind, 895 993 xstrReplace; … … 902 1000 xstrInitSet(&xstrReplace, (PSZ)pcszReplace); 903 1001 904 return (xstrrpl(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat)); 1002 return (xstrFindReplace(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat)); 1003 } 1004 1005 /* 1006 *@@ xstrConvertLineFormat: 1007 * converts between line formats. 1008 * 1009 * If (fToCFormat == TRUE), all \r\n pairs are replaced 1010 * with \n chars (UNIX or C format). 1011 * 1012 * Reversely, if (fToCFormat == FALSE), all \n chars 1013 * are converted to \r\n pairs (DOS and OS/2 formats). 1014 * No check is made whether this has already been done. 1015 * 1016 *@@added V0.9.7 (2001-01-15) [umoeller] 1017 */ 1018 1019 VOID xstrConvertLineFormat(PXSTRING pxstr, 1020 BOOL fToCFormat) // in: if TRUE, to C format; if FALSE, to OS/2 format. 1021 { 1022 XSTRING strFind, 1023 strRepl; 1024 size_t ShiftTable[256]; 1025 BOOL fRepeat = FALSE; 1026 ULONG ulOfs = 0; 1027 1028 if (fToCFormat) 1029 { 1030 // OS/2 to C: 1031 xstrInitSet(&strFind, "\r\n"); 1032 xstrInitSet(&strRepl, "\n"); 1033 } 1034 else 1035 { 1036 // C to OS/2: 1037 xstrInitSet(&strFind, "\n"); 1038 xstrInitSet(&strRepl, "\r\n"); 1039 } 1040 1041 while (xstrFindReplace(pxstr, 1042 &ulOfs, 1043 &strFind, 1044 &strRepl, 1045 ShiftTable, 1046 &fRepeat)) 1047 ; 905 1048 } 906 1049 … … 912 1055 strFind, 913 1056 strReplace; 1057 size_t shift[256]; 1058 BOOL fRepeat = FALSE; 914 1059 ULONG ulOfs = 0; 915 1060 … … 918 1063 xstrInit(&strReplace, 0); 919 1064 920 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !" );921 xstrcpy(&strFind, "Test" );922 xstrcpy(&strReplace, "Dummy" );1065 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0); 1066 xstrcpy(&strFind, "Test", 0); 1067 xstrcpy(&strReplace, "Dummy", 0); 923 1068 924 1069 printf("Old string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 925 1070 926 while (xstrrpl(&str, 927 ulOfs, 1071 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1072 1073 fRepeat = FALSE; 1074 ulOfs = 0; 1075 while (xstrFindReplace(&str, 1076 &ulOfs, 1077 &strFind, 1078 &strReplace, 1079 shift, &fRepeat)); 1080 ; 1081 1082 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1083 1084 xstrcpy(&strFind, strReplace.psz, 0); 1085 xstrClear(&strReplace); 1086 1087 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1088 1089 fRepeat = FALSE; 1090 ulOfs = 0; 1091 while (xstrFindReplace(&str, 1092 &ulOfs, 928 1093 &strFind, 929 1094 &strReplace, 930 &ulOfs))1095 shift, &fRepeat)); 931 1096 ; 932 1097 933 1098 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 934 1099 935 xstrcpy(&strFind, strReplace.psz); 936 xstrClear(&strReplace); 1100 xstrcpy(&strFind, " ", 0); 1101 xstrcpy(&strReplace, ".", 0); 1102 1103 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1104 1105 fRepeat = FALSE; 937 1106 ulOfs = 0; 938 while (xstr rpl(&str,939 ulOfs,1107 while (xstrFindReplace(&str, 1108 &ulOfs, 940 1109 &strFind, 941 1110 &strReplace, 942 &ulOfs))1111 shift, &fRepeat)); 943 1112 ; 944 1113 945 1114 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 946 1115 947 xstrcpy(&strFind, " "); 948 xstrcpy(&strReplace, "."); 1116 xstrcpy(&strFind, ".", 0); 1117 xstrcpy(&strReplace, "*.........................*", 0); 1118 1119 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1120 1121 fRepeat = FALSE; 949 1122 ulOfs = 0; 950 while (xstr rpl(&str,951 ulOfs,1123 while (xstrFindReplace(&str, 1124 &ulOfs, 952 1125 &strFind, 953 1126 &strReplace, 954 &ulOfs))1127 shift, &fRepeat)); 955 1128 ; 956 1129 957 1130 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 958 1131 959 xstrcpy(&strFind, "."); 960 xstrcpy(&strReplace, "***************************"); 1132 printf("Reserving extra mem.\n"); 1133 1134 xstrReserve(&str, 6000); 1135 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1136 1137 xstrcpy(&strFind, "..........", 0); 1138 xstrcpy(&strReplace, "@", 0); 1139 1140 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz); 1141 1142 fRepeat = FALSE; 961 1143 ulOfs = 0; 962 while (xstr rpl(&str,963 ulOfs,1144 while (xstrFindReplace(&str, 1145 &ulOfs, 964 1146 &strFind, 965 1147 &strReplace, 966 &ulOfs))1148 shift, &fRepeat)); 967 1149 ; 968 1150 969 1151 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 970 1152 971 xstrcpy(&strFind, "*"); 972 xstrClear(&strReplace); 973 ulOfs = 0; 974 while (xstrrpl(&str, 975 ulOfs, 976 &strFind, 977 &strReplace, 978 &ulOfs)) 979 ; 980 981 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 982 } */ 983 1153 return (0); 1154 } 1155 */ 1156 1157
Note:
See TracChangeset
for help on using the changeset viewer.