]>
Commit | Line | Data |
---|---|---|
a51447c9 | 1 | |
625fcfe9 | 2 | #include "yaml_private.h" |
a51447c9 | 3 | |
625fcfe9 KS |
4 | /* |
5 | * Get the library version. | |
6 | */ | |
95b98ba9 | 7 | |
5eff53a4 KS |
8 | YAML_DECLARE(const char *) |
9 | yaml_get_version_string(void) | |
10 | { | |
11 | return YAML_VERSION_STRING; | |
12 | } | |
13 | ||
625fcfe9 KS |
14 | /* |
15 | * Get the library version numbers. | |
16 | */ | |
17 | ||
5eff53a4 KS |
18 | YAML_DECLARE(void) |
19 | yaml_get_version(int *major, int *minor, int *patch) | |
20 | { | |
21 | *major = YAML_VERSION_MAJOR; | |
22 | *minor = YAML_VERSION_MINOR; | |
23 | *patch = YAML_VERSION_PATCH; | |
24 | } | |
25 | ||
95b98ba9 KS |
26 | /* |
27 | * Allocate a dynamic memory block. | |
28 | */ | |
29 | ||
f642fd11 | 30 | YAML_DECLARE(void *) |
95b98ba9 KS |
31 | yaml_malloc(size_t size) |
32 | { | |
33 | return malloc(size ? size : 1); | |
34 | } | |
35 | ||
36 | /* | |
37 | * Reallocate a dynamic memory block. | |
38 | */ | |
39 | ||
f642fd11 | 40 | YAML_DECLARE(void *) |
95b98ba9 KS |
41 | yaml_realloc(void *ptr, size_t size) |
42 | { | |
43 | return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1); | |
44 | } | |
45 | ||
a51447c9 | 46 | /* |
95b98ba9 KS |
47 | * Free a dynamic memory block. |
48 | */ | |
49 | ||
f642fd11 | 50 | YAML_DECLARE(void) |
95b98ba9 KS |
51 | yaml_free(void *ptr) |
52 | { | |
53 | if (ptr) free(ptr); | |
54 | } | |
55 | ||
56 | /* | |
625fcfe9 | 57 | * Duplicate a string. |
a51447c9 KS |
58 | */ |
59 | ||
cf616166 KS |
60 | YAML_DECLARE(yaml_char_t *) |
61 | yaml_strdup(const yaml_char_t *str) | |
a51447c9 | 62 | { |
cf616166 KS |
63 | if (!str) |
64 | return NULL; | |
65 | ||
66 | return (yaml_char_t *)strdup((char *)str); | |
625fcfe9 | 67 | } |
a51447c9 | 68 | |
625fcfe9 KS |
69 | /* |
70 | * Extend a string. | |
71 | */ | |
6eb1ded4 | 72 | |
625fcfe9 KS |
73 | YAML_DECLARE(int) |
74 | yaml_string_extend(yaml_char_t **start, | |
75 | yaml_char_t **pointer, yaml_char_t **end) | |
76 | { | |
fc2dd942 | 77 | yaml_char_t *new_start = (yaml_char_t *)yaml_realloc((void*)*start, (*end - *start)*2); |
f2b59d4d | 78 | |
625fcfe9 | 79 | if (!new_start) return 0; |
6eb1ded4 | 80 | |
625fcfe9 | 81 | memset(new_start + (*end - *start), 0, *end - *start); |
6eb1ded4 | 82 | |
625fcfe9 KS |
83 | *pointer = new_start + (*pointer - *start); |
84 | *end = new_start + (*end - *start)*2; | |
85 | *start = new_start; | |
f2b59d4d | 86 | |
625fcfe9 KS |
87 | return 1; |
88 | } | |
6eb1ded4 | 89 | |
625fcfe9 KS |
90 | /* |
91 | * Append a string B to a string A. | |
92 | */ | |
f2b59d4d | 93 | |
625fcfe9 KS |
94 | YAML_DECLARE(int) |
95 | yaml_string_join( | |
96 | yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end, | |
fc2dd942 | 97 | yaml_char_t **b_start, yaml_char_t **b_pointer, SHIM(yaml_char_t **b_end)) |
625fcfe9 | 98 | { |
fc2dd942 | 99 | UNUSED_PARAM(b_end) |
625fcfe9 KS |
100 | if (*b_start == *b_pointer) |
101 | return 1; | |
f2b59d4d | 102 | |
625fcfe9 KS |
103 | while (*a_end - *a_pointer <= *b_pointer - *b_start) { |
104 | if (!yaml_string_extend(a_start, a_pointer, a_end)) | |
105 | return 0; | |
106 | } | |
f2b59d4d | 107 | |
625fcfe9 KS |
108 | memcpy(*a_pointer, *b_start, *b_pointer - *b_start); |
109 | *a_pointer += *b_pointer - *b_start; | |
f2b59d4d | 110 | |
625fcfe9 KS |
111 | return 1; |
112 | } | |
f2b59d4d | 113 | |
625fcfe9 KS |
114 | /* |
115 | * Extend a stack. | |
116 | */ | |
f2b59d4d | 117 | |
625fcfe9 KS |
118 | YAML_DECLARE(int) |
119 | yaml_stack_extend(void **start, void **top, void **end) | |
120 | { | |
0174ed6e | 121 | void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2); |
f2b59d4d | 122 | |
625fcfe9 | 123 | if (!new_start) return 0; |
f2b59d4d | 124 | |
0174ed6e KS |
125 | *top = (char *)new_start + ((char *)*top - (char *)*start); |
126 | *end = (char *)new_start + ((char *)*end - (char *)*start)*2; | |
625fcfe9 | 127 | *start = new_start; |
f2b59d4d | 128 | |
625fcfe9 KS |
129 | return 1; |
130 | } | |
1eb01be7 | 131 | |
625fcfe9 KS |
132 | /* |
133 | * Extend or move a queue. | |
134 | */ | |
1eb01be7 | 135 | |
625fcfe9 KS |
136 | YAML_DECLARE(int) |
137 | yaml_queue_extend(void **start, void **head, void **tail, void **end) | |
138 | { | |
139 | /* Check if we need to resize the queue. */ | |
1eb01be7 | 140 | |
625fcfe9 | 141 | if (*start == *head && *tail == *end) { |
0174ed6e KS |
142 | void *new_start = yaml_realloc(*start, |
143 | ((char *)*end - (char *)*start)*2); | |
1eb01be7 | 144 | |
625fcfe9 | 145 | if (!new_start) return 0; |
1eb01be7 | 146 | |
0174ed6e KS |
147 | *head = (char *)new_start + ((char *)*head - (char *)*start); |
148 | *tail = (char *)new_start + ((char *)*tail - (char *)*start); | |
149 | *end = (char *)new_start + ((char *)*end - (char *)*start)*2; | |
625fcfe9 KS |
150 | *start = new_start; |
151 | } | |
ab01bac8 | 152 | |
625fcfe9 | 153 | /* Check if we need to move the queue at the beginning of the buffer. */ |
ab01bac8 | 154 | |
625fcfe9 KS |
155 | if (*tail == *end) { |
156 | if (*head != *tail) { | |
0174ed6e | 157 | memmove(*start, *head, (char *)*tail - (char *)*head); |
625fcfe9 | 158 | } |
0174ed6e | 159 | *tail = (char *)*tail - (char *)*head + (char *)*start; |
625fcfe9 KS |
160 | *head = *start; |
161 | } | |
ab01bac8 | 162 | |
625fcfe9 KS |
163 | return 1; |
164 | } | |
1eb01be7 | 165 | |
1eb01be7 | 166 | |
625fcfe9 KS |
167 | /* |
168 | * Create a new parser object. | |
169 | */ | |
1eb01be7 | 170 | |
625fcfe9 KS |
171 | YAML_DECLARE(int) |
172 | yaml_parser_initialize(yaml_parser_t *parser) | |
173 | { | |
174 | assert(parser); /* Non-NULL parser object expected. */ | |
f2b59d4d | 175 | |
625fcfe9 | 176 | memset(parser, 0, sizeof(yaml_parser_t)); |
b1a54000 | 177 | if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE)) |
625fcfe9 | 178 | goto error; |
b1a54000 | 179 | if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE)) |
625fcfe9 | 180 | goto error; |
fc2dd942 | 181 | if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE, yaml_token_t*)) |
625fcfe9 | 182 | goto error; |
fc2dd942 | 183 | if (!STACK_INIT(parser, parser->indents, int*)) |
625fcfe9 | 184 | goto error; |
fc2dd942 | 185 | if (!STACK_INIT(parser, parser->simple_keys, yaml_simple_key_t*)) |
625fcfe9 | 186 | goto error; |
fc2dd942 | 187 | if (!STACK_INIT(parser, parser->states, yaml_parser_state_t*)) |
625fcfe9 | 188 | goto error; |
fc2dd942 | 189 | if (!STACK_INIT(parser, parser->marks, yaml_mark_t*)) |
625fcfe9 | 190 | goto error; |
fc2dd942 | 191 | if (!STACK_INIT(parser, parser->tag_directives, yaml_tag_directive_t*)) |
625fcfe9 | 192 | goto error; |
f2b59d4d | 193 | |
625fcfe9 | 194 | return 1; |
f2b59d4d KS |
195 | |
196 | error: | |
197 | ||
625fcfe9 KS |
198 | BUFFER_DEL(parser, parser->raw_buffer); |
199 | BUFFER_DEL(parser, parser->buffer); | |
200 | QUEUE_DEL(parser, parser->tokens); | |
201 | STACK_DEL(parser, parser->indents); | |
202 | STACK_DEL(parser, parser->simple_keys); | |
203 | STACK_DEL(parser, parser->states); | |
204 | STACK_DEL(parser, parser->marks); | |
205 | STACK_DEL(parser, parser->tag_directives); | |
f2b59d4d | 206 | |
625fcfe9 | 207 | return 0; |
a51447c9 KS |
208 | } |
209 | ||
210 | /* | |
211 | * Destroy a parser object. | |
212 | */ | |
213 | ||
f642fd11 | 214 | YAML_DECLARE(void) |
a51447c9 KS |
215 | yaml_parser_delete(yaml_parser_t *parser) |
216 | { | |
95b98ba9 KS |
217 | assert(parser); /* Non-NULL parser object expected. */ |
218 | ||
625fcfe9 KS |
219 | BUFFER_DEL(parser, parser->raw_buffer); |
220 | BUFFER_DEL(parser, parser->buffer); | |
221 | while (!QUEUE_EMPTY(parser, parser->tokens)) { | |
222 | yaml_token_delete(&DEQUEUE(parser, parser->tokens)); | |
223 | } | |
224 | QUEUE_DEL(parser, parser->tokens); | |
225 | STACK_DEL(parser, parser->indents); | |
226 | STACK_DEL(parser, parser->simple_keys); | |
227 | STACK_DEL(parser, parser->states); | |
228 | STACK_DEL(parser, parser->marks); | |
229 | while (!STACK_EMPTY(parser, parser->tag_directives)) { | |
230 | yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives); | |
231 | yaml_free(tag_directive.handle); | |
232 | yaml_free(tag_directive.prefix); | |
233 | } | |
234 | STACK_DEL(parser, parser->tag_directives); | |
95b98ba9 KS |
235 | |
236 | memset(parser, 0, sizeof(yaml_parser_t)); | |
95b98ba9 KS |
237 | } |
238 | ||
239 | /* | |
6eb1ded4 | 240 | * String read handler. |
95b98ba9 KS |
241 | */ |
242 | ||
243 | static int | |
244 | yaml_string_read_handler(void *data, unsigned char *buffer, size_t size, | |
245 | size_t *size_read) | |
246 | { | |
fc2dd942 | 247 | yaml_parser_t *parser = (yaml_parser_t *)data; |
6eb1ded4 | 248 | |
625fcfe9 | 249 | if (parser->input.string.current == parser->input.string.end) { |
6eb1ded4 KS |
250 | *size_read = 0; |
251 | return 1; | |
252 | } | |
253 | ||
0174ed6e KS |
254 | if (size > (size_t)(parser->input.string.end |
255 | - parser->input.string.current)) { | |
625fcfe9 | 256 | size = parser->input.string.end - parser->input.string.current; |
6eb1ded4 KS |
257 | } |
258 | ||
625fcfe9 KS |
259 | memcpy(buffer, parser->input.string.current, size); |
260 | parser->input.string.current += size; | |
6eb1ded4 | 261 | *size_read = size; |
95b98ba9 KS |
262 | return 1; |
263 | } | |
264 | ||
265 | /* | |
266 | * File read handler. | |
267 | */ | |
268 | ||
269 | static int | |
270 | yaml_file_read_handler(void *data, unsigned char *buffer, size_t size, | |
271 | size_t *size_read) | |
272 | { | |
fc2dd942 | 273 | yaml_parser_t *parser = (yaml_parser_t *)data; |
625fcfe9 KS |
274 | |
275 | *size_read = fread(buffer, 1, size, parser->input.file); | |
276 | return !ferror(parser->input.file); | |
95b98ba9 KS |
277 | } |
278 | ||
279 | /* | |
280 | * Set a string input. | |
281 | */ | |
282 | ||
f642fd11 | 283 | YAML_DECLARE(void) |
95b98ba9 | 284 | yaml_parser_set_input_string(yaml_parser_t *parser, |
a907bf85 | 285 | const unsigned char *input, size_t size) |
95b98ba9 KS |
286 | { |
287 | assert(parser); /* Non-NULL parser object expected. */ | |
6eb1ded4 | 288 | assert(!parser->read_handler); /* You can set the source only once. */ |
95b98ba9 KS |
289 | assert(input); /* Non-NULL input string expected. */ |
290 | ||
6eb1ded4 | 291 | parser->read_handler = yaml_string_read_handler; |
625fcfe9 KS |
292 | parser->read_handler_data = parser; |
293 | ||
294 | parser->input.string.start = input; | |
295 | parser->input.string.current = input; | |
296 | parser->input.string.end = input+size; | |
95b98ba9 KS |
297 | } |
298 | ||
299 | /* | |
300 | * Set a file input. | |
301 | */ | |
302 | ||
f642fd11 | 303 | YAML_DECLARE(void) |
95b98ba9 KS |
304 | yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file) |
305 | { | |
306 | assert(parser); /* Non-NULL parser object expected. */ | |
6eb1ded4 | 307 | assert(!parser->read_handler); /* You can set the source only once. */ |
95b98ba9 KS |
308 | assert(file); /* Non-NULL file object expected. */ |
309 | ||
310 | parser->read_handler = yaml_file_read_handler; | |
625fcfe9 KS |
311 | parser->read_handler_data = parser; |
312 | ||
313 | parser->input.file = file; | |
95b98ba9 KS |
314 | } |
315 | ||
316 | /* | |
317 | * Set a generic input. | |
318 | */ | |
319 | ||
f642fd11 | 320 | YAML_DECLARE(void) |
95b98ba9 KS |
321 | yaml_parser_set_input(yaml_parser_t *parser, |
322 | yaml_read_handler_t *handler, void *data) | |
323 | { | |
324 | assert(parser); /* Non-NULL parser object expected. */ | |
6eb1ded4 | 325 | assert(!parser->read_handler); /* You can set the source only once. */ |
95b98ba9 KS |
326 | assert(handler); /* Non-NULL read handler expected. */ |
327 | ||
328 | parser->read_handler = handler; | |
6eb1ded4 | 329 | parser->read_handler_data = data; |
95b98ba9 KS |
330 | } |
331 | ||
332 | /* | |
333 | * Set the source encoding. | |
334 | */ | |
335 | ||
f642fd11 | 336 | YAML_DECLARE(void) |
95b98ba9 KS |
337 | yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding) |
338 | { | |
339 | assert(parser); /* Non-NULL parser object expected. */ | |
340 | assert(!parser->encoding); /* Encoding is already set or detected. */ | |
341 | ||
342 | parser->encoding = encoding; | |
a51447c9 KS |
343 | } |
344 | ||
b1a54000 KS |
345 | /* |
346 | * Create a new emitter object. | |
347 | */ | |
348 | ||
349 | YAML_DECLARE(int) | |
350 | yaml_emitter_initialize(yaml_emitter_t *emitter) | |
351 | { | |
352 | assert(emitter); /* Non-NULL emitter object expected. */ | |
353 | ||
354 | memset(emitter, 0, sizeof(yaml_emitter_t)); | |
355 | if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE)) | |
356 | goto error; | |
357 | if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE)) | |
358 | goto error; | |
fc2dd942 | 359 | if (!STACK_INIT(emitter, emitter->states, yaml_emitter_state_t*)) |
b1a54000 | 360 | goto error; |
fc2dd942 | 361 | if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE, yaml_event_t*)) |
b1a54000 | 362 | goto error; |
fc2dd942 | 363 | if (!STACK_INIT(emitter, emitter->indents, int*)) |
b1a54000 | 364 | goto error; |
fc2dd942 | 365 | if (!STACK_INIT(emitter, emitter->tag_directives, yaml_tag_directive_t*)) |
b1a54000 KS |
366 | goto error; |
367 | ||
368 | return 1; | |
369 | ||
370 | error: | |
371 | ||
372 | BUFFER_DEL(emitter, emitter->buffer); | |
373 | BUFFER_DEL(emitter, emitter->raw_buffer); | |
374 | STACK_DEL(emitter, emitter->states); | |
375 | QUEUE_DEL(emitter, emitter->events); | |
376 | STACK_DEL(emitter, emitter->indents); | |
377 | STACK_DEL(emitter, emitter->tag_directives); | |
378 | ||
379 | return 0; | |
380 | } | |
381 | ||
382 | /* | |
383 | * Destroy an emitter object. | |
384 | */ | |
385 | ||
386 | YAML_DECLARE(void) | |
387 | yaml_emitter_delete(yaml_emitter_t *emitter) | |
388 | { | |
389 | assert(emitter); /* Non-NULL emitter object expected. */ | |
390 | ||
391 | BUFFER_DEL(emitter, emitter->buffer); | |
392 | BUFFER_DEL(emitter, emitter->raw_buffer); | |
393 | STACK_DEL(emitter, emitter->states); | |
394 | while (!QUEUE_EMPTY(emitter, emitter->events)) { | |
395 | yaml_event_delete(&DEQUEUE(emitter, emitter->events)); | |
396 | } | |
6d167281 | 397 | QUEUE_DEL(emitter, emitter->events); |
b1a54000 KS |
398 | STACK_DEL(emitter, emitter->indents); |
399 | while (!STACK_EMPTY(empty, emitter->tag_directives)) { | |
400 | yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives); | |
401 | yaml_free(tag_directive.handle); | |
402 | yaml_free(tag_directive.prefix); | |
403 | } | |
404 | STACK_DEL(emitter, emitter->tag_directives); | |
e27a3c88 | 405 | yaml_free(emitter->anchors); |
b1a54000 KS |
406 | |
407 | memset(emitter, 0, sizeof(yaml_emitter_t)); | |
408 | } | |
409 | ||
410 | /* | |
411 | * String write handler. | |
412 | */ | |
413 | ||
414 | static int | |
415 | yaml_string_write_handler(void *data, unsigned char *buffer, size_t size) | |
416 | { | |
fc2dd942 | 417 | yaml_emitter_t *emitter = (yaml_emitter_t *)data; |
b1a54000 | 418 | |
686b2d85 | 419 | if (emitter->output.string.size - *emitter->output.string.size_written |
b1a54000 KS |
420 | < size) { |
421 | memcpy(emitter->output.string.buffer | |
422 | + *emitter->output.string.size_written, | |
423 | buffer, | |
424 | emitter->output.string.size | |
425 | - *emitter->output.string.size_written); | |
426 | *emitter->output.string.size_written = emitter->output.string.size; | |
427 | return 0; | |
428 | } | |
429 | ||
430 | memcpy(emitter->output.string.buffer | |
431 | + *emitter->output.string.size_written, buffer, size); | |
432 | *emitter->output.string.size_written += size; | |
433 | return 1; | |
434 | } | |
435 | ||
436 | /* | |
437 | * File write handler. | |
438 | */ | |
439 | ||
440 | static int | |
441 | yaml_file_write_handler(void *data, unsigned char *buffer, size_t size) | |
442 | { | |
fc2dd942 | 443 | yaml_emitter_t *emitter = (yaml_emitter_t *)data; |
b1a54000 KS |
444 | |
445 | return (fwrite(buffer, 1, size, emitter->output.file) == size); | |
446 | } | |
447 | /* | |
448 | * Set a string output. | |
449 | */ | |
450 | ||
451 | YAML_DECLARE(void) | |
452 | yaml_emitter_set_output_string(yaml_emitter_t *emitter, | |
453 | unsigned char *output, size_t size, size_t *size_written) | |
454 | { | |
455 | assert(emitter); /* Non-NULL emitter object expected. */ | |
456 | assert(!emitter->write_handler); /* You can set the output only once. */ | |
457 | assert(output); /* Non-NULL output string expected. */ | |
458 | ||
459 | emitter->write_handler = yaml_string_write_handler; | |
460 | emitter->write_handler_data = emitter; | |
461 | ||
462 | emitter->output.string.buffer = output; | |
463 | emitter->output.string.size = size; | |
464 | emitter->output.string.size_written = size_written; | |
465 | *size_written = 0; | |
466 | } | |
467 | ||
468 | /* | |
469 | * Set a file output. | |
470 | */ | |
471 | ||
472 | YAML_DECLARE(void) | |
473 | yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file) | |
474 | { | |
475 | assert(emitter); /* Non-NULL emitter object expected. */ | |
476 | assert(!emitter->write_handler); /* You can set the output only once. */ | |
477 | assert(file); /* Non-NULL file object expected. */ | |
478 | ||
479 | emitter->write_handler = yaml_file_write_handler; | |
480 | emitter->write_handler_data = emitter; | |
481 | ||
482 | emitter->output.file = file; | |
483 | } | |
484 | ||
485 | /* | |
486 | * Set a generic output handler. | |
487 | */ | |
488 | ||
489 | YAML_DECLARE(void) | |
490 | yaml_emitter_set_output(yaml_emitter_t *emitter, | |
491 | yaml_write_handler_t *handler, void *data) | |
492 | { | |
493 | assert(emitter); /* Non-NULL emitter object expected. */ | |
494 | assert(!emitter->write_handler); /* You can set the output only once. */ | |
495 | assert(handler); /* Non-NULL handler object expected. */ | |
496 | ||
497 | emitter->write_handler = handler; | |
498 | emitter->write_handler_data = data; | |
499 | } | |
500 | ||
501 | /* | |
502 | * Set the output encoding. | |
503 | */ | |
504 | ||
505 | YAML_DECLARE(void) | |
506 | yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding) | |
507 | { | |
508 | assert(emitter); /* Non-NULL emitter object expected. */ | |
509 | assert(!emitter->encoding); /* You can set encoding only once. */ | |
510 | ||
511 | emitter->encoding = encoding; | |
512 | } | |
513 | ||
514 | /* | |
515 | * Set the canonical output style. | |
516 | */ | |
517 | ||
518 | YAML_DECLARE(void) | |
519 | yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical) | |
520 | { | |
521 | assert(emitter); /* Non-NULL emitter object expected. */ | |
522 | ||
523 | emitter->canonical = (canonical != 0); | |
524 | } | |
525 | ||
526 | /* | |
527 | * Set the indentation increment. | |
528 | */ | |
529 | ||
530 | YAML_DECLARE(void) | |
531 | yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent) | |
532 | { | |
533 | assert(emitter); /* Non-NULL emitter object expected. */ | |
534 | ||
535 | emitter->best_indent = (1 < indent && indent < 10) ? indent : 2; | |
536 | } | |
537 | ||
538 | /* | |
539 | * Set the preferred line width. | |
540 | */ | |
541 | ||
542 | YAML_DECLARE(void) | |
543 | yaml_emitter_set_width(yaml_emitter_t *emitter, int width) | |
544 | { | |
545 | assert(emitter); /* Non-NULL emitter object expected. */ | |
546 | ||
cf616166 | 547 | emitter->best_width = (width >= 0) ? width : -1; |
b1a54000 KS |
548 | } |
549 | ||
550 | /* | |
551 | * Set if unescaped non-ASCII characters are allowed. | |
552 | */ | |
553 | ||
554 | YAML_DECLARE(void) | |
555 | yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode) | |
556 | { | |
557 | assert(emitter); /* Non-NULL emitter object expected. */ | |
558 | ||
559 | emitter->unicode = (unicode != 0); | |
560 | } | |
561 | ||
562 | /* | |
563 | * Set the preferred line break character. | |
564 | */ | |
565 | ||
566 | YAML_DECLARE(void) | |
567 | yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break) | |
568 | { | |
569 | assert(emitter); /* Non-NULL emitter object expected. */ | |
570 | ||
571 | emitter->line_break = line_break; | |
572 | } | |
573 | ||
f642fd11 KS |
574 | /* |
575 | * Destroy a token object. | |
576 | */ | |
577 | ||
578 | YAML_DECLARE(void) | |
579 | yaml_token_delete(yaml_token_t *token) | |
580 | { | |
581 | assert(token); /* Non-NULL token object expected. */ | |
582 | ||
583 | switch (token->type) | |
584 | { | |
585 | case YAML_TAG_DIRECTIVE_TOKEN: | |
586 | yaml_free(token->data.tag_directive.handle); | |
587 | yaml_free(token->data.tag_directive.prefix); | |
588 | break; | |
589 | ||
590 | case YAML_ALIAS_TOKEN: | |
26687d7d KS |
591 | yaml_free(token->data.alias.value); |
592 | break; | |
593 | ||
f642fd11 | 594 | case YAML_ANCHOR_TOKEN: |
26687d7d | 595 | yaml_free(token->data.anchor.value); |
f642fd11 KS |
596 | break; |
597 | ||
598 | case YAML_TAG_TOKEN: | |
599 | yaml_free(token->data.tag.handle); | |
600 | yaml_free(token->data.tag.suffix); | |
601 | break; | |
602 | ||
603 | case YAML_SCALAR_TOKEN: | |
604 | yaml_free(token->data.scalar.value); | |
605 | break; | |
54815ffd KS |
606 | |
607 | default: | |
608 | break; | |
f642fd11 KS |
609 | } |
610 | ||
611 | memset(token, 0, sizeof(yaml_token_t)); | |
26687d7d KS |
612 | } |
613 | ||
5a00d8fe KS |
614 | /* |
615 | * Check if a string is a valid UTF-8 sequence. | |
616 | * | |
617 | * Check 'reader.c' for more details on UTF-8 encoding. | |
618 | */ | |
619 | ||
620 | static int | |
621 | yaml_check_utf8(yaml_char_t *start, size_t length) | |
622 | { | |
623 | yaml_char_t *end = start+length; | |
624 | yaml_char_t *pointer = start; | |
625 | ||
626 | while (pointer < end) { | |
627 | unsigned char octet; | |
628 | unsigned int width; | |
629 | unsigned int value; | |
0174ed6e | 630 | size_t k; |
5a00d8fe KS |
631 | |
632 | octet = pointer[0]; | |
633 | width = (octet & 0x80) == 0x00 ? 1 : | |
634 | (octet & 0xE0) == 0xC0 ? 2 : | |
635 | (octet & 0xF0) == 0xE0 ? 3 : | |
636 | (octet & 0xF8) == 0xF0 ? 4 : 0; | |
784340c2 | 637 | value = (octet & 0x80) == 0x00 ? octet & 0x7F : |
5a00d8fe KS |
638 | (octet & 0xE0) == 0xC0 ? octet & 0x1F : |
639 | (octet & 0xF0) == 0xE0 ? octet & 0x0F : | |
640 | (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0; | |
641 | if (!width) return 0; | |
642 | if (pointer+width > end) return 0; | |
643 | for (k = 1; k < width; k ++) { | |
644 | octet = pointer[k]; | |
645 | if ((octet & 0xC0) != 0x80) return 0; | |
646 | value = (value << 6) + (octet & 0x3F); | |
647 | } | |
648 | if (!((width == 1) || | |
649 | (width == 2 && value >= 0x80) || | |
650 | (width == 3 && value >= 0x800) || | |
651 | (width == 4 && value >= 0x10000))) return 0; | |
652 | ||
653 | pointer += width; | |
654 | } | |
655 | ||
656 | return 1; | |
657 | } | |
658 | ||
659 | /* | |
660 | * Create STREAM-START. | |
661 | */ | |
662 | ||
663 | YAML_DECLARE(int) | |
664 | yaml_stream_start_event_initialize(yaml_event_t *event, | |
665 | yaml_encoding_t encoding) | |
666 | { | |
667 | yaml_mark_t mark = { 0, 0, 0 }; | |
668 | ||
669 | assert(event); /* Non-NULL event object is expected. */ | |
670 | ||
671 | STREAM_START_EVENT_INIT(*event, encoding, mark, mark); | |
672 | ||
673 | return 1; | |
674 | } | |
675 | ||
676 | /* | |
677 | * Create STREAM-END. | |
678 | */ | |
679 | ||
680 | YAML_DECLARE(int) | |
681 | yaml_stream_end_event_initialize(yaml_event_t *event) | |
682 | { | |
683 | yaml_mark_t mark = { 0, 0, 0 }; | |
684 | ||
685 | assert(event); /* Non-NULL event object is expected. */ | |
686 | ||
687 | STREAM_END_EVENT_INIT(*event, mark, mark); | |
688 | ||
689 | return 1; | |
690 | } | |
691 | ||
692 | /* | |
693 | * Create DOCUMENT-START. | |
694 | */ | |
695 | ||
696 | YAML_DECLARE(int) | |
697 | yaml_document_start_event_initialize(yaml_event_t *event, | |
698 | yaml_version_directive_t *version_directive, | |
699 | yaml_tag_directive_t *tag_directives_start, | |
700 | yaml_tag_directive_t *tag_directives_end, | |
701 | int implicit) | |
702 | { | |
703 | struct { | |
704 | yaml_error_type_t error; | |
705 | } context; | |
706 | yaml_mark_t mark = { 0, 0, 0 }; | |
707 | yaml_version_directive_t *version_directive_copy = NULL; | |
708 | struct { | |
709 | yaml_tag_directive_t *start; | |
710 | yaml_tag_directive_t *end; | |
711 | yaml_tag_directive_t *top; | |
712 | } tag_directives_copy = { NULL, NULL, NULL }; | |
713 | yaml_tag_directive_t value = { NULL, NULL }; | |
714 | ||
715 | assert(event); /* Non-NULL event object is expected. */ | |
716 | assert((tag_directives_start && tag_directives_end) || | |
717 | (tag_directives_start == tag_directives_end)); | |
718 | /* Valid tag directives are expected. */ | |
719 | ||
720 | if (version_directive) { | |
fc2dd942 | 721 | version_directive_copy = YAML_MALLOC_STATIC(yaml_version_directive_t); |
5a00d8fe KS |
722 | if (!version_directive_copy) goto error; |
723 | version_directive_copy->major = version_directive->major; | |
724 | version_directive_copy->minor = version_directive->minor; | |
725 | } | |
726 | ||
727 | if (tag_directives_start != tag_directives_end) { | |
728 | yaml_tag_directive_t *tag_directive; | |
fc2dd942 | 729 | if (!STACK_INIT(&context, tag_directives_copy, yaml_tag_directive_t*)) |
5a00d8fe KS |
730 | goto error; |
731 | for (tag_directive = tag_directives_start; | |
732 | tag_directive != tag_directives_end; tag_directive ++) { | |
733 | assert(tag_directive->handle); | |
734 | assert(tag_directive->prefix); | |
735 | if (!yaml_check_utf8(tag_directive->handle, | |
736 | strlen((char *)tag_directive->handle))) | |
737 | goto error; | |
738 | if (!yaml_check_utf8(tag_directive->prefix, | |
739 | strlen((char *)tag_directive->prefix))) | |
740 | goto error; | |
741 | value.handle = yaml_strdup(tag_directive->handle); | |
742 | value.prefix = yaml_strdup(tag_directive->prefix); | |
743 | if (!value.handle || !value.prefix) goto error; | |
744 | if (!PUSH(&context, tag_directives_copy, value)) | |
745 | goto error; | |
746 | value.handle = NULL; | |
747 | value.prefix = NULL; | |
748 | } | |
749 | } | |
750 | ||
751 | DOCUMENT_START_EVENT_INIT(*event, version_directive_copy, | |
6d167281 | 752 | tag_directives_copy.start, tag_directives_copy.top, |
5a00d8fe KS |
753 | implicit, mark, mark); |
754 | ||
755 | return 1; | |
756 | ||
757 | error: | |
758 | yaml_free(version_directive_copy); | |
759 | while (!STACK_EMPTY(context, tag_directives_copy)) { | |
760 | yaml_tag_directive_t value = POP(context, tag_directives_copy); | |
761 | yaml_free(value.handle); | |
762 | yaml_free(value.prefix); | |
763 | } | |
764 | STACK_DEL(context, tag_directives_copy); | |
765 | yaml_free(value.handle); | |
766 | yaml_free(value.prefix); | |
767 | ||
768 | return 0; | |
769 | } | |
770 | ||
771 | /* | |
772 | * Create DOCUMENT-END. | |
773 | */ | |
774 | ||
775 | YAML_DECLARE(int) | |
776 | yaml_document_end_event_initialize(yaml_event_t *event, int implicit) | |
777 | { | |
778 | yaml_mark_t mark = { 0, 0, 0 }; | |
779 | ||
780 | assert(event); /* Non-NULL emitter object is expected. */ | |
781 | ||
782 | DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark); | |
783 | ||
784 | return 1; | |
785 | } | |
786 | ||
787 | /* | |
788 | * Create ALIAS. | |
789 | */ | |
790 | ||
791 | YAML_DECLARE(int) | |
792 | yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor) | |
793 | { | |
794 | yaml_mark_t mark = { 0, 0, 0 }; | |
795 | yaml_char_t *anchor_copy = NULL; | |
796 | ||
797 | assert(event); /* Non-NULL event object is expected. */ | |
798 | assert(anchor); /* Non-NULL anchor is expected. */ | |
799 | ||
800 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0; | |
801 | ||
802 | anchor_copy = yaml_strdup(anchor); | |
803 | if (!anchor_copy) | |
804 | return 0; | |
805 | ||
806 | ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark); | |
807 | ||
808 | return 1; | |
809 | } | |
810 | ||
811 | /* | |
812 | * Create SCALAR. | |
813 | */ | |
814 | ||
815 | YAML_DECLARE(int) | |
816 | yaml_scalar_event_initialize(yaml_event_t *event, | |
817 | yaml_char_t *anchor, yaml_char_t *tag, | |
2a02dfd2 | 818 | yaml_char_t *value, int length, |
5a00d8fe KS |
819 | int plain_implicit, int quoted_implicit, |
820 | yaml_scalar_style_t style) | |
821 | { | |
822 | yaml_mark_t mark = { 0, 0, 0 }; | |
823 | yaml_char_t *anchor_copy = NULL; | |
824 | yaml_char_t *tag_copy = NULL; | |
825 | yaml_char_t *value_copy = NULL; | |
826 | ||
827 | assert(event); /* Non-NULL event object is expected. */ | |
828 | assert(value); /* Non-NULL anchor is expected. */ | |
829 | ||
5a00d8fe KS |
830 | if (anchor) { |
831 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; | |
832 | anchor_copy = yaml_strdup(anchor); | |
833 | if (!anchor_copy) goto error; | |
834 | } | |
835 | ||
836 | if (tag) { | |
837 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
838 | tag_copy = yaml_strdup(tag); | |
839 | if (!tag_copy) goto error; | |
840 | } | |
841 | ||
2a02dfd2 KS |
842 | if (length < 0) { |
843 | length = strlen((char *)value); | |
844 | } | |
845 | ||
5a00d8fe | 846 | if (!yaml_check_utf8(value, length)) goto error; |
fc2dd942 | 847 | value_copy = YAML_MALLOC(length+1); |
5a00d8fe KS |
848 | if (!value_copy) goto error; |
849 | memcpy(value_copy, value, length); | |
850 | value_copy[length] = '\0'; | |
851 | ||
852 | SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length, | |
853 | plain_implicit, quoted_implicit, style, mark, mark); | |
854 | ||
855 | return 1; | |
856 | ||
857 | error: | |
858 | yaml_free(anchor_copy); | |
859 | yaml_free(tag_copy); | |
860 | yaml_free(value_copy); | |
861 | ||
862 | return 0; | |
863 | } | |
864 | ||
865 | /* | |
866 | * Create SEQUENCE-START. | |
867 | */ | |
868 | ||
869 | YAML_DECLARE(int) | |
870 | yaml_sequence_start_event_initialize(yaml_event_t *event, | |
871 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, | |
872 | yaml_sequence_style_t style) | |
873 | { | |
874 | yaml_mark_t mark = { 0, 0, 0 }; | |
875 | yaml_char_t *anchor_copy = NULL; | |
876 | yaml_char_t *tag_copy = NULL; | |
877 | ||
878 | assert(event); /* Non-NULL event object is expected. */ | |
879 | ||
880 | if (anchor) { | |
881 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; | |
882 | anchor_copy = yaml_strdup(anchor); | |
883 | if (!anchor_copy) goto error; | |
884 | } | |
885 | ||
886 | if (tag) { | |
887 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
888 | tag_copy = yaml_strdup(tag); | |
889 | if (!tag_copy) goto error; | |
890 | } | |
891 | ||
892 | SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy, | |
893 | implicit, style, mark, mark); | |
894 | ||
895 | return 1; | |
896 | ||
897 | error: | |
898 | yaml_free(anchor_copy); | |
899 | yaml_free(tag_copy); | |
900 | ||
901 | return 0; | |
902 | } | |
903 | ||
904 | /* | |
905 | * Create SEQUENCE-END. | |
906 | */ | |
907 | ||
908 | YAML_DECLARE(int) | |
909 | yaml_sequence_end_event_initialize(yaml_event_t *event) | |
910 | { | |
911 | yaml_mark_t mark = { 0, 0, 0 }; | |
912 | ||
913 | assert(event); /* Non-NULL event object is expected. */ | |
914 | ||
915 | SEQUENCE_END_EVENT_INIT(*event, mark, mark); | |
916 | ||
917 | return 1; | |
918 | } | |
919 | ||
920 | /* | |
921 | * Create MAPPING-START. | |
922 | */ | |
923 | ||
924 | YAML_DECLARE(int) | |
925 | yaml_mapping_start_event_initialize(yaml_event_t *event, | |
926 | yaml_char_t *anchor, yaml_char_t *tag, int implicit, | |
927 | yaml_mapping_style_t style) | |
928 | { | |
929 | yaml_mark_t mark = { 0, 0, 0 }; | |
930 | yaml_char_t *anchor_copy = NULL; | |
931 | yaml_char_t *tag_copy = NULL; | |
932 | ||
933 | assert(event); /* Non-NULL event object is expected. */ | |
934 | ||
935 | if (anchor) { | |
936 | if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error; | |
937 | anchor_copy = yaml_strdup(anchor); | |
938 | if (!anchor_copy) goto error; | |
939 | } | |
940 | ||
941 | if (tag) { | |
942 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
943 | tag_copy = yaml_strdup(tag); | |
944 | if (!tag_copy) goto error; | |
945 | } | |
946 | ||
947 | MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy, | |
948 | implicit, style, mark, mark); | |
949 | ||
950 | return 1; | |
951 | ||
952 | error: | |
953 | yaml_free(anchor_copy); | |
954 | yaml_free(tag_copy); | |
955 | ||
956 | return 0; | |
957 | } | |
958 | ||
959 | /* | |
960 | * Create MAPPING-END. | |
961 | */ | |
962 | ||
963 | YAML_DECLARE(int) | |
964 | yaml_mapping_end_event_initialize(yaml_event_t *event) | |
965 | { | |
966 | yaml_mark_t mark = { 0, 0, 0 }; | |
967 | ||
968 | assert(event); /* Non-NULL event object is expected. */ | |
969 | ||
970 | MAPPING_END_EVENT_INIT(*event, mark, mark); | |
971 | ||
972 | return 1; | |
973 | } | |
974 | ||
26687d7d KS |
975 | /* |
976 | * Destroy an event object. | |
977 | */ | |
978 | ||
979 | YAML_DECLARE(void) | |
980 | yaml_event_delete(yaml_event_t *event) | |
981 | { | |
625fcfe9 KS |
982 | yaml_tag_directive_t *tag_directive; |
983 | ||
26687d7d KS |
984 | assert(event); /* Non-NULL event object expected. */ |
985 | ||
986 | switch (event->type) | |
987 | { | |
988 | case YAML_DOCUMENT_START_EVENT: | |
625fcfe9 KS |
989 | yaml_free(event->data.document_start.version_directive); |
990 | for (tag_directive = event->data.document_start.tag_directives.start; | |
991 | tag_directive != event->data.document_start.tag_directives.end; | |
992 | tag_directive++) { | |
993 | yaml_free(tag_directive->handle); | |
994 | yaml_free(tag_directive->prefix); | |
995 | } | |
996 | yaml_free(event->data.document_start.tag_directives.start); | |
26687d7d KS |
997 | break; |
998 | ||
999 | case YAML_ALIAS_EVENT: | |
1000 | yaml_free(event->data.alias.anchor); | |
1001 | break; | |
1002 | ||
1003 | case YAML_SCALAR_EVENT: | |
1004 | yaml_free(event->data.scalar.anchor); | |
1005 | yaml_free(event->data.scalar.tag); | |
1006 | yaml_free(event->data.scalar.value); | |
1007 | break; | |
1008 | ||
1009 | case YAML_SEQUENCE_START_EVENT: | |
1010 | yaml_free(event->data.sequence_start.anchor); | |
1011 | yaml_free(event->data.sequence_start.tag); | |
1012 | break; | |
1013 | ||
1014 | case YAML_MAPPING_START_EVENT: | |
1015 | yaml_free(event->data.mapping_start.anchor); | |
1016 | yaml_free(event->data.mapping_start.tag); | |
1017 | break; | |
54815ffd KS |
1018 | |
1019 | default: | |
1020 | break; | |
26687d7d KS |
1021 | } |
1022 | ||
1023 | memset(event, 0, sizeof(yaml_event_t)); | |
26687d7d KS |
1024 | } |
1025 | ||
e27a3c88 KS |
1026 | /* |
1027 | * Create a document object. | |
1028 | */ | |
1029 | ||
1030 | YAML_DECLARE(int) | |
1031 | yaml_document_initialize(yaml_document_t *document, | |
1032 | yaml_version_directive_t *version_directive, | |
1033 | yaml_tag_directive_t *tag_directives_start, | |
1034 | yaml_tag_directive_t *tag_directives_end, | |
1035 | int start_implicit, int end_implicit) | |
1036 | { | |
1037 | struct { | |
1038 | yaml_error_type_t error; | |
1039 | } context; | |
1040 | struct { | |
1041 | yaml_node_t *start; | |
1042 | yaml_node_t *end; | |
1043 | yaml_node_t *top; | |
1044 | } nodes = { NULL, NULL, NULL }; | |
1045 | yaml_version_directive_t *version_directive_copy = NULL; | |
1046 | struct { | |
1047 | yaml_tag_directive_t *start; | |
1048 | yaml_tag_directive_t *end; | |
1049 | yaml_tag_directive_t *top; | |
1050 | } tag_directives_copy = { NULL, NULL, NULL }; | |
1051 | yaml_tag_directive_t value = { NULL, NULL }; | |
1052 | yaml_mark_t mark = { 0, 0, 0 }; | |
1053 | ||
1054 | assert(document); /* Non-NULL document object is expected. */ | |
1055 | assert((tag_directives_start && tag_directives_end) || | |
1056 | (tag_directives_start == tag_directives_end)); | |
1057 | /* Valid tag directives are expected. */ | |
1058 | ||
fc2dd942 | 1059 | if (!STACK_INIT(&context, nodes, yaml_node_t*)) goto error; |
e27a3c88 KS |
1060 | |
1061 | if (version_directive) { | |
fc2dd942 | 1062 | version_directive_copy = YAML_MALLOC_STATIC(yaml_version_directive_t); |
e27a3c88 KS |
1063 | if (!version_directive_copy) goto error; |
1064 | version_directive_copy->major = version_directive->major; | |
1065 | version_directive_copy->minor = version_directive->minor; | |
1066 | } | |
1067 | ||
1068 | if (tag_directives_start != tag_directives_end) { | |
1069 | yaml_tag_directive_t *tag_directive; | |
fc2dd942 | 1070 | if (!STACK_INIT(&context, tag_directives_copy, yaml_tag_directive_t*)) |
e27a3c88 KS |
1071 | goto error; |
1072 | for (tag_directive = tag_directives_start; | |
1073 | tag_directive != tag_directives_end; tag_directive ++) { | |
1074 | assert(tag_directive->handle); | |
1075 | assert(tag_directive->prefix); | |
1076 | if (!yaml_check_utf8(tag_directive->handle, | |
1077 | strlen((char *)tag_directive->handle))) | |
1078 | goto error; | |
1079 | if (!yaml_check_utf8(tag_directive->prefix, | |
1080 | strlen((char *)tag_directive->prefix))) | |
1081 | goto error; | |
1082 | value.handle = yaml_strdup(tag_directive->handle); | |
1083 | value.prefix = yaml_strdup(tag_directive->prefix); | |
1084 | if (!value.handle || !value.prefix) goto error; | |
1085 | if (!PUSH(&context, tag_directives_copy, value)) | |
1086 | goto error; | |
1087 | value.handle = NULL; | |
1088 | value.prefix = NULL; | |
1089 | } | |
1090 | } | |
1091 | ||
1092 | DOCUMENT_INIT(*document, nodes.start, nodes.end, version_directive_copy, | |
1093 | tag_directives_copy.start, tag_directives_copy.top, | |
1094 | start_implicit, end_implicit, mark, mark); | |
1095 | ||
1096 | return 1; | |
1097 | ||
1098 | error: | |
1099 | STACK_DEL(&context, nodes); | |
1100 | yaml_free(version_directive_copy); | |
1101 | while (!STACK_EMPTY(&context, tag_directives_copy)) { | |
1102 | yaml_tag_directive_t value = POP(&context, tag_directives_copy); | |
1103 | yaml_free(value.handle); | |
1104 | yaml_free(value.prefix); | |
1105 | } | |
1106 | STACK_DEL(&context, tag_directives_copy); | |
1107 | yaml_free(value.handle); | |
1108 | yaml_free(value.prefix); | |
1109 | ||
1110 | return 0; | |
1111 | } | |
1112 | ||
1113 | /* | |
1114 | * Destroy a document object. | |
1115 | */ | |
1116 | ||
1117 | YAML_DECLARE(void) | |
1118 | yaml_document_delete(yaml_document_t *document) | |
1119 | { | |
e27a3c88 KS |
1120 | yaml_tag_directive_t *tag_directive; |
1121 | ||
1122 | assert(document); /* Non-NULL document object is expected. */ | |
1123 | ||
1124 | while (!STACK_EMPTY(&context, document->nodes)) { | |
1125 | yaml_node_t node = POP(&context, document->nodes); | |
1126 | yaml_free(node.tag); | |
1127 | switch (node.type) { | |
1128 | case YAML_SCALAR_NODE: | |
1129 | yaml_free(node.data.scalar.value); | |
1130 | break; | |
1131 | case YAML_SEQUENCE_NODE: | |
1132 | STACK_DEL(&context, node.data.sequence.items); | |
1133 | break; | |
1134 | case YAML_MAPPING_NODE: | |
1135 | STACK_DEL(&context, node.data.mapping.pairs); | |
1136 | break; | |
1137 | default: | |
1138 | assert(0); /* Should not happen. */ | |
1139 | } | |
1140 | } | |
1141 | STACK_DEL(&context, document->nodes); | |
1142 | ||
1143 | yaml_free(document->version_directive); | |
1144 | for (tag_directive = document->tag_directives.start; | |
1145 | tag_directive != document->tag_directives.end; | |
1146 | tag_directive++) { | |
1147 | yaml_free(tag_directive->handle); | |
1148 | yaml_free(tag_directive->prefix); | |
1149 | } | |
1150 | yaml_free(document->tag_directives.start); | |
1151 | ||
1152 | memset(document, 0, sizeof(yaml_document_t)); | |
1153 | } | |
1154 | ||
1155 | /** | |
1156 | * Get a document node. | |
1157 | */ | |
1158 | ||
1159 | YAML_DECLARE(yaml_node_t *) | |
c9b74def | 1160 | yaml_document_get_node(yaml_document_t *document, int index) |
e27a3c88 KS |
1161 | { |
1162 | assert(document); /* Non-NULL document object is expected. */ | |
1163 | ||
c9b74def KS |
1164 | if (index > 0 && document->nodes.start + index <= document->nodes.top) { |
1165 | return document->nodes.start + index - 1; | |
e27a3c88 KS |
1166 | } |
1167 | return NULL; | |
1168 | } | |
1169 | ||
1170 | /** | |
1171 | * Get the root object. | |
1172 | */ | |
1173 | ||
1174 | YAML_DECLARE(yaml_node_t *) | |
1175 | yaml_document_get_root_node(yaml_document_t *document) | |
1176 | { | |
1177 | assert(document); /* Non-NULL document object is expected. */ | |
1178 | ||
1179 | if (document->nodes.top != document->nodes.start) { | |
1180 | return document->nodes.start; | |
1181 | } | |
1182 | return NULL; | |
1183 | } | |
a907bf85 KS |
1184 | |
1185 | /* | |
e27a3c88 | 1186 | * Add a scalar node to a document. |
a907bf85 KS |
1187 | */ |
1188 | ||
1189 | YAML_DECLARE(int) | |
e27a3c88 | 1190 | yaml_document_add_scalar(yaml_document_t *document, |
a907bf85 KS |
1191 | yaml_char_t *tag, yaml_char_t *value, int length, |
1192 | yaml_scalar_style_t style) | |
1193 | { | |
e27a3c88 KS |
1194 | struct { |
1195 | yaml_error_type_t error; | |
1196 | } context; | |
a907bf85 KS |
1197 | yaml_mark_t mark = { 0, 0, 0 }; |
1198 | yaml_char_t *tag_copy = NULL; | |
1199 | yaml_char_t *value_copy = NULL; | |
e27a3c88 | 1200 | yaml_node_t node; |
a907bf85 | 1201 | |
e27a3c88 KS |
1202 | assert(document); /* Non-NULL document object is expected. */ |
1203 | assert(value); /* Non-NULL value is expected. */ | |
a907bf85 KS |
1204 | |
1205 | if (!tag) { | |
c9b74def | 1206 | tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG; |
a907bf85 KS |
1207 | } |
1208 | ||
1209 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; | |
1210 | tag_copy = yaml_strdup(tag); | |
1211 | if (!tag_copy) goto error; | |
1212 | ||
1213 | if (length < 0) { | |
1214 | length = strlen((char *)value); | |
1215 | } | |
1216 | ||
1217 | if (!yaml_check_utf8(value, length)) goto error; | |
fc2dd942 | 1218 | value_copy = YAML_MALLOC(length+1); |
a907bf85 KS |
1219 | if (!value_copy) goto error; |
1220 | memcpy(value_copy, value, length); | |
1221 | value_copy[length] = '\0'; | |
1222 | ||
e27a3c88 KS |
1223 | SCALAR_NODE_INIT(node, tag_copy, value_copy, length, style, mark, mark); |
1224 | if (!PUSH(&context, document->nodes, node)) goto error; | |
a907bf85 | 1225 | |
e27a3c88 | 1226 | return document->nodes.top - document->nodes.start; |
a907bf85 KS |
1227 | |
1228 | error: | |
1229 | yaml_free(tag_copy); | |
1230 | yaml_free(value_copy); | |
1231 | ||
1232 | return 0; | |
1233 | } | |
1234 | ||
1235 | /* | |
e27a3c88 | 1236 | * Add a sequence node to a document. |
a907bf85 KS |
1237 | */ |
1238 | ||
1239 | YAML_DECLARE(int) | |
e27a3c88 | 1240 | yaml_document_add_sequence(yaml_document_t *document, |
a907bf85 KS |
1241 | yaml_char_t *tag, yaml_sequence_style_t style) |
1242 | { | |
1243 | struct { | |
1244 | yaml_error_type_t error; | |
1245 | } context; | |
1246 | yaml_mark_t mark = { 0, 0, 0 }; | |
1247 | yaml_char_t *tag_copy = NULL; | |
1248 | struct { | |
1249 | yaml_node_item_t *start; | |
1250 | yaml_node_item_t *end; | |
1251 | yaml_node_item_t *top; | |
1252 | } items = { NULL, NULL, NULL }; | |
e27a3c88 | 1253 | yaml_node_t node; |
a907bf85 | 1254 | |
e27a3c88 | 1255 | assert(document); /* Non-NULL document object is expected. */ |
a907bf85 KS |
1256 | |
1257 | if (!tag) { | |
c9b74def | 1258 | tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG; |
a907bf85 KS |
1259 | } |
1260 | ||
e27a3c88 KS |
1261 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
1262 | tag_copy = yaml_strdup(tag); | |
1263 | if (!tag_copy) goto error; | |
a907bf85 | 1264 | |
fc2dd942 | 1265 | if (!STACK_INIT(&context, items, yaml_node_item_t*)) goto error; |
a907bf85 | 1266 | |
e27a3c88 KS |
1267 | SEQUENCE_NODE_INIT(node, tag_copy, items.start, items.end, |
1268 | style, mark, mark); | |
1269 | if (!PUSH(&context, document->nodes, node)) goto error; | |
a907bf85 | 1270 | |
e27a3c88 | 1271 | return document->nodes.top - document->nodes.start; |
a907bf85 KS |
1272 | |
1273 | error: | |
e27a3c88 | 1274 | STACK_DEL(&context, items); |
a907bf85 | 1275 | yaml_free(tag_copy); |
a907bf85 KS |
1276 | |
1277 | return 0; | |
1278 | } | |
1279 | ||
1280 | /* | |
e27a3c88 | 1281 | * Add a mapping node to a document. |
a907bf85 KS |
1282 | */ |
1283 | ||
1284 | YAML_DECLARE(int) | |
e27a3c88 | 1285 | yaml_document_add_mapping(yaml_document_t *document, |
a907bf85 KS |
1286 | yaml_char_t *tag, yaml_mapping_style_t style) |
1287 | { | |
1288 | struct { | |
1289 | yaml_error_type_t error; | |
1290 | } context; | |
1291 | yaml_mark_t mark = { 0, 0, 0 }; | |
1292 | yaml_char_t *tag_copy = NULL; | |
1293 | struct { | |
1294 | yaml_node_pair_t *start; | |
1295 | yaml_node_pair_t *end; | |
1296 | yaml_node_pair_t *top; | |
1297 | } pairs = { NULL, NULL, NULL }; | |
e27a3c88 | 1298 | yaml_node_t node; |
a907bf85 | 1299 | |
e27a3c88 | 1300 | assert(document); /* Non-NULL document object is expected. */ |
a907bf85 KS |
1301 | |
1302 | if (!tag) { | |
c9b74def | 1303 | tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG; |
a907bf85 KS |
1304 | } |
1305 | ||
e27a3c88 KS |
1306 | if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error; |
1307 | tag_copy = yaml_strdup(tag); | |
1308 | if (!tag_copy) goto error; | |
a907bf85 | 1309 | |
fc2dd942 | 1310 | if (!STACK_INIT(&context, pairs, yaml_node_pair_t*)) goto error; |
a907bf85 | 1311 | |
e27a3c88 KS |
1312 | MAPPING_NODE_INIT(node, tag_copy, pairs.start, pairs.end, |
1313 | style, mark, mark); | |
1314 | if (!PUSH(&context, document->nodes, node)) goto error; | |
a907bf85 | 1315 | |
e27a3c88 | 1316 | return document->nodes.top - document->nodes.start; |
a907bf85 KS |
1317 | |
1318 | error: | |
e27a3c88 | 1319 | STACK_DEL(&context, pairs); |
a907bf85 | 1320 | yaml_free(tag_copy); |
a907bf85 KS |
1321 | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | /* | |
e27a3c88 | 1326 | * Append an item to a sequence node. |
a907bf85 KS |
1327 | */ |
1328 | ||
e27a3c88 KS |
1329 | YAML_DECLARE(int) |
1330 | yaml_document_append_sequence_item(yaml_document_t *document, | |
1331 | int sequence, int item) | |
a907bf85 KS |
1332 | { |
1333 | struct { | |
1334 | yaml_error_type_t error; | |
1335 | } context; | |
a907bf85 | 1336 | |
e27a3c88 KS |
1337 | assert(document); /* Non-NULL document is required. */ |
1338 | assert(sequence > 0 | |
1339 | && document->nodes.start + sequence <= document->nodes.top); | |
1340 | /* Valid sequence id is required. */ | |
1341 | assert(document->nodes.start[sequence-1].type == YAML_SEQUENCE_NODE); | |
1342 | /* A sequence node is required. */ | |
1343 | assert(item > 0 && document->nodes.start + item <= document->nodes.top); | |
1344 | /* Valid item id is required. */ | |
1345 | ||
1346 | if (!PUSH(&context, | |
1347 | document->nodes.start[sequence-1].data.sequence.items, item)) | |
1348 | return 0; | |
a907bf85 | 1349 | |
e27a3c88 KS |
1350 | return 1; |
1351 | } | |
a907bf85 | 1352 | |
e27a3c88 KS |
1353 | /* |
1354 | * Append a pair of a key and a value to a mapping node. | |
1355 | */ | |
a907bf85 | 1356 | |
e27a3c88 KS |
1357 | YAML_DECLARE(int) |
1358 | yaml_document_append_mapping_pair(yaml_document_t *document, | |
1359 | int mapping, int key, int value) | |
1360 | { | |
1361 | struct { | |
1362 | yaml_error_type_t error; | |
1363 | } context; | |
252c575a KS |
1364 | |
1365 | yaml_node_pair_t pair; | |
e27a3c88 KS |
1366 | |
1367 | assert(document); /* Non-NULL document is required. */ | |
1368 | assert(mapping > 0 | |
1369 | && document->nodes.start + mapping <= document->nodes.top); | |
1370 | /* Valid mapping id is required. */ | |
1371 | assert(document->nodes.start[mapping-1].type == YAML_MAPPING_NODE); | |
1372 | /* A mapping node is required. */ | |
1373 | assert(key > 0 && document->nodes.start + key <= document->nodes.top); | |
1374 | /* Valid key id is required. */ | |
1375 | assert(value > 0 && document->nodes.start + value <= document->nodes.top); | |
1376 | /* Valid value id is required. */ | |
1377 | ||
252c575a KS |
1378 | pair.key = key; |
1379 | pair.value = value; | |
1380 | ||
e27a3c88 KS |
1381 | if (!PUSH(&context, |
1382 | document->nodes.start[mapping-1].data.mapping.pairs, pair)) | |
1383 | return 0; | |
a907bf85 | 1384 | |
e27a3c88 KS |
1385 | return 1; |
1386 | } | |
a907bf85 | 1387 | |
0174ed6e | 1388 |