source: trunk/packages/libyaml/tests/example-deconstructor.c @ 898

Last change on this file since 898 was 898, checked in by hartmans, 16 years ago

Add pyyaml and libyaml packages
backported from lenny.
There is discussion about how these should go in the repository; these are added in this form
in order to make forward progress.

File size: 43.0 KB
Line 
1
2#include <yaml.h>
3
4#include <stdlib.h>
5#include <stdio.h>
6
7int
8main(int argc, char *argv[])
9{
10    int help = 0;
11    int canonical = 0;
12    int unicode = 0;
13    int k;
14    int done = 0;
15
16    yaml_parser_t parser;
17    yaml_emitter_t emitter;
18    yaml_event_t input_event;
19    yaml_event_t output_event;
20
21    /* Clear the objects. */
22
23    memset(&parser, 0, sizeof(parser));
24    memset(&emitter, 0, sizeof(emitter));
25    memset(&input_event, 0, sizeof(input_event));
26    memset(&output_event, 0, sizeof(output_event));
27
28    /* Analyze command line options. */
29
30    for (k = 1; k < argc; k ++)
31    {
32        if (strcmp(argv[k], "-h") == 0
33                || strcmp(argv[k], "--help") == 0) {
34            help = 1;
35        }
36
37        else if (strcmp(argv[k], "-c") == 0
38                || strcmp(argv[k], "--canonical") == 0) {
39            canonical = 1;
40        }
41
42        else if (strcmp(argv[k], "-u") == 0
43                || strcmp(argv[k], "--unicode") == 0) {
44            unicode = 1;
45        }
46
47        else {
48            fprintf(stderr, "Unrecognized option: %s\n"
49                    "Try `%s --help` for more information.\n",
50                    argv[k], argv[0]);
51            return 1;
52        }
53    }
54
55    /* Display the help string. */
56
57    if (help)
58    {
59        printf("%s <input\n"
60                "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n"
61                "-h, --help\t\tdisplay this help and exit\n"
62                "-c, --canonical\t\toutput in the canonical YAML format\n"
63                "-u, --unicode\t\toutput unescaped non-ASCII characters\n",
64                argv[0], argv[0]);
65        return 0;
66    }
67
68    /* Initialize the parser and emitter objects. */
69
70    if (!yaml_parser_initialize(&parser)) {
71        fprintf(stderr, "Could not initialize the parser object\n");
72        return 1;
73    }
74
75    if (!yaml_emitter_initialize(&emitter)) {
76        yaml_parser_delete(&parser);
77        fprintf(stderr, "Could not inialize the emitter object\n");
78        return 1;
79    }
80
81    /* Set the parser parameters. */
82
83    yaml_parser_set_input_file(&parser, stdin);
84
85    /* Set the emitter parameters. */
86
87    yaml_emitter_set_output_file(&emitter, stdout);
88
89    yaml_emitter_set_canonical(&emitter, canonical);
90    yaml_emitter_set_unicode(&emitter, unicode);
91
92    /* Create and emit the STREAM-START event. */
93
94    if (!yaml_stream_start_event_initialize(&output_event, YAML_UTF8_ENCODING))
95        goto event_error;
96    if (!yaml_emitter_emit(&emitter, &output_event))
97        goto emitter_error;
98
99    /* Create and emit the DOCUMENT-START event. */
100
101    if (!yaml_document_start_event_initialize(&output_event,
102                NULL, NULL, NULL, 0))
103        goto event_error;
104    if (!yaml_emitter_emit(&emitter, &output_event))
105        goto emitter_error;
106
107    /* Create and emit the SEQUENCE-START event. */
108
109    if (!yaml_sequence_start_event_initialize(&output_event,
110                NULL, "tag:yaml.org,2002:seq", 1,
111                YAML_BLOCK_SEQUENCE_STYLE))
112        goto event_error;
113    if (!yaml_emitter_emit(&emitter, &output_event))
114        goto emitter_error;
115
116    /* Loop through the input events. */
117
118    while (!done)
119    {
120        /* Get the next event. */
121
122        if (!yaml_parser_parse(&parser, &input_event))
123            goto parser_error;
124
125        /* Check if this is the stream end. */
126
127        if (input_event.type == YAML_STREAM_END_EVENT) {
128            done = 1;
129        }
130
131        /* Create and emit a MAPPING-START event. */
132
133        if (!yaml_mapping_start_event_initialize(&output_event,
134                    NULL, "tag:yaml.org,2002:map", 1,
135                    YAML_BLOCK_MAPPING_STYLE))
136            goto event_error;
137        if (!yaml_emitter_emit(&emitter, &output_event))
138            goto emitter_error;
139
140        /* Analyze the event. */
141
142        switch (input_event.type)
143        {
144            case YAML_STREAM_START_EVENT:
145
146                /* Write 'type'. */
147
148                if (!yaml_scalar_event_initialize(&output_event,
149                            NULL, "tag:yaml.org,2002:str", "type", -1,
150                            1, 1, YAML_PLAIN_SCALAR_STYLE))
151                    goto event_error;
152                if (!yaml_emitter_emit(&emitter, &output_event))
153                    goto emitter_error;
154
155                /* Write 'STREAM-START'. */
156
157                if (!yaml_scalar_event_initialize(&output_event,
158                            NULL, "tag:yaml.org,2002:str", "STREAM-START", -1,
159                            1, 1, YAML_PLAIN_SCALAR_STYLE))
160                    goto event_error;
161                if (!yaml_emitter_emit(&emitter, &output_event))
162                    goto emitter_error;
163
164                /* Display encoding information. */
165
166                if (input_event.data.stream_start.encoding)
167                {
168                    yaml_encoding_t encoding
169                        = input_event.data.stream_start.encoding;
170
171                    /* Write 'encoding'. */
172
173                    if (!yaml_scalar_event_initialize(&output_event,
174                                NULL, "tag:yaml.org,2002:str", "encoding", -1,
175                                1, 1, YAML_PLAIN_SCALAR_STYLE))
176                        goto event_error;
177                    if (!yaml_emitter_emit(&emitter, &output_event))
178                        goto emitter_error;
179
180                    /* Write the stream encoding. */
181
182                    if (!yaml_scalar_event_initialize(&output_event,
183                                NULL, "tag:yaml.org,2002:str",
184                                (encoding == YAML_UTF8_ENCODING ? "utf-8" :
185                                 encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" :
186                                 encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" :
187                                 "unknown"), -1,
188                                1, 1, YAML_PLAIN_SCALAR_STYLE))
189                        goto event_error;
190                    if (!yaml_emitter_emit(&emitter, &output_event))
191                        goto emitter_error;
192                }
193
194                break;
195
196            case YAML_STREAM_END_EVENT:
197
198                /* Write 'type'. */
199
200                if (!yaml_scalar_event_initialize(&output_event,
201                            NULL, "tag:yaml.org,2002:str", "type", -1,
202                            1, 1, YAML_PLAIN_SCALAR_STYLE))
203                    goto event_error;
204                if (!yaml_emitter_emit(&emitter, &output_event))
205                    goto emitter_error;
206
207                /* Write 'STREAM-END'. */
208
209                if (!yaml_scalar_event_initialize(&output_event,
210                            NULL, "tag:yaml.org,2002:str", "STREAM-END", -1,
211                            1, 1, YAML_PLAIN_SCALAR_STYLE))
212                    goto event_error;
213                if (!yaml_emitter_emit(&emitter, &output_event))
214                    goto emitter_error;
215
216                break;
217
218            case YAML_DOCUMENT_START_EVENT:
219
220                /* Write 'type'. */
221
222                if (!yaml_scalar_event_initialize(&output_event,
223                            NULL, "tag:yaml.org,2002:str", "type", -1,
224                            1, 1, YAML_PLAIN_SCALAR_STYLE))
225                    goto event_error;
226                if (!yaml_emitter_emit(&emitter, &output_event))
227                    goto emitter_error;
228
229                /* Write 'DOCUMENT-START'. */
230
231                if (!yaml_scalar_event_initialize(&output_event,
232                            NULL, "tag:yaml.org,2002:str", "DOCUMENT-START", -1,
233                            1, 1, YAML_PLAIN_SCALAR_STYLE))
234                    goto event_error;
235                if (!yaml_emitter_emit(&emitter, &output_event))
236                    goto emitter_error;
237
238                /* Display the document version numbers. */
239
240                if (input_event.data.document_start.version_directive)
241                {
242                    yaml_version_directive_t *version
243                        = input_event.data.document_start.version_directive;
244                    char number[64];
245
246                    /* Write 'version'. */
247                   
248                    if (!yaml_scalar_event_initialize(&output_event,
249                                NULL, "tag:yaml.org,2002:str", "version", -1,
250                                1, 1, YAML_PLAIN_SCALAR_STYLE))
251                        goto event_error;
252                    if (!yaml_emitter_emit(&emitter, &output_event))
253                        goto emitter_error;
254
255                    /* Write '{'. */
256
257                    if (!yaml_mapping_start_event_initialize(&output_event,
258                                NULL, "tag:yaml.org,2002:map", 1,
259                                YAML_FLOW_MAPPING_STYLE))
260                        goto event_error;
261                    if (!yaml_emitter_emit(&emitter, &output_event))
262                        goto emitter_error;
263
264                    /* Write 'major'. */
265
266                    if (!yaml_scalar_event_initialize(&output_event,
267                                NULL, "tag:yaml.org,2002:str", "major", -1,
268                                1, 1, YAML_PLAIN_SCALAR_STYLE))
269                        goto event_error;
270                    if (!yaml_emitter_emit(&emitter, &output_event))
271                        goto emitter_error;
272
273                    /* Write a number. */
274
275                    sprintf(number, "%d", version->major);
276                    if (!yaml_scalar_event_initialize(&output_event,
277                                NULL, "tag:yaml.org,2002:int", number, -1,
278                                1, 1, YAML_PLAIN_SCALAR_STYLE))
279                        goto event_error;
280                    if (!yaml_emitter_emit(&emitter, &output_event))
281                        goto emitter_error;
282
283                    /* Write 'minor'. */
284
285                    if (!yaml_scalar_event_initialize(&output_event,
286                                NULL, "tag:yaml.org,2002:str", "minor", -1,
287                                1, 1, YAML_PLAIN_SCALAR_STYLE))
288                        goto event_error;
289                    if (!yaml_emitter_emit(&emitter, &output_event))
290                        goto emitter_error;
291
292                    /* Write a number. */
293
294                    sprintf(number, "%d", version->minor);
295                    if (!yaml_scalar_event_initialize(&output_event,
296                                NULL, "tag:yaml.org,2002:int", number, -1,
297                                1, 1, YAML_PLAIN_SCALAR_STYLE))
298                        goto event_error;
299                    if (!yaml_emitter_emit(&emitter, &output_event))
300                        goto emitter_error;
301
302                    /* Write '}'. */
303
304                    if (!yaml_mapping_end_event_initialize(&output_event))
305                        goto event_error;
306                    if (!yaml_emitter_emit(&emitter, &output_event))
307                        goto emitter_error;
308                }
309
310                /* Display the document tag directives. */
311
312                if (input_event.data.document_start.tag_directives.start
313                        != input_event.data.document_start.tag_directives.end)
314                {
315                    yaml_tag_directive_t *tag;
316
317                    /* Write 'tags'. */
318                   
319                    if (!yaml_scalar_event_initialize(&output_event,
320                                NULL, "tag:yaml.org,2002:str", "tags", -1,
321                                1, 1, YAML_PLAIN_SCALAR_STYLE))
322                        goto event_error;
323                    if (!yaml_emitter_emit(&emitter, &output_event))
324                        goto emitter_error;
325
326                    /* Start a block sequence. */
327
328                    if (!yaml_sequence_start_event_initialize(&output_event,
329                                NULL, "tag:yaml.org,2002:seq", 1,
330                                YAML_BLOCK_SEQUENCE_STYLE))
331                        goto event_error;
332                    if (!yaml_emitter_emit(&emitter, &output_event))
333                        goto emitter_error;
334
335                    for (tag = input_event.data.document_start.tag_directives.start;
336                            tag != input_event.data.document_start.tag_directives.end;
337                            tag ++)
338                    {
339                        /* Write '{'. */
340
341                        if (!yaml_mapping_start_event_initialize(&output_event,
342                                    NULL, "tag:yaml.org,2002:map", 1,
343                                    YAML_FLOW_MAPPING_STYLE))
344                            goto event_error;
345                        if (!yaml_emitter_emit(&emitter, &output_event))
346                            goto emitter_error;
347
348                        /* Write 'handle'. */
349
350                        if (!yaml_scalar_event_initialize(&output_event,
351                                    NULL, "tag:yaml.org,2002:str", "handle", -1,
352                                    1, 1, YAML_PLAIN_SCALAR_STYLE))
353                            goto event_error;
354                        if (!yaml_emitter_emit(&emitter, &output_event))
355                            goto emitter_error;
356
357                        /* Write the tag directive handle. */
358
359                        if (!yaml_scalar_event_initialize(&output_event,
360                                    NULL, "tag:yaml.org,2002:str",
361                                    tag->handle, -1,
362                                    0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
363                            goto event_error;
364                        if (!yaml_emitter_emit(&emitter, &output_event))
365                            goto emitter_error;
366
367                        /* Write 'prefix'. */
368
369                        if (!yaml_scalar_event_initialize(&output_event,
370                                    NULL, "tag:yaml.org,2002:str", "prefix", -1,
371                                    1, 1, YAML_PLAIN_SCALAR_STYLE))
372                            goto event_error;
373                        if (!yaml_emitter_emit(&emitter, &output_event))
374                            goto emitter_error;
375
376                        /* Write the tag directive prefix. */
377
378                        if (!yaml_scalar_event_initialize(&output_event,
379                                    NULL, "tag:yaml.org,2002:str",
380                                    tag->prefix, -1,
381                                    0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
382                            goto event_error;
383                        if (!yaml_emitter_emit(&emitter, &output_event))
384                            goto emitter_error;
385
386                        /* Write '}'. */
387
388                        if (!yaml_mapping_end_event_initialize(&output_event))
389                            goto event_error;
390                        if (!yaml_emitter_emit(&emitter, &output_event))
391                            goto emitter_error;
392                    }
393
394                    /* End a block sequence. */
395
396                    if (!yaml_sequence_end_event_initialize(&output_event))
397                        goto event_error;
398                    if (!yaml_emitter_emit(&emitter, &output_event))
399                        goto emitter_error;
400                }
401
402                /* Write 'implicit'. */
403
404                if (!yaml_scalar_event_initialize(&output_event,
405                            NULL, "tag:yaml.org,2002:str", "implicit", -1,
406                            1, 1, YAML_PLAIN_SCALAR_STYLE))
407                    goto event_error;
408                if (!yaml_emitter_emit(&emitter, &output_event))
409                    goto emitter_error;
410
411                /* Write if the document is implicit. */
412
413                if (!yaml_scalar_event_initialize(&output_event,
414                            NULL, "tag:yaml.org,2002:bool",
415                            (input_event.data.document_start.implicit ?
416                             "true" : "false"), -1,
417                            1, 0, YAML_PLAIN_SCALAR_STYLE))
418                    goto event_error;
419                if (!yaml_emitter_emit(&emitter, &output_event))
420                    goto emitter_error;
421
422                break;
423
424            case YAML_DOCUMENT_END_EVENT:
425
426                /* Write 'type'. */
427
428                if (!yaml_scalar_event_initialize(&output_event,
429                            NULL, "tag:yaml.org,2002:str", "type", -1,
430                            1, 1, YAML_PLAIN_SCALAR_STYLE))
431                    goto event_error;
432                if (!yaml_emitter_emit(&emitter, &output_event))
433                    goto emitter_error;
434
435                /* Write 'DOCUMENT-END'. */
436
437                if (!yaml_scalar_event_initialize(&output_event,
438                            NULL, "tag:yaml.org,2002:str", "DOCUMENT-END", -1,
439                            1, 1, YAML_PLAIN_SCALAR_STYLE))
440                    goto event_error;
441                if (!yaml_emitter_emit(&emitter, &output_event))
442                    goto emitter_error;
443
444                /* Write 'implicit'. */
445
446                if (!yaml_scalar_event_initialize(&output_event,
447                            NULL, "tag:yaml.org,2002:str", "implicit", -1,
448                            1, 1, YAML_PLAIN_SCALAR_STYLE))
449                    goto event_error;
450                if (!yaml_emitter_emit(&emitter, &output_event))
451                    goto emitter_error;
452
453                /* Write if the document is implicit. */
454
455                if (!yaml_scalar_event_initialize(&output_event,
456                            NULL, "tag:yaml.org,2002:bool",
457                            (input_event.data.document_end.implicit ?
458                             "true" : "false"), -1,
459                            1, 0, YAML_PLAIN_SCALAR_STYLE))
460                    goto event_error;
461                if (!yaml_emitter_emit(&emitter, &output_event))
462                    goto emitter_error;
463
464                break;
465
466            case YAML_ALIAS_EVENT:
467
468                /* Write 'type'. */
469
470                if (!yaml_scalar_event_initialize(&output_event,
471                            NULL, "tag:yaml.org,2002:str", "type", -1,
472                            1, 1, YAML_PLAIN_SCALAR_STYLE))
473                    goto event_error;
474                if (!yaml_emitter_emit(&emitter, &output_event))
475                    goto emitter_error;
476
477                /* Write 'ALIAS'. */
478
479                if (!yaml_scalar_event_initialize(&output_event,
480                            NULL, "tag:yaml.org,2002:str", "ALIAS", -1,
481                            1, 1, YAML_PLAIN_SCALAR_STYLE))
482                    goto event_error;
483                if (!yaml_emitter_emit(&emitter, &output_event))
484                    goto emitter_error;
485
486                /* Write 'anchor'. */
487
488                if (!yaml_scalar_event_initialize(&output_event,
489                            NULL, "tag:yaml.org,2002:str", "anchor", -1,
490                            1, 1, YAML_PLAIN_SCALAR_STYLE))
491                    goto event_error;
492                if (!yaml_emitter_emit(&emitter, &output_event))
493                    goto emitter_error;
494
495                /* Write the alias anchor. */
496
497                if (!yaml_scalar_event_initialize(&output_event,
498                            NULL, "tag:yaml.org,2002:str",
499                            input_event.data.alias.anchor, -1,
500                            0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
501                    goto event_error;
502                if (!yaml_emitter_emit(&emitter, &output_event))
503                    goto emitter_error;
504
505                break;
506
507            case YAML_SCALAR_EVENT:
508
509                /* Write 'type'. */
510
511                if (!yaml_scalar_event_initialize(&output_event,
512                            NULL, "tag:yaml.org,2002:str", "type", -1,
513                            1, 1, YAML_PLAIN_SCALAR_STYLE))
514                    goto event_error;
515                if (!yaml_emitter_emit(&emitter, &output_event))
516                    goto emitter_error;
517
518                /* Write 'SCALAR'. */
519
520                if (!yaml_scalar_event_initialize(&output_event,
521                            NULL, "tag:yaml.org,2002:str", "SCALAR", -1,
522                            1, 1, YAML_PLAIN_SCALAR_STYLE))
523                    goto event_error;
524                if (!yaml_emitter_emit(&emitter, &output_event))
525                    goto emitter_error;
526
527                /* Display the scalar anchor. */
528
529                if (input_event.data.scalar.anchor)
530                {
531                    /* Write 'anchor'. */
532
533                    if (!yaml_scalar_event_initialize(&output_event,
534                                NULL, "tag:yaml.org,2002:str", "anchor", -1,
535                                1, 1, YAML_PLAIN_SCALAR_STYLE))
536                        goto event_error;
537                    if (!yaml_emitter_emit(&emitter, &output_event))
538                        goto emitter_error;
539
540                    /* Write the scalar anchor. */
541
542                    if (!yaml_scalar_event_initialize(&output_event,
543                                NULL, "tag:yaml.org,2002:str",
544                                input_event.data.scalar.anchor, -1,
545                                0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
546                        goto event_error;
547                    if (!yaml_emitter_emit(&emitter, &output_event))
548                        goto emitter_error;
549                }
550
551                /* Display the scalar tag. */
552
553                if (input_event.data.scalar.tag)
554                {
555                    /* Write 'tag'. */
556
557                    if (!yaml_scalar_event_initialize(&output_event,
558                                NULL, "tag:yaml.org,2002:str", "tag", -1,
559                                1, 1, YAML_PLAIN_SCALAR_STYLE))
560                        goto event_error;
561                    if (!yaml_emitter_emit(&emitter, &output_event))
562                        goto emitter_error;
563
564                    /* Write the scalar tag. */
565
566                    if (!yaml_scalar_event_initialize(&output_event,
567                                NULL, "tag:yaml.org,2002:str",
568                                input_event.data.scalar.tag, -1,
569                                0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
570                        goto event_error;
571                    if (!yaml_emitter_emit(&emitter, &output_event))
572                        goto emitter_error;
573                }
574
575                /* Display the scalar value. */
576
577                /* Write 'value'. */
578
579                if (!yaml_scalar_event_initialize(&output_event,
580                            NULL, "tag:yaml.org,2002:str", "value", -1,
581                            1, 1, YAML_PLAIN_SCALAR_STYLE))
582                    goto event_error;
583                if (!yaml_emitter_emit(&emitter, &output_event))
584                    goto emitter_error;
585
586                /* Write the scalar value. */
587
588                if (!yaml_scalar_event_initialize(&output_event,
589                            NULL, "tag:yaml.org,2002:str",
590                            input_event.data.scalar.value,
591                            input_event.data.scalar.length,
592                            0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
593                    goto event_error;
594                if (!yaml_emitter_emit(&emitter, &output_event))
595                    goto emitter_error;
596
597                /* Display if the scalar tag is implicit. */
598
599                /* Write 'implicit'. */
600               
601                if (!yaml_scalar_event_initialize(&output_event,
602                            NULL, "tag:yaml.org,2002:str", "implicit", -1,
603                            1, 1, YAML_PLAIN_SCALAR_STYLE))
604                    goto event_error;
605                if (!yaml_emitter_emit(&emitter, &output_event))
606                    goto emitter_error;
607
608                /* Write '{'. */
609
610                if (!yaml_mapping_start_event_initialize(&output_event,
611                            NULL, "tag:yaml.org,2002:map", 1,
612                            YAML_FLOW_MAPPING_STYLE))
613                    goto event_error;
614                if (!yaml_emitter_emit(&emitter, &output_event))
615                    goto emitter_error;
616
617                /* Write 'plain'. */
618
619                if (!yaml_scalar_event_initialize(&output_event,
620                            NULL, "tag:yaml.org,2002:str", "plain", -1,
621                            1, 1, YAML_PLAIN_SCALAR_STYLE))
622                    goto event_error;
623                if (!yaml_emitter_emit(&emitter, &output_event))
624                    goto emitter_error;
625
626                /* Write if the scalar is implicit in the plain style. */
627
628                if (!yaml_scalar_event_initialize(&output_event,
629                            NULL, "tag:yaml.org,2002:bool",
630                            (input_event.data.scalar.plain_implicit ?
631                             "true" : "false"), -1,
632                            1, 0, YAML_PLAIN_SCALAR_STYLE))
633                    goto event_error;
634                if (!yaml_emitter_emit(&emitter, &output_event))
635                    goto emitter_error;
636
637                /* Write 'quoted'. */
638
639                if (!yaml_scalar_event_initialize(&output_event,
640                            NULL, "tag:yaml.org,2002:str", "non-plain", -1,
641                            1, 1, YAML_PLAIN_SCALAR_STYLE))
642                    goto event_error;
643                if (!yaml_emitter_emit(&emitter, &output_event))
644                    goto emitter_error;
645
646                /* Write if the scalar is implicit in a non-plain style. */
647
648                if (!yaml_scalar_event_initialize(&output_event,
649                            NULL, "tag:yaml.org,2002:bool",
650                            (input_event.data.scalar.quoted_implicit ?
651                             "true" : "false"), -1,
652                            1, 0, YAML_PLAIN_SCALAR_STYLE))
653                    goto event_error;
654                if (!yaml_emitter_emit(&emitter, &output_event))
655                    goto emitter_error;
656
657                /* Write '}'. */
658
659                if (!yaml_mapping_end_event_initialize(&output_event))
660                    goto event_error;
661                if (!yaml_emitter_emit(&emitter, &output_event))
662                    goto emitter_error;
663
664                /* Display the style information. */
665
666                if (input_event.data.scalar.style)
667                {
668                    yaml_scalar_style_t style = input_event.data.scalar.style;
669
670                    /* Write 'style'. */
671
672                    if (!yaml_scalar_event_initialize(&output_event,
673                                NULL, "tag:yaml.org,2002:str", "style", -1,
674                                1, 1, YAML_PLAIN_SCALAR_STYLE))
675                        goto event_error;
676                    if (!yaml_emitter_emit(&emitter, &output_event))
677                        goto emitter_error;
678
679                    /* Write the scalar style. */
680
681                    if (!yaml_scalar_event_initialize(&output_event,
682                                NULL, "tag:yaml.org,2002:str",
683                                (style == YAML_PLAIN_SCALAR_STYLE ? "plain" :
684                                 style == YAML_SINGLE_QUOTED_SCALAR_STYLE ?
685                                        "single-quoted" :
686                                 style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ?
687                                        "double-quoted" :
688                                 style == YAML_LITERAL_SCALAR_STYLE ? "literal" :
689                                 style == YAML_FOLDED_SCALAR_STYLE ? "folded" :
690                                 "unknown"), -1,
691                                1, 1, YAML_PLAIN_SCALAR_STYLE))
692                        goto event_error;
693                    if (!yaml_emitter_emit(&emitter, &output_event))
694                        goto emitter_error;
695                }
696
697                break;
698
699            case YAML_SEQUENCE_START_EVENT:
700
701                /* Write 'type'. */
702
703                if (!yaml_scalar_event_initialize(&output_event,
704                            NULL, "tag:yaml.org,2002:str", "type", -1,
705                            1, 1, YAML_PLAIN_SCALAR_STYLE))
706                    goto event_error;
707                if (!yaml_emitter_emit(&emitter, &output_event))
708                    goto emitter_error;
709
710                /* Write 'SEQUENCE-START'. */
711
712                if (!yaml_scalar_event_initialize(&output_event,
713                            NULL, "tag:yaml.org,2002:str", "SEQUENCE-START", -1,
714                            1, 1, YAML_PLAIN_SCALAR_STYLE))
715                    goto event_error;
716                if (!yaml_emitter_emit(&emitter, &output_event))
717                    goto emitter_error;
718
719                /* Display the sequence anchor. */
720
721                if (input_event.data.sequence_start.anchor)
722                {
723                    /* Write 'anchor'. */
724
725                    if (!yaml_scalar_event_initialize(&output_event,
726                                NULL, "tag:yaml.org,2002:str", "anchor", -1,
727                                1, 1, YAML_PLAIN_SCALAR_STYLE))
728                        goto event_error;
729                    if (!yaml_emitter_emit(&emitter, &output_event))
730                        goto emitter_error;
731
732                    /* Write the sequence anchor. */
733
734                    if (!yaml_scalar_event_initialize(&output_event,
735                                NULL, "tag:yaml.org,2002:str",
736                                input_event.data.sequence_start.anchor, -1,
737                                0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
738                        goto event_error;
739                    if (!yaml_emitter_emit(&emitter, &output_event))
740                        goto emitter_error;
741                }
742
743                /* Display the sequence tag. */
744
745                if (input_event.data.sequence_start.tag)
746                {
747                    /* Write 'tag'. */
748
749                    if (!yaml_scalar_event_initialize(&output_event,
750                                NULL, "tag:yaml.org,2002:str", "tag", -1,
751                                1, 1, YAML_PLAIN_SCALAR_STYLE))
752                        goto event_error;
753                    if (!yaml_emitter_emit(&emitter, &output_event))
754                        goto emitter_error;
755
756                    /* Write the sequence tag. */
757
758                    if (!yaml_scalar_event_initialize(&output_event,
759                                NULL, "tag:yaml.org,2002:str",
760                                input_event.data.sequence_start.tag, -1,
761                                0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
762                        goto event_error;
763                    if (!yaml_emitter_emit(&emitter, &output_event))
764                        goto emitter_error;
765                }
766
767                /* Write 'implicit'. */
768
769                if (!yaml_scalar_event_initialize(&output_event,
770                            NULL, "tag:yaml.org,2002:str", "implicit", -1,
771                            1, 1, YAML_PLAIN_SCALAR_STYLE))
772                    goto event_error;
773                if (!yaml_emitter_emit(&emitter, &output_event))
774                    goto emitter_error;
775
776                /* Write if the sequence tag is implicit. */
777
778                if (!yaml_scalar_event_initialize(&output_event,
779                            NULL, "tag:yaml.org,2002:bool",
780                            (input_event.data.sequence_start.implicit ?
781                             "true" : "false"), -1,
782                            1, 0, YAML_PLAIN_SCALAR_STYLE))
783                    goto event_error;
784                if (!yaml_emitter_emit(&emitter, &output_event))
785                    goto emitter_error;
786
787                /* Display the style information. */
788
789                if (input_event.data.sequence_start.style)
790                {
791                    yaml_sequence_style_t style
792                        = input_event.data.sequence_start.style;
793
794                    /* Write 'style'. */
795
796                    if (!yaml_scalar_event_initialize(&output_event,
797                                NULL, "tag:yaml.org,2002:str", "style", -1,
798                                1, 1, YAML_PLAIN_SCALAR_STYLE))
799                        goto event_error;
800                    if (!yaml_emitter_emit(&emitter, &output_event))
801                        goto emitter_error;
802
803                    /* Write the scalar style. */
804
805                    if (!yaml_scalar_event_initialize(&output_event,
806                                NULL, "tag:yaml.org,2002:str",
807                                (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" :
808                                 style == YAML_FLOW_SEQUENCE_STYLE ? "flow" :
809                                 "unknown"), -1,
810                                1, 1, YAML_PLAIN_SCALAR_STYLE))
811                        goto event_error;
812                    if (!yaml_emitter_emit(&emitter, &output_event))
813                        goto emitter_error;
814                }
815
816                break;
817
818            case YAML_SEQUENCE_END_EVENT:
819
820                /* Write 'type'. */
821
822                if (!yaml_scalar_event_initialize(&output_event,
823                            NULL, "tag:yaml.org,2002:str", "type", -1,
824                            1, 1, YAML_PLAIN_SCALAR_STYLE))
825                    goto event_error;
826                if (!yaml_emitter_emit(&emitter, &output_event))
827                    goto emitter_error;
828
829                /* Write 'SEQUENCE-END'. */
830
831                if (!yaml_scalar_event_initialize(&output_event,
832                            NULL, "tag:yaml.org,2002:str", "SEQUENCE-END", -1,
833                            1, 1, YAML_PLAIN_SCALAR_STYLE))
834                    goto event_error;
835                if (!yaml_emitter_emit(&emitter, &output_event))
836                    goto emitter_error;
837
838                break;
839
840            case YAML_MAPPING_START_EVENT:
841
842                /* Write 'type'. */
843
844                if (!yaml_scalar_event_initialize(&output_event,
845                            NULL, "tag:yaml.org,2002:str", "type", -1,
846                            1, 1, YAML_PLAIN_SCALAR_STYLE))
847                    goto event_error;
848                if (!yaml_emitter_emit(&emitter, &output_event))
849                    goto emitter_error;
850
851                /* Write 'MAPPING-START'. */
852
853                if (!yaml_scalar_event_initialize(&output_event,
854                            NULL, "tag:yaml.org,2002:str", "MAPPING-START", -1,
855                            1, 1, YAML_PLAIN_SCALAR_STYLE))
856                    goto event_error;
857                if (!yaml_emitter_emit(&emitter, &output_event))
858                    goto emitter_error;
859
860                /* Display the mapping anchor. */
861
862                if (input_event.data.mapping_start.anchor)
863                {
864                    /* Write 'anchor'. */
865
866                    if (!yaml_scalar_event_initialize(&output_event,
867                                NULL, "tag:yaml.org,2002:str", "anchor", -1,
868                                1, 1, YAML_PLAIN_SCALAR_STYLE))
869                        goto event_error;
870                    if (!yaml_emitter_emit(&emitter, &output_event))
871                        goto emitter_error;
872
873                    /* Write the mapping anchor. */
874
875                    if (!yaml_scalar_event_initialize(&output_event,
876                                NULL, "tag:yaml.org,2002:str",
877                                input_event.data.mapping_start.anchor, -1,
878                                0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
879                        goto event_error;
880                    if (!yaml_emitter_emit(&emitter, &output_event))
881                        goto emitter_error;
882                }
883
884                /* Display the mapping tag. */
885
886                if (input_event.data.mapping_start.tag)
887                {
888                    /* Write 'tag'. */
889
890                    if (!yaml_scalar_event_initialize(&output_event,
891                                NULL, "tag:yaml.org,2002:str", "tag", -1,
892                                1, 1, YAML_PLAIN_SCALAR_STYLE))
893                        goto event_error;
894                    if (!yaml_emitter_emit(&emitter, &output_event))
895                        goto emitter_error;
896
897                    /* Write the mapping tag. */
898
899                    if (!yaml_scalar_event_initialize(&output_event,
900                                NULL, "tag:yaml.org,2002:str",
901                                input_event.data.mapping_start.tag, -1,
902                                0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE))
903                        goto event_error;
904                    if (!yaml_emitter_emit(&emitter, &output_event))
905                        goto emitter_error;
906                }
907
908                /* Write 'implicit'. */
909
910                if (!yaml_scalar_event_initialize(&output_event,
911                            NULL, "tag:yaml.org,2002:str", "implicit", -1,
912                            1, 1, YAML_PLAIN_SCALAR_STYLE))
913                    goto event_error;
914                if (!yaml_emitter_emit(&emitter, &output_event))
915                    goto emitter_error;
916
917                /* Write if the mapping tag is implicit. */
918
919                if (!yaml_scalar_event_initialize(&output_event,
920                            NULL, "tag:yaml.org,2002:bool",
921                            (input_event.data.mapping_start.implicit ?
922                             "true" : "false"), -1,
923                            1, 0, YAML_PLAIN_SCALAR_STYLE))
924                    goto event_error;
925                if (!yaml_emitter_emit(&emitter, &output_event))
926                    goto emitter_error;
927
928                /* Display the style information. */
929
930                if (input_event.data.mapping_start.style)
931                {
932                    yaml_mapping_style_t style
933                        = input_event.data.mapping_start.style;
934
935                    /* Write 'style'. */
936
937                    if (!yaml_scalar_event_initialize(&output_event,
938                                NULL, "tag:yaml.org,2002:str", "style", -1,
939                                1, 1, YAML_PLAIN_SCALAR_STYLE))
940                        goto event_error;
941                    if (!yaml_emitter_emit(&emitter, &output_event))
942                        goto emitter_error;
943
944                    /* Write the scalar style. */
945
946                    if (!yaml_scalar_event_initialize(&output_event,
947                                NULL, "tag:yaml.org,2002:str",
948                                (style == YAML_BLOCK_MAPPING_STYLE ? "block" :
949                                 style == YAML_FLOW_MAPPING_STYLE ? "flow" :
950                                 "unknown"), -1,
951                                1, 1, YAML_PLAIN_SCALAR_STYLE))
952                        goto event_error;
953                    if (!yaml_emitter_emit(&emitter, &output_event))
954                        goto emitter_error;
955                }
956
957                break;
958
959            case YAML_MAPPING_END_EVENT:
960
961                /* Write 'type'. */
962
963                if (!yaml_scalar_event_initialize(&output_event,
964                            NULL, "tag:yaml.org,2002:str", "type", -1,
965                            1, 1, YAML_PLAIN_SCALAR_STYLE))
966                    goto event_error;
967                if (!yaml_emitter_emit(&emitter, &output_event))
968                    goto emitter_error;
969
970                /* Write 'MAPPING-END'. */
971
972                if (!yaml_scalar_event_initialize(&output_event,
973                            NULL, "tag:yaml.org,2002:str", "MAPPING-END", -1,
974                            1, 1, YAML_PLAIN_SCALAR_STYLE))
975                    goto event_error;
976                if (!yaml_emitter_emit(&emitter, &output_event))
977                    goto emitter_error;
978
979                break;
980
981            default:
982                /* It couldn't really happen. */
983                break;
984        }
985
986        /* Delete the event object. */
987
988        yaml_event_delete(&input_event);
989
990        /* Create and emit a MAPPING-END event. */
991
992        if (!yaml_mapping_end_event_initialize(&output_event))
993            goto event_error;
994        if (!yaml_emitter_emit(&emitter, &output_event))
995            goto emitter_error;
996    }
997
998    /* Create and emit the SEQUENCE-END event. */
999
1000    if (!yaml_sequence_end_event_initialize(&output_event))
1001        goto event_error;
1002    if (!yaml_emitter_emit(&emitter, &output_event))
1003        goto emitter_error;
1004
1005    /* Create and emit the DOCUMENT-END event. */
1006
1007    if (!yaml_document_end_event_initialize(&output_event, 0))
1008        goto event_error;
1009    if (!yaml_emitter_emit(&emitter, &output_event))
1010        goto emitter_error;
1011
1012    /* Create and emit the STREAM-END event. */
1013
1014    if (!yaml_stream_end_event_initialize(&output_event))
1015        goto event_error;
1016    if (!yaml_emitter_emit(&emitter, &output_event))
1017        goto emitter_error;
1018
1019    yaml_parser_delete(&parser);
1020    yaml_emitter_delete(&emitter);
1021
1022    return 0;
1023
1024parser_error:
1025
1026    /* Display a parser error message. */
1027
1028    switch (parser.error)
1029    {
1030        case YAML_MEMORY_ERROR:
1031            fprintf(stderr, "Memory error: Not enough memory for parsing\n");
1032            break;
1033
1034        case YAML_READER_ERROR:
1035            if (parser.problem_value != -1) {
1036                fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem,
1037                        parser.problem_value, parser.problem_offset);
1038            }
1039            else {
1040                fprintf(stderr, "Reader error: %s at %d\n", parser.problem,
1041                        parser.problem_offset);
1042            }
1043            break;
1044
1045        case YAML_SCANNER_ERROR:
1046            if (parser.context) {
1047                fprintf(stderr, "Scanner error: %s at line %d, column %d\n"
1048                        "%s at line %d, column %d\n", parser.context,
1049                        parser.context_mark.line+1, parser.context_mark.column+1,
1050                        parser.problem, parser.problem_mark.line+1,
1051                        parser.problem_mark.column+1);
1052            }
1053            else {
1054                fprintf(stderr, "Scanner error: %s at line %d, column %d\n",
1055                        parser.problem, parser.problem_mark.line+1,
1056                        parser.problem_mark.column+1);
1057            }
1058            break;
1059
1060        case YAML_PARSER_ERROR:
1061            if (parser.context) {
1062                fprintf(stderr, "Parser error: %s at line %d, column %d\n"
1063                        "%s at line %d, column %d\n", parser.context,
1064                        parser.context_mark.line+1, parser.context_mark.column+1,
1065                        parser.problem, parser.problem_mark.line+1,
1066                        parser.problem_mark.column+1);
1067            }
1068            else {
1069                fprintf(stderr, "Parser error: %s at line %d, column %d\n",
1070                        parser.problem, parser.problem_mark.line+1,
1071                        parser.problem_mark.column+1);
1072            }
1073            break;
1074
1075        default:
1076            /* Couldn't happen. */
1077            fprintf(stderr, "Internal error\n");
1078            break;
1079    }
1080
1081    yaml_event_delete(&input_event);
1082    yaml_event_delete(&output_event);
1083    yaml_parser_delete(&parser);
1084    yaml_emitter_delete(&emitter);
1085
1086    return 1;
1087
1088emitter_error:
1089
1090    /* Display an emitter error message. */
1091
1092    switch (emitter.error)
1093    {
1094        case YAML_MEMORY_ERROR:
1095            fprintf(stderr, "Memory error: Not enough memory for emitting\n");
1096            break;
1097
1098        case YAML_WRITER_ERROR:
1099            fprintf(stderr, "Writer error: %s\n", emitter.problem);
1100            break;
1101
1102        case YAML_EMITTER_ERROR:
1103            fprintf(stderr, "Emitter error: %s\n", emitter.problem);
1104            break;
1105
1106        default:
1107            /* Couldn't happen. */
1108            fprintf(stderr, "Internal error\n");
1109            break;
1110    }
1111
1112    yaml_event_delete(&input_event);
1113    yaml_event_delete(&output_event);
1114    yaml_parser_delete(&parser);
1115    yaml_emitter_delete(&emitter);
1116
1117    return 1;
1118
1119event_error:
1120
1121    fprintf(stderr, "Memory error: Not enough memory for creating an event\n");
1122
1123    yaml_event_delete(&input_event);
1124    yaml_event_delete(&output_event);
1125    yaml_parser_delete(&parser);
1126    yaml_emitter_delete(&emitter);
1127
1128    return 1;
1129}
1130
Note: See TracBrowser for help on using the repository browser.