diff --git a/lib/filelike/stringfilelike.inc.php b/lib/filelike/stringfilelike.inc.php new file mode 100644 index 0000000..d752632 --- /dev/null +++ b/lib/filelike/stringfilelike.inc.php @@ -0,0 +1,91 @@ +sString = $sString; + $this->iLen = strlen($sString); + } + + /** + * Set up any resources needed to perform work. + */ + function open($mode = "r") { } + + /** + * Take care of getting rid of any active resources. + */ + function close() { } + + /** + * Behaves like fread + */ + function read($iBytes) { + if (($this->iPos + $iBytes) > $this->iLen) { + $iBytes = $this->iLen - $this->iPos; + } + $sRet = substr($this->sString, $this->iPos, $iBytes); + $this->iPos += $iBytes; + return $sRet; + } + + /** + * Behaves like fwrite + */ + function write($sData) { + $this->sString .= $sData; + return true; + } + + function get_contents() { + return $this->sString; + } + + function put_contents($sData) { + $this->sString = $sData; + return true; + } + + function eof() { + if ($this->iPos >= $this->iLen) { + return true; + } + return false; + } + + function filesize() { + return strlen($this->sString); + } +} + +?> diff --git a/tests/filelike/testStringFileLike.php b/tests/filelike/testStringFileLike.php new file mode 100644 index 0000000..79d822c --- /dev/null +++ b/tests/filelike/testStringFileLike.php @@ -0,0 +1,55 @@ +read(64); + } + + function testEof() { + $sContents = "asdf"; + $f = new KTStringFileLike($sContents); + $sBack = $f->read(64); + $this->assertEqual($f->eof(), true); + } + + function testEof2() { + $sContents = "asdf"; + $f = new KTStringFileLike($sContents); + $sBack = $f->read(3); + $this->assertEqual($f->eof(), false); + } + + function testEof3() { + $sContents = "asdf"; + $f = new KTStringFileLike($sContents); + $sBack = $f->read(4); + $this->assertEqual($f->eof(), true); + } + + function testShortRead() { + $sContents = "asdf"; + $f = new KTStringFileLike($sContents); + $sBack = $f->read(3); + $this->assertEqual($sBack, 'asd'); + } + + function testGetContents() { + $sContents = "asdf"; + $f = new KTStringFileLike($sContents); + $sBack = $f->get_contents(); + $this->assertEqual($sBack, $sContents); + } + + function testCheckPos() { + $f = new KTStringFileLike($sContents); + $sBack = $f->get_contents(); + $this->assertEqual($sBack, $sContents); + } +} + +?>