Changeset 42
- Timestamp:
- Mar 5, 2001, 8:50:19 PM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/xstring.c
r41 r42 12 12 * 13 13 * Instead of char* pointers, the functions in this file 14 * operate on an XSTRING structure, which contains a char* 15 * pointer instead. 14 * operate on XSTRING structures, which in turn contain 15 * a char* pointer pointing to heap memory, which is managed 16 * automatically. 16 17 * 17 18 * Using these functions has the following advantages: … … 120 121 /* 121 122 *@@ xstrInit: 122 * initializes an empty XSTRING. 123 * initializes a new XSTRING. Always call this before 124 * using an XSTRING from the stack. 123 125 * 124 126 * If (ulPreAllocate != 0), memory is pre-allocated … … 129 131 * 130 132 * Do not use this on an XSTRING which is already 131 * initialized. Use xstrset instead. 133 * initialized (this would cause memory leaks). 134 * Use xstrcpy or xstrset instead. 132 135 * 133 136 *@@added V0.9.6 (2000-11-01) [umoeller] … … 158 161 * 159 162 * Do not use this on an XSTRING which is already 160 * initialized. Use xstrset instead. 163 * initialized (this would cause memory leaks). 164 * Use xstrcpy or xstrset instead. 161 165 * 162 166 * Example: … … 195 199 * 196 200 * Do not use this on an XSTRING which is already 197 * initialized. Use xstrcpy instead. 201 * initialized (this would cause memory leaks). 202 * Use xstrcpy or xstrset instead. 198 203 * 199 204 * Example: … … 215 220 216 221 if (pcszSource) 222 { 217 223 pxstr->ulLength = strlen(pcszSource); 218 224 219 if (pxstr->ulLength) 220 { 221 // we do have a source string: 222 pxstr->cbAllocated = pxstr->ulLength + 1 + ulExtraAllocate; 223 pxstr->psz = (PSZ)malloc(pxstr->cbAllocated); 224 strcpy(pxstr->psz, pcszSource); 225 if (pxstr->ulLength) 226 { 227 // we do have a source string: 228 pxstr->cbAllocated = pxstr->ulLength + 1 + ulExtraAllocate; 229 pxstr->psz = (PSZ)malloc(pxstr->cbAllocated); 230 strcpy(pxstr->psz, pcszSource); 231 } 225 232 } 226 233 } … … 396 403 * This sequence can be abbreviated using xstrInitCopy. 397 404 * 405 * Memory cost: If there's enough room in pxstr for 406 * pcszSource, none. Otherwise pxstr is reallocated 407 * to hold enough room for pcszSource. 408 * 398 409 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy 399 410 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten … … 431 442 // we need more memory than we have previously 432 443 // allocated: 433 if (pxstr->psz)444 /* if (pxstr->psz) 434 445 free(pxstr->psz); // V0.9.9 (2001-01-28) [lafaix] 435 446 pxstr->cbAllocated = cbNeeded; 436 pxstr->psz = (PSZ)malloc(cbNeeded); 447 pxstr->psz = (PSZ)malloc(cbNeeded); */ 448 449 // V0.9.9 (2001-03-05) [umoeller]: use realloc; 450 // this gives the C runtime a chance to expand the 451 // existing block 452 pxstr->psz = (PSZ)realloc(pxstr->psz, cbNeeded); 453 // if pxstr->psz is NULL, realloc behaves like malloc 454 pxstr->cbAllocated = cbNeeded; 437 455 } 438 456 // else: we have enough memory … … 513 531 * After this, str.psz points to a new string containing 514 532 * "blahblup". 533 * 534 * Memory cost: If there's enough room in pxstr for 535 * pcszSource, none. Otherwise pxstr is reallocated 536 * to hold enough room for pcszSource. 515 537 * 516 538 *@@changed V0.9.1 (99-12-20) [umoeller]: fixed memory leak … … 617 639 * After this, str.psz points to a new string containing 618 640 * "blup". 641 * 642 * Memory cost: If there's enough room in pxstr for 643 * c, none. Otherwise pxstr is reallocated 644 * to hold enough room for c. 619 645 * 620 646 *@@added V0.9.7 (2000-12-10) [umoeller] … … 720 746 * This would yield "This is a stupid string." 721 747 * 748 * Memory cost: If there's enough room in pxstr for 749 * the replacement, none. Otherwise pxstr is reallocated 750 * to hold enough room for the replacement. 751 * 722 752 *@@added V0.9.7 (2001-01-15) [umoeller] 723 753 *@@changed V0.9.9 (2001-01-29) [lafaix]: fixed unnecessary allocation when pxstr was big enough 724 754 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash 725 755 */ 726 756 … … 959 989 * would replace all occurences of "Test" in str with 960 990 * "Dummy". 991 * 992 * Memory cost: Calls xstrrpl if pstrSearch was found. 961 993 * 962 994 *@@changed V0.9.0 [umoeller]: totally rewritten. … … 1084 1116 + S%61mple %63hara%63ters. 1085 1117 * 1118 * Memory cost: None, except for that of xstrFindReplace. 1119 * 1086 1120 *@@added V0.9.9 (2001-02-28) [umoeller] 1087 1121 */ … … 1133 1167 * 1134 1168 * This simply assumes that all '%' characters in 1135 * pxstr containencodings and the next two characters1169 * pxstr introduce encodings and the next two characters 1136 1170 * after '%' always are a hex character code. This 1137 * only recognizes hex in upper case. 1138 * 1139 * Returns the no. of characters replaced. 1171 * only recognizes hex in upper case. All this will 1172 * work properly with encodings from xstrEncode. 1173 * 1174 * Returns the no. of encodings replaced. 1175 * 1176 * Memory cost: Creates a temporary buffer with the 1177 * length of pxstr and replaces pxstr with it, if any 1178 * encodings were found. In other words, this is 1179 * expensive for very large strings. 1140 1180 * 1141 1181 *@@added V0.9.9 (2001-02-28) [umoeller] … … 1167 1207 if (c == '%') 1168 1208 { 1169 static char ach[] = "01234567 989ABCDEF";1209 static char ach[] = "0123456789ABCDEF"; 1170 1210 1171 1211 // convert two chars after '%' 1172 CHAR c2, // first char after '%' 1173 c3; // second char after '%' 1212 CHAR c2, // first char after '%' --> hi-nibble 1213 c3; // second char after '%' --> lo-nibble 1174 1214 const char *p2, // for first char: points into ach or is NULL 1175 1215 *p3; // for second char: points into ach or is NULL … … 1181 1221 { 1182 1222 // both chars after '%' were valid: 1183 *pDest++ = (p2 - ach) // 0 for '0', 10 for 'A', ... 1184 + ((p3 - ach) << 4); 1223 *pDest++ = // lo-nibble: 1224 (p3 - ach) // 0 for '0', 10 for 'A', ... 1225 // hi-nibble: 1226 + ((p2 - ach) << 4); 1185 1227 // go on after that 1186 1228 pSource += 2; … … 1366 1408 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1367 1409 1410 printf("Encoding @* chars.\n"); 1411 xstrEncode(&str, "@*"); 1412 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1413 1414 printf("Decoding @* chars.\n"); 1415 xstrDecode(&str); 1416 printf("New string is: \"%s\" (%d/%d)\n", str.psz, str.ulLength, str.cbAllocated); 1417 1368 1418 return (0); 1369 1419 } 1420 1370 1421 */ 1371 1372
Note:
See TracChangeset
for help on using the changeset viewer.