source: libyaml/trunk/src/api.c @ 211

Revision 211, 15.7 KB checked in by xi, 8 years ago (diff)

Add Emitter definitions and implement the Writer.

RevLine 
[178]1
[208]2#include "yaml_private.h"
[178]3
[208]4/*
5 * Get the library version.
6 */
[178]7
[200]8YAML_DECLARE(const char *)
9yaml_get_version_string(void)
10{
11    return YAML_VERSION_STRING;
12}
13
[208]14/*
15 * Get the library version numbers.
16 */
17
[200]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
[178]26/*
[179]27 * Allocate a dynamic memory block.
[178]28 */
29
[183]30YAML_DECLARE(void *)
[179]31yaml_malloc(size_t size)
32{
33    return malloc(size ? size : 1);
34}
35
36/*
37 * Reallocate a dynamic memory block.
38 */
39
[183]40YAML_DECLARE(void *)
[179]41yaml_realloc(void *ptr, size_t size)
42{
43    return ptr ? realloc(ptr, size ? size : 1) : malloc(size ? size : 1);
44}
45
46/*
47 * Free a dynamic memory block.
48 */
49
[183]50YAML_DECLARE(void)
[179]51yaml_free(void *ptr)
52{
53    if (ptr) free(ptr);
54}
55
56/*
[208]57 * Duplicate a string.
[179]58 */
59
[208]60YAML_DECLARE(char *)
61yaml_strdup(const char *str)
[178]62{
[208]63    return strdup(str);
64}
[178]65
[208]66/*
67 * Extend a string.
68 */
[180]69
[208]70YAML_DECLARE(int)
71yaml_string_extend(yaml_char_t **start,
72        yaml_char_t **pointer, yaml_char_t **end)
73{
74    void *new_start = yaml_realloc(*start, (*end - *start)*2);
[178]75
[208]76    if (!new_start) return 0;
[178]77
[208]78    memset(new_start + (*end - *start), 0, *end - *start);
[180]79
[208]80    *pointer = new_start + (*pointer - *start);
81    *end = new_start + (*end - *start)*2;
82    *start = new_start;
[185]83
[208]84    return 1;
85}
[180]86
[208]87/*
88 * Append a string B to a string A.
89 */
[180]90
[208]91YAML_DECLARE(int)
92yaml_string_join(
93        yaml_char_t **a_start, yaml_char_t **a_pointer, yaml_char_t **a_end,
94        yaml_char_t **b_start, yaml_char_t **b_pointer, yaml_char_t **b_end)
95{
96    if (*b_start == *b_pointer)
97        return 1;
[185]98
[208]99    while (*a_end - *a_pointer <= *b_pointer - *b_start) {
100        if (!yaml_string_extend(a_start, a_pointer, a_end))
101            return 0;
102    }
[180]103
[208]104    memcpy(*a_pointer, *b_start, *b_pointer - *b_start);
105    *a_pointer += *b_pointer - *b_start;
[185]106
[208]107    return 1;
108}
[185]109
[208]110/*
111 * Extend a stack.
112 */
[185]113
[208]114YAML_DECLARE(int)
115yaml_stack_extend(void **start, void **top, void **end)
116{
117    void *new_start = yaml_realloc(*start, (*end - *start)*2);
[185]118
[208]119    if (!new_start) return 0;
[185]120
[208]121    *top = new_start + (*top - *start);
122    *end = new_start + (*end - *start)*2;
123    *start = new_start;
[185]124
[208]125    return 1;
126}
[185]127
[208]128/*
129 * Extend or move a queue.
130 */
[185]131
[208]132YAML_DECLARE(int)
133yaml_queue_extend(void **start, void **head, void **tail, void **end)
134{
135    /* Check if we need to resize the queue. */
[185]136
[208]137    if (*start == *head && *tail == *end) {
138        void *new_start = yaml_realloc(*start, (*end - *start)*2);
[201]139
[208]140        if (!new_start) return 0;
[201]141
[208]142        *head = new_start + (*head - *start);
143        *tail = new_start + (*tail - *start);
144        *end = new_start + (*end - *start)*2;
145        *start = new_start;
146    }
[201]147
[208]148    /* Check if we need to move the queue at the beginning of the buffer. */
[201]149
[208]150    if (*tail == *end) {
151        if (*head != *tail) {
152            memmove(*start, *head, *tail - *head);
153        }
154        *tail -= *head - *start;
155        *head = *start;
156    }
[201]157
[208]158    return 1;
159}
[203]160
161
[208]162/*
163 * Create a new parser object.
164 */
[203]165
[208]166YAML_DECLARE(int)
167yaml_parser_initialize(yaml_parser_t *parser)
168{
169    assert(parser);     /* Non-NULL parser object expected. */
[201]170
[208]171    memset(parser, 0, sizeof(yaml_parser_t));
[211]172    if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE))
[208]173        goto error;
[211]174    if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE))
[208]175        goto error;
176    if (!QUEUE_INIT(parser, parser->tokens, INITIAL_QUEUE_SIZE))
177        goto error;
178    if (!STACK_INIT(parser, parser->indents, INITIAL_STACK_SIZE))
179        goto error;
180    if (!STACK_INIT(parser, parser->simple_keys, INITIAL_STACK_SIZE))
181        goto error;
182    if (!STACK_INIT(parser, parser->states, INITIAL_STACK_SIZE))
183        goto error;
184    if (!STACK_INIT(parser, parser->marks, INITIAL_STACK_SIZE))
185        goto error;
186    if (!STACK_INIT(parser, parser->tag_directives, INITIAL_STACK_SIZE))
187        goto error;
[201]188
[208]189    return 1;
[201]190
[185]191error:
192
[208]193    BUFFER_DEL(parser, parser->raw_buffer);
194    BUFFER_DEL(parser, parser->buffer);
195    QUEUE_DEL(parser, parser->tokens);
196    STACK_DEL(parser, parser->indents);
197    STACK_DEL(parser, parser->simple_keys);
198    STACK_DEL(parser, parser->states);
199    STACK_DEL(parser, parser->marks);
200    STACK_DEL(parser, parser->tag_directives);
[185]201
[208]202    return 0;
[178]203}
204
205/*
206 * Destroy a parser object.
207 */
208
[183]209YAML_DECLARE(void)
[178]210yaml_parser_delete(yaml_parser_t *parser)
211{
[179]212    assert(parser); /* Non-NULL parser object expected. */
213
[208]214    BUFFER_DEL(parser, parser->raw_buffer);
215    BUFFER_DEL(parser, parser->buffer);
216    while (!QUEUE_EMPTY(parser, parser->tokens)) {
217        yaml_token_delete(&DEQUEUE(parser, parser->tokens));
218    }
219    QUEUE_DEL(parser, parser->tokens);
220    STACK_DEL(parser, parser->indents);
221    STACK_DEL(parser, parser->simple_keys);
222    STACK_DEL(parser, parser->states);
223    STACK_DEL(parser, parser->marks);
224    while (!STACK_EMPTY(parser, parser->tag_directives)) {
225        yaml_tag_directive_t tag_directive = POP(parser, parser->tag_directives);
226        yaml_free(tag_directive.handle);
227        yaml_free(tag_directive.prefix);
228    }
229    STACK_DEL(parser, parser->tag_directives);
[179]230
231    memset(parser, 0, sizeof(yaml_parser_t));
[178]232}
233
[179]234/*
[180]235 * String read handler.
[179]236 */
237
238static int
239yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
240        size_t *size_read)
241{
[208]242    yaml_parser_t *parser = data;
[180]243
[208]244    if (parser->input.string.current == parser->input.string.end) {
[180]245        *size_read = 0;
246        return 1;
247    }
248
[208]249    if (size > (parser->input.string.end - parser->input.string.current)) {
250        size = parser->input.string.end - parser->input.string.current;
[180]251    }
252
[208]253    memcpy(buffer, parser->input.string.current, size);
254    parser->input.string.current += size;
[180]255    *size_read = size;
[179]256    return 1;
257}
258
259/*
260 * File read handler.
261 */
262
263static int
264yaml_file_read_handler(void *data, unsigned char *buffer, size_t size,
265        size_t *size_read)
266{
[208]267    yaml_parser_t *parser = data;
268
269    *size_read = fread(buffer, 1, size, parser->input.file);
270    return !ferror(parser->input.file);
[179]271}
272
273/*
274 * Set a string input.
275 */
276
[183]277YAML_DECLARE(void)
[179]278yaml_parser_set_input_string(yaml_parser_t *parser,
279        unsigned char *input, size_t size)
280{
281    assert(parser); /* Non-NULL parser object expected. */
[180]282    assert(!parser->read_handler);  /* You can set the source only once. */
[179]283    assert(input);  /* Non-NULL input string expected. */
284
285    parser->read_handler = yaml_string_read_handler;
[208]286    parser->read_handler_data = parser;
287
288    parser->input.string.start = input;
289    parser->input.string.current = input;
290    parser->input.string.end = input+size;
[179]291}
292
293/*
294 * Set a file input.
295 */
296
[183]297YAML_DECLARE(void)
[179]298yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
299{
300    assert(parser); /* Non-NULL parser object expected. */
[180]301    assert(!parser->read_handler);  /* You can set the source only once. */
[179]302    assert(file);   /* Non-NULL file object expected. */
303
304    parser->read_handler = yaml_file_read_handler;
[208]305    parser->read_handler_data = parser;
306
307    parser->input.file = file;
[179]308}
309
310/*
311 * Set a generic input.
312 */
313
[183]314YAML_DECLARE(void)
[179]315yaml_parser_set_input(yaml_parser_t *parser,
316        yaml_read_handler_t *handler, void *data)
317{
318    assert(parser); /* Non-NULL parser object expected. */
[180]319    assert(!parser->read_handler);  /* You can set the source only once. */
[179]320    assert(handler);    /* Non-NULL read handler expected. */
321
322    parser->read_handler = handler;
[180]323    parser->read_handler_data = data;
[179]324}
325
326/*
327 * Set the source encoding.
328 */
329
[183]330YAML_DECLARE(void)
[179]331yaml_parser_set_encoding(yaml_parser_t *parser, yaml_encoding_t encoding)
332{
333    assert(parser); /* Non-NULL parser object expected. */
334    assert(!parser->encoding); /* Encoding is already set or detected. */
335
336    parser->encoding = encoding;
337}
338
[183]339/*
[211]340 * Create a new emitter object.
341 */
342
343YAML_DECLARE(int)
344yaml_emitter_initialize(yaml_emitter_t *emitter)
345{
346    assert(emitter);    /* Non-NULL emitter object expected. */
347
348    memset(emitter, 0, sizeof(yaml_emitter_t));
349    if (!BUFFER_INIT(emitter, emitter->buffer, OUTPUT_BUFFER_SIZE))
350        goto error;
351    if (!BUFFER_INIT(emitter, emitter->raw_buffer, OUTPUT_RAW_BUFFER_SIZE))
352        goto error;
353    if (!STACK_INIT(emitter, emitter->states, INITIAL_STACK_SIZE))
354        goto error;
355    if (!QUEUE_INIT(emitter, emitter->events, INITIAL_QUEUE_SIZE))
356        goto error;
357    if (!STACK_INIT(emitter, emitter->indents, INITIAL_STACK_SIZE))
358        goto error;
359    if (!STACK_INIT(emitter, emitter->tag_directives, INITIAL_STACK_SIZE))
360        goto error;
361
362    return 1;
363
364error:
365
366    BUFFER_DEL(emitter, emitter->buffer);
367    BUFFER_DEL(emitter, emitter->raw_buffer);
368    STACK_DEL(emitter, emitter->states);
369    QUEUE_DEL(emitter, emitter->events);
370    STACK_DEL(emitter, emitter->indents);
371    STACK_DEL(emitter, emitter->tag_directives);
372
373    return 0;
374}
375
376/*
377 * Destroy an emitter object.
378 */
379
380YAML_DECLARE(void)
381yaml_emitter_delete(yaml_emitter_t *emitter)
382{
383    assert(emitter);    /* Non-NULL emitter object expected. */
384
385    BUFFER_DEL(emitter, emitter->buffer);
386    BUFFER_DEL(emitter, emitter->raw_buffer);
387    STACK_DEL(emitter, emitter->states);
388    while (!QUEUE_EMPTY(emitter, emitter->events)) {
389        yaml_event_delete(&DEQUEUE(emitter, emitter->events));
390    }
391    STACK_DEL(emitter, emitter->indents);
392    while (!STACK_EMPTY(empty, emitter->tag_directives)) {
393        yaml_tag_directive_t tag_directive = POP(emitter, emitter->tag_directives);
394        yaml_free(tag_directive.handle);
395        yaml_free(tag_directive.prefix);
396    }
397    STACK_DEL(emitter, emitter->tag_directives);
398
399    memset(emitter, 0, sizeof(yaml_emitter_t));
400}
401
402/*
403 * String write handler.
404 */
405
406static int
407yaml_string_write_handler(void *data, unsigned char *buffer, size_t size)
408{
409    yaml_emitter_t *emitter = data;
410
411    if (emitter->output.string.size + *emitter->output.string.size_written
412            < size) {
413        memcpy(emitter->output.string.buffer
414                + *emitter->output.string.size_written,
415                buffer,
416                emitter->output.string.size
417                - *emitter->output.string.size_written);
418        *emitter->output.string.size_written = emitter->output.string.size;
419        return 0;
420    }
421
422    memcpy(emitter->output.string.buffer
423            + *emitter->output.string.size_written, buffer, size);
424    *emitter->output.string.size_written += size;
425    return 1;
426}
427
428/*
429 * File write handler.
430 */
431
432static int
433yaml_file_write_handler(void *data, unsigned char *buffer, size_t size)
434{
435    yaml_emitter_t *emitter = data;
436
437    return (fwrite(buffer, 1, size, emitter->output.file) == size);
438}
439/*
440 * Set a string output.
441 */
442
443YAML_DECLARE(void)
444yaml_emitter_set_output_string(yaml_emitter_t *emitter,
445        unsigned char *output, size_t size, size_t *size_written)
446{
447    assert(emitter);    /* Non-NULL emitter object expected. */
448    assert(!emitter->write_handler);    /* You can set the output only once. */
449    assert(output);     /* Non-NULL output string expected. */
450
451    emitter->write_handler = yaml_string_write_handler;
452    emitter->write_handler_data = emitter;
453
454    emitter->output.string.buffer = output;
455    emitter->output.string.size = size;
456    emitter->output.string.size_written = size_written;
457    *size_written = 0;
458}
459
460/*
461 * Set a file output.
462 */
463
464YAML_DECLARE(void)
465yaml_emitter_set_output_file(yaml_emitter_t *emitter, FILE *file)
466{
467    assert(emitter);    /* Non-NULL emitter object expected. */
468    assert(!emitter->write_handler);    /* You can set the output only once. */
469    assert(file);       /* Non-NULL file object expected. */
470
471    emitter->write_handler = yaml_file_write_handler;
472    emitter->write_handler_data = emitter;
473
474    emitter->output.file = file;
475}
476
477/*
478 * Set a generic output handler.
479 */
480
481YAML_DECLARE(void)
482yaml_emitter_set_output(yaml_emitter_t *emitter,
483        yaml_write_handler_t *handler, void *data)
484{
485    assert(emitter);    /* Non-NULL emitter object expected. */
486    assert(!emitter->write_handler);    /* You can set the output only once. */
487    assert(handler);    /* Non-NULL handler object expected. */
488
489    emitter->write_handler = handler;
490    emitter->write_handler_data = data;
491}
492
493/*
494 * Set the output encoding.
495 */
496
497YAML_DECLARE(void)
498yaml_emitter_set_encoding(yaml_emitter_t *emitter, yaml_encoding_t encoding)
499{
500    assert(emitter);    /* Non-NULL emitter object expected. */
501    assert(!emitter->encoding);     /* You can set encoding only once. */
502
503    emitter->encoding = encoding;
504}
505
506/*
507 * Set the canonical output style.
508 */
509
510YAML_DECLARE(void)
511yaml_emitter_set_canonical(yaml_emitter_t *emitter, int canonical)
512{
513    assert(emitter);    /* Non-NULL emitter object expected. */
514
515    emitter->canonical = (canonical != 0);
516}
517
518/*
519 * Set the indentation increment.
520 */
521
522YAML_DECLARE(void)
523yaml_emitter_set_indent(yaml_emitter_t *emitter, int indent)
524{
525    assert(emitter);    /* Non-NULL emitter object expected. */
526
527    emitter->best_indent = (1 < indent && indent < 10) ? indent : 2;
528}
529
530/*
531 * Set the preferred line width.
532 */
533
534YAML_DECLARE(void)
535yaml_emitter_set_width(yaml_emitter_t *emitter, int width)
536{
537    assert(emitter);    /* Non-NULL emitter object expected. */
538
539    emitter->best_width = (width > 0) ? width : 0;
540}
541
542/*
543 * Set if unescaped non-ASCII characters are allowed.
544 */
545
546YAML_DECLARE(void)
547yaml_emitter_set_unicode(yaml_emitter_t *emitter, int unicode)
548{
549    assert(emitter);    /* Non-NULL emitter object expected. */
550
551    emitter->unicode = (unicode != 0);
552}
553
554/*
555 * Set the preferred line break character.
556 */
557
558YAML_DECLARE(void)
559yaml_emitter_set_break(yaml_emitter_t *emitter, yaml_break_t line_break)
560{
561    assert(emitter);    /* Non-NULL emitter object expected. */
562
563    emitter->line_break = line_break;
564}
565
566/*
[183]567 * Destroy a token object.
568 */
569
570YAML_DECLARE(void)
571yaml_token_delete(yaml_token_t *token)
572{
573    assert(token);  /* Non-NULL token object expected. */
574
575    switch (token->type)
576    {
577        case YAML_TAG_DIRECTIVE_TOKEN:
578            yaml_free(token->data.tag_directive.handle);
579            yaml_free(token->data.tag_directive.prefix);
580            break;
581
582        case YAML_ALIAS_TOKEN:
[199]583            yaml_free(token->data.alias.value);
584            break;
585
[183]586        case YAML_ANCHOR_TOKEN:
[199]587            yaml_free(token->data.anchor.value);
[183]588            break;
589
590        case YAML_TAG_TOKEN:
591            yaml_free(token->data.tag.handle);
592            yaml_free(token->data.tag.suffix);
593            break;
594
595        case YAML_SCALAR_TOKEN:
596            yaml_free(token->data.scalar.value);
597            break;
[210]598
599        default:
600            break;
[183]601    }
602
603    memset(token, 0, sizeof(yaml_token_t));
604}
605
[199]606/*
607 * Destroy an event object.
608 */
609
610YAML_DECLARE(void)
611yaml_event_delete(yaml_event_t *event)
612{
[208]613    yaml_tag_directive_t *tag_directive;
614
[199]615    assert(event);  /* Non-NULL event object expected. */
616
617    switch (event->type)
618    {
619        case YAML_DOCUMENT_START_EVENT:
[208]620            yaml_free(event->data.document_start.version_directive);
621            for (tag_directive = event->data.document_start.tag_directives.start;
622                    tag_directive != event->data.document_start.tag_directives.end;
623                    tag_directive++) {
624                yaml_free(tag_directive->handle);
625                yaml_free(tag_directive->prefix);
626            }
627            yaml_free(event->data.document_start.tag_directives.start);
[199]628            break;
629
630        case YAML_ALIAS_EVENT:
631            yaml_free(event->data.alias.anchor);
632            break;
633
634        case YAML_SCALAR_EVENT:
635            yaml_free(event->data.scalar.anchor);
636            yaml_free(event->data.scalar.tag);
637            yaml_free(event->data.scalar.value);
638            break;
639
640        case YAML_SEQUENCE_START_EVENT:
641            yaml_free(event->data.sequence_start.anchor);
642            yaml_free(event->data.sequence_start.tag);
643            break;
644
645        case YAML_MAPPING_START_EVENT:
646            yaml_free(event->data.mapping_start.anchor);
647            yaml_free(event->data.mapping_start.tag);
648            break;
[210]649
650        default:
651            break;
[199]652    }
653
654    memset(event, 0, sizeof(yaml_event_t));
655}
656
Note: See TracBrowser for help on using the repository browser.