static bool emit_mapping(node *mapping, void *context) { log_trace(component, "emitting mapping"); yaml_emitter_t *emitter = (yaml_emitter_t *)context; yaml_event_t event; log_trace(component, "mapping start"); uint8_t *name = node_name(mapping); yaml_char_t *tag = NULL == name ? (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG : (yaml_char_t *)name; yaml_mapping_start_event_initialize(&event, NULL, tag, NULL == name, YAML_BLOCK_MAPPING_STYLE); if (!yaml_emitter_emit(emitter, &event)) return false; if(!mapping_iterate(mapping, emit_mapping_item, context)) { return false; } log_trace(component, "mapping end"); yaml_mapping_end_event_initialize(&event); if (!yaml_emitter_emit(emitter, &event)) return false; return true; }
/* Emit a MAPPING_START event. */ static int emit_MAPPING_START (lua_State *L, lyaml_emitter *emitter) { yaml_event_t event; yaml_mapping_style_t yaml_style; yaml_char_t *anchor = NULL, *tag = NULL; int implicit = 1; const char *style = NULL; RAWGET_STRDUP (style); lua_pop (L, 1); #define MENTRY(_s) (STREQ (style, #_s)) { yaml_style = YAML_##_s##_MAPPING_STYLE; } if (style == NULL) { yaml_style = YAML_ANY_MAPPING_STYLE; } else if MENTRY( BLOCK ) else if MENTRY( FLOW ) else { emitter->error++; lua_pushfstring (L, "invalid mapping style '%s'", style); lua_insert (L, -2); } #undef MENTRY RAWGET_YAML_CHARP (anchor); lua_pop (L, 1); RAWGET_YAML_CHARP (tag); lua_pop (L, 1); RAWGET_BOOLEAN (implicit); lua_pop (L, 1); yaml_mapping_start_event_initialize (&event, anchor, tag, implicit, yaml_style); return yaml_emitter_emit (&emitter->emitter, &event); }
void yamlBeginMap(yaml_emitter_t *emitter) { yaml_event_t event; if (!yaml_mapping_start_event_initialize( &event, NULL, NULL, 1, YAML_ANY_MAPPING_STYLE)) { throw YamlException(); } yamlTryEmit(emitter, &event); yaml_event_delete(&event); }
static VALUE rb_yaml_emitter_mapping(VALUE self, SEL sel, VALUE taguri, VALUE style) { yaml_event_t ev; yaml_emitter_t *emitter = &RYAMLEmitter(self)->emitter; int can_omit_tag = 0; int string_tag = 0; yaml_char_t *tag = rb_yaml_tag_or_null(taguri, &can_omit_tag, &string_tag); yaml_mapping_start_event_initialize(&ev, NULL, tag, can_omit_tag, rb_symbol_to_mapping_style(style)); yaml_emitter_emit(emitter, &ev); rb_yield(self); yaml_mapping_end_event_initialize(&ev); yaml_emitter_emit(emitter, &ev); return self; }
int emit(yaml_emitter_t *emitter, const config_t conf) { int r = CONF_OK; yaml_event_t evt; //doc start yaml_document_start_event_initialize(&evt, NULL, NULL, NULL, CONF_F); yaml_emitter_emit(emitter, &evt); // log_yml_event(evt); yaml_mapping_start_event_initialize(&evt, NULL, NULL, CONF_F, YAML_BLOCK_MAPPING_STYLE); yaml_emitter_emit(emitter, &evt); // log_yml_event(evt); entry_t iter = conf->map[0]; int i = 0; while(i < conf->size) { if(iter){ emit_entry(iter, emitter); } iter = conf->map[++i]; } //mapping end yaml_mapping_end_event_initialize(&evt); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); //doc end yaml_document_end_event_initialize(&evt,CONF_F); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); yaml_event_delete(&evt); return r; }
/* call-seq: emitter.start_mapping(anchor, tag, implicit, style) * * Start emitting a YAML map with +anchor+, +tag+, an +implicit+ start * and end, and +style+. * * See Psych::Handler#start_mapping */ static VALUE start_mapping( VALUE self, VALUE anchor, VALUE tag, VALUE implicit, VALUE style ) { yaml_emitter_t * emitter; yaml_event_t event; #ifdef HAVE_RUBY_ENCODING_H rb_encoding *encoding; #endif Data_Get_Struct(self, yaml_emitter_t, emitter); #ifdef HAVE_RUBY_ENCODING_H encoding = rb_utf8_encoding(); if(!NIL_P(anchor)) { Check_Type(anchor, T_STRING); anchor = rb_str_export_to_enc(anchor, encoding); } if(!NIL_P(tag)) { Check_Type(tag, T_STRING); tag = rb_str_export_to_enc(tag, encoding); } #endif yaml_mapping_start_event_initialize( &event, (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValuePtr(anchor)), (yaml_char_t *)(NIL_P(tag) ? NULL : StringValuePtr(tag)), implicit ? 1 : 0, (yaml_mapping_style_t)NUM2INT(style) ); emit(emitter, &event); return self; }
int MapObject::yamlMap(yaml_emitter_t *emitter, yaml_event_t *event, std::shared_ptr<MapObject::mapMapObject> mapObj) { if (mapObj->map.size()) { yaml_mapping_start_event_initialize(event, NULL, NULL, true, (mapObj->flow == true ? YAML_FLOW_MAPPING_STYLE : YAML_BLOCK_MAPPING_STYLE)); yaml_emitter_emit(emitter, event); for (std::unordered_map<std::string, MapObject>::iterator it = mapObj->map.begin(); it != mapObj->map.end(); it++) { yaml_scalar_event_initialize(event, NULL, NULL, (yaml_char_t*) (it->first.c_str()), (int) it->first.length(), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(emitter, event); if (it->second.mapPtr->map.size()) yamlMap(emitter, event, it->second.mapPtr); else if (it->second.mapObjects.size()) yamlSequence(emitter, event, &it->second.mapObjects, (it->second.flow == true ? 1 : 0)); else { yaml_scalar_event_initialize(event, NULL, NULL, (yaml_char_t*) it->second.value.c_str(), (int) it->second.value.length(), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(emitter, event); } } yaml_mapping_end_event_initialize(event); yaml_emitter_emit(emitter, event); } return 1; }
int main(int argc, char *argv[]) { int help = 0; int canonical = 0; int unicode = 0; int k; int done = 0; yaml_parser_t parser; yaml_emitter_t emitter; yaml_event_t input_event; yaml_event_t output_event; /* Clear the objects. */ memset(&parser, 0, sizeof(parser)); memset(&emitter, 0, sizeof(emitter)); memset(&input_event, 0, sizeof(input_event)); memset(&output_event, 0, sizeof(output_event)); /* Analyze command line options. */ for (k = 1; k < argc; k ++) { if (strcmp(argv[k], "-h") == 0 || strcmp(argv[k], "--help") == 0) { help = 1; } else if (strcmp(argv[k], "-c") == 0 || strcmp(argv[k], "--canonical") == 0) { canonical = 1; } else if (strcmp(argv[k], "-u") == 0 || strcmp(argv[k], "--unicode") == 0) { unicode = 1; } else { fprintf(stderr, "Unrecognized option: %s\n" "Try `%s --help` for more information.\n", argv[k], argv[0]); return 1; } } /* Display the help string. */ if (help) { printf("%s <input\n" "or\n%s -h | --help\nDeconstruct a YAML stream\n\nOptions:\n" "-h, --help\t\tdisplay this help and exit\n" "-c, --canonical\t\toutput in the canonical YAML format\n" "-u, --unicode\t\toutput unescaped non-ASCII characters\n", argv[0], argv[0]); return 0; } /* Initialize the parser and emitter objects. */ if (!yaml_parser_initialize(&parser)) { fprintf(stderr, "Could not initialize the parser object\n"); return 1; } if (!yaml_emitter_initialize(&emitter)) { yaml_parser_delete(&parser); fprintf(stderr, "Could not inialize the emitter object\n"); return 1; } /* Set the parser parameters. */ yaml_parser_set_input_file(&parser, stdin); /* Set the emitter parameters. */ yaml_emitter_set_output_file(&emitter, stdout); yaml_emitter_set_canonical(&emitter, canonical); yaml_emitter_set_unicode(&emitter, unicode); /* Create and emit the STREAM-START event. */ if (!yaml_stream_start_event_initialize(&output_event, YAML_UTF8_ENCODING)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the DOCUMENT-START event. */ if (!yaml_document_start_event_initialize(&output_event, NULL, NULL, NULL, 0)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the SEQUENCE-START event. */ if (!yaml_sequence_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:seq", 1, YAML_BLOCK_SEQUENCE_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Loop through the input events. */ while (!done) { /* Get the next event. */ if (!yaml_parser_parse(&parser, &input_event)) goto parser_error; /* Check if this is the stream end. */ if (input_event.type == YAML_STREAM_END_EVENT) { done = 1; } /* Create and emit a MAPPING-START event. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_BLOCK_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Analyze the event. */ switch (input_event.type) { case YAML_STREAM_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'STREAM-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "STREAM-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display encoding information. */ if (input_event.data.stream_start.encoding) { yaml_encoding_t encoding = input_event.data.stream_start.encoding; /* Write 'encoding'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "encoding", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the stream encoding. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (encoding == YAML_UTF8_ENCODING ? "utf-8" : encoding == YAML_UTF16LE_ENCODING ? "utf-16-le" : encoding == YAML_UTF16BE_ENCODING ? "utf-16-be" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_STREAM_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'STREAM-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "STREAM-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_DOCUMENT_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'DOCUMENT-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "DOCUMENT-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the document version numbers. */ if (input_event.data.document_start.version_directive) { yaml_version_directive_t *version = input_event.data.document_start.version_directive; char number[64]; /* Write 'version'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "version", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '{'. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_FLOW_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'major'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "major", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write a number. */ sprintf(number, "%d", version->major); if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:int", number, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'minor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "minor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write a number. */ sprintf(number, "%d", version->minor); if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:int", number, -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '}'. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the document tag directives. */ if (input_event.data.document_start.tag_directives.start != input_event.data.document_start.tag_directives.end) { yaml_tag_directive_t *tag; /* Write 'tags'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tags", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Start a block sequence. */ if (!yaml_sequence_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:seq", 1, YAML_BLOCK_SEQUENCE_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; for (tag = input_event.data.document_start.tag_directives.start; tag != input_event.data.document_start.tag_directives.end; tag ++) { /* Write '{'. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_FLOW_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'handle'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "handle", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the tag directive handle. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", tag->handle, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'prefix'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "prefix", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the tag directive prefix. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", tag->prefix, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '}'. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* End a block sequence. */ if (!yaml_sequence_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the document is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.document_start.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_DOCUMENT_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'DOCUMENT-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "DOCUMENT-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the document is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.document_end.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_ALIAS_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'ALIAS'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "ALIAS", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the alias anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.alias.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_SCALAR_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'SCALAR'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "SCALAR", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the scalar anchor. */ if (input_event.data.scalar.anchor) { /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.scalar.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the scalar tag. */ if (input_event.data.scalar.tag) { /* Write 'tag'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tag", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar tag. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.scalar.tag, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the scalar value. */ /* Write 'value'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "value", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar value. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.scalar.value, input_event.data.scalar.length, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display if the scalar tag is implicit. */ /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '{'. */ if (!yaml_mapping_start_event_initialize(&output_event, NULL, "tag:yaml.org,2002:map", 1, YAML_FLOW_MAPPING_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'plain'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "plain", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the scalar is implicit in the plain style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.scalar.plain_implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'quoted'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "non-plain", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the scalar is implicit in a non-plain style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.scalar.quoted_implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write '}'. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the style information. */ if (input_event.data.scalar.style) { yaml_scalar_style_t style = input_event.data.scalar.style; /* Write 'style'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "style", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (style == YAML_PLAIN_SCALAR_STYLE ? "plain" : style == YAML_SINGLE_QUOTED_SCALAR_STYLE ? "single-quoted" : style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ? "double-quoted" : style == YAML_LITERAL_SCALAR_STYLE ? "literal" : style == YAML_FOLDED_SCALAR_STYLE ? "folded" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_SEQUENCE_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'SEQUENCE-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "SEQUENCE-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the sequence anchor. */ if (input_event.data.sequence_start.anchor) { /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the sequence anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.sequence_start.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the sequence tag. */ if (input_event.data.sequence_start.tag) { /* Write 'tag'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tag", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the sequence tag. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.sequence_start.tag, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the sequence tag is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.sequence_start.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the style information. */ if (input_event.data.sequence_start.style) { yaml_sequence_style_t style = input_event.data.sequence_start.style; /* Write 'style'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "style", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (style == YAML_BLOCK_SEQUENCE_STYLE ? "block" : style == YAML_FLOW_SEQUENCE_STYLE ? "flow" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_SEQUENCE_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'SEQUENCE-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "SEQUENCE-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; case YAML_MAPPING_START_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'MAPPING-START'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "MAPPING-START", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the mapping anchor. */ if (input_event.data.mapping_start.anchor) { /* Write 'anchor'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "anchor", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the mapping anchor. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.mapping_start.anchor, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Display the mapping tag. */ if (input_event.data.mapping_start.tag) { /* Write 'tag'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "tag", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the mapping tag. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", input_event.data.mapping_start.tag, -1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Write 'implicit'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "implicit", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write if the mapping tag is implicit. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:bool", (input_event.data.mapping_start.implicit ? "true" : "false"), -1, 1, 0, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Display the style information. */ if (input_event.data.mapping_start.style) { yaml_mapping_style_t style = input_event.data.mapping_start.style; /* Write 'style'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "style", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write the scalar style. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", (style == YAML_BLOCK_MAPPING_STYLE ? "block" : style == YAML_FLOW_MAPPING_STYLE ? "flow" : "unknown"), -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } break; case YAML_MAPPING_END_EVENT: /* Write 'type'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "type", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Write 'MAPPING-END'. */ if (!yaml_scalar_event_initialize(&output_event, NULL, "tag:yaml.org,2002:str", "MAPPING-END", -1, 1, 1, YAML_PLAIN_SCALAR_STYLE)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; break; default: /* It couldn't really happen. */ break; } /* Delete the event object. */ yaml_event_delete(&input_event); /* Create and emit a MAPPING-END event. */ if (!yaml_mapping_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; } /* Create and emit the SEQUENCE-END event. */ if (!yaml_sequence_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the DOCUMENT-END event. */ if (!yaml_document_end_event_initialize(&output_event, 0)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; /* Create and emit the STREAM-END event. */ if (!yaml_stream_end_event_initialize(&output_event)) goto event_error; if (!yaml_emitter_emit(&emitter, &output_event)) goto emitter_error; yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 0; parser_error: /* Display a parser error message. */ switch (parser.error) { case YAML_MEMORY_ERROR: fprintf(stderr, "Memory error: Not enough memory for parsing\n"); break; case YAML_READER_ERROR: if (parser.problem_value != -1) { fprintf(stderr, "Reader error: %s: #%X at %d\n", parser.problem, parser.problem_value, parser.problem_offset); } else { fprintf(stderr, "Reader error: %s at %d\n", parser.problem, parser.problem_offset); } break; case YAML_SCANNER_ERROR: if (parser.context) { fprintf(stderr, "Scanner error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } else { fprintf(stderr, "Scanner error: %s at line %d, column %d\n", parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } break; case YAML_PARSER_ERROR: if (parser.context) { fprintf(stderr, "Parser error: %s at line %d, column %d\n" "%s at line %d, column %d\n", parser.context, parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } else { fprintf(stderr, "Parser error: %s at line %d, column %d\n", parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1); } break; default: /* Couldn't happen. */ fprintf(stderr, "Internal error\n"); break; } yaml_event_delete(&input_event); yaml_event_delete(&output_event); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; emitter_error: /* Display an emitter error message. */ switch (emitter.error) { case YAML_MEMORY_ERROR: fprintf(stderr, "Memory error: Not enough memory for emitting\n"); break; case YAML_WRITER_ERROR: fprintf(stderr, "Writer error: %s\n", emitter.problem); break; case YAML_EMITTER_ERROR: fprintf(stderr, "Emitter error: %s\n", emitter.problem); break; default: /* Couldn't happen. */ fprintf(stderr, "Internal error\n"); break; } yaml_event_delete(&input_event); yaml_event_delete(&output_event); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; event_error: fprintf(stderr, "Memory error: Not enough memory for creating an event\n"); yaml_event_delete(&input_event); yaml_event_delete(&output_event); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); return 1; }
int main (int argc, char **argv) { SwfdecPlayer *player; SwfdecURL *url; yaml_emitter_t emitter; yaml_event_t event; FILE *output; int i; const char* directory_prefix = NULL; char output_filename_serial[256] = {0}; unsigned long frame_interval = 0; unsigned long accum_interval = 0; int frame_count = 1; unsigned int override_width = 0; unsigned int override_height = 0; float override_scale = 1.0f; const char* input_filename = NULL; const char* output_filename = NULL; int base_index = 0; cairo_surface_t *surface[2] = {NULL, NULL}; cairo_t *cr[2] = {NULL, NULL}; int loop = 0; int center_x = -1; int center_y = -1; /* Parse the command line arguments */ for(i = 1; i < argc; ++i ) { const char* arg = argv[i]; if ( arg[0] == '-' ) { if ( arg[1] == 'w' ) { override_width = atoi(arg+3); } else if ( arg[1] == 'h' ) { override_height = atoi(arg+3); } else if ( arg[1] == 's' ) { override_scale = (float)atof(arg+3); } else if ( arg[1] == 'f' ) { frame_count = atof(arg+3); } else if ( arg[1] == 'l' ) { loop = atoi(arg+3); } else if ( arg[1] == 'd' ) { directory_prefix = arg+3; } else if ( arg[1] == 'x' ) { center_x = atoi(arg+3); } else if ( arg[1] == 'y' ) { center_y = atoi(arg+3); } else { printf("Unknown option: %s\n", arg); } } else { if (!input_filename ) { input_filename = arg; } else { output_filename = arg; } } } if (!input_filename || !output_filename) { print_usage(argv[0]); return 1; } /* Create the Emitter object. */ yaml_emitter_initialize(&emitter); // init swfdec_init (); url = swfdec_url_new_from_input (input_filename); if (!url) { printf("Input file not found: %s.\n", input_filename); return 1; } /* Set a file output. */ sprintf(output_filename_serial, "%s.yaml", output_filename); output = fopen(output_filename_serial, "wb"); yaml_emitter_set_output_file(&emitter, output); //yaml_emitter_set_output(&emitter, write_handler, output); player = swfdec_player_new (NULL); swfdec_player_set_url (player, url); /* Create and emit the STREAM-START event. */ yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING); yaml_emitter_emit(&emitter, &event); /* Create document */ yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 1); yaml_emitter_emit(&emitter, &event); /* Create mapping */ yaml_mapping_start_event_initialize(&event, NULL, NULL, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"type", strlen("type"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"keyframe", strlen("keyframe"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); swfdec_player_advance (player, 0); if ( override_width == 0 || override_height == 0 ) { swfdec_player_get_default_size(player, &override_width, &override_height); } /* need to set player size */ swfdec_player_set_size(player, (unsigned int)override_width * override_scale, (unsigned int)override_height * override_scale); { char buf[256] = {0}; yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"defaultframerate", strlen("defaultframerate"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); sprintf(buf, "%f", swfdec_player_get_rate(player)); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)buf, strlen(buf), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); } yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"loop", strlen("loop"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); if ( loop ) { yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"true", strlen("true"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); } else { yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"false", strlen("false"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); } if (center_x >= 0 ) { char buf[256] = {0}; yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"center_point_x", strlen("center_point_x"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); sprintf(buf, "%d", center_x); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)buf, strlen(buf), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); } if (center_y >= 0 ) { char buf[256] = {0}; yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"center_point_y", strlen("center_point_y"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); sprintf(buf, "%d", center_y); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)buf, strlen(buf), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); } printf("Default frame rate: %f\n", swfdec_player_get_rate(player)); frame_interval = (unsigned long)(1000.0 / swfdec_player_get_rate(player)); printf("Default frame interval: %ld\n", frame_interval); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)"content", strlen("content"), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); /* Create mapping */ yaml_mapping_start_event_initialize(&event, NULL, NULL, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); for(i = 0; i < frame_count; ++i ) { int new_index = (base_index + 1) % 2; surface[new_index] = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, (unsigned int)(override_width * override_scale), (unsigned int)(override_height * override_scale)); cr[new_index] = cairo_create (surface[new_index]); // render the image swfdec_player_render (player, cr[new_index]); frame_interval = swfdec_player_get_next_event(player); accum_interval += frame_interval; if ( compare_images(surface[base_index], surface[new_index]) <= 5 ) { cairo_destroy (cr[new_index]); cairo_surface_destroy (surface[new_index]); cr[new_index] = NULL; surface[new_index] = NULL; if ( frame_interval > 0 ) swfdec_player_advance (player, frame_interval); } else { if ( surface[base_index] && surface[new_index] ) { char buf[256] = {0}; sprintf(buf, "%f", (double)accum_interval / 1000.0); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)output_filename_serial, strlen(output_filename_serial), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)buf, strlen(buf), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); accum_interval = 0; } cairo_destroy (cr[base_index]); cairo_surface_destroy (surface[base_index]); cr[base_index] = NULL; surface[base_index] = NULL; if (directory_prefix) sprintf(output_filename_serial, "%s/%s%3.3d.png", directory_prefix, output_filename, i+1); else sprintf(output_filename_serial, "%s%3.3d.png", output_filename, i+1); cairo_surface_write_to_png (surface[new_index], output_filename_serial); if ( frame_interval > 0 ) swfdec_player_advance (player, frame_interval); base_index = new_index; } } /* the last frame output */ if ( surface[base_index] ) { char buf[256] = {0}; sprintf(buf, "%f", (double)accum_interval / 1000.0); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)output_filename_serial, strlen(output_filename_serial), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*)buf, strlen(buf), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); accum_interval = 0; } cairo_destroy (cr[base_index]); cairo_surface_destroy (surface[base_index]); g_object_unref (player); swfdec_url_free(url); /* End mapping */ yaml_mapping_end_event_initialize(&event); yaml_emitter_emit(&emitter, &event); /* End mapping */ yaml_mapping_end_event_initialize(&event); yaml_emitter_emit(&emitter, &event); /* End of document */ yaml_stream_end_event_initialize(&event); yaml_emitter_emit(&emitter, &event); /* Create and emit the STREAM-END event. */ yaml_stream_end_event_initialize(&event); yaml_emitter_emit(&emitter, &event); yaml_emitter_flush(&emitter); /* Destroy the Emitter object. */ yaml_emitter_delete(&emitter); fclose(output); return 0; }
int copy_event(yaml_event_t *event_to, yaml_event_t *event_from) { switch (event_from->type) { case YAML_STREAM_START_EVENT: return yaml_stream_start_event_initialize(event_to, event_from->data.stream_start.encoding); case YAML_STREAM_END_EVENT: return yaml_stream_end_event_initialize(event_to); case YAML_DOCUMENT_START_EVENT: return yaml_document_start_event_initialize(event_to, event_from->data.document_start.version_directive, event_from->data.document_start.tag_directives.start, event_from->data.document_start.tag_directives.end, event_from->data.document_start.implicit); case YAML_DOCUMENT_END_EVENT: return yaml_document_end_event_initialize(event_to, event_from->data.document_end.implicit); case YAML_ALIAS_EVENT: return yaml_alias_event_initialize(event_to, event_from->data.alias.anchor); case YAML_SCALAR_EVENT: return yaml_scalar_event_initialize(event_to, event_from->data.scalar.anchor, event_from->data.scalar.tag, event_from->data.scalar.value, event_from->data.scalar.length, event_from->data.scalar.plain_implicit, event_from->data.scalar.quoted_implicit, event_from->data.scalar.style); case YAML_SEQUENCE_START_EVENT: return yaml_sequence_start_event_initialize(event_to, event_from->data.sequence_start.anchor, event_from->data.sequence_start.tag, event_from->data.sequence_start.implicit, event_from->data.sequence_start.style); case YAML_SEQUENCE_END_EVENT: return yaml_sequence_end_event_initialize(event_to); case YAML_MAPPING_START_EVENT: return yaml_mapping_start_event_initialize(event_to, event_from->data.mapping_start.anchor, event_from->data.mapping_start.tag, event_from->data.mapping_start.implicit, event_from->data.mapping_start.style); case YAML_MAPPING_END_EVENT: return yaml_mapping_end_event_initialize(event_to); default: assert(1); } return 0; }
static void emit_entry(const entry_t entry, yaml_emitter_t *emitter) { int i = 0; yaml_event_t evt; yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)entry->name, strlen(entry->name), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); char buf[CONF_NUM]; memset(buf, 0, CONF_NUM); entry_t iter = NULL; variable_t viter = NULL; switch (entry->type_tag){ case ENTRY_STR: yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)entry->e.scalar_str, strlen(entry->e.scalar_str), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); break; case ENTRY_INT: sprintf(buf, "%d", entry->e.scalar_int); yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)buf, strlen(buf), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); break; case ENTRY_MAP: yaml_mapping_start_event_initialize( &evt, NULL, NULL, CONF_F, YAML_BLOCK_MAPPING_STYLE); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); iter = *(entry->e.conf->map); while(i < entry->e.conf->size){ if(iter != NULL) { emit_entry(iter, emitter); } iter = (entry->e.conf->map)[++i]; } yaml_mapping_end_event_initialize(&evt); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); break; case ENTRY_SEQ: yaml_sequence_start_event_initialize( &evt, NULL, NULL, CONF_T, YAML_BLOCK_SEQUENCE_STYLE); //log_yml_event(evt); yaml_emitter_emit(emitter, &evt); //emit size as int sprintf(buf, "%d", entry->e.seq->size); yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)buf, strlen(buf), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); //emit values as map viter = entry->e.seq->vars; i = 0; while(i < entry->e.seq->size){ if(viter) { emit_variable(viter, emitter); } viter = &(entry->e.seq->vars)[++i]; } yaml_sequence_end_event_initialize(&evt); yaml_emitter_emit(emitter, &evt); //log_yml_event(evt); break; default:break; } }
static void emit_variable(const variable_t var, yaml_emitter_t *emitter) { yaml_event_t evt; if(var->name != NULL && var->name[0]) { char idx[4]; memset(idx, 0, 4); yaml_mapping_start_event_initialize( &evt, NULL, NULL, CONF_F, YAML_BLOCK_MAPPING_STYLE); yaml_emitter_emit(emitter, &evt); yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)"INDEX", 5, CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); sprintf(idx, "%d", var->index); yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)idx, strlen(idx), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)"ID", 2, CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)var->name, strlen(var->name), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); param_t it = var->params; while(it){ yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)it->key, strlen(it->key), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); yaml_emitter_emit(emitter, &evt); //fixme: mulitiline yaml_scalar_event_initialize( &evt, NULL, NULL, (yaml_char_t *)it->value, strlen(it->value), CONF_T, CONF_T, YAML_PLAIN_SCALAR_STYLE); //log_yml_event(evt); yaml_emitter_emit(emitter, &evt); it = it->next; } yaml_mapping_end_event_initialize(&evt); yaml_emitter_emit(emitter, &evt); } }