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

source_changes at macosforge.org source_changes at macosforge.org
Wed Jul 7 13:04:02 PDT 2010


Revision: 4324
          http://trac.macosforge.org/projects/ruby/changeset/4324
Author:   pthomson at apple.com
Date:     2010-07-07 13:04:02 -0700 (Wed, 07 Jul 2010)
Log Message:
-----------
Implement IO#syswrite.

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

Modified: MacRuby/trunk/io.c
===================================================================
--- MacRuby/trunk/io.c	2010-07-07 17:56:01 UTC (rev 4323)
+++ MacRuby/trunk/io.c	2010-07-07 20:04:02 UTC (rev 4324)
@@ -390,6 +390,35 @@
  *     f.syswrite("ABCDEF")   #=> 6
  */
 
+
+static VALUE
+rb_io_syswrite(VALUE io, SEL sel, VALUE data)
+{
+    rb_secure(4);
+    rb_io_t *io_struct = ExtractIOStruct(io);
+    rb_io_assert_writable(io_struct);
+
+    data = rb_str_bstr(rb_obj_as_string(data));
+    
+    if (CFDataGetLength(io_struct->buf) > 0) {
+	rb_warn("Calling #syswrite on buffered I/O may lead to unexpected results");
+    }
+    
+    const uint8_t *buffer = rb_bstr_bytes(data);
+    const long length = rb_bstr_length(data);
+    
+    if (length == 0) {
+        return INT2FIX(0);
+    }
+    
+    ssize_t result = write(io_struct->write_fd, buffer, length);
+    if (result == -1) {
+	rb_sys_fail("write(2) failed.");
+    }
+    
+    return LONG2FIX(result);
+}
+
 /*
  *  call-seq:
  *     ios.write(string)    => integer
@@ -4636,7 +4665,7 @@
     rb_objc_define_method(rb_cIO, "bytes",  rb_io_bytes, 0);
     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, "syswrite", rb_io_syswrite, 1);
     rb_objc_define_method(rb_cIO, "sysread",  rb_io_sysread, -1);
 
     rb_objc_define_method(rb_cIO, "fileno", rb_io_fileno, 0);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100707/f4c669a4/attachment.html>


More information about the macruby-changes mailing list