about summary refs log tree commit diff
path: root/sternenblog/xml.c
blob: 5965a09f2758e66701205aa94d0939d0cbf1333e (plain) (blame)
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// TODO indent, html escaping
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "xml.h"

#define DEBUG_WARN(ctx, ...) \
    if(ctx->warn != NULL) { \
        fprintf(ctx->warn, __VA_ARGS__); \
    }

void debug_xml_stack(FILE *out, struct xml_stack *stack) {
    if(stack != NULL) {
        fprintf(out, "%s ", stack->tag);
        debug_xml_stack(out, stack->next);
    } else {
        fputc('\n', out);
    }
}

void free_xml_stack(struct xml_stack *stack) {
    if(stack == NULL) {
        return;
    }

    if(stack->tag != NULL) {
        free(stack->tag);
    }

    if(stack->next != NULL) {
        free_xml_stack(stack->next);
    }

    free(stack);
}

void new_xml_context(struct xml_context *ctx) {
    ctx->stack = NULL;
    ctx->warn = NULL;
    ctx->out = stdout;
    ctx->closing_slash = 1;
}

void del_xml_context(struct xml_context *ctx) {
    if(ctx->stack != NULL) {
        if(ctx->warn != NULL) {
            fputs("Unclosed tags remaining: ", ctx->warn);
            debug_xml_stack(ctx->warn, ctx->stack);
        }

        free_xml_stack(ctx->stack);
    }
}

void output_xml_escaped_char(FILE *out, char c) {
    switch(c) {
        case '&':
            fputs("&amp;", out);
            break;
        case '<':
            fputs("&lt;", out);
            break;
        case '>':
            fputs("&gt;", out);
            break;
        case '\'':
            fputs("&apos;", out);
            break;
        case '\"':
            fputs("&quot;", out);
            break;
        default:
            fputc(c, out);
            break;
    }
}

void xml_escaped(struct xml_context *ctx, const char *str) {
    for(size_t i = 0; str[i] != '\0'; i++) {
        output_xml_escaped_char(ctx->out, str[i]);
    }
}

void xml_raw(struct xml_context *ctx, const char *str) {
    fputs(str, ctx->out);
}

void output_attrs(FILE *out, va_list attrs, size_t arg_count) {
    if(arg_count > 0) {
        for(size_t i = 1; i<=arg_count; i++) {
            if(i % 2) {
                char *name = va_arg(attrs, char *);
                if(name == NULL) {
                    break;
                }

                fputc(' ', out);
                fputs(name, out);
            } else {
                char *maybe_val = va_arg(attrs, char *);
                if(maybe_val != NULL) {
                    fputs("=\"", out);
                    for(size_t i = 0; maybe_val[i] != '\0'; i++) {
                        output_xml_escaped_char(out, maybe_val[i]);
                    }
                    fputc('\"', out);
                }
            }
        }
    }
}

void xml_empty_tag(struct xml_context *ctx, const char *tag, size_t attr_count, ...) {
    if(tag == NULL || ctx == NULL) {
        DEBUG_WARN(ctx, "Got no tag or ctx\n");
        return;
    }

    fputc('<', ctx->out);
    fputs(tag, ctx->out);

    if(attr_count > 0) {
        size_t arg_count = attr_count * 2;

        va_list attrs;
        va_start(attrs, attr_count);

        output_attrs(ctx->out, attrs, arg_count);

        va_end(attrs);
    }

    if(ctx->closing_slash) {
        fputc('/', ctx->out);
    }

    fputc('>', ctx->out);
}

void xml_open_tag_attrs(struct xml_context *ctx, const char *tag, size_t attr_count, ...) {
    if(tag == NULL || ctx == NULL) {
        DEBUG_WARN(ctx, "Got no tag or ctx\n");
        return;
    }

    struct xml_stack *old_stack = ctx->stack;

    fputc('<', ctx->out);
    fputs(tag, ctx->out);


    if(attr_count > 0) {
        size_t arg_count = attr_count * 2;

        va_list attrs;
        va_start(attrs, attr_count);

        output_attrs(ctx->out, attrs, arg_count);

        va_end(attrs);
    }

    fputc('>', ctx->out);

    ctx->stack = malloc(sizeof(struct xml_context));

    if(ctx->stack == NULL) {
        ctx->stack = old_stack;
        DEBUG_WARN(ctx, "Could not allocate memory for tag stack, now everything will break.\n")
        return;
    }

    ctx->stack->next = old_stack;

    size_t tag_size = strlen(tag) + 1;
    ctx->stack->type = XML_NORMAL_TAG;
    ctx->stack->tag = malloc(sizeof(char) * tag_size);
    memcpy(ctx->stack->tag, tag, tag_size);
}

