Project

General

Profile

Feature #6643 » io-seek-whence-symbol.patch

akr (Akira Tanaka), 06/25/2012 07:31 PM

View differences:

io.c (working copy)
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;
......
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
......
* 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:
*
......
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);
......
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);
......
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"));
}
test/ruby/test_io.rb (working copy)
}
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
t = make_tempfile
(1-1/4)