Permalink
Browse files

Further work in making even a semi-usable YAML parser out of this code.

  • Loading branch information...
1 parent c120e52 commit 738eab636f663a06e8be8d05838bc9c0bb675600 Patrick Thomson committed Aug 3, 2009
Showing with 108 additions and 1 deletion.
  1. +5 −1 .gitignore
  2. +96 −0 ext/libyaml/rubyext.c
  3. +1 −0 include/ruby/intern.h
  4. +6 −0 io.c
View
@@ -14,4 +14,8 @@ spec/ruby
spec/frozen/upstream_patches
mspec_upstream
mspec/upstream_patches
-spec/frozen/optional/ffi/fixtures/build
+spec/frozen/optional/ffi/fixtures/build
+*.bundle
+mkmf.log
+Makefile
+>>>>>>> Further work in making even a semi-usable YAML parser out of this code.:.gitignore
View
@@ -0,0 +1,96 @@
+/*
+ *
+ * rubyext.c - ruby extensions to libYAML
+ * author: Patrick Thomson
+ * date: July 27, 2009
+ *
+ */
+
+#include "ruby/ruby.h"
+#include "yaml.h"
+
+static VALUE
+yaml_load(VALUE module, SEL sel, VALUE input)
+{
+ return Qnil;
+}
+
+static VALUE
+yaml_dump(VALUE module, SEL sel, int argc, VALUE* argv)
+{
+ return Qnil;
+}
+
+static VALUE
+rb_yaml_parser_alloc(VALUE klass, SEL sel)
+{
+ yaml_parser_t *parser = ALLOC(yaml_parser_t);
+ yaml_parser_initialize(parser);
+ // XXX: Figure out how to pass the yaml_parser_delete() method to the parser upon deallocation.
+ return Data_Wrap_Struct(klass, NULL, NULL, parser);
+}
+
+static int
+rb_yaml_parser_io_handler(VALUE io, unsigned char *out_buffer, size_t size, size_t *size_read)
+{
+ if (rb_io_eof(io, 0) == Qtrue)
+ {
+ *size_read = 0;
+ return 1;
+ }
+ long ret = rb_io_primitive_read(ExtractIOStruct(io), (UInt8*)buffer, size);
+ if (ret == -1) {
+ return 0;
+ }
+ *size_read = (size_t)ret;
+ return 1;
+}
+
+static VALUE
+rb_yaml_parser_load(VALUE self, SEL sel, VALUE io)
+{
+ yaml_parser_t *parser;
+ Data_Get_Struct(self, yaml_parser_t, parser);
+ yaml_parser_set_input(parser, (yaml_read_handler_t*)rb_yaml_parser_io_handler, io);
+}
+
+static IMP rb_yaml_parser_finalize_super = NULL;
+
+static void
+rb_yaml_parser_finalize(void *rcv, SEL sel)
+{
+ yaml_parser_t *parser;
+ Data_Get_Struct(rcv, yaml_parser_t, parser);
+ yaml_parser_delete(parser);
+ if (rb_yaml_parser_finalize_super != NULL)
+ {
+ ((void(*)(void *, SEL))rb_yaml_parser_finalize_super)(rcv, sel);
+ }
+}
+
+static VALUE
+rb_yaml_emitter_emit(VALUE self, SEL sel, int argc, VALUE *argv)
+{
+ return Qnil;
+}
+
+void
+Init_yaml()
+{
+ VALUE rb_mYAML = rb_define_module("YAML");
+
+ rb_objc_define_method(*(VALUE *)rb_mYAML, "load", yaml_load, 1);
+ rb_objc_define_method(*(VALUE *)rb_mYAML, "dump", yaml_dump, -1);
+
+ VALUE rb_mLibYAML = rb_define_module_under(rb_mYAML, "LibYAML");
+ rb_define_const(rb_mLibYAML, "VERSION", rb_str_new2(yaml_get_version_string()));
+
+ VALUE rb_cParser = rb_define_class_under(rb_mLibYAML, "Parser", rb_cObject);
+ rb_objc_define_method(*(VALUE *)rb_cParser, "alloc", rb_yaml_parser_alloc, 0);
+ rb_objc_define_method(rb_cParser, "load", rb_yaml_parser_load, 1);
+
+ rb_yaml_parser_finalize_super = rb_objc_install_method2((Class)rb_cParser, "finalize", (IMP)rb_yaml_parser_finalize);
+
+ VALUE rb_cEmitter = rb_define_class_under(rb_mLibYAML, "Emitter", rb_cObject);
+ rb_objc_define_method(rb_cEmitter, "emit", rb_yaml_emitter_emit, -1);
+}
View
@@ -394,6 +394,7 @@ VALUE rb_io_addstr(VALUE, SEL, VALUE);
VALUE rb_io_printf(VALUE, SEL, int, VALUE *);
VALUE rb_io_print(VALUE, SEL, int, VALUE *);
VALUE rb_io_fdopen(int, int, const char*);
+long rb_io_primitive_read(rb_io_t *, UInt8 *, long);
VALUE rb_gets(void);
void rb_write_error(const char*);
void rb_write_error2(const char*, long);
View
6 io.c
@@ -1076,6 +1076,12 @@ rb_io_read_all_file(VALUE io, UInt8 *buffer, size_t buffer_len)
return rb_io_read_internal(io_struct, buffer, buffer_len) > 0;
}
+long
+rb_io_primitive_read(rb_io_t *io_struct, UInt8 *buffer, long len)
+{
+ return rb_io_read_internal(io_struct, buffer, len);
+}
+
/*
* call-seq:
* ios.read_nonblock(maxlen) => string

0 comments on commit 738eab6

Please sign in to comment.