[macruby-changes] [912] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Fri Mar 13 19:38:39 PDT 2009


Revision: 912
          http://trac.macosforge.org/projects/ruby/changeset/912
Author:   lsansonetti at apple.com
Date:     2009-03-13 19:38:38 -0700 (Fri, 13 Mar 2009)
Log Message:
-----------
implemented IO.readlines (easy cases)

Modified Paths:
--------------
    MacRuby/branches/experimental/io.c
    MacRuby/branches/experimental/string.c

Modified: MacRuby/branches/experimental/io.c
===================================================================
--- MacRuby/branches/experimental/io.c	2009-03-14 02:19:57 UTC (rev 911)
+++ MacRuby/branches/experimental/io.c	2009-03-14 02:38:38 UTC (rev 912)
@@ -2910,30 +2910,6 @@
 
 /*
  *  call-seq:
- *     IO.readlines(name, sep=$/)     => array
- *     IO.readlines(name, limit)      => array
- *     IO.readlines(name, sep, limit) => array
- *
- *  Reads the entire file specified by <i>name</i> as individual
- *  lines, and returns those lines in an array. Lines are separated by
- *  <i>sep</i>.
- *
- *     a = IO.readlines("testfile")
- *     a[0]   #=> "This is line one\n"
- *
- *  If the last argument is a hash, it's the keyword argument to open.
- *  See <code>IO.read</code> for detail.
- *
- */
-
-static VALUE
-rb_io_s_readlines(VALUE recv, SEL sel, int argc, VALUE *argv)
-{
-rb_notimplement();
-}
-
-/*
- *  call-seq:
  *     IO.read(name, [length [, offset]] )   => string
  *     IO.read(name, [length [, offset]], opt)   => string
  *
@@ -3026,6 +3002,93 @@
 
 /*
  *  call-seq:
+ *     IO.readlines(name, sep=$/)     => array
+ *     IO.readlines(name, limit)      => array
+ *     IO.readlines(name, sep, limit) => array
+ *
+ *  Reads the entire file specified by <i>name</i> as individual
+ *  lines, and returns those lines in an array. Lines are separated by
+ *  <i>sep</i>.
+ *
+ *     a = IO.readlines("testfile")
+ *     a[0]   #=> "This is line one\n"
+ *
+ *  If the last argument is a hash, it's the keyword argument to open.
+ *  See <code>IO.read</code> for detail.
+ *
+ */
+
+static VALUE
+rb_io_s_readlines(VALUE recv, SEL sel, int argc, VALUE *argv)
+{
+    VALUE fname, arg2, arg3, opt;
+
+    rb_scan_args(argc, argv, "13", &fname, &arg2, &arg3, &opt);
+
+    // Read everything.
+    VALUE io_s_read_args[] = { fname, Qnil, Qnil, opt };
+    VALUE outbuf = rb_io_s_read(recv, 0, 4, io_s_read_args);
+
+    // Prepare arguments.
+    VALUE rs, limit;
+    if (argc == 1) {
+	rs = rb_rs;
+	limit = Qnil;
+    }
+    else {
+	if (!NIL_P(arg2) && NIL_P(arg3)) {
+	    if (TYPE(arg2) == T_STRING) {
+		rs = arg2;
+		limit = Qnil;
+	    }
+	    else {
+		limit = arg2;
+		rs = rb_rs;
+	    }
+	}
+	else {
+	    StringValue(arg2);
+	    rs = arg2;
+	    limit = arg3;
+	}
+    }
+
+    CFMutableDataRef data = rb_bytestring_wrapped_data(outbuf);
+    UInt8 *buf = CFDataGetMutableBytePtr(data);
+    const long length = CFDataGetLength(data);
+
+    VALUE ary = rb_ary_new();
+
+    if (RSTRING_LEN(rs) == 1) {
+	UInt8 byte = RSTRING_PTR(rs)[0];
+
+	long pos = 0;
+	void *ptr;
+	do {
+	    ptr = memchr(&buf[pos], byte, length - pos);
+	    long s;
+	    if (ptr == NULL) {
+		// Remaining data.
+		s = length - pos;
+	    }
+	    else {
+		s =  (long)ptr - (long)&buf[pos] + 1;
+	    }
+	    rb_ary_push(ary, rb_bytestring_new_with_data(&buf[pos], s));
+	    pos += s; 
+	}
+	while (ptr != NULL);
+    }
+    else {
+	// TODO
+	abort();
+    }	
+
+    return ary;
+}
+
+/*
+ *  call-seq:
  *     IO.copy_stream(src, dst)
  *     IO.copy_stream(src, dst, copy_length)
  *     IO.copy_stream(src, dst, copy_length, src_offset)
@@ -3538,6 +3601,7 @@
 
     rb_global_variable(&rb_default_rs);
     rb_rs = rb_default_rs = rb_str_new2("\n");
+    rb_objc_retain((void *)rb_rs);
     rb_output_rs = Qnil;
     OBJ_FREEZE(rb_default_rs);	/* avoid modifying RS_default */
     rb_define_hooked_variable("$/", &rb_rs, 0, rb_str_setter);

Modified: MacRuby/branches/experimental/string.c
===================================================================
--- MacRuby/branches/experimental/string.c	2009-03-14 02:19:57 UTC (rev 911)
+++ MacRuby/branches/experimental/string.c	2009-03-14 02:38:38 UTC (rev 912)
@@ -5395,10 +5395,14 @@
 rb_bytestring_alloc(VALUE klass, SEL sel)
 {
     VALUE bstr = (VALUE)class_createInstance((Class)rb_cByteString, 0);
+
     CFMutableDataRef data = CFDataCreateMutable(NULL, 0);
-    CFDataIncreaseLength(data, 1);
+    //CFDataIncreaseLength(data, 1);
+
     // TODO: Maybe we should access this with wrappedDataOffset...
-    object_setInstanceVariable((id)bstr, "wrappedData", (void*)CFMakeCollectable(data));
+    object_setInstanceVariable((id)bstr, "wrappedData", (void *)data);
+    
+    CFMakeCollectable(data);
     return bstr;
 }
 
@@ -5411,6 +5415,14 @@
     return bs;
 }
 
+VALUE
+rb_bytestring_new_with_data(UInt8 *buf, long size)
+{
+    VALUE v = rb_bytestring_new();
+    CFDataAppendBytes(rb_bytestring_wrapped_data(v), buf, size);
+    return v;
+}
+
 static VALUE
 rb_bytestring_initialize(VALUE recv, SEL sel, int argc, VALUE *argv)
 {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090313/4a6caab2/attachment.html>


More information about the macruby-changes mailing list