void xml_open_tag(struct xml_context *ctx, const char *tag) {
    xml_open_tag_attrs(ctx, tag, 0);
}

void xml_close_tag(struct xml_context *ctx, const char *tag) {
    if(tag == NULL || ctx == NULL) {
        DEBUG_WARN(ctx, "Got no tag or ctx\n");
        return;
    }

    if(ctx->stack == NULL) {
        DEBUG_WARN(ctx, "Refusing to close tag %s, no tags left to be closed\n", tag);
        return;
    }

    if(ctx->stack->type != XML_NORMAL_TAG) {
        DEBUG_WARN(ctx, "Refusing to close tag %s, wrong tag type\n", tag);
        return;
    }

    if(strcmp(tag, ctx->stack->tag) != 0) {
        DEBUG_WARN(ctx, "Refusing to close tag %s, unclosed tags remaining\n", tag);
        return;
    }

    fputs("</", ctx->out);
    fputs(tag, ctx->out);
    fputc('>', ctx->out);

    struct xml_stack *old_head = ctx->stack;

    ctx->stack = old_head->next;

    free(old_head->tag);
    free(old_head);
}

void xml_close_all(struct xml_context *ctx) {
    xml_close_including(ctx, NULL);
}

void xml_close_including(struct xml_context *ctx, const char *tag) {
    if(ctx == NULL) {
        DEBUG_WARN(ctx, "Got no ctx\n");
        return;
    }

    if(ctx->stack == NULL) {
        if(tag != NULL) {
            DEBUG_WARN(ctx, "Hit end of tag stack while searching for tag %s to close\n", tag);
        }
        return;
    } else {
        int last_tag = tag != NULL && strcmp(tag, ctx->stack->tag) == 0;

        switch(ctx->stack->type) {
            case XML_NORMAL_TAG:
                xml_close_tag(ctx, ctx->stack->tag);
                break;
            case XML_CDATA:
                xml_close_cdata(ctx);
                break;
            default:
                DEBUG_WARN(ctx, "Unexpected tag type on stack, aborting\n");
                return;
        }

        if(!last_tag) {
            xml_close_including(ctx, tag);
        }
    }
}

void xml_open_cdata(struct xml_context *ctx) {
    if(ctx == NULL) {
        DEBUG_WARN(ctx, "Got no ctx\n");
        return;
    }

    struct xml_stack *old_stack = ctx->stack;

    ctx->stack = malloc(sizeof(struct xml_stack));

    if(ctx->stack == NULL) {
        ctx->stack = old_stack;

        DEBUG_WARN(ctx, "Could not allocate memory for tag stack, now everything will break.\n");
        return;
    }

    ctx->stack->next = old_stack;
    ctx->stack->tag = NULL;
    ctx->stack->type = XML_CDATA;

    fputs("<![CDATA[", ctx->out);
}

void xml_close_cdata(struct xml_context *ctx) {
    if(ctx == NULL) {
        DEBUG_WARN(ctx, "Got no ctx\n");
        return;
    }

    if(ctx->stack == NULL) {
        DEBUG_WARN(ctx, "No CDATA to close\n");
        return;
    }

    if(ctx->stack->type != XML_CDATA) {
        DEBUG_WARN(ctx, "No CDATA on top of stack, refusing to close\n");
        return;
    }

    struct xml_stack *old_head = ctx->stack;

    ctx->stack = old_head->next;

    if(old_head->tag != NULL) {
        // shouldn't happen though
        free(old_head->tag);
    }

    free(old_head);

    fputs("]]>", ctx->out);
}