]> andersk Git - libyaml.git/blame - src/api.c
Add the run-emitter test.
[libyaml.git] / src / api.c
CommitLineData
a51447c9 1
625fcfe9 2#include "yaml_private.h"
a51447c9 3
625fcfe9
KS
4/*
5 * Get the library version.
6 */
95b98ba9 7
5eff53a4
KS
8YAML_DECLARE(const char *)
9yaml_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
18YAML_DECLARE(void)
19yaml_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 30YAML_DECLARE(void *)
95b98ba9
KS
31yaml_malloc(size_t size)
32{
33 return malloc(size ? size : 1);
34}
35
36/*
37 * Reallocate a dynamic memory block.
38 */
39
f642fd11 40YAML_DECLARE(void *)
95b98ba9
KS
41yaml_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 50YAML_DECLARE(void)
95b98ba9
KS
51yaml_free(void *ptr)
52{
53 if (ptr) free(ptr);
54}
55
56/*
625fcfe9 57 * Duplicate a string.
a51447c9
KS
58 */
59
cf616166
KS
60YAML_DECLARE(yaml_char_t *)
61yaml_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
73YAML_DECLARE(int)
74yaml_string_extend(yaml_char_t **start,
75 yaml_char_t **pointer, yaml_char_t **end)
76{
77 void *new_start = yaml_realloc(*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
94YAML_DECLARE(int)
95yaml_string_join(
96 yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end,
97 yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end)
98{
99 if (*b_start == *b_pointer)
100 return 1;
f2b59d4d 101
625fcfe9
KS
102 while (*a_end - *a_pointer <= *b_pointer - *b_start) {
103 if (!yaml_string_extend(a_start, a_pointer, a_end))
104 return 0;
105 }
f2b59d4d 106
625fcfe9
KS
107 memcpy(*a_pointer, *b_start, *b_pointer - *b_start);
108 *a_pointer += *b_pointer - *b_start;
f2b59d4d 109
625fcfe9
KS
110 return 1;
111}
f2b59d4d 112
625fcfe9
KS
113/*
114 * Extend a stack.
115 */
f2b59d4d 116
625fcfe9
KS
117YAML_DECLARE(int)
118yaml_stack_extend(void **start, void **top, void **end)
119{
120 void *new_start = yaml_realloc(*start, (*end - *start)*2);
f2b59d4d 121
625fcfe9 122 if (!new_start) return 0;
f2b59d4d 123
625fcfe9
KS
124 *top = new_start + (*top - *start);
125 *end = new_start + (*end - *start)*2;
126 *start = new_start;
f2b59d4d 127
625fcfe9
KS
128 return 1;
129}
1eb01be7 130
625fcfe9
KS
131/*
132 * Extend or move a queue.
133 */
1eb01be7 134
625fcfe9
KS
135YAML_DECLARE(int)
136yaml_queue_extend(void **start, void **head, void **tail, void **end)
137{
138 /* Check if we need to resize the queue. */
1eb01be7 139
625fcfe9
KS
140 if (*start == *head && *tail == *end) {
141 void *new_start = yaml_realloc(*start, (*end - *start)*2);
1eb01be7 142
625fcfe9 143 if (!new_start) return 0;
1eb01be7 144
625fcfe9
KS
145 *head = new_start + (*head - *start);
146 *tail = new_start + (*tail - *start);
147 *end = new_start + (*end - *start)*2;
148 *start = new_start;
149 }
ab01bac8 150
625fcfe9 151 /* Check if we need to move the queue at the beginning of the buffer. */
ab01bac8 152
625fcfe9
KS
153 if (*tail == *end) {
154 if (*head != *tail) {
155 memmove(*start, *head, *tail - *head);
156 }
157 *tail -= *head - *start;
158 *head = *start;
159 }
ab01bac8 160
625fcfe9
KS
161 return 1;
162}
1eb01be7 163
1eb01be7 164
625fcfe9
KS
165/*
166 * Create a new parser object.
167 */
1eb01be7 168
625fcfe9
KS
169YAML_DECLARE(int)
170yaml_parser_initialize(yaml_parser_t *parser)
171{
172 assert(parser); /* Non-NULL parser object expected. */
f2b59d4d 173
625fcfe9 174 memset(parser, 0, sizeof(yaml_parser_t));
b1a54000 175 if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE))
625fcfe9 176 goto error;
b1a54000 177 if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE))
625fcfe9
KS
178 goto error;
179 if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE))
180 goto error;
181 if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE))
182 goto error;
183 if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE))
184 goto error;
185 if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE))
186 goto error;
187 if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE))
188 goto error;
189 if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE))
190 goto error;
f2b59d4d 191
625fcfe9 192 return 1;
f2b59d4d
KS
193
194error:
195
625fcfe9
KS
196 BUFFER_DEL(parser, parser->raw_buffer);
197 BUFFER_DEL(parser, parser->buffer);
198 QUEUE_DEL(parser, parser->tokens);
199 STACK_DEL(parser, parser->indents);
200 STACK_DEL(parser, parser->simple_keys);
201 STACK_DEL(parser, parser->states);
202 STACK_DEL(parser, parser->marks);
203 STACK_DEL(parser, parser->tag_directives);
f2b59d4d 204
625fcfe9 205 return 0;
a51447c9
KS
206}
207
208/*
209 * Destroy a parser object.
210 */
211
f642fd11 212YAML_DECLARE(void)
a51447c9
KS
213yaml_parser_delete(yaml_parser_t *parser)
214{
95b98ba9
KS
215 assert(parser); /* Non-NULL parser object expected. */
216
625fcfe9
KS
217 BUFFER_DEL(parser, parser->raw_buffer);
218 BUFFER_DEL(parser, parser->buffer);
219 while (!QUEUE_EMPTY(parser, parser->tokens)) {
220 yaml_token_delete(&DEQUEUE(parser, parser->tokens));
221 }
222 QUEUE_DEL(parser, parser->tokens);
223 STACK_DEL(parser, parser->indents);
224 STACK_DEL(parser, parser->simple_keys);
225 STACK_DEL(parser, parser->states);
226 STACK_DEL(parser, parser->marks);
227 while (!STACK_EMPTY(parser, parser->tag_directives)) {
228 yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives);
229 yaml_free(tag_directive.handle);
230 yaml_free(tag_directive.prefix);
231 }
232 STACK_DEL(parser, parser->tag_directives);
95b98ba9
KS
233
234 memset(parser, 0, sizeof(yaml_parser_t));
95b98ba9
KS
235}
236
237/*
6eb1ded4 238 * String read handler.
95b98ba9
KS
239 */
240
241static int
242yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
243 size_t *size_read)
244{
625fcfe9 245 yaml_parser_t *parser = data;
6eb1ded4 246
625fcfe9 247 if (parser->input.string.current == parser->input.string.end) {
6eb1ded4
KS
248 *size_read = 0;
249 return 1;
250 }
251
625fcfe9
KS
252 if (size > (parser->input.string.end - parser->input.string.current)) {
253 size = parser->input.string.end - parser->input.string.current;
6eb1ded4
KS
254 }
255
625fcfe9
KS
256 memcpy(buffer, parser->input.string.current, size);
257 parser->input.string.current += size;
6eb1ded4 258 *size_read = size;
95b98ba9
KS
259 return 1;
260}
261
262/*
263 * File read handler.
264 */
265
266static int
267yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
268 size_t *size_read)
269{
625fcfe9
KS
270 yaml_parser_t *parser = data;
271
272 *size_read = fread(buffer, 1, size, parser->input.file);
273 return !ferror(parser->input.file);
95b98ba9
KS
274}
275
276/*
277 * Set a string input.
278 */
279
f642fd11 280YAML_DECLARE(void)
95b98ba9
KS
281yaml_parser_set_input_string(yaml_parser_t *parser,
282 unsigned char *input, size_t size)
283{
284 assert(parser); /* Non-NULL parser object expected. */
6eb1ded4 285 assert(!parser->read_handler); /* You can set the source only once. */
95b98ba9
KS
286 assert(input); /* Non-NULL input string expected. */
287
6eb1ded4 288 parser->read_handler = yaml_string_read_handler;
625fcfe9
KS
289 parser->read_handler_data = parser;
290
291 parser->input.string.start = input;
292 parser->input.string.current = input;
293 parser->input.string.end = input+size;
95b98ba9
KS
294}
295
296/*
297 * Set a file input.
298 */
299
f642fd11 300YAML_DECLARE(void)
95b98ba9
KS
301yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
302{
303 assert(parser); /* Non-NULL parser object expected. */
6eb1ded4 304 assert(!parser->read_handler); /* You can set the source only once. */
95b98ba9
KS
305 assert(file); /* Non-NULL file object expected. */
306
307 parser->read_handler = yaml_file_read_handler;
625fcfe9
KS
308 parser->read_handler_data = parser;
309
310 parser->input.file = file;
95b98ba9
KS
311}
312
313/*
314 * Set a generic input.
315 */
316
f642fd11 317YAML_DECLARE(void)
95b98ba9
KS
318yaml_parser_set_input(yaml_parser_t *parser,
319 yaml_read_handler_t *handler, void *data)
320{
321 assert(parser); /* Non-NULL parser object expected. */
6eb1ded4 322 assert(!parser->read_handler); /* You can set the source only once. */
95b98ba9
KS
323 assert(handler); /* Non-NULL read handler expected. */
324
325 parser->read_handler = handler;
6eb1ded4 326 parser->read_handler_data = data;
95b98ba9
KS
327}
328
329/*
330 * Set the source encoding.
331 */
332
f642fd11 333YAML_DECLARE(void)
95b98ba9
KS
334yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
335{
336 assert(parser); /* Non-NULL parser object expected. */
337 assert(!parser->encoding); /* Encoding is already set or detected. */
338
339 parser->encoding = encoding;
a51447c9
KS
340}
341
b1a54000
KS
342/*
343 * Create a new emitter object.
344 */
345
346YAML_DECLARE(int)
347yaml_emitter_initialize(yaml_emitter_t *emitter)
348{
349 assert(emitter); /* Non-NULL emitter object expected. */
350
351 memset(emitter, 0, sizeof(yaml_emitter_t));
352 if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE))
353 goto error;
354 if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE))
355 goto error;
356 if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE))
357 goto error;
358 if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE))
359 goto error;
360 if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE))
361 goto error;
362 if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE))
363 goto error;
364
365 return 1;
366
367error:
368
369 BUFFER_DEL(emitter, emitter->buffer);
370 BUFFER_DEL(emitter, emitter->raw_buffer);
371 STACK_DEL(emitter, emitter->states);
372 QUEUE_DEL(emitter, emitter->events);
373 STACK_DEL(emitter, emitter->indents);
374 STACK_DEL(emitter, emitter->tag_directives);
375
376 return 0;
377}
378
379/*
380 * Destroy an emitter object.
381 */
382
383YAML_DECLARE(void)
384yaml_emitter_delete(yaml_emitter_t *emitter)
385{
386 assert(emitter); /* Non-NULL emitter object expected. */
387
388 BUFFER_DEL(emitter, emitter->buffer);
389 BUFFER_DEL(emitter, emitter->raw_buffer);
390 STACK_DEL(emitter, emitter->states);
391 while (!QUEUE_EMPTY(emitter, emitter->events)) {
392 yaml_event_delete(&DEQUEUE(emitter, emitter->events));
393 }
6d167281 394 QUEUE_DEL(emitter, emitter->events);
b1a54000
KS
395 STACK_DEL(emitter, emitter->indents);
396 while (!STACK_EMPTY(empty, emitter->tag_directives)) {
397 yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives);
398 yaml_free(tag_directive.handle);
399 yaml_free(tag_directive.prefix);
400 }
401 STACK_DEL(emitter, emitter->tag_directives);
402
403 memset(emitter, 0, sizeof(yaml_emitter_t));
404}
405
406/*
407 * String write handler.
408 */
409
410static int
411yaml_string_write_handler(void *data, unsigned char *buffer, size_t size)
412{
413 yaml_emitter_t *emitter = data;
414
415 if (emitter->output.string.size + *emitter->output.string.size_written
416 < size) {
417 memcpy(emitter->output.string.buffer
418 + *emitter->output.string.size_written,
419 buffer,
420 emitter->output.string.size
421 - *emitter->output.string.size_written);
422 *emitter->output.string.size_written = emitter->output.string.size;
423 return 0;
424 }
425
426 memcpy(emitter->output.string.buffer
427 + *emitter->output.string.size_written, buffer, size);
428 *emitter->output.string.size_written += size;
429 return 1;
430}
431
432/*
433 * File write handler.
434 */
435
436static int
437yaml_file_write_handler(void *data, unsigned char *buffer, size_t size)
438{
439 yaml_emitter_t *emitter = data;
440
441 return (fwrite(buffer, 1, size, emitter->output.file) == size);
442}
443/*
444 * Set a string output.
445 */
446
447YAML_DECLARE(void)
448yaml_emitter_set_output_string(yaml_emitter_t *emitter,
449 unsigned char *output, size_t size, size_t *size_written)
450{
451 assert(emitter); /* Non-NULL emitter object expected. */
452 assert(!emitter->write_handler); /* You can set the output only once. */
453 assert(output); /* Non-NULL output string expected. */
454
455 emitter->write_handler = yaml_string_write_handler;
456 emitter->write_handler_data = emitter;
457
458 emitter->output.string.buffer = output;
459 emitter->output.string.size = size;
460 emitter->output.string.size_written = size_written;
461 *size_written = 0;
462}
463
464/*
465 * Set a file output.
466 */
467
468YAML_DECLARE(void)
469yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file)
470{
471 assert(emitter); /* Non-NULL emitter object expected. */
472 assert(!emitter->write_handler); /* You can set the output only once. */
473 assert(file); /* Non-NULL file object expected. */
474
475 emitter->write_handler = yaml_file_write_handler;
476 emitter->write_handler_data = emitter;
477
478 emitter->output.file = file;
479}
480
481/*
482 * Set a generic output handler.
483 */
484
485YAML_DECLARE(void)
486yaml_emitter_set_output(yaml_emitter_t *emitter,
487 yaml_write_handler_t *handler, void *data)
488{
489 assert(emitter); /* Non-NULL emitter object expected. */
490 assert(!emitter->write_handler); /* You can set the output only once. */
491 assert(handler); /* Non-NULL handler object expected. */
492
493 emitter->write_handler = handler;
494 emitter->write_handler_data = data;
495}
496
497/*
498 * Set the output encoding.
499 */
500
501YAML_DECLARE(void)
502yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding)
503{
504 assert(emitter); /* Non-NULL emitter object expected. */
505 assert(!emitter->encoding); /* You can set encoding only once. */
506
507 emitter->encoding = encoding;
508}
509
510/*
511 * Set the canonical output style.
512 */
513
514YAML_DECLARE(void)
515yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical)
516{
517 assert(emitter); /* Non-NULL emitter object expected. */
518
519 emitter->canonical = (canonical != 0);
520}
521
522/*
523 * Set the indentation increment.
524 */
525
526YAML_DECLARE(void)
527yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent)
528{
529 assert(emitter); /* Non-NULL emitter object expected. */
530
531 emitter->best_indent = (1 < indent && indent < 10) ? indent : 2;
532}
533
534/*
535 * Set the preferred line width.
536 */
537
538YAML_DECLARE(void)
539yaml_emitter_set_width(yaml_emitter_t *emitter, int width)
540{
541 assert(emitter); /* Non-NULL emitter object expected. */
542
cf616166 543 emitter->best_width = (width >= 0) ? width : -1;
b1a54000
KS
544}
545
546/*
547 * Set if unescaped non-ASCII characters are allowed.
548 */
549
550YAML_DECLARE(void)
551yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode)
552{
553 assert(emitter); /* Non-NULL emitter object expected. */
554
555 emitter->unicode = (unicode != 0);
556}
557
558/*
559 * Set the preferred line break character.
560 */
561
562YAML_DECLARE(void)
563yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break)
564{
565 assert(emitter); /* Non-NULL emitter object expected. */
566
567 emitter->line_break = line_break;
568}
569
f642fd11
KS
570/*
571 * Destroy a token object.
572 */
573
574YAML_DECLARE(void)
575yaml_token_delete(yaml_token_t *token)
576{
577 assert(token); /* Non-NULL token object expected. */
578
579 switch (token->type)
580 {
581 case YAML_TAG_DIRECTIVE_TOKEN:
582 yaml_free(token->data.tag_directive.handle);
583 yaml_free(token->data.tag_directive.prefix);
584 break;
585
586 case YAML_ALIAS_TOKEN:
26687d7d
KS
587 yaml_free(token->data.alias.value);
588 break;
589
f642fd11 590 case YAML_ANCHOR_TOKEN:
26687d7d 591 yaml_free(token->data.anchor.value);
f642fd11
KS
592 break;
593
594 case YAML_TAG_TOKEN:
595 yaml_free(token->data.tag.handle);
596 yaml_free(token->data.tag.suffix);
597 break;
598
599 case YAML_SCALAR_TOKEN:
600 yaml_free(token->data.scalar.value);
601 break;
54815ffd
KS
602
603 default:
604 break;
f642fd11
KS
605 }
606
607 memset(token, 0, sizeof(yaml_token_t));
26687d7d
KS
608}
609
5a00d8fe
KS
610/*
611 * Check if a string is a valid UTF-8 sequence.
612 *
613 * Check 'reader.c' for more details on UTF-8 encoding.
614 */
615
616static int
617yaml_check_utf8(yaml_char_t *start, size_t length)
618{
619 yaml_char_t *end = start+length;
620 yaml_char_t *pointer = start;
621
622 while (pointer < end) {
623 unsigned char octet;
624 unsigned int width;
625 unsigned int value;
626 int k;
627
628 octet = pointer[0];
629 width = (octet & 0x80) == 0x00 ? 1 :
630 (octet & 0xE0) == 0xC0 ? 2 :
631 (octet & 0xF0) == 0xE0 ? 3 :
632 (octet & 0xF8) == 0xF0 ? 4 : 0;
784340c2 633 value = (octet & 0x80) == 0x00 ? octet & 0x7F :
5a00d8fe
KS
634 (octet & 0xE0) == 0xC0 ? octet & 0x1F :
635 (octet & 0xF0) == 0xE0 ? octet & 0x0F :
636 (octet & 0xF8) == 0xF0 ? octet & 0x07 : 0;
637 if (!width) return 0;
638 if (pointer+width > end) return 0;
639 for (k = 1; k < width; k ++) {
640 octet = pointer[k];
641 if ((octet & 0xC0) != 0x80) return 0;
642 value = (value << 6) + (octet & 0x3F);
643 }
644 if (!((width == 1) ||
645 (width == 2 && value >= 0x80) ||
646 (width == 3 && value >= 0x800) ||
647 (width == 4 && value >= 0x10000))) return 0;
648
649 pointer += width;
650 }
651
652 return 1;
653}
654
655/*
656 * Create STREAM-START.
657 */
658
659YAML_DECLARE(int)
660yaml_stream_start_event_initialize(yaml_event_t *event,
661 yaml_encoding_t encoding)
662{
663 yaml_mark_t mark = { 0, 0, 0 };
664
665 assert(event); /* Non-NULL event object is expected. */
666
667 STREAM_START_EVENT_INIT(*event, encoding, mark, mark);
668
669 return 1;
670}
671
672/*
673 * Create STREAM-END.
674 */
675
676YAML_DECLARE(int)
677yaml_stream_end_event_initialize(yaml_event_t *event)
678{
679 yaml_mark_t mark = { 0, 0, 0 };
680
681 assert(event); /* Non-NULL event object is expected. */
682
683 STREAM_END_EVENT_INIT(*event, mark, mark);
684
685 return 1;
686}
687
688/*
689 * Create DOCUMENT-START.
690 */
691
692YAML_DECLARE(int)
693yaml_document_start_event_initialize(yaml_event_t *event,
694 yaml_version_directive_t *version_directive,
695 yaml_tag_directive_t *tag_directives_start,
696 yaml_tag_directive_t *tag_directives_end,
697 int implicit)
698{
699 struct {
700 yaml_error_type_t error;
701 } context;
702 yaml_mark_t mark = { 0, 0, 0 };
703 yaml_version_directive_t *version_directive_copy = NULL;
704 struct {
705 yaml_tag_directive_t *start;
706 yaml_tag_directive_t *end;
707 yaml_tag_directive_t *top;
708 } tag_directives_copy = { NULL, NULL, NULL };
709 yaml_tag_directive_t value = { NULL, NULL };
710
711 assert(event); /* Non-NULL event object is expected. */
712 assert((tag_directives_start && tag_directives_end) ||
713 (tag_directives_start == tag_directives_end));
714 /* Valid tag directives are expected. */
715
716 if (version_directive) {
717 version_directive_copy = yaml_malloc(sizeof(yaml_version_directive_t));
718 if (!version_directive_copy) goto error;
719 version_directive_copy->major = version_directive->major;
720 version_directive_copy->minor = version_directive->minor;
721 }
722
723 if (tag_directives_start != tag_directives_end) {
724 yaml_tag_directive_t *tag_directive;
725 if (!STACK_INIT(&context, tag_directives_copy, INITIAL_STACK_SIZE))
726 goto error;
727 for (tag_directive = tag_directives_start;
728 tag_directive != tag_directives_end; tag_directive ++) {
729 assert(tag_directive->handle);
730 assert(tag_directive->prefix);
731 if (!yaml_check_utf8(tag_directive->handle,
732 strlen((char *)tag_directive->handle)))
733 goto error;
734 if (!yaml_check_utf8(tag_directive->prefix,
735 strlen((char *)tag_directive->prefix)))
736 goto error;
737 value.handle = yaml_strdup(tag_directive->handle);
738 value.prefix = yaml_strdup(tag_directive->prefix);
739 if (!value.handle || !value.prefix) goto error;
740 if (!PUSH(&context, tag_directives_copy, value))
741 goto error;
742 value.handle = NULL;
743 value.prefix = NULL;
744 }
745 }
746
747 DOCUMENT_START_EVENT_INIT(*event, version_directive_copy,
6d167281 748 tag_directives_copy.start, tag_directives_copy.top,
5a00d8fe
KS
749 implicit, mark, mark);
750
751 return 1;
752
753error:
754 yaml_free(version_directive_copy);
755 while (!STACK_EMPTY(context, tag_directives_copy)) {
756 yaml_tag_directive_t value = POP(context, tag_directives_copy);
757 yaml_free(value.handle);
758 yaml_free(value.prefix);
759 }
760 STACK_DEL(context, tag_directives_copy);
761 yaml_free(value.handle);
762 yaml_free(value.prefix);
763
764 return 0;
765}
766
767/*
768 * Create DOCUMENT-END.
769 */
770
771YAML_DECLARE(int)
772yaml_document_end_event_initialize(yaml_event_t *event, int implicit)
773{
774 yaml_mark_t mark = { 0, 0, 0 };
775
776 assert(event); /* Non-NULL emitter object is expected. */
777
778 DOCUMENT_END_EVENT_INIT(*event, implicit, mark, mark);
779
780 return 1;
781}
782
783/*
784 * Create ALIAS.
785 */
786
787YAML_DECLARE(int)
788yaml_alias_event_initialize(yaml_event_t *event, yaml_char_t *anchor)
789{
790 yaml_mark_t mark = { 0, 0, 0 };
791 yaml_char_t *anchor_copy = NULL;
792
793 assert(event); /* Non-NULL event object is expected. */
794 assert(anchor); /* Non-NULL anchor is expected. */
795
796 if (!yaml_check_utf8(anchor, strlen((char *)anchor))) return 0;
797
798 anchor_copy = yaml_strdup(anchor);
799 if (!anchor_copy)
800 return 0;
801
802 ALIAS_EVENT_INIT(*event, anchor_copy, mark, mark);
803
804 return 1;
805}
806
807/*
808 * Create SCALAR.
809 */
810
811YAML_DECLARE(int)
812yaml_scalar_event_initialize(yaml_event_t *event,
813 yaml_char_t *anchor, yaml_char_t *tag,
814 yaml_char_t *value, size_t length,
815 int plain_implicit, int quoted_implicit,
816 yaml_scalar_style_t style)
817{
818 yaml_mark_t mark = { 0, 0, 0 };
819 yaml_char_t *anchor_copy = NULL;
820 yaml_char_t *tag_copy = NULL;
821 yaml_char_t *value_copy = NULL;
822
823 assert(event); /* Non-NULL event object is expected. */
824 assert(value); /* Non-NULL anchor is expected. */
825
826
827 if (anchor) {
828 if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
829 anchor_copy = yaml_strdup(anchor);
830 if (!anchor_copy) goto error;
831 }
832
833 if (tag) {
834 if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
835 tag_copy = yaml_strdup(tag);
836 if (!tag_copy) goto error;
837 }
838
839 if (!yaml_check_utf8(value, length)) goto error;
840 value_copy = yaml_malloc(length+1);
841 if (!value_copy) goto error;
842 memcpy(value_copy, value, length);
843 value_copy[length] = '\0';
844
845 SCALAR_EVENT_INIT(*event, anchor_copy, tag_copy, value_copy, length,
846 plain_implicit, quoted_implicit, style, mark, mark);
847
848 return 1;
849
850error:
851 yaml_free(anchor_copy);
852 yaml_free(tag_copy);
853 yaml_free(value_copy);
854
855 return 0;
856}
857
858/*
859 * Create SEQUENCE-START.
860 */
861
862YAML_DECLARE(int)
863yaml_sequence_start_event_initialize(yaml_event_t *event,
864 yaml_char_t *anchor, yaml_char_t *tag, int implicit,
865 yaml_sequence_style_t style)
866{
867 yaml_mark_t mark = { 0, 0, 0 };
868 yaml_char_t *anchor_copy = NULL;
869 yaml_char_t *tag_copy = NULL;
870
871 assert(event); /* Non-NULL event object is expected. */
872
873 if (anchor) {
874 if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
875 anchor_copy = yaml_strdup(anchor);
876 if (!anchor_copy) goto error;
877 }
878
879 if (tag) {
880 if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
881 tag_copy = yaml_strdup(tag);
882 if (!tag_copy) goto error;
883 }
884
885 SEQUENCE_START_EVENT_INIT(*event, anchor_copy, tag_copy,
886 implicit, style, mark, mark);
887
888 return 1;
889
890error:
891 yaml_free(anchor_copy);
892 yaml_free(tag_copy);
893
894 return 0;
895}
896
897/*
898 * Create SEQUENCE-END.
899 */
900
901YAML_DECLARE(int)
902yaml_sequence_end_event_initialize(yaml_event_t *event)
903{
904 yaml_mark_t mark = { 0, 0, 0 };
905
906 assert(event); /* Non-NULL event object is expected. */
907
908 SEQUENCE_END_EVENT_INIT(*event, mark, mark);
909
910 return 1;
911}
912
913/*
914 * Create MAPPING-START.
915 */
916
917YAML_DECLARE(int)
918yaml_mapping_start_event_initialize(yaml_event_t *event,
919 yaml_char_t *anchor, yaml_char_t *tag, int implicit,
920 yaml_mapping_style_t style)
921{
922 yaml_mark_t mark = { 0, 0, 0 };
923 yaml_char_t *anchor_copy = NULL;
924 yaml_char_t *tag_copy = NULL;
925
926 assert(event); /* Non-NULL event object is expected. */
927
928 if (anchor) {
929 if (!yaml_check_utf8(anchor, strlen((char *)anchor))) goto error;
930 anchor_copy = yaml_strdup(anchor);
931 if (!anchor_copy) goto error;
932 }
933
934 if (tag) {
935 if (!yaml_check_utf8(tag, strlen((char *)tag))) goto error;
936 tag_copy = yaml_strdup(tag);
937 if (!tag_copy) goto error;
938 }
939
940 MAPPING_START_EVENT_INIT(*event, anchor_copy, tag_copy,
941 implicit, style, mark, mark);
942
943 return 1;
944
945error:
946 yaml_free(anchor_copy);
947 yaml_free(tag_copy);
948
949 return 0;
950}
951
952/*
953 * Create MAPPING-END.
954 */
955
956YAML_DECLARE(int)
957yaml_mapping_end_event_initialize(yaml_event_t *event)
958{
959 yaml_mark_t mark = { 0, 0, 0 };
960
961 assert(event); /* Non-NULL event object is expected. */
962
963 MAPPING_END_EVENT_INIT(*event, mark, mark);
964
965 return 1;
966}
967
26687d7d
KS
968/*
969 * Destroy an event object.
970 */
971
972YAML_DECLARE(void)
973yaml_event_delete(yaml_event_t *event)
974{
625fcfe9
KS
975 yaml_tag_directive_t *tag_directive;
976
26687d7d
KS
977 assert(event); /* Non-NULL event object expected. */
978
979 switch (event->type)
980 {
981 case YAML_DOCUMENT_START_EVENT:
625fcfe9
KS
982 yaml_free(event->data.document_start.version_directive);
983 for (tag_directive = event->data.document_start.tag_directives.start;
984 tag_directive != event->data.document_start.tag_directives.end;
985 tag_directive++) {
986 yaml_free(tag_directive->handle);
987 yaml_free(tag_directive->prefix);
988 }
989 yaml_free(event->data.document_start.tag_directives.start);
26687d7d
KS
990 break;
991
992 case YAML_ALIAS_EVENT:
993 yaml_free(event->data.alias.anchor);
994 break;
995
996 case YAML_SCALAR_EVENT:
997 yaml_free(event->data.scalar.anchor);
998 yaml_free(event->data.scalar.tag);
999 yaml_free(event->data.scalar.value);
1000 break;
1001
1002 case YAML_SEQUENCE_START_EVENT:
1003 yaml_free(event->data.sequence_start.anchor);
1004 yaml_free(event->data.sequence_start.tag);
1005 break;
1006
1007 case YAML_MAPPING_START_EVENT:
1008 yaml_free(event->data.mapping_start.anchor);
1009 yaml_free(event->data.mapping_start.tag);
1010 break;
54815ffd
KS
1011
1012 default:
1013 break;
26687d7d
KS
1014 }
1015
1016 memset(event, 0, sizeof(yaml_event_t));
26687d7d
KS
1017}
1018
This page took 0.19045 seconds and 5 git commands to generate.