start_document(p1, p2, p3)
public
Show source
static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
{
yaml_emitter_t * emitter;
yaml_tag_directive_t * head = NULL;
yaml_tag_directive_t * tail = NULL;
yaml_event_t event;
yaml_version_directive_t version_directive;
Data_Get_Struct(self, yaml_emitter_t, emitter);
Check_Type(version, T_ARRAY);
if(RARRAY_LEN(version) > 0) {
VALUE major = rb_ary_entry(version, (long)0);
VALUE minor = rb_ary_entry(version, (long)1);
version_directive.major = NUM2INT(major);
version_directive.minor = NUM2INT(minor);
}
if(RTEST(tags)) {
int i = 0;
rb_encoding * encoding = rb_utf8_encoding();
Check_Type(tags, T_ARRAY);
head = xcalloc((size_t)RARRAY_LEN(tags), sizeof(yaml_tag_directive_t));
tail = head;
for(i = 0; i < RARRAY_LEN(tags); i++) {
VALUE tuple = RARRAY_PTR(tags)[i];
VALUE name;
VALUE value;
Check_Type(tuple, T_ARRAY);
if(RARRAY_LEN(tuple) < 2) {
xfree(head);
rb_raise(rb_eRuntimeError, "tag tuple must be of length 2");
}
name = RARRAY_PTR(tuple)[0];
value = RARRAY_PTR(tuple)[1];
name = rb_str_export_to_enc(name, encoding);
value = rb_str_export_to_enc(value, encoding);
tail->handle = (yaml_char_t *)StringValuePtr(name);
tail->prefix = (yaml_char_t *)StringValuePtr(value);
tail++;
}
}
yaml_document_start_event_initialize(
&event,
(RARRAY_LEN(version) > 0) ? &version_directive : NULL,
head,
tail,
imp ? 1 : 0
);
emit(emitter, &event);
if(head) xfree(head);
return self;
}