mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* io.c (rb_io_seek_m): Accept :CUR, :END, :SET as "whence" argument.
(interpret_seek_whence): New function. [ruby-dev:45818] [Feature #6643] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40084 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
2767139e05
commit
b81ea661db
4 changed files with 54 additions and 8 deletions
|
@ -1,3 +1,9 @@
|
|||
Wed Apr 3 21:23:29 2013 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* io.c (rb_io_seek_m): Accept :CUR, :END, :SET as "whence" argument.
|
||||
(interpret_seek_whence): New function.
|
||||
[ruby-dev:45818] [Feature #6643]
|
||||
|
||||
Wed Apr 3 20:52:49 2013 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* process.c: Describe the behavior which Ruby invokes a commandline
|
||||
|
|
4
NEWS
4
NEWS
|
@ -18,6 +18,10 @@ with all sufficient information, see the ChangeLog file.
|
|||
* added environment variable:
|
||||
* RUBY_HEAP_SLOTS_GROWTH_FACTOR: growth rate of the heap.
|
||||
|
||||
* IO
|
||||
* extended methods:
|
||||
* IO#seek accepts symbols (:CUR, :END, :SET) for 2nd argument.
|
||||
|
||||
* Mutex
|
||||
* misc
|
||||
* Mutex#owned? is no longer experimental.
|
||||
|
|
32
io.c
32
io.c
|
@ -148,6 +148,7 @@ static VALUE argf;
|
|||
static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding;
|
||||
static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
|
||||
static VALUE sym_textmode, sym_binmode, sym_autoclose;
|
||||
static VALUE sym_SET, sym_CUR, sym_END;
|
||||
|
||||
struct argf {
|
||||
VALUE filename, current_file;
|
||||
|
@ -1533,6 +1534,18 @@ rb_io_seek(VALUE io, VALUE offset, int whence)
|
|||
return INT2FIX(0);
|
||||
}
|
||||
|
||||
static int
|
||||
interpret_seek_whence(VALUE vwhence)
|
||||
{
|
||||
if (vwhence == sym_SET)
|
||||
return SEEK_SET;
|
||||
if (vwhence == sym_CUR)
|
||||
return SEEK_CUR;
|
||||
if (vwhence == sym_END)
|
||||
return SEEK_END;
|
||||
return NUM2INT(vwhence);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* ios.seek(amount, whence=IO::SEEK_SET) -> 0
|
||||
|
@ -1540,12 +1553,12 @@ rb_io_seek(VALUE io, VALUE offset, int whence)
|
|||
* Seeks to a given offset <i>anInteger</i> in the stream according to
|
||||
* the value of <i>whence</i>:
|
||||
*
|
||||
* IO::SEEK_CUR | Seeks to _amount_ plus current position
|
||||
* --------------+----------------------------------------------------
|
||||
* IO::SEEK_END | Seeks to _amount_ plus end of stream (you probably
|
||||
* | want a negative value for _amount_)
|
||||
* --------------+----------------------------------------------------
|
||||
* IO::SEEK_SET | Seeks to the absolute location given by _amount_
|
||||
* :CUR or IO::SEEK_CUR | Seeks to _amount_ plus current position
|
||||
* ----------------------+--------------------------------------------------
|
||||
* :END or IO::SEEK_END | Seeks to _amount_ plus end of stream (you
|
||||
* | probably want a negative value for _amount_)
|
||||
* ----------------------+--------------------------------------------------
|
||||
* :SET or IO::SEEK_SET | Seeks to the absolute location given by _amount_
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
|
@ -1561,7 +1574,7 @@ rb_io_seek_m(int argc, VALUE *argv, VALUE io)
|
|||
int whence = SEEK_SET;
|
||||
|
||||
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
|
||||
whence = NUM2INT(ptrname);
|
||||
whence = interpret_seek_whence(ptrname);
|
||||
}
|
||||
|
||||
return rb_io_seek(io, offset, whence);
|
||||
|
@ -4418,7 +4431,7 @@ rb_io_sysseek(int argc, VALUE *argv, VALUE io)
|
|||
off_t pos;
|
||||
|
||||
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
|
||||
whence = NUM2INT(ptrname);
|
||||
whence = interpret_seek_whence(ptrname);
|
||||
}
|
||||
pos = NUM2OFFT(offset);
|
||||
GetOpenFile(io, fptr);
|
||||
|
@ -11904,4 +11917,7 @@ Init_IO(void)
|
|||
sym_willneed = ID2SYM(rb_intern("willneed"));
|
||||
sym_dontneed = ID2SYM(rb_intern("dontneed"));
|
||||
sym_noreuse = ID2SYM(rb_intern("noreuse"));
|
||||
sym_SET = ID2SYM(rb_intern("SET"));
|
||||
sym_CUR = ID2SYM(rb_intern("CUR"));
|
||||
sym_END = ID2SYM(rb_intern("END"));
|
||||
}
|
||||
|
|
|
@ -1514,6 +1514,26 @@ class TestIO < Test::Unit::TestCase
|
|||
}
|
||||
end
|
||||
|
||||
def test_seek_symwhence
|
||||
make_tempfile {|t|
|
||||
open(t.path) { |f|
|
||||
f.seek(9, :SET)
|
||||
assert_equal("az\n", f.read)
|
||||
}
|
||||
|
||||
open(t.path) { |f|
|
||||
f.seek(-4, :END)
|
||||
assert_equal("baz\n", f.read)
|
||||
}
|
||||
|
||||
open(t.path) { |f|
|
||||
assert_equal("foo\n", f.gets)
|
||||
f.seek(2, :CUR)
|
||||
assert_equal("r\nbaz\n", f.read)
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_sysseek
|
||||
make_tempfile {|t|
|
||||
open(t.path) do |f|
|
||||
|
|
Loading…
Reference in a new issue