summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-01-01 11:25:43 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-01-01 11:25:43 +0000
commit571aa4db39067f319ab5a8d7554f6d0985133a11 (patch)
treef2ac6b1c7769d150586c8927aa8e3389feba6aa8
parent8af8ebfb92324f10b2fa62175594f66feb70b0d1 (diff)
* io.c (argf_readpartial): new method ARGF.readpartial.
(io_getpartial): extracted from io_readpartial. (io_readpartial): call io_getpartial. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7703 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--io.c154
2 files changed, 111 insertions, 49 deletions
diff --git a/ChangeLog b/ChangeLog
index fd1d865e755..361f9c3bdaa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sat Jan 1 20:23:02 2005 Tanaka Akira <akr@m17n.org>
+
+ * io.c (argf_readpartial): new method ARGF.readpartial.
+ (io_getpartial): extracted from io_readpartial.
+ (io_readpartial): call io_getpartial.
+
Sat Jan 1 17:44:54 2005 Minero Aoki <aamine@loveruby.net>
* lib/net/http.rb (each_capitalized): should join header field
diff --git a/io.c b/io.c
index fd0b81dd1f0..c1cb7b694fb 100644
--- a/io.c
+++ b/io.c
@@ -1173,6 +1173,61 @@ read_all(fptr, siz, str)
return str;
}
+static VALUE
+io_getpartial(int argc, VALUE *argv, VALUE io)
+{
+ OpenFile *fptr;
+ VALUE length, str;
+ long n, len;
+
+ rb_scan_args(argc, argv, "11", &length, &str);
+
+ if ((len = NUM2LONG(length)) < 0) {
+ rb_raise(rb_eArgError, "negative length %ld given", len);
+ }
+
+ if (NIL_P(str)) {
+ str = rb_str_new(0, len);
+ }
+ else {
+ StringValue(str);
+ rb_str_modify(str);
+ rb_str_resize(str, len);
+ }
+ OBJ_TAINT(str);
+
+ GetOpenFile(io, fptr);
+ rb_io_check_readable(fptr);
+
+ if (len == 0)
+ return str;
+
+ READ_CHECK(fptr);
+ if (RSTRING(str)->len != len) {
+ modified:
+ rb_raise(rb_eRuntimeError, "buffer string modified");
+ }
+ n = read_buffered_data(RSTRING(str)->ptr, len, fptr);
+ if (n <= 0) {
+ again:
+ if (RSTRING(str)->len != len) goto modified;
+ TRAP_BEG;
+ n = read(fptr->fd, RSTRING(str)->ptr, len);
+ TRAP_END;
+ if (n < 0) {
+ if (rb_io_wait_readable(fptr->fd))
+ goto again;
+ rb_sys_fail(fptr->path);
+ }
+ }
+ rb_str_resize(str, n);
+
+ if (n == 0)
+ return Qnil;
+ else
+ return str;
+}
+
/*
* call-seq:
* ios.readpartial(maxlen[, outbuf]) => string, outbuf
@@ -1232,56 +1287,13 @@ io_readpartial(argc, argv, io)
VALUE *argv;
VALUE io;
{
- OpenFile *fptr;
- VALUE length, str;
- long n, len;
-
- rb_scan_args(argc, argv, "11", &length, &str);
-
- if ((len = NUM2LONG(length)) < 0) {
- rb_raise(rb_eArgError, "negative length %ld given", len);
- }
-
- if (NIL_P(str)) {
- str = rb_str_new(0, len);
- }
- else {
- StringValue(str);
- rb_str_modify(str);
- rb_str_resize(str, len);
- }
- OBJ_TAINT(str);
-
- GetOpenFile(io, fptr);
- rb_io_check_readable(fptr);
-
- if (len == 0)
- return str;
-
- READ_CHECK(fptr);
- if (RSTRING(str)->len != len) {
- modified:
- rb_raise(rb_eRuntimeError, "buffer string modified");
- }
- n = read_buffered_data(RSTRING(str)->ptr, len, fptr);
- if (n <= 0) {
- again:
- if (RSTRING(str)->len != len) goto modified;
- TRAP_BEG;
- n = read(fptr->fd, RSTRING(str)->ptr, len);
- TRAP_END;
- if (n < 0) {
- if (rb_io_wait_readable(fptr->fd))
- goto again;
- rb_sys_fail(fptr->path);
- }
- }
- rb_str_resize(str, n);
+ VALUE ret;
- if (n == 0)
+ ret = io_getpartial(argc, argv, io);
+ if (NIL_P(ret))
rb_eof_error();
else
- return str;
+ return ret;
}
/*
@@ -4132,8 +4144,7 @@ rb_io_s_for_fd(argc, argv, klass)
static int binmode = 0;
static VALUE
-argf_forward(
- VALUE *argv)
+argf_forward(VALUE *argv)
{
return rb_funcall3(current_file, ruby_frame->last_func, ruby_frame->argc, argv);
}
@@ -5227,6 +5238,50 @@ argf_read(argc, argv)
}
static VALUE
+argf_readpartial_rescue(VALUE dummy)
+{
+ return Qnil;
+}
+
+static VALUE
+argf_readpartial(int argc, VALUE *argv)
+{
+ VALUE tmp, str, length;
+
+ rb_scan_args(argc, argv, "11", &length, &str);
+ if (!NIL_P(str)) {
+ StringValue(str);
+ argv[1] = str;
+ }
+
+ if (!next_argv()) {
+ rb_str_resize(str, 0);
+ rb_eof_error();
+ }
+ if (TYPE(current_file) != T_FILE) {
+ tmp = rb_rescue2(argf_forward, (VALUE)argv,
+ argf_readpartial_rescue, (VALUE)Qnil,
+ rb_eEOFError, (VALUE)0);
+ }
+ else {
+ tmp = io_getpartial(argc, argv, current_file);
+ }
+ if (NIL_P(tmp)) {
+ if (next_p == -1) {
+ rb_eof_error();
+ }
+ argf_close(current_file);
+ next_p = 1;
+ if (RARRAY(rb_argv)->len == 0)
+ rb_eof_error();
+ if (NIL_P(str))
+ str = rb_str_new(NULL, 0);
+ return str;
+ }
+ return tmp;
+}
+
+static VALUE
argf_getc()
{
VALUE byte;
@@ -5624,6 +5679,7 @@ Init_IO()
rb_define_singleton_method(argf, "each_byte", argf_each_byte, 0);
rb_define_singleton_method(argf, "read", argf_read, -1);
+ rb_define_singleton_method(argf, "readpartial", argf_readpartial, -1);
rb_define_singleton_method(argf, "readlines", rb_f_readlines, -1);
rb_define_singleton_method(argf, "to_a", rb_f_readlines, -1);
rb_define_singleton_method(argf, "gets", rb_f_gets, -1);