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

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 15 19:06:02 PST 2010


Revision: 4908
          http://trac.macosforge.org/projects/ruby/changeset/4908
Author:   watson1978 at gmail.com
Date:     2010-11-15 19:05:59 -0800 (Mon, 15 Nov 2010)
Log Message:
-----------
ARGF.read will be passed the multiple file path.

Test Script:
{{{
require 'tempfile'
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

t1 = Tempfile.new("foo")
t1.puts "1"
t1.puts "2"
t1.close
t2 = Tempfile.new("bar")
t2.puts "3"
t2.puts "4"
t2.close

def ruby(*args)
  args = ['-e', '$>.write($<.read)'] if args.empty?
  ruby = ARGV[0] || "/usr/local/bin/macruby"
  f = IO.popen([ruby] + args, 'r+')
  yield(f)
ensure
  f.close unless !f || f.closed?
end

ruby('-e', "p ARGF.read(5)", t1.path, t2.path) do |f|
  assert_equal("\"1\\n2\\n3\"\n", f.read)
end

puts :ok
}}}

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

Modified: MacRuby/trunk/io.c
===================================================================
--- MacRuby/trunk/io.c	2010-11-16 03:05:55 UTC (rev 4907)
+++ MacRuby/trunk/io.c	2010-11-16 03:05:59 UTC (rev 4908)
@@ -4318,9 +4318,51 @@
 static VALUE
 argf_read(VALUE argf, SEL sel, int argc, VALUE *argv)
 {
-    next_argv();
-    ARGF_FORWARD(0, 0);
-    return io_read(ARGF.current_file, sel, argc, argv);
+    VALUE tmp, str, length;
+    long len = 0;
+
+    rb_scan_args(argc, argv, "02", &length, &str);
+    if (!NIL_P(length)) {
+	len = NUM2LONG(argv[0]);
+    }
+    if (!NIL_P(str)) {
+	StringValue(str);
+	rb_str_resize(str,0);
+	argv[1] = Qnil;
+    }
+
+  retry:
+    if (!next_argv()) {
+	return str;
+    }
+    if (ARGF_GENERIC_INPUT_P()) {
+	tmp = argf_forward(argf, sel, argc, argv);
+    }
+    else {
+	tmp = io_read(ARGF.current_file, sel, argc, argv);
+    }
+    if (NIL_P(str)) {
+	str = tmp;
+    }
+    else if (!NIL_P(tmp)) {
+	rb_str_append(str, tmp);
+    }
+
+    if (NIL_P(tmp) || NIL_P(length)) {
+	if (ARGF.next_p != -1) {
+	    argf_close(ARGF.current_file, sel);
+	    ARGF.next_p = 1;
+	    goto retry;
+	}
+    }
+    else if (argc >= 1) {
+	if (RSTRING_LEN(str) < len) {
+	    len -= RSTRING_LEN(str);
+	    argv[0] = INT2NUM(len);
+	    goto retry;
+	}
+    }
+    return str;
 }
 
 struct argf_call_arg {
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101115/da93cb9a/attachment-0001.html>


More information about the macruby-changes mailing list