Skip to content

Commit

Permalink
[mms] Add Horde_Stream#pos().
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Sep 24, 2013
1 parent 202e24e commit 68f0931
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 27 deletions.
54 changes: 52 additions & 2 deletions framework/Stream/lib/Horde/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function length()
{
$pos = ftell($this->stream);

if (fseek($this->stream, 0, SEEK_END) == -1) {
if (!$this->end()) {
throw new Horde_Stream_Exception('ERROR');
}

Expand Down Expand Up @@ -278,7 +278,7 @@ public function getEOL()
{
$pos = ftell($this->stream);

rewind($this->stream);
$this->rewind();
$pos2 = $this->search("\n", false, false);
if ($pos2) {
fseek($this->stream, -1, SEEK_CUR);
Expand All @@ -296,6 +296,56 @@ public function getEOL()
return $eol;
}

/**
* Return the current stream pointer position.
*
* @since 1.4.0
*
* @return mixed The current position (integer), or false.
*/
public function pos()
{
return ftell($this->stream);
}

/**
* Rewind the internal stream to the beginning.
*
* @since 1.4.0
*/
public function rewind()
{
rewind($this->stream);
}

/**
* Move internal pointer.
*
* @since 1.4.0
*
* @return boolean True if successful.
*/
public function seek($offset = 0, $curr = true)
{
return $offset
? (fseek($this->stream, $offset, $curr ? SEEK_CUR : SEEK_SET) === 0)
: true;
}

/**
* Move internal pointer to the end of the stream.
*
* @since 1.4.0
*
* @param integer $offset TODO
*
* @return boolean True if successful.
*/
public function end($offset = 0)
{
return (fseek($this->stream, $offset, SEEK_END) === 0);
}

/* Serializable methods. */

/**
Expand Down
2 changes: 2 additions & 0 deletions framework/Stream/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Add Horde_Stream#pos().
* [mms] Add Horde_Stream#__destruct().
</notes>
<contents>
Expand Down Expand Up @@ -194,6 +195,7 @@
<date>2013-08-05</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [mms] Add Horde_Stream#pos().
* [mms] Add Horde_Stream#__destruct().
</notes>
</release>
Expand Down
28 changes: 14 additions & 14 deletions framework/Stream/test/Horde/Stream/ExistingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setUp()

public function testFgetToChar()
{
rewind($this->stream->stream);
$this->stream->rewind();

$this->assertEquals(
'A',
Expand All @@ -40,7 +40,7 @@ public function testFgetToChar()

public function testLength()
{
rewind($this->stream->stream);
$this->stream->rewind();

$this->assertEquals(
3,
Expand All @@ -54,7 +54,7 @@ public function testLength()

public function testGetString()
{
rewind($this->stream->stream);
$this->stream->rewind();

$this->assertEquals(
'A B',
Expand All @@ -65,7 +65,7 @@ public function testGetString()
$this->stream->getString(0)
);

rewind($this->stream->stream);
$this->stream->rewind();

$this->assertEquals(
'A B',
Expand All @@ -76,19 +76,19 @@ public function testGetString()
$this->stream->getString(0)
);

fseek($this->stream->stream, 2, SEEK_SET);
$this->stream->seek(2, false);
$this->assertEquals(
'B',
$this->stream->getString()
);

fseek($this->stream->stream, 2, SEEK_SET);
$this->stream->seek(2, false);
$this->assertEquals(
'A ',
$this->stream->getString(0, -1)
);

fseek($this->stream->stream, 0, SEEK_END);
$this->stream->end();
$this->assertEquals(
'',
$this->stream->getString(null, -1)
Expand All @@ -97,7 +97,7 @@ public function testGetString()

public function testPeek()
{
rewind($this->stream->stream);
$this->stream->rewind();

$this->assertEquals(
'A',
Expand All @@ -108,21 +108,21 @@ public function testPeek()
fgetc($this->stream->stream)
);

fseek($this->stream->stream, -1, SEEK_END);
$this->stream->end(-1);

$this->assertEquals(
'B',
$this->stream->peek()
);
$this->assertEquals(
'B',
fgetc($this->stream->stream)
$this->stream->getChar()
);
}

public function testSearch()
{
rewind($this->stream->stream);
$this->stream->rewind();

$this->assertEquals(
2,
Expand All @@ -134,7 +134,7 @@ public function testSearch()
);
$this->assertEquals(
0,
ftell($this->stream->stream)
$this->stream->pos()
);

$this->assertEquals(
Expand All @@ -149,7 +149,7 @@ public function testSearch()
);
$this->assertEquals(
2,
ftell($this->stream->stream)
$this->stream->pos()
);

$this->assertEquals(
Expand All @@ -158,7 +158,7 @@ public function testSearch()
);
$this->assertEquals(
0,
ftell($this->stream->stream)
$this->stream->pos()
);
}

Expand Down
22 changes: 11 additions & 11 deletions framework/Stream/test/Horde/Stream/TempTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testLength()
);
$this->assertFalse(fgetc($stream->stream));

rewind($stream->stream);
$stream->rewind();

$this->assertEquals(
4,
Expand All @@ -86,7 +86,7 @@ public function testGetString()
$stream->getString(0)
);

rewind($stream->stream);
$stream->rewind();

$this->assertEquals(
'A B C',
Expand All @@ -97,19 +97,19 @@ public function testGetString()
$stream->getString(0)
);

fseek($stream->stream, 2, SEEK_SET);
$stream->seek(2, false);
$this->assertEquals(
'B C',
$stream->getString()
);

fseek($stream->stream, 2, SEEK_SET);
$stream->seek(2, false);
$this->assertEquals(
'B',
$stream->getString(null, -2)
);

fseek($stream->stream, 0, SEEK_END);
$stream->end();
$this->assertEquals(
'',
$stream->getString(null, -1)
Expand All @@ -131,7 +131,7 @@ public function testPeek()
fgetc($stream->stream)
);

fseek($stream->stream, -1, SEEK_END);
$stream->end(-1);

$this->assertEquals(
'B',
Expand Down Expand Up @@ -163,7 +163,7 @@ public function testSearch()
);
$this->assertEquals(
0,
ftell($stream->stream)
$stream->pos()
);

$this->assertEquals(
Expand All @@ -182,7 +182,7 @@ public function testSearch()
);
$this->assertEquals(
8,
ftell($stream->stream)
$stream->pos()
);

$this->assertEquals(
Expand All @@ -191,7 +191,7 @@ public function testSearch()
);
$this->assertEquals(
3,
ftell($stream->stream)
$stream->pos()
);
}

Expand All @@ -209,7 +209,7 @@ public function testAddMethod()
$stream->getString(0)
);

fseek($stream->stream, 0);
$stream->rewind();

$stream2 = new Horde_Stream_Temp();
$stream2->add($stream->stream, true);
Expand All @@ -227,7 +227,7 @@ public function testAddMethod()
$stream2->getString(0)
);

fseek($stream->stream, 0);
$stream->rewind();

$stream3 = new Horde_Stream_Temp();
$stream3->add($stream);
Expand Down

0 comments on commit 68f0931

Please sign in to comment.