-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.c
167 lines (154 loc) · 4.06 KB
/
xml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* See one of the index files for license and other details. */
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdbool.h>
#include "sanity.h"
#include "json.h"
#include "xml.h"
static void indent( FILE *fp, unsigned depth );
static bool xstr( FILE *fp, const char *s );
static bool xvalue( FILE *fp, const jvalue *j, unsigned depth );
/** Writes the parsed JSON value tree out to the supplied file
* descriptor in XML, using the grammar described in the man page for
* jsoncvt. */
bool
writexml( FILE *fp, const jvalue *j )
{
fputs( "<?xml version='1.0' encoding='utf-8' ?>\n", fp );
fputs( "<!DOCTYPE jsoncvt PUBLIC '-//KRZ//DTD jsoncvt 1.0.8//EN' 'http://www.cis.rit.edu/~krz/hacks/jsoncvt/jsoncvt.dtd'>\n", fp );
fputs( "<jsoncvt>\n", fp );
int r = xvalue( fp, j, 1 );
fputs( "</jsoncvt>\n", fp );
return r;
}
/** For a given jvalue, print its opening element. It may optionally
* contain a name attribute. */
static void
xopen( FILE *fp, const jvalue *j, unsigned depth )
{
indent( fp, depth );
switch( j->d ) {
case jnull:
fputs( "<null", fp );
break;
case jtrue:
fputs( "<true", fp );
break;
case jfalse:
fputs( "<false", fp );
break;
case jstring:
fputs( "<string", fp );
break;
case jnumber: case jint: case jreal:
fputs( "<number", fp );
break;
case jarray:
fputs( "<array", fp );
break;
case jobject:
fputs( "<object", fp );
break;
}
if( j->n ) {
fputs( " name='", fp );
xstr( fp, j->n );
fputc( '\'', fp );
}
switch( j->d ) {
case jnull: case jtrue: case jfalse:
fputs( " />", fp );
break;
case jarray: case jobject:
fputs( ">\n", fp );
break;
default:
fputc( '>', fp );
break;
}
}
/** For a given jvalue, print its closing element. */
static void
xclose( FILE *fp, const jvalue *j, unsigned depth )
{
switch( j->d ) {
case jnull: case jtrue: case jfalse:
break;
case jarray:
indent( fp, depth );
fputs( "</array>", fp );
break;
case jobject:
indent( fp, depth );
fputs( "</object>", fp );
break;
case jstring:
fputs( "</string>", fp );
break;
case jnumber: case jint: case jreal:
fputs( "</number>", fp );
break;
}
fputc( '\n', fp );
}
/** Given a JSON value, write its value to the supplied output stream. */
static bool
xvalue( FILE *fp, const jvalue *j, unsigned depth )
{
xopen( fp, j, depth );
switch( j->d ) {
case jnull: case jtrue: case jfalse:
break;
case jstring:
xstr( fp, j->u.s );
break;
case jnumber:
fputs( j->u.s, fp );
break;
case jint:
fprintf( fp, "%llu", j->u.i );
break;
case jreal:
fprintf( fp, "%Lg", j->u.r );
break;
case jarray: case jobject:
for( jvalue **jj = j->u.v; *jj; ++jj )
xvalue( fp, *jj, depth+1 );
break;
}
xclose( fp, j, depth );
return true;
}
/** Write the supplied string onto the outfile file stream, escaping
* the main five standard entities along the way. Because we know
* that the JSON parser went out of its way to store text as UTF-8,
* we don't actually have to do anything special here. Returns <0 if
* there's an error. */
static bool
xstr( FILE *fp, const char *s )
{
while( *s ) {
if( *s == '<' )
fputs( "<", fp );
else if( *s == '>' )
fputs( ">", fp );
else if( *s == '&' )
fputs( "&", fp );
else if( *s == '\'' )
fputs( "'", fp );
else if( *s == '"' )
fputs( """, fp );
else
fputc( *s, fp );
++s;
}
return true;
}
/** Given a nesting depth (zero being the outermost element), emit
* some number of spaces that are appropriate for that depth. */
static void
indent( FILE *fp, unsigned depth )
{
while( depth-- )
fputs( " ", fp );
}