Ignore:
Timestamp:
Mar 7, 2001, 10:41:07 PM (24 years ago)
Author:
umoeller
Message:

Misc. changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/xstring.c

    r42 r43  
    10831083}
    10841084
     1085// static encoding table for xstrEncode
     1086static PSZ apszEncoding[] =
     1087{
     1088    "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
     1089    "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
     1090    "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
     1091    "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F",
     1092    "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
     1093    "%28", "%29", "%2A", "%2B", "%2C", "%2D", "%2E", "%2F",
     1094    "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
     1095    "%38", "%39", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F",
     1096    "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
     1097    "%48", "%49", "%4A", "%4B", "%4C", "%4D", "%4E", "%4F",
     1098    "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
     1099    "%58", "%59", "%5A", "%5B", "%5C", "%5D", "%5E", "%5F",
     1100    "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
     1101    "%68", "%69", "%6A", "%6B", "%6C", "%6D", "%6E", "%6F",
     1102    "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
     1103    "%78", "%79", "%7A", "%7B", "%7C", "%7D", "%7E", "%7F",
     1104    "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
     1105    "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F",
     1106    "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
     1107    "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F",
     1108    "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7",
     1109    "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF",
     1110    "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7",
     1111    "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF",
     1112    "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7",
     1113    "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF",
     1114    "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7",
     1115    "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF",
     1116    "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7",
     1117    "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF",
     1118    "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7",
     1119    "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF"
     1120};
     1121
    10851122/*
    10861123 *@@ xstrEncode:
     
    11161153 +          S%61mple %63hara%63ters.
    11171154 *
    1118  *      Memory cost: None, except for that of xstrFindReplace.
     1155 *      Memory cost: None, except for that of xstrcpy.
    11191156 *
    11201157 *@@added V0.9.9 (2001-02-28) [umoeller]
     1158 *@@changed V0.9.9 (2001-03-06) [lafaix]: rewritten.
    11211159 */
    11221160
     
    11251163{
    11261164    ULONG ulrc = 0,
    1127           ul;
    1128 
    1129     // now encode the widget setup string...
    1130     for (ul = 0;
    1131          ul < strlen(pcszEncode);
    1132          ul++)
    1133     {
    1134         CHAR        szFind[3] = "?",
    1135                     szReplace[10] = "%xx";
    1136         XSTRING     strFind,
    1137                     strReplace;
    1138         size_t      ShiftTable[256];
    1139         BOOL        fRepeat = FALSE;
    1140         ULONG       ulOfs = 0;
    1141 
    1142         // search string:
    1143         szFind[0] = pcszEncode[ul];
    1144         xstrInitSet(&strFind, szFind);
    1145 
    1146         // replace string: ASCII encoding
    1147         sprintf(szReplace, "%%%lX", pcszEncode[ul]);
    1148         xstrInitSet(&strReplace, szReplace);
    1149 
    1150         // replace all occurences
    1151         while (xstrFindReplace(pxstr,
    1152                                &ulOfs,
    1153                                &strFind,
    1154                                &strReplace,
    1155                                ShiftTable,
    1156                                &fRepeat))
    1157                 ulrc++;
    1158 
    1159     } // for ul; next encoding
     1165          ul,
     1166          ulEncodeLength;
     1167
     1168    if (    (pxstr)
     1169         && (pxstr->ulLength)
     1170         && (pcszEncode)
     1171         && (ulEncodeLength = strlen(pcszEncode)))
     1172    {
     1173        PSZ pszDest = (PSZ)malloc(pxstr->ulLength * 3
     1174                                  + 1),
     1175            pszDestCurr = pszDest;
     1176
     1177        if (pszDest)
     1178        {
     1179            for (ul = 0;
     1180                 ul < pxstr->ulLength;
     1181                 ul++)
     1182            {
     1183                ULONG ulEncode;
     1184
     1185                for (ulEncode = 0;
     1186                     ulEncode < ulEncodeLength;
     1187                     ulEncode++)
     1188                {
     1189                    if (pxstr->psz[ul] == pcszEncode[ulEncode])
     1190                    {
     1191                        // use the static encoding table for speed
     1192                        memcpy(pszDestCurr,
     1193                               apszEncoding[(unsigned char)pcszEncode[ulEncode]],
     1194                               3);
     1195                        pszDestCurr += 3;
     1196                        ulrc++;
     1197                        goto iterate;
     1198                    }
     1199                }
     1200
     1201                *pszDestCurr++ = pxstr->psz[ul];
     1202
     1203                iterate:
     1204                    ;
     1205            }
     1206        }
     1207
     1208        // something was encoded; update pxstr
     1209        if (ulrc)
     1210        {
     1211            *pszDestCurr = 0;
     1212
     1213            xstrcpy(pxstr, pszDest, pszDestCurr-pszDest);
     1214        }
     1215
     1216        free(pszDest);
     1217    }
    11601218
    11611219    return (ulrc);
     
    11741232 *      Returns the no. of encodings replaced.
    11751233 *
    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.
     1234 *      Memory cost: None.
    11801235 *
    11811236 *@@added V0.9.9 (2001-02-28) [umoeller]
     1237 *@@changed V0.9.9 (2001-03-06) [lafaix]: removed memory allocation
    11821238 */
    11831239
     
    11901246       )
    11911247    {
    1192         ULONG   cbAllocated = pxstr->ulLength + 1;
    1193             // decoded string cannot be longer than source
    1194         PSZ     pszDest = (PSZ)malloc(cbAllocated);
    1195 
    1196         if (pszDest)
     1248        const char  *pSource = pxstr->psz;
     1249        PSZ         pszDest  = (PSZ)pSource,
     1250                    pDest    = (PSZ)pSource;
     1251        CHAR        c;
     1252
     1253        while ((c = *pSource++))
    11971254        {
    1198             const char  *pSource = pxstr->psz;
    1199             PSZ         pDest = pszDest;
    1200 
    1201             CHAR    c;
    1202 
    1203             while ((c = *pSource++))
     1255            // pSource points to next char now
     1256
     1257            if (c == '%')
    12041258            {
    1205                 // pSource points to next char now
    1206 
    1207                 if (c == '%')
     1259                static char ach[] = "0123456789ABCDEF";
     1260
     1261                // convert two chars after '%'
     1262                CHAR        c2,         // first char after '%'     --> hi-nibble
     1263                            c3;         // second char after '%'    --> lo-nibble
     1264                const char  *p2,        // for first char: points into ach or is NULL
     1265                            *p3;        // for second char: points into ach or is NULL
     1266                if (    (c2 = *pSource)
     1267                     && (p2 = strchr(ach, c2))
     1268                     && (c3 = *(pSource + 1))
     1269                     && (p3 = strchr(ach, c3))
     1270                   )
    12081271                {
    1209                     static char ach[] = "0123456789ABCDEF";
    1210 
    1211                     // convert two chars after '%'
    1212                     CHAR        c2,         // first char after '%'     --> hi-nibble
    1213                                 c3;         // second char after '%'    --> lo-nibble
    1214                     const char  *p2,        // for first char: points into ach or is NULL
    1215                                 *p3;        // for second char: points into ach or is NULL
    1216                     if (    (c2 = *pSource)
    1217                          && (p2 = strchr(ach, c2))
    1218                          && (c3 = *(pSource + 1))
    1219                          && (p3 = strchr(ach, c3))
    1220                        )
    1221                     {
    1222                         // both chars after '%' were valid:
    1223                         *pDest++ =    // lo-nibble:
    1224                                       (p3 - ach) // 0 for '0', 10 for 'A', ...
    1225                                       // hi-nibble:
    1226                                     + ((p2 - ach) << 4);
    1227                         // go on after that
    1228                         pSource += 2;
    1229                         // raise return count
    1230                         ulrc++;
    1231                         // next in loop
    1232                         continue;
    1233                     }
     1272                    // both chars after '%' were valid:
     1273                    *pDest++ =    // lo-nibble:
     1274                                  (p3 - ach) // 0 for '0', 10 for 'A', ...
     1275                                  // hi-nibble:
     1276                                + ((p2 - ach) << 4);
     1277                    // go on after that
     1278                    pSource += 2;
     1279                    // raise return count
     1280                    ulrc++;
     1281                    // next in loop
     1282                    continue;
    12341283                }
    1235 
    1236                 // not encoding, or null after '%', or invalid encoding:
    1237                 // just copy this
    1238                 *pDest++ = c;
    1239             } // while ((ch = *pSource++))
    1240 
    1241             if (ulrc)
    1242             {
    1243                 // any encodings found:
    1244                 // terminate target
    1245                 *pDest = 0;
    1246 
    1247                 // replace source with target
    1248                 free(pxstr->psz);
    1249                 pxstr->psz = pszDest;
    1250                 pxstr->cbAllocated = cbAllocated;
    1251                 pxstr->ulLength = (pDest - pszDest);
    12521284            }
    1253             else
    1254                 // no encodings found:
    1255                 free(pszDest);
     1285
     1286            // not encoding, or null after '%', or invalid encoding:
     1287            // just copy this
     1288            *pDest++ = c;
     1289        } // while ((ch = *pSource++))
     1290
     1291        if (ulrc)
     1292        {
     1293            *pDest = 0;
     1294            pxstr->ulLength = (pDest - pszDest);
    12561295        }
    12571296    }
Note: See TracChangeset for help on using the changeset viewer.