Changeset 41 for trunk/src/helpers/xstring.c
- Timestamp:
- Mar 3, 2001, 11:58:48 AM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/xstring.c
r39 r41 106 106 #include <stdio.h> 107 107 #include <string.h> 108 #include <ctype.h> 108 109 109 110 #include "setup.h" // code generation and debugging options … … 1051 1052 1052 1053 /* 1054 *@@ xstrEncode: 1055 * encodes characters in a string. 1056 * 1057 * This searches pxstr for all occurences of the 1058 * characters in pcszEncode (which must be a 1059 * null-terminated list of characters to be 1060 * encoded). Each occurence that is found is 1061 * replaced with "%hh", with "hh" being the 1062 * two-digit hex number of the encoded character. 1063 * 1064 * For example, to encode strings for the XCenter, 1065 * set pcszEncode to "%,();=". 1066 * 1067 * Returns the no. of characters replaced. 1068 * 1069 * NOTE: You must make sure that pcszEncode ALWAYS 1070 * contains the "%" character as well, which must 1071 * always be encoded (i.e. escaped) because it is 1072 * used for encoding the characters. Otherwise 1073 * you won't be able to decode the string again. 1074 * 1075 * Example: To encode all occurences of 1076 * "a", "b", and "c" in a string, do this: 1077 * 1078 + XSTRING str; 1079 + xstrInitCopy(&str, "Sample characters."; 1080 + xstrEncode(&str, "abc%"; 1081 * 1082 * would convert str to contain: 1083 * 1084 + S%61mple %63hara%63ters. 1085 * 1086 *@@added V0.9.9 (2001-02-28) [umoeller] 1087 */ 1088 1089 ULONG xstrEncode(PXSTRING pxstr, // in/out: string to convert 1090 const char *pcszEncode) // in: characters to encode (e.g. "%,();=") 1091 { 1092 ULONG ulrc = 0, 1093 ul; 1094 1095 // now encode the widget setup string... 1096 for (ul = 0; 1097 ul < strlen(pcszEncode); 1098 ul++) 1099 { 1100 CHAR szFind[3] = "?", 1101 szReplace[10] = "%xx"; 1102 XSTRING strFind, 1103 strReplace; 1104 size_t ShiftTable[256]; 1105 BOOL fRepeat = FALSE; 1106 ULONG ulOfs = 0; 1107 1108 // search string: 1109 szFind[0] = pcszEncode[ul]; 1110 xstrInitSet(&strFind, szFind); 1111 1112 // replace string: ASCII encoding 1113 sprintf(szReplace, "%%%lX", pcszEncode[ul]); 1114 xstrInitSet(&strReplace, szReplace); 1115 1116 // replace all occurences 1117 while (xstrFindReplace(pxstr, 1118 &ulOfs, 1119 &strFind, 1120 &strReplace, 1121 ShiftTable, 1122 &fRepeat)) 1123 ulrc++; 1124 1125 } // for ul; next encoding 1126 1127 return (ulrc); 1128 } 1129 1130 /* 1131 *@@ xstrDecode: 1132 * decodes a string previously encoded by xstrEncode. 1133 * 1134 * This simply assumes that all '%' characters in 1135 * pxstr contain encodings and the next two characters 1136 * after '%' always are a hex character code. This 1137 * only recognizes hex in upper case. 1138 * 1139 * Returns the no. of characters replaced. 1140 * 1141 *@@added V0.9.9 (2001-02-28) [umoeller] 1142 */ 1143 1144 ULONG xstrDecode(PXSTRING pxstr) // in/out: string to be decoded 1145 { 1146 ULONG ulrc = 0; 1147 1148 if ( (pxstr) 1149 && (pxstr->ulLength) 1150 ) 1151 { 1152 ULONG cbAllocated = pxstr->ulLength + 1; 1153 // decoded string cannot be longer than source 1154 PSZ pszDest = (PSZ)malloc(cbAllocated); 1155 1156 if (pszDest) 1157 { 1158 const char *pSource = pxstr->psz; 1159 PSZ pDest = pszDest; 1160 1161 CHAR c; 1162 1163 while ((c = *pSource++)) 1164 { 1165 // pSource points to next char now 1166 1167 if (c == '%') 1168 { 1169 static char ach[] = "01234567989ABCDEF"; 1170 1171 // convert two chars after '%' 1172 CHAR c2, // first char after '%' 1173 c3; // second char after '%' 1174 const char *p2, // for first char: points into ach or is NULL 1175 *p3; // for second char: points into ach or is NULL 1176 if ( (c2 = *pSource) 1177 && (p2 = strchr(ach, c2)) 1178 && (c3 = *(pSource + 1)) 1179 && (p3 = strchr(ach, c3)) 1180 ) 1181 { 1182 // both chars after '%' were valid: 1183 *pDest++ = (p2 - ach) // 0 for '0', 10 for 'A', ... 1184 + ((p3 - ach) << 4); 1185 // go on after that 1186 pSource += 2; 1187 // raise return count 1188 ulrc++; 1189 // next in loop 1190 continue; 1191 } 1192 } 1193 1194 // not encoding, or null after '%', or invalid encoding: 1195 // just copy this 1196 *pDest++ = c; 1197 } // while ((ch = *pSource++)) 1198 1199 if (ulrc) 1200 { 1201 // any encodings found: 1202 // terminate target 1203 *pDest = 0; 1204 1205 // replace source with target 1206 free(pxstr->psz); 1207 pxstr->psz = pszDest; 1208 pxstr->cbAllocated = cbAllocated; 1209 pxstr->ulLength = (pDest - pszDest); 1210 } 1211 else 1212 // no encodings found: 1213 free(pszDest); 1214 } 1215 } 1216 1217 return (ulrc); 1218 } 1219 1220 /* 1053 1221 *@@ xstrConvertLineFormat: 1054 1222 * converts between line formats.
Note:
See TracChangeset
for help on using the changeset viewer.