static int yaml_emitter_dump_mapping(yaml_emitter_t *emitter, yaml_node_t *node, yaml_char_t *anchor) { yaml_event_t event; yaml_mark_t mark = { 0, 0, 0 }; int implicit = (strcmp((char *)node->tag, YAML_DEFAULT_MAPPING_TAG) == 0); yaml_node_pair_t *pair; MAPPING_START_EVENT_INIT(event, anchor, node->tag, implicit, node->data.mapping.style, mark, mark); if (!yaml_emitter_emit(emitter, &event)) return 0; for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair ++) { if (!yaml_emitter_dump_node(emitter, pair->key)) return 0; if (!yaml_emitter_dump_node(emitter, pair->value)) return 0; } MAPPING_END_EVENT_INIT(event, mark, mark); if (!yaml_emitter_emit(emitter, &event)) return 0; return 1; }
static int yaml_emitter_dump_sequence(yaml_emitter_t *emitter, yaml_node_t *node, yaml_char_t *anchor) { yaml_event_t event; yaml_mark_t mark = { 0, 0, 0 }; int implicit = (strcmp((char *)node->tag, YAML_DEFAULT_SEQUENCE_TAG) == 0); yaml_node_item_t *item; SEQUENCE_START_EVENT_INIT(event, anchor, node->tag, implicit, node->data.sequence.style, mark, mark); if (!yaml_emitter_emit(emitter, &event)) return 0; for (item = node->data.sequence.items.start; item < node->data.sequence.items.top; item ++) { if (!yaml_emitter_dump_node(emitter, *item)) return 0; } SEQUENCE_END_EVENT_INIT(event, mark, mark); if (!yaml_emitter_emit(emitter, &event)) return 0; return 1; }
yaml_emitter_dump(yaml_emitter_t *emitter, yaml_document_t *document) { yaml_event_t event; yaml_mark_t mark = { 0, 0, 0 }; assert(emitter); /* Non-NULL emitter object is required. */ assert(document); /* Non-NULL emitter object is expected. */ emitter->document = document; if (!emitter->opened) { if (!yaml_emitter_open(emitter)) goto error; } if (STACK_EMPTY(emitter, document->nodes)) { if (!yaml_emitter_close(emitter)) goto error; yaml_emitter_delete_document_and_anchors(emitter); return 1; } assert(emitter->opened); /* Emitter should be opened. */ emitter->anchors = yaml_malloc(sizeof(*(emitter->anchors)) * (document->nodes.top - document->nodes.start)); if (!emitter->anchors) goto error; memset(emitter->anchors, 0, sizeof(*(emitter->anchors)) * (document->nodes.top - document->nodes.start)); DOCUMENT_START_EVENT_INIT(event, document->version_directive, document->tag_directives.start, document->tag_directives.end, document->start_implicit, mark, mark); if (!yaml_emitter_emit(emitter, &event)) goto error; yaml_emitter_anchor_node(emitter, 1); if (!yaml_emitter_dump_node(emitter, 1)) goto error; DOCUMENT_END_EVENT_INIT(event, document->end_implicit, mark, mark); if (!yaml_emitter_emit(emitter, &event)) goto error; yaml_emitter_delete_document_and_anchors(emitter); return 1; error: yaml_emitter_delete_document_and_anchors(emitter); return 0; }