mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
8af8ebfb92
commit
571aa4db39
2 changed files with 111 additions and 49 deletions
|
@ -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>
|
Sat Jan 1 17:44:54 2005 Minero Aoki <aamine@loveruby.net>
|
||||||
|
|
||||||
* lib/net/http.rb (each_capitalized): should join header field
|
* lib/net/http.rb (each_capitalized): should join header field
|
||||||
|
|
154
io.c
154
io.c
|
@ -1173,6 +1173,61 @@ read_all(fptr, siz, str)
|
||||||
return 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:
|
* call-seq:
|
||||||
* ios.readpartial(maxlen[, outbuf]) => string, outbuf
|
* ios.readpartial(maxlen[, outbuf]) => string, outbuf
|
||||||
|
@ -1232,56 +1287,13 @@ io_readpartial(argc, argv, io)
|
||||||
VALUE *argv;
|
VALUE *argv;
|
||||||
VALUE io;
|
VALUE io;
|
||||||
{
|
{
|
||||||
OpenFile *fptr;
|
VALUE ret;
|
||||||
VALUE length, str;
|
|
||||||
long n, len;
|
|
||||||
|
|
||||||
rb_scan_args(argc, argv, "11", &length, &str);
|
ret = io_getpartial(argc, argv, io);
|
||||||
|
if (NIL_P(ret))
|
||||||
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)
|
|
||||||
rb_eof_error();
|
rb_eof_error();
|
||||||
else
|
else
|
||||||
return str;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -4132,8 +4144,7 @@ rb_io_s_for_fd(argc, argv, klass)
|
||||||
static int binmode = 0;
|
static int binmode = 0;
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
argf_forward(
|
argf_forward(VALUE *argv)
|
||||||
VALUE *argv)
|
|
||||||
{
|
{
|
||||||
return rb_funcall3(current_file, ruby_frame->last_func, ruby_frame->argc, argv);
|
return rb_funcall3(current_file, ruby_frame->last_func, ruby_frame->argc, argv);
|
||||||
}
|
}
|
||||||
|
@ -5226,6 +5237,50 @@ argf_read(argc, argv)
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
static VALUE
|
||||||
argf_getc()
|
argf_getc()
|
||||||
{
|
{
|
||||||
|
@ -5624,6 +5679,7 @@ Init_IO()
|
||||||
rb_define_singleton_method(argf, "each_byte", argf_each_byte, 0);
|
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, "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, "readlines", rb_f_readlines, -1);
|
||||||
rb_define_singleton_method(argf, "to_a", 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);
|
rb_define_singleton_method(argf, "gets", rb_f_gets, -1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue