Debian Codegraph @

ext/xml.c

raw symbols SCIP
1 /* markdown: a C implementation of John Gruber's Markdown markup language.
2 *
3 * Copyright (C) 2007 David L Parsons.
4 * The redistribution terms are provided in the COPYRIGHT file that must
5 * be distributed with this source code.
6 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdarg.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <ctype.h>
13
14 #include "config.h"
15
16 #include "cstring.h"
17 #include "markdown.h"
18 #include "amalloc.h"
19
20 /* return the xml version of a character
21 */
22 static char *
23 mkd_xmlchar(unsigned char c)
24 {
25 switch (c) {
26 case '<': return "&lt;";
27 case '>': return "&gt;";
28 case '&': return "&amp;";
29 case '"': return "&quot;";
30 case '\'': return "&apos;";
31 default: if ( isascii(c) || (c & 0x80) )
32 return 0;
33 return "";
34 }
35 }
36
37
38 /* write output in XML format
39 */
40 int
41 mkd_generatexml(char *p, int size, FILE *out)
42 {
43 unsigned char c;
44 char *entity;
45
46 while ( size-- > 0 ) {
47 c = *p++;
48
49 if ( entity = mkd_xmlchar(c) )
50 DO_OR_DIE( fputs(entity, out) );
51 else
52 DO_OR_DIE( fputc(c, out) );
53 }
54 return 0;
55 }
56
57
58 /* build a xml'ed version of a string
59 */
60 int
61 mkd_xml(char *p, int size, char **res)
62 {
63 unsigned char c;
64 char *entity;
65 Cstring f;
66
67 CREATE(f);
68 RESERVE(f, 100);
69
70 while ( size-- > 0 ) {
71 c = *p++;
72 if ( entity = mkd_xmlchar(c) )
73 Cswrite(&f, entity, strlen(entity));
74 else
75 Csputc(c, &f);
76 }
77 /* null terminate, strdup() into a free()able memory block,
78 * and return the size of everything except the null terminator
79 */
80 EXPAND(f) = 0;
81 *res = strdup(T(f));
82 return S(f)-1;
83 }