[macruby-changes] [4196] MacRuby/trunk/io.c

source_changes at macosforge.org source_changes at macosforge.org
Thu Jun 3 11:21:57 PDT 2010


Revision: 4196
          http://trac.macosforge.org/projects/ruby/changeset/4196
Author:   pthomson at apple.com
Date:     2010-06-03 11:21:54 -0700 (Thu, 03 Jun 2010)
Log Message:
-----------
implement IO#sysread (unbuffered reading).

Modified Paths:
--------------
    MacRuby/trunk/io.c

Modified: MacRuby/trunk/io.c
===================================================================
--- MacRuby/trunk/io.c	2010-06-02 22:35:19 UTC (rev 4195)
+++ MacRuby/trunk/io.c	2010-06-03 18:21:54 UTC (rev 4196)
@@ -1087,6 +1087,44 @@
  *     f.sysread(16)   #=> "This is line one"
  */
 
+static VALUE
+rb_io_sysread(VALUE self, SEL sel, int argc, VALUE *argv)
+{
+    VALUE count, buffer;
+    rb_scan_args(argc, argv, "11", &count, &buffer);
+    const long to_read = NUM2LONG(count);
+
+    rb_io_t *io = ExtractIOStruct(self);
+    rb_io_assert_readable(io);
+
+    // TODO: throw error if the buffer is not empty;
+
+    if (to_read == 0) {
+	return INT2FIX(0);
+    }
+
+    if (!NIL_P(buffer)) {
+	buffer = rb_obj_as_string(buffer);
+    }
+    
+    uint8_t *bytes = xmalloc(to_read);
+    
+    if (read(io->read_fd, bytes, (size_t)to_read) == -1) {
+	bytes = NULL;
+	rb_sys_fail("read(2) failed.");
+    }
+
+    VALUE fresh = rb_bstr_new_with_data(bytes, to_read);
+    if (NIL_P(buffer)) {
+	buffer = fresh;
+    }
+    else {
+	str_replace_with_string(str_need_string(buffer), str_need_string(fresh));
+    }
+    bytes = NULL;
+    return buffer;
+}
+
 /*
  *  call-seq:
  *     ios.read([length [, buffer]])    => string, buffer, or nil
@@ -4596,7 +4634,7 @@
     rb_objc_define_method(rb_cIO, "chars",  rb_io_chars, 0);
 
     rb_objc_define_method(rb_cIO, "syswrite", io_write, 1);
-    rb_objc_define_method(rb_cIO, "sysread",  io_read, -1);
+    rb_objc_define_method(rb_cIO, "sysread",  rb_io_sysread, -1);
 
     rb_objc_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
     rb_define_alias(rb_cIO, "to_i", "fileno");
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100603/e2a6a683/attachment.html>


More information about the macruby-changes mailing list