Skip to content

Commit 6b90013

Browse files
author
matz
committed
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19485 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 743765c commit 6b90013

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Tue Sep 23 19:21:03 2008 Tadayoshi Funaba <tadf@dotrb.org>
1313

1414
* rational.c: ditto.
1515

16+
Tue Sep 23 19:20:59 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
17+
18+
* io.c (rb_io_s_binread): a new method to read binary chunk from a
19+
file. [ruby-core:18674]
20+
21+
* io.c (open_key_args): wrong permission specified for read-open.
22+
1623
Tue Sep 23 18:24:34 2008 Tanaka Akira <akr@fsij.org>
1724

1825
* hash.c (rb_obj_is_proc): declaration moved for rdoc.

io.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6824,7 +6824,7 @@ open_key_args(int argc, VALUE *argv, struct foreach_arg *arg)
68246824
arg->io = rb_io_open_with_args(RARRAY_LEN(args), RARRAY_PTR(args));
68256825
return;
68266826
}
6827-
arg->io = rb_io_open(argv[0], Qnil, INT2NUM(O_RDONLY), opt);
6827+
arg->io = rb_io_open(argv[0], Qnil, Qnil, opt);
68286828
}
68296829

68306830
static VALUE
@@ -6963,6 +6963,37 @@ rb_io_s_read(int argc, VALUE *argv, VALUE io)
69636963
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
69646964
}
69656965

6966+
/*
6967+
* call-seq:
6968+
* IO.binread(name, [length [, offset]] ) => string
6969+
*
6970+
* Opens the file, optionally seeks to the given offset, then returns
6971+
* <i>length</i> bytes (defaulting to the rest of the file).
6972+
* <code>read</code> ensures the file is closed before returning.
6973+
* The open mode would be "rb:ASCII-8BIT".
6974+
*
6975+
* IO.binread("testfile") #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
6976+
* IO.binread("testfile", 20) #=> "This is line one\nThi"
6977+
* IO.binread("testfile", 20, 10) #=> "ne one\nThis is line "
6978+
*/
6979+
6980+
static VALUE
6981+
rb_io_s_binread(int argc, VALUE *argv, VALUE io)
6982+
{
6983+
VALUE offset;
6984+
struct foreach_arg arg;
6985+
6986+
rb_scan_args(argc, argv, "12", NULL, NULL, &offset);
6987+
arg.io = rb_io_open(argv[0], rb_str_new_cstr("rb:ASCII-8BIT"), Qnil, Qnil);
6988+
if (NIL_P(arg.io)) return Qnil;
6989+
arg.argv = argv+1;
6990+
arg.argc = (argc > 2) ? 2 : argc;
6991+
if (!NIL_P(offset)) {
6992+
rb_io_seek(arg.io, offset, SEEK_SET);
6993+
}
6994+
return rb_ensure(io_s_read, (VALUE)&arg, rb_io_close, arg.io);
6995+
}
6996+
69666997
struct copy_stream_struct {
69676998
VALUE src;
69686999
VALUE dst;
@@ -8185,6 +8216,7 @@ Init_IO(void)
81858216
rb_define_singleton_method(rb_cIO, "foreach", rb_io_s_foreach, -1);
81868217
rb_define_singleton_method(rb_cIO, "readlines", rb_io_s_readlines, -1);
81878218
rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
8219+
rb_define_singleton_method(rb_cIO, "binread", rb_io_s_binread, -1);
81888220
rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
81898221
rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, -1);
81908222
rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy