io-seek-whence-symbol.patch

Akira Tanaka, 06/25/2012 07:31 PM

Download (3.53 KB)

View differences:

io.c (working copy)
140 140
static ID id_write, id_read, id_getc, id_flush, id_readpartial, id_set_encoding;
141 141
static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args;
142 142
static VALUE sym_textmode, sym_binmode, sym_autoclose;
143
static VALUE sym_set, sym_cur, sym_end;
143 144

  
144 145
struct argf {
145 146
    VALUE filename, current_file;
......
1443 1444
    return INT2FIX(0);
1444 1445
}
1445 1446

  
1447
static int
1448
interpret_seek_whence(VALUE vwhence)
1449
{
1450
    if (vwhence == sym_set)
1451
        return SEEK_SET;
1452
    if (vwhence == sym_cur)
1453
        return SEEK_CUR;
1454
    if (vwhence == sym_end)
1455
        return SEEK_END;
1456
    return NUM2INT(vwhence);
1457
}
1458

  
1446 1459
/*
1447 1460
 *  call-seq:
1448 1461
 *     ios.seek(amount, whence=IO::SEEK_SET)  ->  0
......
1450 1463
 *  Seeks to a given offset <i>anInteger</i> in the stream according to
1451 1464
 *  the value of <i>whence</i>:
1452 1465
 *
1453
 *    IO::SEEK_CUR  | Seeks to _amount_ plus current position
1454
 *    --------------+----------------------------------------------------
1455
 *    IO::SEEK_END  | Seeks to _amount_ plus end of stream (you probably
1456
 *                  | want a negative value for _amount_)
1457
 *    --------------+----------------------------------------------------
1458
 *    IO::SEEK_SET  | Seeks to the absolute location given by _amount_
1466
 *    :cur or IO::SEEK_CUR  | Seeks to _amount_ plus current position
1467
 *    ----------------------+--------------------------------------------------
1468
 *    :end or IO::SEEK_END  | Seeks to _amount_ plus end of stream (you
1469
 *                          | probably want a negative value for _amount_)
1470
 *    ----------------------+--------------------------------------------------
1471
 *    :set or IO::SEEK_SET  | Seeks to the absolute location given by _amount_
1459 1472
 *
1460 1473
 *  Example:
1461 1474
 *
......
1471 1484
    int whence = SEEK_SET;
1472 1485

  
1473 1486
    if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
1474
	whence = NUM2INT(ptrname);
1487
	whence = interpret_seek_whence(ptrname);
1475 1488
    }
1476 1489

  
1477 1490
    return rb_io_seek(io, offset, whence);
......
4233 4246
    off_t pos;
4234 4247

  
4235 4248
    if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
4236
	whence = NUM2INT(ptrname);
4249
	whence = interpret_seek_whence(ptrname);
4237 4250
    }
4238 4251
    pos = NUM2OFFT(offset);
4239 4252
    GetOpenFile(io, fptr);
......
11623 11636
    sym_willneed = ID2SYM(rb_intern("willneed"));
11624 11637
    sym_dontneed = ID2SYM(rb_intern("dontneed"));
11625 11638
    sym_noreuse = ID2SYM(rb_intern("noreuse"));
11639
    sym_set = ID2SYM(rb_intern("set"));
11640
    sym_cur = ID2SYM(rb_intern("cur"));
11641
    sym_end = ID2SYM(rb_intern("end"));
11626 11642
}
test/ruby/test_io.rb (working copy)
1477 1477
    }
1478 1478
  end
1479 1479

  
1480
  def test_seek_symwhence
1481
    make_tempfile {|t|
1482
      open(t.path) { |f|
1483
        f.seek(9, :set)
1484
        assert_equal("az\n", f.read)
1485
      }
1486

  
1487
      open(t.path) { |f|
1488
        f.seek(-4, :end)
1489
        assert_equal("baz\n", f.read)
1490
      }
1491

  
1492
      open(t.path) { |f|
1493
        assert_equal("foo\n", f.gets)
1494
        f.seek(2, :cur)
1495
        assert_equal("r\nbaz\n", f.read)
1496
      }
1497
    }
1498
  end
1499

  
1480 1500
  def test_sysseek
1481 1501
    t = make_tempfile
1482 1502