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;
TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, 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)) {
long i = 0;
long len;
rb_encoding * encoding = rb_utf8_encoding();
Check_Type(tags, T_ARRAY);
len = RARRAY_LEN(tags);
head = xcalloc((size_t)len, sizeof(yaml_tag_directive_t));
tail = head;
for(i = 0; i < len && i < RARRAY_LEN(tags); i++) {
VALUE tuple = RARRAY_AREF(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_AREF(tuple, 0);
value = RARRAY_AREF(tuple, 1);
StringValue(name);
StringValue(value);
name = rb_str_export_to_enc(name, encoding);
value = rb_str_export_to_enc(value, encoding);
tail->handle = (yaml_char_t *)StringValueCStr(name);
tail->prefix = (yaml_char_t *)StringValueCStr(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;
}