patch2.diff

Masaki Matsushita, 10/22/2012 11:08 AM

Download (4.44 KB)

View differences:

io.c
1445 1445
    return INT2FIX(0);
1446 1446
}
1447 1447

  
1448
static VALUE sym_set, sym_cur, sym_current, sym_end;
1449

  
1450
static int
1451
sym_to_whence(VALUE ptrname)
1452
{
1453
  retry:
1454
    if (ptrname == sym_set){
1455
	return SEEK_SET;
1456
    }
1457
    else if (ptrname == sym_cur || ptrname == sym_current) {
1458
	return SEEK_CUR;
1459
    }
1460
    else if (ptrname == sym_end) {
1461
	return SEEK_END;
1462
    }
1463
    else {
1464
	VALUE lower_ptrname;
1465

  
1466
	lower_ptrname = rb_funcall2(rb_sym_to_s(ptrname), rb_intern("downcase!"), 0, 0);
1467
	if (!NIL_P(lower_ptrname)) {
1468
	    ptrname = rb_str_intern(lower_ptrname);
1469
	    goto retry;
1470
	}
1471
    }
1472
    rb_raise(rb_eArgError, "unknown whence: %s", rb_id2name(SYM2ID(ptrname)));
1473
}
1474

  
1448 1475
/*
1449 1476
 *  call-seq:
1450 1477
 *     ios.seek(amount, whence=IO::SEEK_SET)  ->  0
......
1452 1479
 *  Seeks to a given offset <i>anInteger</i> in the stream according to
1453 1480
 *  the value of <i>whence</i>:
1454 1481
 *
1455
 *    IO::SEEK_CUR  | Seeks to _amount_ plus current position
1456
 *    --------------+----------------------------------------------------
1457
 *    IO::SEEK_END  | Seeks to _amount_ plus end of stream (you probably
1458
 *                  | want a negative value for _amount_)
1459
 *    --------------+----------------------------------------------------
1460
 *    IO::SEEK_SET  | Seeks to the absolute location given by _amount_
1482
 *    :cur or IO::SEEK_CUR  | Seeks to _amount_ plus current position
1483
 *    ----------------------+--------------------------------------------------
1484
 *    :end or IO::SEEK_END  | Seeks to _amount_ plus end of stream (you
1485
 *                          | probably want a negative value for _amount_)
1486
 *    ----------------------+--------------------------------------------------
1487
 *    :set or IO::SEEK_SET  | Seeks to the absolute location given by _amount_
1461 1488
 *
1462 1489
 *  Example:
1463 1490
 *
......
1473 1500
    int whence = SEEK_SET;
1474 1501

  
1475 1502
    if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
1476
	whence = NUM2INT(ptrname);
1503
	switch (TYPE(ptrname)) {
1504
	  case T_FIXNUM:
1505
	    whence = NUM2INT(ptrname);
1506
	    break;
1507
	  case T_SYMBOL:
1508
	    whence = sym_to_whence(ptrname);
1509
	    break;
1510
	  default:
1511
	    rb_raise(rb_eTypeError, "whence must be an Integer or Symbol: %s given", rb_obj_classname(ptrname));
1512
	}
1477 1513
    }
1478 1514

  
1479 1515
    return rb_io_seek(io, offset, whence);
......
11694 11730
    sym_willneed = ID2SYM(rb_intern("willneed"));
11695 11731
    sym_dontneed = ID2SYM(rb_intern("dontneed"));
11696 11732
    sym_noreuse = ID2SYM(rb_intern("noreuse"));
11733
    sym_set = ID2SYM(rb_intern("set"));
11734
    sym_cur = ID2SYM(rb_intern("cur"));
11735
    sym_current = ID2SYM(rb_intern("current"));
11736
    sym_end = ID2SYM(rb_intern("end"));
11697 11737
}
test/ruby/test_io.rb
1482 1482
    }
1483 1483
  end
1484 1484

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

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

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

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

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

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

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

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

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

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

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

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