Escape token for output as XML text.
| 1245 | |
| 1246 | // Escape token for output as XML text. |
| 1247 | void XML_escape(FILE *out, const char *token) |
| 1248 | { |
| 1249 | #if 1 |
| 1250 | // Alternative: escape every <, >, and &: |
| 1251 | const char *p; |
| 1252 | for (p = token; *p; p++) { |
| 1253 | if (*p == '<') |
| 1254 | fputs("<", out); |
| 1255 | else |
| 1256 | if (*p == '>') |
| 1257 | fputs(">", out); |
| 1258 | else |
| 1259 | if (*p == '&') |
| 1260 | fputs("&", out); |
| 1261 | else |
| 1262 | fputc(*p, out); |
| 1263 | } |
| 1264 | #else |
| 1265 | // User CDATA construct for escaping. |
| 1266 | // Impossible to escape ]]> occurring in token! |
| 1267 | // Must chop up the substring ]]> in ]] and >. |
| 1268 | const char *p; |
| 1269 | const char *q = token; |
| 1270 | // "abc]]>hello" => <![CDATA["abc]]]]><![CDATA[>hello"]]> |
| 1271 | // "]]>]]>" => <![CDATA[]]]]><!CDATA[>]]]]><![CDATA[>"]]> |
| 1272 | while ((p = strstr(q, "]]>"))) { |
| 1273 | int len = p - q; // always > 0 |
| 1274 | fputs("<![CDATA[", out); |
| 1275 | fwrite(q, 1, len, out); |
| 1276 | fputs("]]]]>", out); |
| 1277 | q = p+2; // q start at >... |
| 1278 | } |
| 1279 | if (q < token+strlen(token)) |
| 1280 | fprintf(out, "<![CDATA[%s]]>", q); |
| 1281 | #endif |
| 1282 | } |