Changeset 12
- Timestamp:
- Nov 4, 2000, 8:55:45 PM (25 years ago)
- Location:
- trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/helpers/stringh.h
r7 r12 59 59 ULONG strhStrip(PSZ psz); 60 60 61 PSZ strhins(const char *pcszBuffer, 62 ULONG ulInsertOfs, 63 const char *pcszInsert); 64 65 ULONG strhrpl(PSZ *ppszBuf, 66 ULONG ulOfs, 67 const char *pcszSearch, 68 const char *pcszReplace, 69 PULONG pulAfterOfs); 70 61 71 ULONG strhWords(PSZ psz); 62 72 … … 101 111 PSZ strhFindNextLine(PSZ pszSearchIn, PULONG pulOffset); 102 112 103 PSZ strhFindKey(PSZ pszSearchIn, 104 PSZ pszKey, 105 BOOL *pfIsAllUpperCase); 106 107 PSZ strhGetParameter(PSZ pszSearchIn, PSZ pszKey, PSZ pszCopyTo, ULONG cbCopyTo); 108 109 PSZ strhSetParameter(PSZ* ppszSearchIn, 110 PSZ pszKey, 113 PSZ strhFindKey(const char *pcszSearchIn, 114 const char *pcszKey, 115 PBOOL pfIsAllUpperCase); 116 117 PSZ strhGetParameter(const char *pcszSearchIn, 118 const char *pcszKey, 119 PSZ pszCopyTo, 120 ULONG cbCopyTo); 121 122 PSZ strhSetParameter(PSZ* ppszBuf, 123 const char *pcszKey, 111 124 PSZ pszNewParam, 112 125 BOOL fRespectCase); -
trunk/include/helpers/textview.h
r7 r12 19 19 * GNU General Public License for more details. 20 20 * 21 *@@include #define INCL_SPL // for printing functions21 *@@include #define INCL_SPL // for printing functions 22 22 *@@include #include <os2.h> 23 *@@include #include "linklist.h" // for device-independent functions 23 *@@include #include "linklist.h" // for device-independent functions 24 *@@include #include "xstring.h" // for device-independent functions 24 25 *@@include #include "textview.h" 25 26 */ … … 329 330 330 331 #ifdef LINKLIST_HEADER_INCLUDED 332 #ifdef XSTRING_HEADER_INCLUDED 331 333 332 334 /* … … 340 342 { 341 343 // input to txvFormatText 342 PSZ pszViewText; // original view text344 XSTRING strViewText; // original view text 343 345 // from WinSetWindowText 344 346 … … 371 373 PRECTL prclView, 372 374 BOOL fFullRecalc); 373 375 #endif 374 376 #endif 375 377 -
trunk/include/helpers/xstring.h
r7 r12 33 33 34 34 /* 35 *@@ XSTR :36 * string type for the strhx* functions.37 * That's a simple pointer to a PSZ.35 *@@ XSTRING: 36 * 37 *@@added V0.9.6 (2000-11-01) [umoeller] 38 38 */ 39 39 40 typedef PSZ* XSTR; 40 typedef struct _XSTRING 41 { 42 PSZ psz; // ptr to string or NULL 43 ULONG ulLength; // length of *psz 44 ULONG cbAllocated; // memory allocated in *psz 45 // (>= ulLength + 1) 46 } XSTRING, *PXSTRING; 47 48 void xstrInit(PXSTRING pxstr, 49 ULONG ulPreAllocate); 50 51 void xstrInitSet(PXSTRING pxstr, 52 PSZ pszNew); 53 54 void xstrInitCopy(PXSTRING pxstr, 55 const char *pcszSource); 56 57 void xstrClear(PXSTRING pxstr); 58 59 PXSTRING xstrCreate(ULONG ulPreAllocate); 60 61 VOID xstrFree(PXSTRING pxstr); 62 63 ULONG xstrset(PXSTRING pxstr, 64 PSZ pszNew); 65 66 ULONG xstrcpy(PXSTRING pxstr, 67 const char *pcszSource); 68 69 ULONG xstrcat(PXSTRING pxstr, 70 const char *pcszSource); 41 71 42 72 /* … … 49 79 #define xstrIsString(psz) ( (psz != 0) && (*(psz) != 0) ) 50 80 51 ULONG xstrcpy(XSTR ppszBuf, 52 const char *pszString); 81 ULONG xstrrpl(PXSTRING pxstr, 82 ULONG ulOfs, 83 const XSTRING *pstrSearch, 84 const XSTRING *pstrReplace, 85 PULONG pulAfterOfs); 53 86 54 #ifdef __XWPMEMDEBUG__ // setup.h, helpers\memdebug.c 55 ULONG xstrcatDebug(XSTR ppszBuf, 56 const char *pszString, 57 const char *file, 58 unsigned long line, 59 const char *function); 60 #define xstrcat(a, b) xstrcatDebug((a), (b), __FILE__, __LINE__, __FUNCTION__) 61 #else 62 ULONG xstrcat(XSTR ppszBuf, 63 const char *pszString); 64 #endif 65 66 #ifdef __XWPMEMDEBUG__ // setup.h, helpers\memdebug.c 67 ULONG xstrrplDebug(PSZ *ppszBuf, 68 ULONG ulOfs, 69 const char *pszSearch, 70 const char *pszReplace, 71 PULONG pulAfterOfs, 72 const char *file, 73 unsigned long line, 74 const char *function); 75 #define xstrrpl(a, b, c, d, e) xstrrplDebug((a), (b), (c), (d), (e), __FILE__, __LINE__, __FUNCTION__) 76 #else 77 ULONG xstrrpl(PSZ *ppszBuf, 78 ULONG ulOfs, 79 const char *pszSearch, 80 const char *pszReplace, 81 PULONG pulAfterOfs); 82 #endif 83 84 PSZ xstrins(PSZ pszBuffer, 85 ULONG ulInsertOfs, 86 const char *pszInsert); 87 87 ULONG xstrcrpl(PXSTRING pxstr, 88 ULONG ulOfs, 89 const char *pcszSearch, 90 const char *pcszReplace, 91 PULONG pulAfterOfs); 88 92 #endif 89 93 -
trunk/src/helpers/prfh2.c
r8 r12 622 622 } 623 623 624 if ( (Profiles.pszSysName) 625 && (Profiles.pszUserName) 626 ) 627 { 624 if (Profiles.pszSysName) 628 625 free(Profiles.pszSysName); 626 if (Profiles.pszUserName) 629 627 free(Profiles.pszUserName); 630 }631 628 632 629 if (fLog) -
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); -
trunk/src/helpers/textview.c
r8 r12 269 269 lstInit(&pxfd->llWords, 270 270 TRUE); // auto-free items 271 xstrInit(&pxfd->strViewText, 0); 271 272 } 272 273 … … 1088 1089 pxfd->ulViewportCY = 0; 1089 1090 1090 if (pxfd-> pszViewText)1091 if (pxfd->strViewText.cbAllocated) 1091 1092 { 1092 ULONG ulTextLen = strlen(pxfd->pszViewText);1093 ULONG ulTextLen = pxfd->strViewText.ulLength; 1093 1094 1094 1095 FORMATLINEBUF flbuf; … … 1102 1103 flbuf.pfmtc = &pxfd->fmtcStandard; 1103 1104 flbuf.lPointSize = pxfd->fmtcStandard.lPointSize; 1104 flbuf.pLastChar = pxfd-> pszViewText+ ulTextLen;1105 flbuf.pLastChar = pxfd->strViewText.psz + ulTextLen; 1105 1106 1106 1107 if (ulTextLen) … … 1115 1116 */ 1116 1117 1117 PSZ pCurrent = pxfd-> pszViewText;1118 PSZ pCurrent = pxfd->strViewText.psz; 1118 1119 1119 1120 // loop until null terminator … … 2394 2395 if (pwndParams->fsStatus & WPM_TEXT) 2395 2396 { 2396 xstrcpy(&ptxvd->xfd. pszViewText, pwndParams->pszText);2397 xstrcpy(&ptxvd->xfd.strViewText, pwndParams->pszText); 2397 2398 ptxvd->lViewXOfs = 0; 2398 2399 ptxvd->lViewYOfs = 0; … … 3061 3062 3062 3063 case WM_DESTROY: 3063 if (ptxvd->xfd.pszViewText) 3064 free(ptxvd->xfd.pszViewText); 3064 xstrClear(&ptxvd->xfd.strViewText); 3065 3065 lstClear(&ptxvd->xfd.llRectangles); 3066 3066 lstClear(&ptxvd->xfd.llWords); … … 3472 3472 3473 3473 // use text from window 3474 x fd.pszViewText = pszViewText;3474 xstrcpy(&xfd.strViewText, pszViewText); 3475 3475 3476 3476 // setup page … … 3621 3621 hdc, 3622 3622 hps, 3623 ptxvd->xfd. pszViewText,3623 ptxvd->xfd.strViewText.psz, 3624 3624 ulSize, 3625 3625 pszFaceName, -
trunk/src/helpers/winh.c
r11 r12 2368 2368 // XXX.CMD file as executable: 2369 2369 // fix args to /C XXX.CMD 2370 PSZ pszNewParams = strdup("/C "); 2371 xstrcat(&pszNewParams, pProgDetails->pszExecutable); 2370 XSTRING strNewParams; 2371 xstrInit(&strNewParams, 200); 2372 xstrcpy(&strNewParams, "/C "); 2373 xstrcat(&strNewParams, pProgDetails->pszExecutable); 2372 2374 if (*ppszParams) 2373 2375 { 2374 2376 // append old params 2375 xstrcat(& pszNewParams, " ");2376 xstrcat(& pszNewParams, *ppszParams);2377 xstrcat(&strNewParams, " "); 2378 xstrcat(&strNewParams, *ppszParams); 2377 2379 free(*ppszParams); 2378 2380 } 2379 *ppszParams = pszNewParams;2381 *ppszParams = strNewParams.psz; 2380 2382 // freed by caller 2381 2383 … … 2493 2495 { 2494 2496 // enhanced Win-OS/2 session: 2495 PSZ psz2 = strdup("/3 "); 2497 XSTRING str2; 2498 xstrInit(&str2, 200); 2499 xstrcpy(&str2, "/3 "); 2496 2500 if (pszParamsPatched) 2497 2501 { 2498 2502 // append existing params 2499 xstrcat(& psz2, pszParamsPatched);2503 xstrcat(&str2, pszParamsPatched); 2500 2504 free(pszParamsPatched); 2501 2505 } 2502 2506 2503 pszParamsPatched = psz2;2507 pszParamsPatched = str2.psz; 2504 2508 } 2505 2509 … … 2975 2979 if (pszText) 2976 2980 { 2977 if ( xstrrpl(&pszText, 0, pszSearch, pszReplaceWith, 0) > 0)2981 if (strhrpl(&pszText, 0, pszSearch, pszReplaceWith, 0) > 0) 2978 2982 { 2979 2983 WinSetWindowText(hwnd, pszText); -
trunk/src/helpers/xml.c
r11 r12 40 40 * 2) This doesn't use 16-bit characters, but 8-bit characters. 41 41 * 42 *@@header " xml.h"42 *@@header "helpers\xml.h" 43 43 *@@added V0.9.6 (2000-10-29) [umoeller] 44 44 */ … … 77 77 78 78 /* 79 *@@category: Helpers\C helpers\XML parsing 80 */ 79 *@@category: Helpers\C helpers\XML\Node management 80 */ 81 82 /* ****************************************************************** 83 * 84 * Node Management 85 * 86 ********************************************************************/ 81 87 82 88 /* … … 183 189 return (ulrc); 184 190 } 191 192 /* 193 *@@category: Helpers\C helpers\XML\Parsing 194 */ 195 196 /* ****************************************************************** 197 * 198 * Tokenizing (Compiling) 199 * 200 ********************************************************************/ 201 202 /* 203 *@@ xmlTokenize: 204 * this takes any block of XML text and "tokenizes" 205 * it. 206 * 207 * Tokenizing (or compiling, or "scanning" in bison/flex 208 * terms) means preparing the XML code for parsing later. 209 * This finds all tags and tag attributes and creates 210 * special codes for them in the output buffer. 211 * 212 * For example: 213 + 214 + <TAG ATTR="text"> block </TAG> 215 + 216 * becomes 217 * 218 + 0xFF escape code 219 + 0x01 tag start code 220 + "TAG" tag name 221 + 0xFF end of tag name code 222 + 223 + 0xFF escape code 224 + 0x03 attribute name code 225 + "ATTR" attribute name 226 + 0xFF 227 + "text" attribute value (without quotes) 228 + 0xFF end of attribute code 229 + 230 + " block " regular text 231 + 232 + 0xFF escape code 233 + 0x01 tag start code 234 + "/TAG" tag name 235 + 0xFF end of tag name code 236 * 237 *@@added V0.9.6 (2000-11-01) [umoeller] 238 */ 239 240 PSZ xmlTokenize(const char *pcszXML) 241 { 242 } 243 244 /* ****************************************************************** 245 * 246 * Parsing 247 * 248 ********************************************************************/ 185 249 186 250 /* … … 308 372 && (*pFirstAfterTagName != '\n') 309 373 && (*pFirstAfterTagName != '\r') 374 && (*pFirstAfterTagName != '\t') // tab 310 375 && (*pFirstAfterTagName != '>') 311 376 ) … … 405 470 406 471 case ' ': 472 case '\t': // tab 407 473 case '\n': 408 474 case '\r': … … 475 541 476 542 case ' ': 543 case '\t': // tab 477 544 case '\n': 478 545 case '\r': -
trunk/src/helpers/xprf2.c
r8 r12 220 220 if (!pszNew) 221 221 arc = ERROR_INVALID_PARAMETER; 222 223 if (arc == NO_ERROR) 224 { 225 222 else 223 { 226 224 DosDelete(pszNew); 227 225 228 226 // open new profile 229 230 227 arc = xprfOpenProfile(pszNew, 231 228 &pxiniNew); 232 } 233 234 // get size of applications list 235 if (arc == NO_ERROR) 236 { 237 if (!PrfQueryProfileSize(hOld, NULL, NULL, &ulSizeOfAppsList)) 238 arc = PRFERR_APPSLIST; 239 else 240 if (ulSizeOfAppsList == 0) 229 230 // get size of applications list 231 if (arc == NO_ERROR) 232 { 233 if (!PrfQueryProfileSize(hOld, NULL, NULL, &ulSizeOfAppsList)) 241 234 arc = PRFERR_APPSLIST; 242 } 243 244 if (arc == NO_ERROR) 245 { 246 // get applications list 247 PSZ pApps = (PSZ)malloc(ulSizeOfAppsList); 248 PSZ pApp2 = pApps; 249 if (!PrfQueryProfileData(hOld, 250 NULL, 251 NULL, 252 pApps, 253 &ulSizeOfAppsList)) 254 arc = PRFERR_APPSLIST; 255 256 // applications loop 257 258 while ( (*pApp2 != 0) 259 && (arc == NO_ERROR) 260 ) 261 { 262 CHAR szErrorKey[1000]; 263 // copy application (this will call prfhCopyKey in turn) 264 arc = xprfCopyApp(hOld, 265 pApp2, 266 pxiniNew, 267 pApp2, 268 szErrorKey); 269 235 else 236 if (ulSizeOfAppsList == 0) 237 arc = PRFERR_APPSLIST; 238 239 if (arc == NO_ERROR) 240 { 241 // get applications list 242 PSZ pApps = (PSZ)malloc(ulSizeOfAppsList); 243 PSZ pApp2 = pApps; 244 if (!PrfQueryProfileData(hOld, 245 NULL, 246 NULL, 247 pApps, 248 &ulSizeOfAppsList)) 249 arc = PRFERR_APPSLIST; 250 251 // applications loop 252 253 while ( (*pApp2 != 0) 254 && (arc == NO_ERROR) 255 ) 256 { 257 CHAR szErrorKey[1000]; 258 // copy application (this will call prfhCopyKey in turn) 259 arc = xprfCopyApp(hOld, 260 pApp2, 261 pxiniNew, 262 pApp2, 263 szErrorKey); 264 265 if (pfnProgressCallback) 266 { 267 ULONG ulNow2, ulMax2; 268 ulNow2 = ((1000*(pApp2-pApps)) / ulSizeOfAppsList) + (ulCount*1000); 269 ulMax2 = (ulMax+1)*1000; 270 if (!pfnProgressCallback(ulUser, ulNow2, ulMax2)) 271 // aborted: 272 arc = PRFERR_ABORTED; 273 } 274 275 // go for next app 276 pApp2 += strlen(pApp2)+1; 277 278 } // end while (*pApp2 != 0) && MBID_NOERROR 279 280 if (pApps) 281 free(pApps); 282 } 283 284 xprfCloseProfile(pxiniNew); 285 286 // progress 270 287 if (pfnProgressCallback) 271 { 272 ULONG ulNow2, ulMax2; 273 ulNow2 = ((1000*(pApp2-pApps)) / ulSizeOfAppsList) + (ulCount*1000); 274 ulMax2 = (ulMax+1)*1000; 275 if (!pfnProgressCallback(ulUser, ulNow2, ulMax2)) 276 // aborted: 277 arc = PRFERR_ABORTED; 278 } 279 280 // go for next app 281 pApp2 += strlen(pApp2)+1; 282 283 } // end while (*pApp2 != 0) && MBID_NOERROR 284 285 if (pApps) 286 free(pApps); 287 } 288 289 xprfCloseProfile(pxiniNew); 290 291 // progress 292 if (pfnProgressCallback) 293 pfnProgressCallback(ulUser, (ulCount+1) * 1000, (ulMax+1) * 1000); 288 pfnProgressCallback(ulUser, (ulCount+1) * 1000, (ulMax+1) * 1000); 289 } 290 } 294 291 295 292 return (arc); … … 461 458 } 462 459 463 if ( (Profiles.pszSysName) 464 && (Profiles.pszUserName) 465 ) 466 { 460 if (Profiles.pszSysName) 467 461 free(Profiles.pszSysName); 462 if (Profiles.pszUserName) 468 463 free(Profiles.pszUserName); 469 }470 464 471 465 return (arc); -
trunk/src/helpers/xstring.c
r8 r12 6 6 * Usage: All OS/2 programs. 7 7 * 8 * The functions in this file are intended as a replacement 9 * to the C library string functions (such as strcpy, strcat) 10 * in cases where the length of the string is unknown and 11 * dynamic memory management is desirable. 12 * 13 * Instead of char* pointers, the functions in this file 14 * operate on an XSTRING structure, which contains a char* 15 * pointer instead. 16 * 17 * Using these functions has the following advantages: 18 * 19 * -- Automatic memory management. For example, xstrcat will 20 * automatically allocate new memory if the new string 21 * does not fit into the present buffer. 22 * 23 * -- The length of the string is always known. Instead 24 * of running strlen (which consumes time), XSTRING.ulLength 25 * always contains the current length of the string. 26 * 27 * -- The functions also differentiate between allocated 28 * memory and the length of the string. That is, for 29 * iterative appends, you can pre-allocate memory to 30 * avoid excessive reallocations. 31 * 32 * Usage: 33 * 34 * 1) Allocate an XSTRING structure on the stack. Always 35 * call xstrInit on the structure, like this: 36 * 37 + XSTRING str; 38 + xstrInit(&str, 0); // no pre-allocation 39 * 40 * Alternatively, use xstrCreate to have an XSTRING 41 * allocated from the heap. 42 * 43 * Always call xstrClear(&str) to free allocated 44 * memory. Otherwise you'll get memory leaks. 45 * (For heap XSTRING's from xstrCreate, use xstrFree.) 46 * 47 * 2) To copy something into the string, use xstrcpy. 48 * To append something to the string, use xstrcat. 49 * See those functions for samples. 50 * 51 * 3) If you need the char* pointer (e.g. for a call 52 * to another function), use XSTRING.psz. However, 53 * you should NEVER modify the psz pointer yourself 54 * because then these functions will get into trouble. 55 * 56 * Also, you should never assume that the "psz" 57 * pointer has not changed after you have called 58 * one of the xstr* functions because these can 59 * always reallocate the buffer if needed. 60 * 61 * 4) If (and only if) you have a char* buffer which 62 * is free()'able (e.g. from strdup()), you can 63 * use xstrset to avoid duplicate copying. 64 * 8 65 * Function prefixes: 9 66 * -- xstr* extended string functions. 10 67 * 11 68 * The functions in this file used to be in stringh.c 12 * before V0.9.3 (2000-04-01). 13 * 14 * Note: Version numbering in this file relates to XWorkplace version 15 * numbering. 69 * before V0.9.3 (2000-04-01). These have been largely 70 * rewritten with V0.9.6 (2000-11-01). 71 * 72 * Note: Version numbering in this file relates to XWorkplace 73 * version numbering. 16 74 * 17 75 *@@added V0.9.3 (2000-04-01) [umoeller] … … 53 111 54 112 /* 113 *@@ xstrInit: 114 * initializes an empty XSTRING. 115 * 116 * If (ulPreAllocate != 0), memory is pre-allocated 117 * for the string, but the string will be empty. 118 * This is useful if you plan to add more stuff to 119 * the string later so we don't have to reallocate 120 * all the time in xstrcat. 121 * 122 * Do not use this on an XSTRING which is already 123 * initialized. Use xstrset instead. 124 * 125 *@@added V0.9.6 (2000-11-01) [umoeller] 126 */ 127 128 void xstrInit(PXSTRING pxstr, // in/out: string 129 ULONG ulPreAllocate) // in: if > 0, memory to allocate 130 { 131 memset(pxstr, 0, sizeof(XSTRING)); 132 if (ulPreAllocate) 133 { 134 pxstr->psz = (PSZ)malloc(ulPreAllocate); 135 pxstr->cbAllocated = ulPreAllocate; 136 // ulLength is still zero 137 *(pxstr->psz) = 0; 138 } 139 } 140 141 /* 142 *@@ xstrInitSet: 143 * this can be used instead of xstrInit if you 144 * have a free()'able string you want to initialize 145 * the XSTRING with. 146 * 147 * Do not use this on an XSTRING which is already 148 * initialized. Use xstrset instead. 149 * 150 *@@added V0.9.6 (2000-11-01) [umoeller] 151 */ 152 153 void xstrInitSet(PXSTRING pxstr, 154 PSZ pszNew) 155 { 156 pxstr->psz = pszNew; 157 if (!pszNew) 158 { 159 pxstr->cbAllocated = 0; 160 pxstr->ulLength = 0; 161 } 162 else 163 { 164 pxstr->ulLength = strlen(pszNew); 165 pxstr->cbAllocated = pxstr->ulLength + 1; 166 } 167 } 168 169 /* 170 *@@ xstrInitCopy: 171 * this can be used instead of xstrInit if you 172 * want to initialize an XSTRING with a copy 173 * of an existing string. 174 * 175 * Do not use this on an XSTRING which is already 176 * initialized. Use xstrcpy instead. 177 * 178 *@@added V0.9.6 (2000-11-01) [umoeller] 179 */ 180 181 void xstrInitCopy(PXSTRING pxstr, 182 const char *pcszSource) 183 { 184 if (pxstr) 185 { 186 memset(pxstr, 0, sizeof(XSTRING)); 187 188 if (pcszSource) 189 pxstr->ulLength = strlen(pcszSource); 190 191 if (pxstr->ulLength) 192 { 193 // we do have a source string: 194 pxstr->cbAllocated = pxstr->ulLength + 1; 195 pxstr->psz = (PSZ)malloc(pxstr->cbAllocated); 196 strcpy(pxstr->psz, pcszSource); 197 } 198 } 199 } 200 201 /* 202 *@@ xstrClear: 203 * clears the specified stack XSTRING and 204 * frees allocated memory. 205 * 206 * This is the reverse to xstrInit. 207 * 208 *@@added V0.9.6 (2000-11-01) [umoeller] 209 */ 210 211 void xstrClear(PXSTRING pxstr) // in/out: string 212 { 213 if (pxstr->psz) 214 free(pxstr->psz); 215 memset(pxstr, 0, sizeof(XSTRING)); 216 } 217 218 /* 219 *@@ xstrCreate: 220 * allocates a new XSTRING from the heap 221 * and calls xstrInit on it. 222 * 223 * Always use xstrFree to free associated 224 * resources. 225 * 226 *@@added V0.9.6 (2000-11-01) [umoeller] 227 */ 228 229 PXSTRING xstrCreate(ULONG ulPreAllocate) 230 { 231 PXSTRING pxstr = (PXSTRING)malloc(sizeof(XSTRING)); 232 if (pxstr) 233 xstrInit(pxstr, ulPreAllocate); 234 235 return (pxstr); 236 } 237 238 /* 239 *@@ xstrFree: 240 * frees the specified heap XSTRING, which must 241 * have been created using xstrCreate. 242 * 243 *@@added V0.9.6 (2000-11-01) [umoeller] 244 */ 245 246 VOID xstrFree(PXSTRING pxstr) // in/out: string 247 { 248 if (pxstr) 249 { 250 xstrClear(pxstr); 251 free(pxstr); 252 } 253 } 254 255 /* 256 *@@ xstrset: 257 * sets the specified XSTRING to a new string 258 * without copying it. 259 * 260 * pxstr is cleared before the new string is set. 261 * 262 * This ONLY works if pszNew has been allocated from 263 * the heap using malloc() or strdup() and is thus 264 * free()'able. 265 * 266 * This assumes that exactly strlen(pszNew) + 1 267 * bytes have been allocated for pszNew, which 268 * is true if pszNew comes from strdup(). 269 * 270 *@@added V0.9.6 (2000-11-01) [umoeller] 271 */ 272 273 ULONG xstrset(PXSTRING pxstr, // in/out: string 274 PSZ pszNew) // in: heap PSZ to use 275 { 276 xstrClear(pxstr); 277 pxstr->psz = pszNew; 278 if (pszNew) 279 { 280 pxstr->ulLength = strlen(pszNew); 281 pxstr->cbAllocated = pxstr->ulLength + 1; 282 } 283 // else null string: cbAllocated and ulLength are 0 already 284 285 return (pxstr->ulLength); 286 } 287 288 /* 55 289 *@@ xstrcpy: 56 * copies p szString to *ppszBuf, for which memory is allocated290 * copies pcszSource to pxstr, for which memory is allocated 57 291 * as necessary. 58 292 * 59 * If *ppszBuf != NULL, the existing memory is freed.60 * 61 * Returns the length of the new string ( including the null293 * If pxstr contains something, its contents are destroyed. 294 * 295 * Returns the length of the new string (excluding the null 62 296 * terminator), or null upon errors. 63 297 * 64 298 * Example: 65 + PSZ psz = NULL;66 + xstrcpy(&psz, "blah");67 * would have "psz" point to newly allocated buffer containing68 * "blah".299 * 300 + XSTRING str; 301 + xstrInit(&str, 0); 302 + xstrcpy(&str, "blah"); 69 303 * 70 304 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy 71 */ 72 73 ULONG xstrcpy(XSTR ppszBuf, 74 const char *pszString) 75 { 76 ULONG ulrc = 0; 77 if (ppszBuf) 78 { 79 if (*ppszBuf) 80 free(*ppszBuf); 81 ulrc = strlen(pszString) + 1; 82 *ppszBuf = (PSZ)malloc(ulrc); 83 strcpy(*ppszBuf, pszString); 84 } 85 return (ulrc); 86 } 87 88 #ifdef __XWPMEMDEBUG__ // setup.h, helpers\memdebug.c 89 90 /* 91 *@@ xstrcatDebug: 92 * debug version of xstrcat. 93 * 94 * stringh.h automatically maps xstrcat to this 95 * function if __XWPMEMDEBUG__ is defined. 96 * 97 *@@addded V0.9.1 (99-12-20) [umoeller] 98 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcatDebug 99 */ 100 101 ULONG xstrcatDebug(XSTR ppszBuf, 102 const char *pszString, 103 const char *file, 104 unsigned long line, 105 const char *function) 106 { 107 ULONG ulrc = 0; 108 if ((ppszBuf) && (pszString)) 109 { 110 if (*ppszBuf == NULL) 111 xstrcpy(ppszBuf, pszString); 305 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 306 */ 307 308 ULONG xstrcpy(PXSTRING pxstr, // in/out: string 309 const char *pcszSource) // in: source, can be NULL 310 { 311 xstrClear(pxstr); 312 313 if (pxstr) 314 { 315 ULONG ulSourceLength = 0; 316 if (pcszSource) 317 ulSourceLength = strlen(pcszSource); 318 319 if (ulSourceLength) 320 { 321 // we do have a source string: 322 ULONG cbNeeded = ulSourceLength + 1; 323 if (cbNeeded > pxstr->cbAllocated) 324 { 325 // we need more memory than we have previously 326 // allocated: 327 pxstr->cbAllocated = cbNeeded; 328 pxstr->psz = (PSZ)malloc(cbNeeded); 329 } 330 // else: we have enough memory 331 332 strcpy(pxstr->psz, pcszSource); 333 } 112 334 else 113 335 { 114 ULONG cbOld = strlen(*ppszBuf), 115 cbString = strlen(pszString); 116 PSZ pszOldCopy = strdup(*ppszBuf); 117 118 ulrc = cbOld + cbString + 1; 119 if (*ppszBuf) 120 free(*ppszBuf); 121 *ppszBuf = (PSZ)memdMalloc(ulrc, file, line, function); 122 // copy old string 123 memcpy(*ppszBuf, 124 pszOldCopy, 125 cbOld); 126 // append new string 127 memcpy(*ppszBuf + cbOld, 128 pszString, 129 cbString + 1); // include null terminator 130 free(pszOldCopy); // fixed V0.9.1 (99-12-20) [umoeller] 336 // no source specified or source is empty: 337 if (pxstr->cbAllocated) 338 // we did have a string: set to empty, 339 // but leave allocated memory intact 340 *(pxstr->psz) = 0; 341 // else: pxstr->psz is still NULL 131 342 } 132 } 133 return (ulrc); 134 } 135 136 #else // __XWPMEMDEBUG__ 343 344 // in all cases, set new length 345 pxstr->ulLength = ulSourceLength; 346 } 347 348 return (pxstr->ulLength); 349 } 137 350 138 351 /* 139 352 *@@ xstrcat: 140 * appends p szString to *ppszBuf, which is re-allocated as141 * necessary.142 * 143 * If *ppszBuf is NULL, this behaves just asxstrcpy.144 * 145 * Returns the length of the new string ( including the null353 * appends pcszSource to pxstr, for which memory is allocated 354 * as necessary. 355 * 356 * If pxstr is empty, this behaves just like xstrcpy. 357 * 358 * Returns the length of the new string (excluding the null 146 359 * terminator), or null upon errors. 147 360 * 148 361 * Example: 149 + PSZ psz = strdup("blah"); 150 + xstrcat(&psz, "blup"); 362 * 363 + XSTRING str; 364 + xstrInit(&str, 0); 365 + xstrcpy(&str, "blah"); 366 + xstrcat(&str, "blup"); 367 * 151 368 * would do the following: 152 * a) free the old value of psz("blah");153 * b) reallocate psz;369 * a) free the old value of str ("blah"); 370 * b) reallocate str; 154 371 * c) so that psz afterwards points to a new string containing 155 372 * "blahblup". … … 159 376 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcat 160 377 *@@changed V0.9.3 (2000-05-11) [umoeller]: returned 0 if pszString was initially empty; fixed 161 */ 162 163 ULONG xstrcat(XSTR ppszBuf, 164 const char *pszString) 378 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 379 */ 380 381 ULONG xstrcat(PXSTRING pxstr, // in/out: string 382 const char *pcszSource) // in: source, can be NULL 165 383 { 166 384 ULONG ulrc = 0; 167 if ((ppszBuf) && (pszString)) 168 { 169 if (*ppszBuf == NULL) 170 ulrc = xstrcpy(ppszBuf, pszString); 171 else 385 386 if (pxstr) 387 { 388 ULONG ulSourceLength = 0; 389 if (pcszSource) 390 ulSourceLength = strlen(pcszSource); 391 392 if (ulSourceLength) 172 393 { 173 ULONG cbOld = strlen(*ppszBuf), 174 cbString = strlen(pszString); 175 PSZ pszOldCopy = strdup(*ppszBuf); 176 177 ulrc = cbOld + cbString + 1; 178 if (*ppszBuf) 179 free(*ppszBuf); 180 *ppszBuf = (PSZ)malloc(ulrc); 181 // copy old string 182 memcpy(*ppszBuf, 183 pszOldCopy, 184 cbOld); 185 // append new string 186 memcpy(*ppszBuf + cbOld, 187 pszString, 188 cbString + 1); // include null terminator 189 free(pszOldCopy); // fixed V0.9.1 (99-12-20) [umoeller] 394 // we do have a source string: 395 396 // 1) memory management 397 ULONG cbNeeded = pxstr->ulLength + ulSourceLength + 1; 398 if (cbNeeded > pxstr->cbAllocated) 399 { 400 // we need more memory than we have previously 401 // allocated: 402 if (pxstr->cbAllocated) 403 // appendee already had memory: 404 // reallocate 405 pxstr->psz = (PSZ)realloc(pxstr->psz, 406 cbNeeded); 407 else 408 // appendee has no memory: 409 pxstr->psz = (PSZ)malloc(cbNeeded); 410 411 pxstr->cbAllocated = cbNeeded; 412 // ulLength is unchanged yet 413 } 414 // else: we have enough memory, both if appendee 415 // is empty or not empty 416 417 // now we have: 418 // -- if appendee (pxstr) had enough memory, no problem 419 // -- if appendee (pxstr) needed more memory 420 // -- and was not empty: pxstr->psz now points to a 421 // reallocated copy of the old string 422 // -- and was empty: pxstr->psz now points to a 423 // new (unitialized) buffer 424 425 // 2) append source string: 426 strcpy(pxstr->psz + pxstr->ulLength, 427 pcszSource); 428 429 // in all cases, set new length 430 pxstr->ulLength += ulSourceLength; 431 ulrc = ulSourceLength; 190 432 } 191 } 433 // else no source specified or source is empty: 434 // do nothing 435 } 436 192 437 return (ulrc); 193 438 } 194 439 195 #endif // else __XWPMEMDEBUG__196 197 #ifdef __XWPMEMDEBUG__ // setup.h, helpers\memdebug.c198 199 /*200 *@@ xstrrplDebug:201 *202 *@@added V0.9.3 (2000-04-11) [umoeller]203 */204 205 ULONG xstrrplDebug(PSZ *ppszBuf, // in/out: text buffer206 ULONG ulOfs, // in: where to begin search (can be 0)207 const char *pszSearch, // in: search string208 const char *pszReplace, // in: replacement string209 PULONG pulAfterOfs, // out: offset where found (can be NULL)210 const char *file,211 unsigned long line,212 const char *function)213 {214 ULONG ulrc = 0;215 216 if ((ppszBuf) && (pszSearch) && (pszReplace))217 {218 ULONG cbBuf = 0,219 cbSearch = strlen(pszSearch);220 if (*ppszBuf) // fixed V0.9.0 (99-11-08) [umoeller]221 cbBuf = strlen(*ppszBuf);222 223 if ((ulOfs < cbBuf) && (cbSearch))224 {225 PSZ pFound = strstr((*ppszBuf) + ulOfs,226 pszSearch);227 228 if (pFound)229 {230 ULONG cbReplace = strlen(pszReplace),231 // length of new string232 cbNew = cbBuf233 + cbReplace234 - cbSearch235 + 1, // null terminator236 // offset where pszSearch was found237 ulFoundOfs = pFound - *ppszBuf;238 239 // allocate new buffer240 PSZ pszNew = (PSZ)memdMalloc(cbNew,241 file, line, function);242 243 if (ulFoundOfs)244 {245 // copy until offset246 strncpy(pszNew,247 *ppszBuf,248 ulFoundOfs);249 }250 251 if (cbReplace)252 {253 // copy replacement254 strncpy(pszNew + ulFoundOfs,255 pszReplace,256 cbReplace);257 }258 // copy rest259 strcpy(pszNew + ulFoundOfs + cbReplace,260 pFound + cbSearch);261 262 // replace PSZ pointer263 memdFree(*ppszBuf, file, line, function);264 *ppszBuf = pszNew;265 266 // return new length267 ulrc = cbNew;268 if (pulAfterOfs)269 *pulAfterOfs = ulFoundOfs + cbReplace;270 }271 }272 }273 return (ulrc);274 }275 276 #else277 278 440 /* 279 441 *@@ xstrrpl: 280 * replaces pszSearch with pszReplace in *ppszBuf. 281 * 282 * If pszSearch was found, *ppszBuf is 283 * re-allocated so the buffer cannot overflow. As 284 * a result, *ppszBuf must be free()'able. 285 * 286 * Returns the length of the new string or 0 if 287 * pszSearch was not found (and ppszBuf was therefore 288 * not changed). 442 * replaces pstrSearch with pstrReplace in pxstr. 443 * 444 * Starting with V0.9.6, this operates entirely on 445 * XSTRING's for speed because we then know the string 446 * lengths already and can use memcpy instead of strcpy. 447 * This new version should be magnitudes faster. 448 * 449 * None of the pointers can be NULL, but if pstrReplace 450 * is empty, this effectively erases pstrSearch in pxstr. 451 * 452 * Returns the length of the new string (exclusing the 453 * null terminator) or 0 if pszSearch was not found 454 * (and pxstr was therefore not changed). 289 455 * 290 456 * If the string was found and (pulAfterOfs != NULL), … … 298 464 * function until it returns 0. 299 465 * 466 * There are two wrappers around this function which 467 * work on C strings instead (however, thus losing the 468 * speed advantage): 469 * 470 * -- strhrpl operates on C strings only; 471 * 472 * -- xstrcrpl uses C strings for the search and replace 473 * parameters. 474 * 300 475 * <B>Example usage:</B> 301 + PSZ psz = strdup("Test string"); 302 + xstrrpl(&psz, "Test", "Dummy"); 303 * 304 * would reallocate psz to point to a new string 305 * containing "Dummy string". 476 * 477 + XSTRING str; 478 + ULONG ulPos = 0; 479 + xstrInit(&str, 0); 480 + xstrcpy(&str, "Test phrase 1. Test phrase 2."); 481 + while (xstrrpl(&str, 482 + ulPos, 483 + "Test", // search 484 + "Dummy", // replace 485 + &ulPos)) 486 + ; 487 * 488 * would replace all occurences of "Test" in str with 489 * "Dummy". 306 490 * 307 491 *@@changed V0.9.0 [umoeller]: totally rewritten. 308 492 *@@changed V0.9.0 (99-11-08) [umoeller]: crashed if *ppszBuf was NULL. Fixed. 309 493 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxrpl 310 */ 311 312 ULONG xstrrpl(PSZ *ppszBuf, // in/out: text buffer; cannot be NULL 313 ULONG ulOfs, // in: where to begin search (can be 0) 314 const char *pszSearch, // in: search string; cannot be NULL 315 const char *pszReplace, // in: replacement string; cannot be NULL 316 PULONG pulAfterOfs) // out: offset where found (can be NULL) 494 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten 495 */ 496 497 ULONG xstrrpl(PXSTRING pxstr, // in/out: string 498 ULONG ulOfs, // in: where to begin search (0 = start) 499 const XSTRING *pstrSearch, // in: search string; cannot be NULL 500 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL 501 PULONG pulAfterOfs) // out: offset where found (ptr can be NULL) 317 502 { 318 503 ULONG ulrc = 0; 319 504 320 if ((p pszBuf) && (pszSearch) && (pszReplace))321 { 322 ULONG c bBuf = 0,323 cbSearch = strlen(pszSearch); 324 if (*ppszBuf) // fixed V0.9.0 (99-11-08) [umoeller]325 cbBuf = strlen(*ppszBuf);326 327 if ((ulOfs < cbBuf) && (cbSearch))505 if ((pxstr) && (pstrSearch) && (pstrReplace)) 506 { 507 ULONG cSearchLen = pstrSearch->ulLength; 508 509 // can we search this? 510 if ( (ulOfs < pxstr->ulLength) 511 && (cSearchLen) 512 ) 328 513 { 329 PSZ pFound = strstr((*ppszBuf) + ulOfs, 330 pszSearch); 514 // yes: 515 PSZ pFound = strstr(pxstr->psz + ulOfs, 516 pstrSearch->psz); 331 517 332 518 if (pFound) 333 519 { 334 ULONG cbReplace = strlen(pszReplace), 335 // length of new string 336 cbNew = cbBuf 337 + cbReplace 338 - cbSearch 339 + 1, // null terminator 340 // offset where pszSearch was found 341 ulFoundOfs = pFound - *ppszBuf; 342 343 // allocate new buffer 344 PSZ pszNew = (PSZ)malloc(cbNew); 345 346 if (ulFoundOfs) 520 // found in buffer from ofs: 521 ULONG cReplaceLen = pstrReplace->ulLength; 522 // can be 0! 523 524 // length of new string 525 ULONG cbNeeded = pxstr->ulLength 526 + cReplaceLen 527 - cSearchLen 528 + 1, // null terminator 529 // offset where pszSearch was found 530 ulFoundOfs = pFound - pxstr->psz; 531 532 // now check if we have enough memory... 533 if (pxstr->cbAllocated < cbNeeded) 347 534 { 348 // copy until offset 349 strncpy(pszNew, 350 *ppszBuf, 351 ulFoundOfs); 535 // no, we need more memory: 536 // allocate new buffer 537 PSZ pszNew = (PSZ)malloc(cbNeeded); 538 539 if (ulFoundOfs) 540 // "found" was not at the beginning: 541 // copy from beginning up to found-offset 542 memcpy(pszNew, 543 pxstr->psz, 544 ulFoundOfs); // up to "found" 545 546 if (cReplaceLen) 547 { 548 // we have a replacement: 549 // insert it next 550 memcpy(pszNew + ulFoundOfs, 551 pstrReplace->psz, 552 cReplaceLen + 1); // include null terminator 553 } 554 555 // copy rest: 556 // pxstr frontFOUNDtail 557 // 0 1 558 // 01234567890123 559 // ³ ³ ³ ³ 560 // ³ ³ ÀÄ ulFoundOfs + cSearchLen = 10 561 // ³ ³ ³ 562 // ³ ÀÄ ulFoundOfs = 5 563 // ³ ³ 564 // pxstr->ulLength = 14 565 memcpy(pszNew + ulFoundOfs + cReplaceLen, 566 pFound + cSearchLen, 567 // remaining bytes: 568 pxstr->ulLength - ulFoundOfs - cSearchLen // 9 569 + 1); // null terminator 570 571 free(pxstr->psz); 572 pxstr->psz = pszNew; 573 pxstr->ulLength = cbNeeded - 1; 574 pxstr->cbAllocated = cbNeeded; 575 } // end if (pxstr->cbAllocated < cbNeeded) 576 else 577 { 578 // we have enough memory left, 579 // we can just overwrite in the middle... 580 581 PSZ pszAfterFoundBackup = 0; 582 // calc length of string after "found" 583 ULONG cTailLength = pxstr->ulLength - ulFoundOfs - cSearchLen; 584 585 // if "replace" is longer than "found", 586 // make a backup of the stuff after "found", 587 // or this would get overwritten 588 if (cReplaceLen > cSearchLen) 589 { 590 pszAfterFoundBackup = (PSZ)malloc(cTailLength + 1); 591 memcpy(pszAfterFoundBackup, 592 pFound + cSearchLen, 593 cTailLength + 1); 594 } 595 596 // now overwrite "found" in the middle 597 if (cReplaceLen) 598 { 599 memcpy(pxstr->psz + ulFoundOfs, 600 pstrReplace->psz, 601 cReplaceLen); // no null terminator 602 } 603 604 // now append tail (stuff after "found") again... 605 if (pszAfterFoundBackup) 606 { 607 // we made a backup above: 608 memcpy(pxstr->psz + ulFoundOfs + cReplaceLen, 609 pszAfterFoundBackup, 610 cTailLength + 1); 611 free(pszAfterFoundBackup); 612 // done! 613 } 614 else 615 // no backup: 616 if (cReplaceLen < cSearchLen) 617 // "replace" is shorter than "found: 618 memcpy(pxstr->psz + ulFoundOfs + cReplaceLen, 619 pFound + cSearchLen, 620 cTailLength + 1); 621 // else (cReplaceLen == cSearchLen): 622 // we can leave the tail as it is 623 624 pxstr->ulLength = cbNeeded - 1; 352 625 } 353 626 354 if (cbReplace)355 {356 // copy replacement357 strncpy(pszNew + ulFoundOfs,358 pszReplace,359 cbReplace);360 }361 // copy rest362 strcpy(pszNew + ulFoundOfs + cbReplace,363 pFound + cbSearch);364 365 // replace PSZ pointer366 free(*ppszBuf);367 *ppszBuf = pszNew;368 369 627 // return new length 370 ulrc = cbNe w;628 ulrc = cbNeeded - 1; 371 629 if (pulAfterOfs) 372 *pulAfterOfs = ulFoundOfs + c bReplace;630 *pulAfterOfs = ulFoundOfs + cReplaceLen; 373 631 } 374 632 } 375 633 } 634 376 635 return (ulrc); 377 636 } 378 637 379 #endif // else __XWPMEMDEBUG__ 380 381 /* 382 *@@ xstrins: 383 * this inserts one string into another. 384 * 385 * pszInsert is inserted into pszBuffer at offset 386 * ulInsertOfs (which counts from 0). 387 * 388 * A newly allocated string is returned. pszBuffer is 389 * not changed. The new string should be free()'d after 390 * use. 391 * 392 * Upon errors, NULL is returned. 393 * 394 *@@changed V0.9.0 [umoeller]: completely rewritten. 395 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxins 396 */ 397 398 PSZ xstrins(PSZ pszBuffer, 399 ULONG ulInsertOfs, 400 const char *pszInsert) 401 { 402 PSZ pszNew = NULL; 403 404 if ((pszBuffer) && (pszInsert)) 405 { 406 do { 407 ULONG cbBuffer = strlen(pszBuffer); 408 ULONG cbInsert = strlen(pszInsert); 409 410 // check string length 411 if (ulInsertOfs > cbBuffer + 1) 412 break; // do 413 414 // OK, let's go. 415 pszNew = (PSZ)malloc(cbBuffer + cbInsert + 1); // additional null terminator 416 417 // copy stuff before pInsertPos 418 memcpy(pszNew, 419 pszBuffer, 420 ulInsertOfs); 421 // copy string to be inserted 422 memcpy(pszNew + ulInsertOfs, 423 pszInsert, 424 cbInsert); 425 // copy stuff after pInsertPos 426 strcpy(pszNew + ulInsertOfs + cbInsert, 427 pszBuffer + ulInsertOfs); 428 } while (FALSE); 429 } 430 431 return (pszNew); 432 } 433 434 638 /* 639 *@@ xstrcrpl: 640 * wrapper around xstrrpl which allows using C strings 641 * for the find and replace parameters. 642 * 643 *@@added V0.9.6 (2000-11-01) [umoeller] 644 */ 645 646 ULONG xstrcrpl(PXSTRING pxstr, // in/out: string 647 ULONG ulOfs, // in: where to begin search (0 = start) 648 const char *pcszSearch, // in: search string; cannot be NULL 649 const char *pcszReplace, // in: replacement string; cannot be NULL 650 PULONG pulAfterOfs) // out: offset where found (ptr can be NULL) 651 { 652 ULONG ulrc = 0; 653 XSTRING xstrFind, 654 xstrReplace; 655 xstrInit(&xstrFind, 0); 656 xstrset(&xstrFind, (PSZ)pcszSearch); 657 xstrInit(&xstrReplace, 0); 658 xstrset(&xstrReplace, (PSZ)pcszReplace); 659 660 return (xstrrpl(pxstr, ulOfs, &xstrFind, &xstrReplace, pulAfterOfs)); 661 } 662 663 // test case 664 665 /* int main(void) 666 { 667 XSTRING str, 668 strFind, 669 strReplace; 670 ULONG ulOfs = 0; 671 672 xstrInit(&str, 100); 673 xstrInit(&strFind, 0); 674 xstrInit(&strReplace, 0); 675 676 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !"); 677 xstrcpy(&strFind, "Test"); 678 xstrcpy(&strReplace, "Dummy"); 679 680 printf("Old string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 681 682 while (xstrrpl(&str, 683 ulOfs, 684 &strFind, 685 &strReplace, 686 &ulOfs)) 687 ; 688 689 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 690 691 xstrcpy(&strFind, strReplace.psz); 692 xstrClear(&strReplace); 693 ulOfs = 0; 694 while (xstrrpl(&str, 695 ulOfs, 696 &strFind, 697 &strReplace, 698 &ulOfs)) 699 ; 700 701 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 702 703 xstrcpy(&strFind, " "); 704 xstrcpy(&strReplace, "."); 705 ulOfs = 0; 706 while (xstrrpl(&str, 707 ulOfs, 708 &strFind, 709 &strReplace, 710 &ulOfs)) 711 ; 712 713 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 714 715 xstrcpy(&strFind, "."); 716 xstrcpy(&strReplace, "***************************"); 717 ulOfs = 0; 718 while (xstrrpl(&str, 719 ulOfs, 720 &strFind, 721 &strReplace, 722 &ulOfs)) 723 ; 724 725 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 726 727 xstrcpy(&strFind, "*"); 728 xstrClear(&strReplace); 729 ulOfs = 0; 730 while (xstrrpl(&str, 731 ulOfs, 732 &strFind, 733 &strReplace, 734 &ulOfs)) 735 ; 736 737 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 738 } */ 739
Note:
See TracChangeset
for help on using the changeset viewer.