Implemented File::offset

Added test for it

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5748 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2007-10-11 07:49:57 +00:00
parent 6dc279c7b7
commit e7bccfe7a8
2 changed files with 48 additions and 2 deletions

View file

@ -152,6 +152,27 @@ class File extends Object{
$this->open($mode, $forceMode);
return fread($this->handle, $bytes);
}
/**
* Sets or gets the offset for the currently opened file.
*
* @param mixed $offset The $offset in bytes to seek. If set to false then the current offset is returned.
* @param integer $whence PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
* @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
* @access public
*/
function offset($offset = false, $whence = SEEK_SET) {
if ($offset === false) {
if (!$this->opened()) {
return false;
}
return ftell($this->handle);
}
if (!$this->open()) {
return false;
}
return fseek($this->handle, $offset, $whence) === 0;
}
/**
* Append given data string to this File.
*

View file

@ -88,8 +88,6 @@ class FileTest extends UnitTestCase {
}
function testRead() {
$this->File =& new File(__FILE__);
$result = $this->File->read();
$expecting = file_get_contents(__FILE__);
$this->assertEqual($result, $expecting);
@ -105,6 +103,33 @@ class FileTest extends UnitTestCase {
$result = $this->File->read(3);
$this->assertEqual($result, $expecting);
}
function testOffset() {
$this->File->close();
$result = $this->File->offset();
$this->assertFalse($result);
$this->assertFalse(is_resource($this->File->handle));
$success = $this->File->offset(0);
$this->assertTrue($success);
$this->assertTrue(is_resource($this->File->handle));
$result = $this->File->offset();
$expecting = 0;
$this->assertIdentical($result, $expecting);
$data = file_get_contents(__FILE__);
$success = $this->File->offset(5);
$expecting = substr($data, 5, 3);
$result = $this->File->read(3);
$this->assertTrue($success);
$this->assertEqual($result, $expecting);
$result = $this->File->offset();
$expecting = 5+3;
$this->assertIdentical($result, $expecting);
}
function testOpen() {
$this->File->handle = null;