-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_helper.c
More file actions
274 lines (224 loc) · 5.96 KB
/
string_helper.c
File metadata and controls
274 lines (224 loc) · 5.96 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <printf.h>
#include <postgres.h>
#include "string_helper.h"
static int append_string( char ** buffer, char * fmt, ... ) __attribute__ ((format (printf, 2, 3)));
static int print_xml(FILE *stream, const struct printf_info *info, const void *const *args);
static int print_xml_arginfo (const struct printf_info *info, size_t n, int *argtypes);
// register printf conversion specifiers
void string_helper_init()
{
register_printf_function('M', print_xml, print_xml_arginfo);
register_printf_function('N', print_xml, print_xml_arginfo);
}
static int xml_indent( DumpContext * context )
{
int i;
for( i=0; i<context->indent; i++ )
append_string( context->output, " " );
return 2 * context->indent;
}
DumpContext * new_dump_context()
{
DumpContext * context = palloc0( sizeof( DumpContext ) );
context->output = palloc0( sizeof( char *));
return context;
}
int xml_tag( DumpContext * context, const char * tagname, ... )
{
va_list ap;
int written = 0, len;
written = xml_indent( context );
len = append_string( context->output, "<%N", tagname );
if ( len < 0 ) return len;
written += len;
va_start( ap, tagname );
len = xml_attributes( context, ap );
va_end( ap );
if ( len < 0 ) return len;
written += len;
len = append_string( context->output, "/>\n" );
if ( len < 0 ) return len;
written += len;
return written;
}
int xml_pi( DumpContext * context, const char * tagname, ... )
{
va_list ap;
int written = 0, len;
len = append_string( context->output, "<?%N", tagname );
if ( len < 0 ) return len;
written += len;
va_start( ap, tagname );
len = xml_attributes( context, ap );
va_end( ap );
if ( len < 0 ) return len;
written += len;
len = append_string( context->output, "?>\n" );
if ( len < 0 ) return len;
written += len;
return written;
}
int xml_textnode( DumpContext * context, const char * tagname, const char * format, ... )
{
va_list ap;
int written = 0, len;
written = xml_indent( context );
len = append_string( context->output, "<%N>", tagname );
if ( len < 0 ) return len;
written += len;
va_start( ap, format );
char * tmp;
len = vasprintf( &tmp, format, ap );
va_end( ap );
if ( len < 0 ) return len;
len = append_string( context->output, "%M", tmp );
free( tmp );
if ( len < 0 ) return len;
written += len;
len = append_string( context->output, "</%N>\n", tagname );
if ( len < 0 ) return len;
written += len;
return written;
}
int xml_tag_open( DumpContext * context, const char * tagname )
{
int written = 0, len;
written = xml_indent( context );
len = append_string( context->output, "<%N>\n", tagname );
if ( len < 0 ) return len;
written += len;
context->indent++;
return written;
}
int xml_tag_open_namespace( DumpContext * context, const char * tagname, const char * namespace )
{
int written = 0, len;
written = xml_indent( context );
len = append_string( context->output, "<%N xmlns=\"%N\">\n", tagname, namespace );
if ( len < 0 ) return len;
written += len;
context->indent++;
return written;
}
int xml_attributes( DumpContext * context, va_list ap )
{
int written = 0, len;
char *attribute, *format, *tmp;
while( 1 ) {
attribute = va_arg( ap, char * );
if ( attribute ) {
format = va_arg( ap, char * );
if ( format ) {
len = vasprintf(&tmp, format, ap);
if ( len < 0 ) return len;
len = append_string( context->output, " %N=\"%N\"", attribute, tmp );
free( tmp );
if ( len < 0 ) return len;
written += len;
} else {
break;
}
} else {
break;
}
}
return written;
}
int xml_tag_close( DumpContext * context, const char * tagname )
{
context->indent--;
xml_indent( context );
return append_string( context->output, "</%N>\n", tagname );
}
int xml_content( DumpContext * context, const char * fmt, ... )
{
va_list ap;
char * tmp;
va_start( ap, fmt );
int len = vasprintf(&tmp, fmt, ap);
va_end( ap );
if ( len < 0 ) return len;
xml_indent( context );
return append_string( context->output, "%M\n", tmp );
}
/*
* printf format handler that does xml escaping
*
* FIXME: This is not safe for all encodings.
* FIXME: integer overflow
*/
static int print_xml(FILE *stream, const struct printf_info *info, const void *const *args)
{
const char * xml;
int written = 0;
xml = *((const char **) args[0]);
for( ; *xml; xml++ ){
int len;
switch( *xml ) {
case '<':
len = fprintf( stream, "%s", "<" );
break;
case '>':
len = fprintf( stream, "%s", ">" );
break;
case '&':
len = fprintf( stream, "%s", "&" );
break;
case '"':
if ( info->spec == 'N' ) {
len = fprintf( stream, "%s", """ );
} else {
len = fprintf( stream, "%c", *xml );
}
break;
default:
len = fprintf( stream, "%c", *xml );
break;
}
if ( len < 0 )
return len;
else
written += len;
}
return written;
}
static int print_xml_arginfo (const struct printf_info *info, size_t n, int *argtypes)
{
/* We always take exactly one argument and this is a pointer to the structure.. */
if (n > 0)
argtypes[0] = PA_POINTER;
return 1;
}
/** helper function to append formatted string at the end of an existing string */
static int
append_string( char ** buffer, char * fmt, ... )
{
va_list ap;
char * tmp;
va_start( ap, fmt );
int len = vasprintf(&tmp, fmt, ap);
va_end( ap );
if ( len == -1 ) {
/* error */
return -1;
} else {
size_t offset = 0;
if ( !*buffer ) {
*buffer = (char *) palloc( len + 1 );
} else {
offset = strlen( *buffer );
*buffer = (char *) repalloc( *buffer, offset + len + 1 );
}
if (!*buffer) {
free( tmp );
return -1;
} else {
memcpy( *buffer + offset, tmp, len + 1 );
free( tmp );
}
return len;
}
}