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

Revision 212, 15.8 KB checked in by xi, 8 years ago (diff)

Add yaml_emitter_emit_* set of functions.

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
[212]60YAML_DECLARE(yaml_char_t *)
61yaml_strdup(const yaml_char_t *str)
[178]62{
[212]63    if (!str)
64        return NULL;
65
66    return (yaml_char_t *)strdup((char *)str);
[208]67}
[178]68
[208]69/*
70 * Extend a string.
71 */
[180]72
[208]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);
[178]78
[208]79    if (!new_start) return 0;
[178]80
[208]81    memset(new_start + (*end - *start), 0, *end - *start);
[180]82
[208]83    *pointer = new_start + (*pointer - *start);
84    *end = new_start + (*end - *start)*2;
85    *start = new_start;
[185]86
[208]87    return 1;
88}
[180]89
[208]90/*
91 * Append a string B to a string A.
92 */
[180]93
[208]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;
[185]101
[208]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    }
[180]106
[208]107    memcpy(*a_pointer, *b_start, *b_pointer - *b_start);
108    *a_pointer += *b_pointer - *b_start;
[185]109
[208]110    return 1;
111}
[185]112
[208]113/*
114 * Extend a stack.
115 */
[185]116
[208]117YAML_DECLARE(int)
118yaml_stack_extend(void **start, void **top, void **end)
119{
120    void *new_start = yaml_realloc(*start, (*end - *start)*2);
[185]121
[208]122    if (!new_start) return 0;
[185]123
[208]124    *top = new_start + (*top - *start);
125    *end = new_start + (*end - *start)*2;
126    *start = new_start;
[185]127
[208]128    return 1;
129}
[185]130
[208]131/*
132 * Extend or move a queue.
133 */
[185]134
[208]135YAML_DECLARE(int)
136yaml_queue_extend(void **start, void **head, void **tail, void **end)
137{
138    /* Check if we need to resize the queue. */
[185]139
[208]140    if (*start == *head && *tail == *end) {
141        void *new_start = yaml_realloc(*start, (*end - *start)*2);
[201]142
[208]143        if (!new_start) return 0;
[201]144
[208]145        *head = new_start + (*head - *start);
146        *tail = new_start + (*tail - *start);
147        *end = new_start + (*end - *start)*2;
148        *start = new_start;
149    }
[201]150
[208]151    /* Check if we need to move the queue at the beginning of the buffer. */
[201]152
[208]153    if (*tail == *end) {
154        if (*head != *tail) {
155            memmove(*start, *head, *tail - *head);
156        }
157        *tail -= *head - *start;
158        *head = *start;
159    }
[201]160
[208]161    return 1;
162}
[203]163
164
[208]165/*
166 * Create a new parser object.
167 */
[203]168
[208]169YAML_DECLARE(int)
170yaml_parser_initialize(yaml_parser_t *parser)
171{
172    assert(parser);     /* Non-NULL parser object expected. */
[201]173
[208]174    memset(parser, 0, sizeof(yaml_parser_t));
[211]175    if (!BUFFER_INIT(parser, parser->raw_buffer, INPUT_RAW_BUFFER_SIZE))
[208]176        goto error;
[211]177    if (!BUFFER_INIT(parser, parser->buffer, INPUT_BUFFER_SIZE))
[208]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;
[201]191
[208]192    return 1;
[201]193
[185]194error:
195
[208]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);
[185]204
[208]205    return 0;
[178]206}
207
208/*
209 * Destroy a parser object.
210 */
211
[183]212YAML_DECLARE(void)
[178]213yaml_parser_delete(yaml_parser_t *parser)
214{
[179]215    assert(parser); /* Non-NULL parser object expected. */
216
[208]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);
[179]233
234    memset(parser, 0, sizeof(yaml_parser_t));
[178]235}
236
[179]237/*
[180]238 * String read handler.
[179]239 */
240
241static int
242yaml_string_read_handler(void *data, unsigned char *buffer, size_t size,
243        size_t *size_read)
244{
[208]245    yaml_parser_t *parser = data;
[180]246
[208]247    if (parser->input.string.current == parser->input.string.end) {
[180]248        *size_read = 0;
249        return 1;
250    }
251
[208]252    if (size > (parser->input.string.end - parser->input.string.current)) {
253        size = parser->input.string.end - parser->input.string.current;
[180]254    }
255
[208]256    memcpy(buffer, parser->input.string.current, size);
257    parser->input.string.current += size;
[180]258    *size_read = size;
[179]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{
[208]270    yaml_parser_t *parser = data;
271
272    *size_read = fread(buffer, 1, size, parser->input.file);
273    return !ferror(parser->input.file);
[179]274}
275
276/*
277 * Set a string input.
278 */
279
[183]280YAML_DECLARE(void)
[179]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. */
[180]285    assert(!parser->read_handler);  /* You can set the source only once. */
[179]286    assert(input);  /* Non-NULL input string expected. */
287
288    parser->read_handler = yaml_string_read_handler;
[208]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;
[179]294}
295
296/*
297 * Set a file input.
298 */
299
[183]300YAML_DECLARE(void)
[179]301yaml_parser_set_input_file(yaml_parser_t *parser, FILE *file)
302{
303    assert(parser); /* Non-NULL parser object expected. */
[180]304    assert(!parser->read_handler);  /* You can set the source only once. */
[179]305    assert(file);   /* Non-NULL file object expected. */
306
307    parser->read_handler = yaml_file_read_handler;
[208]308    parser->read_handler_data = parser;
309
310    parser->input.file = file;
[179]311}
312
313/*
314 * Set a generic input.
315 */
316
[183]317YAML_DECLARE(void)
[179]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. */
[180]322    assert(!parser->read_handler);  /* You can set the source only once. */
[179]323    assert(handler);    /* Non-NULL read handler expected. */
324
325    parser->read_handler = handler;
[180]326    parser->read_handler_data = data;
[179]327}
328
329/*
330 * Set the source encoding.
331 */
332
[183]333YAML_DECLARE(void)
[179]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;
340}
341
[183]342/*
[211]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    }
394    STACK_DEL(emitter, emitter->indents);
[212]395    yaml_event_delete(&emitter->event);
[211]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
[212]543    emitter->best_width = (width >= 0) ? width : -1;
[211]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
570/*
[183]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:
[199]587            yaml_free(token->data.alias.value);
588            break;
589
[183]590        case YAML_ANCHOR_TOKEN:
[199]591            yaml_free(token->data.anchor.value);
[183]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;
[210]602
603        default:
604            break;
[183]605    }
606
607    memset(token, 0, sizeof(yaml_token_t));
608}
609
[199]610/*
611 * Destroy an event object.
612 */
613
614YAML_DECLARE(void)
615yaml_event_delete(yaml_event_t *event)
616{
[208]617    yaml_tag_directive_t *tag_directive;
618
[199]619    assert(event);  /* Non-NULL event object expected. */
620
621    switch (event->type)
622    {
623        case YAML_DOCUMENT_START_EVENT:
[208]624            yaml_free(event->data.document_start.version_directive);
625            for (tag_directive = event->data.document_start.tag_directives.start;
626                    tag_directive != event->data.document_start.tag_directives.end;
627                    tag_directive++) {
628                yaml_free(tag_directive->handle);
629                yaml_free(tag_directive->prefix);
630            }
631            yaml_free(event->data.document_start.tag_directives.start);
[199]632            break;
633
634        case YAML_ALIAS_EVENT:
635            yaml_free(event->data.alias.anchor);
636            break;
637
638        case YAML_SCALAR_EVENT:
639            yaml_free(event->data.scalar.anchor);
640            yaml_free(event->data.scalar.tag);
641            yaml_free(event->data.scalar.value);
642            break;
643
644        case YAML_SEQUENCE_START_EVENT:
645            yaml_free(event->data.sequence_start.anchor);
646            yaml_free(event->data.sequence_start.tag);
647            break;
648
649        case YAML_MAPPING_START_EVENT:
650            yaml_free(event->data.mapping_start.anchor);
651            yaml_free(event->data.mapping_start.tag);
652            break;
[210]653
654        default:
655            break;
[199]656    }
657
658    memset(event, 0, sizeof(yaml_event_t));
659}
660
Note: See TracBrowser for help on using the repository browser.