MCPcopy Create free account
hub / github.com/IBM/Project_CodeNet / XML_escape

Function XML_escape

tools/tokenizer/libtoken.c:1247–1282  ·  view source on GitHub ↗

Escape token for output as XML text.

Source from the content-addressed store, hash-verified

1245
1246// Escape token for output as XML text.
1247void 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("&lt;", out);
1255 else
1256 if (*p == '>')
1257 fputs("&gt;", out);
1258 else
1259 if (*p == '&')
1260 fputs("&amp;", 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}

Callers 2

mainFunction · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected