patch3.diff

Masaki Matsushita, 11/11/2012 11:15 PM

Download (4.55 KB)

View differences:

io.c
1448 1448
    return INT2FIX(0);
1449 1449
}
1450 1450

  
1451
static VALUE sym_set, sym_cur, sym_current, sym_end;
1452
static VALUE sym_set_u, sym_cur_u, sym_current_u, sym_end_u;
1453

  
1454
static int
1455
sym_to_whence(VALUE ptrname)
1456
{
1457
    if (ptrname == sym_set || ptrname == sym_set_u){
1458
	return SEEK_SET;
1459
    }
1460
    else if (ptrname == sym_cur || ptrname == sym_cur_u ||
1461
	     ptrname == sym_current || ptrname == sym_current_u) {
1462
	return SEEK_CUR;
1463
    }
1464
    else if (ptrname == sym_end || ptrname == sym_end_u) {
1465
	return SEEK_END;
1466
    }
1467
    rb_raise(rb_eArgError, "unknown whence: %s", rb_id2name(SYM2ID(ptrname)));
1468
}
1469

  
1451 1470
/*
1452 1471
 *  call-seq:
1453 1472
 *     ios.seek(amount, whence=IO::SEEK_SET)  ->  0
......
1455 1474
 *  Seeks to a given offset <i>anInteger</i> in the stream according to
1456 1475
 *  the value of <i>whence</i>:
1457 1476
 *
1458
 *    IO::SEEK_CUR  | Seeks to _amount_ plus current position
1459
 *    --------------+----------------------------------------------------
1460
 *    IO::SEEK_END  | Seeks to _amount_ plus end of stream (you probably
1461
 *                  | want a negative value for _amount_)
1462
 *    --------------+----------------------------------------------------
1463
 *    IO::SEEK_SET  | Seeks to the absolute location given by _amount_
1477
 *    :cur or IO::SEEK_CUR  | Seeks to _amount_ plus current position
1478
 *    ----------------------+--------------------------------------------------
1479
 *    :end or IO::SEEK_END  | Seeks to _amount_ plus end of stream (you
1480
 *                          | probably want a negative value for _amount_)
1481
 *    ----------------------+--------------------------------------------------
1482
 *    :set or IO::SEEK_SET  | Seeks to the absolute location given by _amount_
1464 1483
 *
1465 1484
 *  Example:
1466 1485
 *
......
1476 1495
    int whence = SEEK_SET;
1477 1496

  
1478 1497
    if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
1479
	whence = NUM2INT(ptrname);
1498
	switch (TYPE(ptrname)) {
1499
	  case T_FIXNUM:
1500
	    whence = NUM2INT(ptrname);
1501
	    break;
1502
	  case T_SYMBOL:
1503
	    whence = sym_to_whence(ptrname);
1504
	    break;
1505
	  default:
1506
	    rb_raise(rb_eTypeError, "whence must be an Integer or Symbol: %s given", rb_obj_classname(ptrname));
1507
	}
1480 1508
    }
1481 1509

  
1482 1510
    return rb_io_seek(io, offset, whence);
......
11698 11726
    sym_willneed = ID2SYM(rb_intern("willneed"));
11699 11727
    sym_dontneed = ID2SYM(rb_intern("dontneed"));
11700 11728
    sym_noreuse = ID2SYM(rb_intern("noreuse"));
11729
    sym_set = ID2SYM(rb_intern("set"));
11730
    sym_cur = ID2SYM(rb_intern("cur"));
11731
    sym_current = ID2SYM(rb_intern("current"));
11732
    sym_end = ID2SYM(rb_intern("end"));
11733
    sym_set_u = ID2SYM(rb_intern("SET"));
11734
    sym_cur_u = ID2SYM(rb_intern("CUR"));
11735
    sym_current_u = ID2SYM(rb_intern("CURRENT"));
11736
    sym_end_u = ID2SYM(rb_intern("END"));
11701 11737
}
test/ruby/test_io.rb
1483 1483
    }
1484 1484
  end
1485 1485

  
1486
  def test_seek_sym
1487
    make_tempfile {|t|
1488
      open(t.path) { |f|
1489
        f.seek(9, :set)
1490
        assert_equal("az\n", f.read)
1491
      }
1492

  
1493
      open(t.path) { |f|
1494
        f.seek(-4, :end)
1495
        assert_equal("baz\n", f.read)
1496
      }
1497

  
1498
      open(t.path) { |f|
1499
        assert_equal("foo\n", f.gets)
1500
        f.seek(2, :cur)
1501
        assert_equal("r\nbaz\n", f.read)
1502
      }
1503

  
1504
      open(t.path) { |f|
1505
        assert_equal("foo\n", f.gets)
1506
        f.seek(2, :current)
1507
        assert_equal("r\nbaz\n", f.read)
1508
      }
1509

  
1510
      open(t.path) { |f|
1511
        f.seek(9, :SET)
1512
        assert_equal("az\n", f.read)
1513
      }
1514

  
1515
      open(t.path) { |f|
1516
        f.seek(-4, :END)
1517
        assert_equal("baz\n", f.read)
1518
      }
1519

  
1520
      open(t.path) { |f|
1521
        assert_equal("foo\n", f.gets)
1522
        f.seek(2, :CUR)
1523
        assert_equal("r\nbaz\n", f.read)
1524
      }
1525

  
1526
      open(t.path) { |f|
1527
        assert_equal("foo\n", f.gets)
1528
        f.seek(2, :CURRENT)
1529
        assert_equal("r\nbaz\n", f.read)
1530
      }
1531

  
1532
      open(t.path) { |f|
1533
        assert_raise(ArgumentError) do
1534
          f.seek(42, :hoge)
1535
        end
1536
      }
1537

  
1538
      open(t.path) { |f|
1539
        assert_raise(ArgumentError) do
1540
          f.seek(42, :HOGE)
1541
        end
1542
      }
1543

  
1544
      open(t.path) { |f|
1545
        assert_raise(TypeError) do
1546
          f.seek(42, "hoge")
1547
        end
1548
      }
1549
    }
1550
  end
1551

  
1486 1552
  def test_sysseek
1487 1553
    make_tempfile {|t|
1488 1554
      open(t.path) do |f|