summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-03 12:25:09 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-03 12:25:09 +0000
commitb81ea661dbc3c31073de782a8b0b9f3f86e3de5e (patch)
tree4290400ce4aa2b7b0f74904ae584061ae199f647 /io.c
parent2767139e056cb9b8585634f8eb9d174589f85905 (diff)
* 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
Diffstat (limited to 'io.c')
-rw-r--r--io.c32
1 files changed, 24 insertions, 8 deletions
diff --git a/io.c b/io.c
index 2cd1325b03..ce6dc70af7 100644
--- a/io.c
+++ b/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"));
}