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

Function XML_escape

tools/tokenizer/jstokenize.c:641–676  ·  view source on GitHub ↗

Escape token for output as XML text.

Source from the content-addressed store, hash-verified

639
640// Escape token for output as XML text.
641static void XML_escape(FILE *out, const char *token)
642{
643#if 1
644 // Alternative: escape every <, >, and &:
645 const char *p;
646 for (p = token; *p; p++) {
647 if (*p == '<')
648 fputs("&lt;", out);
649 else
650 if (*p == '>')
651 fputs("&gt;", out);
652 else
653 if (*p == '&')
654 fputs("&amp;", out);
655 else
656 fputc(*p, out);
657 }
658#else
659 // User CDATA construct for escaping.
660 // Impossible to escape ]]> occurring in token!
661 // Must chop up the substring ]]> in ]] and >.
662 const char *p;
663 const char *q = token;
664 // "abc]]>hello" => <![CDATA["abc]]]]><![CDATA[>hello"]]>
665 // "]]>]]>" => <![CDATA[]]]]><!CDATA[>]]]]><![CDATA[>"]]>
666 while ((p = strstr(q, "]]>"))) {
667 int len = p - q; // always > 0
668 fputs("<![CDATA[", out);
669 fwrite(q, 1, len, out);
670 fputs("]]]]>", out);
671 q = p+2; // q start at >...
672 }
673 if (q < token+strlen(token))
674 fprintf(out, "<![CDATA[%s]]>", q);
675#endif
676}
677
678int main(int argc, char *argv[])
679{

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected