static VALUE rb_yaml_emitter_set_output(VALUE self, SEL sel, VALUE output) { rb_yaml_emitter_t *remitter = RYAMLEmitter(self); GC_WB(&remitter->output, output); yaml_emitter_t *emitter = &remitter->emitter; if (!NIL_P(output)) { switch (TYPE(output)) { case T_FILE: yaml_emitter_set_output(emitter, rb_yaml_io_output_handler, (void *)output); break; case T_STRING: yaml_emitter_set_output(emitter, rb_yaml_str_output_handler, (void *)output); break; default: rb_raise(rb_eArgError, "unsupported YAML output type %s", rb_obj_classname(output)); } } return output; }
std::string MapObject::exportYaml() { yaml_emitter_t emitter; yaml_event_t event; std::string exportValue = ""; yaml_emitter_initialize(&emitter); yaml_emitter_set_output(&emitter, write_handler, &exportValue); yaml_emitter_set_encoding(&emitter, YAML_UTF8_ENCODING); if (! yamlDocProlog(&emitter, &event)) return ""; if (this->mapPtr->map.size()) yamlMap(&emitter, &event, this->mapPtr); else if (this->mapObjects.size()) yamlSequence(&emitter, &event, &this->mapObjects); else { yaml_scalar_event_initialize(&event, NULL, NULL, (yaml_char_t*) (this->value.c_str()), (int) this->value.length(), 1, 1, YAML_ANY_SCALAR_STYLE); yaml_emitter_emit(&emitter, &event); } yamlDocEpilog(&emitter, &event); yaml_event_delete(&event); yaml_emitter_delete(&emitter); return exportValue; }
mrb_value mrb_yaml_dump(mrb_state *mrb, mrb_value self) { yaml_emitter_t emitter; yaml_document_t document; mrb_value root; yaml_write_data_t write_data; /* Extract arguments */ mrb_get_args(mrb, "o", &root); /* Build the document */ yaml_document_initialize(&document, NULL, NULL, NULL, 0, 0); value_to_node(mrb, &document, root); /* Initialize the emitter */ yaml_emitter_initialize(&emitter); write_data.mrb = mrb; write_data.str = mrb_str_new(mrb, NULL, 0); yaml_emitter_set_output(&emitter, &yaml_write_handler, &write_data); /* Dump the document */ yaml_emitter_open(&emitter); yaml_emitter_dump(&emitter, &document); yaml_emitter_close(&emitter); /* Clean up */ yaml_emitter_delete(&emitter); yaml_document_delete(&document); return write_data.str; }
/* call-seq: Psych::Emitter.new(io) * * Create a new Psych::Emitter that writes to +io+. */ static VALUE initialize(VALUE self, VALUE io) { yaml_emitter_t * emitter; Data_Get_Struct(self, yaml_emitter_t, emitter); yaml_emitter_set_output(emitter, writer, (void *)io); return self; }
result_t CYamlEmitter::SaveToFile ( IFile::smart_ptr_t _spFile ) const { result_t result_t = eResult_OK; yaml_emitter_t emitter; // int result = yaml_emitter_initialize(&emitter); ex_assert( result, "failed to initialize yaml emitter." ); // // TODO { bool useCanonical = 0; bool useUnicode = 0; if ( useCanonical ) { yaml_emitter_set_canonical ( &emitter, 1 ); } if ( useUnicode ) { yaml_emitter_set_unicode ( &emitter, 1 ); } // } TODO end // DELME { // static const uint BUFFER_SIZE = 65536; // size_t written = 0; // uint8 buffer[BUFFER_SIZE]; // memset(buffer, 0, BUFFER_SIZE); // yaml_emitter_set_output_string ( &emitter, buffer, BUFFER_SIZE, &written ); // } DELME end yaml_emitter_set_output ( &emitter, _private::yaml_write_handler, &(_spFile) ); // save stream start event ex_check_return ( SaveStreamStart ( emitter ), "save stream start failed." ); // for ( yaml_nodes_t::ConstIterator iter = m_Children.Begin(); iter != m_Children.End(); ++iter ) { eResult = (*iter)->SaveToFile ( &emitter ); if ( eResult != eResult_OK ) break; } // save stream end event ex_check_return ( SaveStreamEnd ( emitter ), "save stream end failed." ); // yaml_emitter_delete(&emitter); return eResult; }
int Pemitter (lua_State *L) { lyaml_emitter *emitter; lua_newtable (L); /* object table */ /* Create a user datum to store the emitter. */ emitter = (lyaml_emitter *) lua_newuserdata (L, sizeof (*emitter)); emitter->error = 0; /* Initialize the emitter. */ if (!yaml_emitter_initialize (&emitter->emitter)) { if (!emitter->emitter.problem) emitter->emitter.problem = "cannot initialize emitter"; return luaL_error (L, "%s", emitter->emitter.problem); } yaml_emitter_set_unicode (&emitter->emitter, 1); yaml_emitter_set_width (&emitter->emitter, 2); yaml_emitter_set_output (&emitter->emitter, &append_output, emitter); /* Set it's metatable, and ensure it is garbage collected properly. */ luaL_newmetatable (L, "lyaml.emitter"); lua_pushcfunction (L, emitter_gc); lua_setfield (L, -2, "__gc"); lua_setmetatable (L, -2); /* Set the emit method of object as a closure over the user datum, and return the whole object. */ lua_pushcclosure (L, emit, 1); lua_setfield (L, -2, "emit"); /* Set up a separate thread to collect error messages; save the thread in the returned table so that it's not garbage collected when the function call stack for Pemitter is cleaned up. */ emitter->errL = lua_newthread (L); luaL_buffinit (emitter->errL, &emitter->errbuff); lua_setfield (L, -2, "errthread"); /* Create a thread for the YAML buffer. */ emitter->outputL = lua_newthread (L); luaL_buffinit (emitter->outputL, &emitter->yamlbuff); lua_setfield (L, -2, "outputthread"); return 1; }
void tap_yaml_write(const char *yaml, va_list vl) { yaml_parser_t parser; yaml_document_t document; yaml_emitter_t emitter; char buffer[8192]; size_t length; if (yaml == NULL) { return; } length = vsnprintf(buffer, 8192, yaml, vl); yaml_parser_initialize(&parser); yaml_parser_set_input_string(&parser, (unsigned char*) buffer, length); if (yaml_parser_load(&parser, &document)) { document.start_implicit = 0; document.end_implicit = 0; tap_yaml_document_set_block_style(&document); yaml_emitter_initialize(&emitter); yaml_emitter_set_output(&emitter, tap_yaml_write_handler, NULL); yaml_emitter_set_indent(&emitter, 2); // Initial indent tap_puts(" "); yaml_emitter_open(&emitter); yaml_emitter_dump(&emitter, &document); yaml_emitter_close(&emitter); yaml_parser_delete(&parser); yaml_emitter_delete(&emitter); yaml_document_delete(&document); } else { fprintf(stderr, "Error: %s %s at offset %zu\n", parser.problem, parser.context, parser.problem_mark.index); } }
/* call-seq: Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS) * * Create a new Psych::Emitter that writes to +io+. */ static VALUE initialize(int argc, VALUE *argv, VALUE self) { yaml_emitter_t * emitter; VALUE io, options; VALUE line_width; VALUE indent; VALUE canonical; TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter); if (rb_scan_args(argc, argv, "11", &io, &options) == 2) { line_width = rb_funcall(options, id_line_width, 0); indent = rb_funcall(options, id_indentation, 0); canonical = rb_funcall(options, id_canonical, 0); yaml_emitter_set_width(emitter, NUM2INT(line_width)); yaml_emitter_set_indent(emitter, NUM2INT(indent)); yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0); } yaml_emitter_set_output(emitter, writer, (void *)io); return self; }
void my_emitter_set_output(yaml_emitter_t *e, buffer_t *b) { yaml_emitter_set_output(e, buffer_append, b); }