ruby-changes:7963
From: matz <ko1@a...>
Date: Tue, 23 Sep 2008 19:48:31 +0900 (JST)
Subject: [ruby-changes:7963] Ruby:r19485 (trunk): * io.c (rb_io_s_binread): a new method to read binary chunk from a
matz 2008-09-23 19:48:17 +0900 (Tue, 23 Sep 2008) New Revision: 19485 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19485 Log: * io.c (rb_io_s_binread): a new method to read binary chunk from a file. [ruby-core:18674] * io.c (open_key_args): wrong permission specified for read-open. Modified files: trunk/ChangeLog trunk/io.c Index: ChangeLog =================================================================== --- ChangeLog (revision 19484) +++ ChangeLog (revision 19485) @@ -13,6 +13,13 @@ * rational.c: ditto. +Tue Sep 23 19:20:59 2008 Yukihiro Matsumoto <matz@r...> + + * io.c (rb_io_s_binread): a new method to read binary chunk from a + file. [ruby-core:18674] + + * io.c (open_key_args): wrong permission specified for read-open. + Tue Sep 23 18:24:34 2008 Tanaka Akira <akr@f...> * hash.c (rb_obj_is_proc): declaration moved for rdoc. Index: io.c =================================================================== --- io.c (revision 19484) +++ io.c (revision 19485) @@ -6824,7 +6824,7 @@ arg->io = rb_io_open_with_args(RARRAY_LEN(args), RARRAY_PTR(args)); return; } - arg->io = rb_io_open(argv[0], Qnil, INT2NUM(O_RDONLY), opt); + arg->io = rb_io_open(argv[0], Qnil, Qnil, opt); } static VALUE @@ -6963,6 +6963,37 @@ return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io); } +/* + * call-seq: + * IO.binread(name, [length [, offset]] ) => string + * + * Opens the file, optionally seeks to the given offset, then returns + * <i>length</i> bytes (defaulting to the rest of the file). + * <code>read</code> ensures the file is closed before returning. + * The open mode would be "rb:ASCII-8BIT". + * + * IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n" + * IO.binread("testfile", 20) #=> "This is line one\nThi" + * IO.binread("testfile", 20, 10) #=> "ne one\nThis is line " + */ + +static VALUE +rb_io_s_binread(int argc, VALUE *argv, VALUE io) +{ + VALUE offset; + struct foreach_arg arg; + + rb_scan_args(argc, argv, "12", NULL, NULL, &offset); + arg.io = rb_io_open(argv[0], rb_str_new_cstr("rb:ASCII-8BIT"), Qnil, Qnil); + if (NIL_P(arg.io)) return Qnil; + arg.argv = argv+1; + arg.argc = (argc > 2) ? 2 : argc; + if (!NIL_P(offset)) { + rb_io_seek(arg.io, offset, SEEK_SET); + } + return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io); +} + struct copy_stream_struct { VALUE src; VALUE dst; @@ -8185,6 +8216,7 @@ rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1); rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1); rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1); + rb_define_singleton_method(rb_cIO, "binread", rb_io_s_binread, -1); rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1); rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1); rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/