Commit 98595eade721d090a7d85339a358a80f488eb7d3

Authored by Neil Blakey-Milner
1 parent 0ec605fa

Add a filelike that works from a string (and associated tests)


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5409 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/filelike/stringfilelike.inc.php 0 → 100644
  1 +<?php
  2 +/**
  3 + * $Id$
  4 + *
  5 + * Filelike wrapper for strings
  6 + *
  7 + * Copyright (c) 2006 Jam Warehouse http://www.jamwarehouse.com
  8 + *
  9 + * This program is free software; you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation; using version 2 of the License.
  12 + *
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program; if not, write to the Free Software
  21 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22 + *
  23 + * @version $Revision$
  24 + * @author Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
  25 + */
  26 +
  27 +require_once(KT_LIB_DIR . '/filelike/filelike.inc.php');
  28 +
  29 +class KTStringFileLike extends KTFileLike {
  30 + var $bSupportChunking = true;
  31 + var $bIsFSFile = false;
  32 + var $iPos = 0;
  33 + var $iLen = 0;
  34 +
  35 + function KTStringFileLike ($sString) {
  36 + $this->sString = $sString;
  37 + $this->iLen = strlen($sString);
  38 + }
  39 +
  40 + /**
  41 + * Set up any resources needed to perform work.
  42 + */
  43 + function open($mode = "r") { }
  44 +
  45 + /**
  46 + * Take care of getting rid of any active resources.
  47 + */
  48 + function close() { }
  49 +
  50 + /**
  51 + * Behaves like fread
  52 + */
  53 + function read($iBytes) {
  54 + if (($this->iPos + $iBytes) > $this->iLen) {
  55 + $iBytes = $this->iLen - $this->iPos;
  56 + }
  57 + $sRet = substr($this->sString, $this->iPos, $iBytes);
  58 + $this->iPos += $iBytes;
  59 + return $sRet;
  60 + }
  61 +
  62 + /**
  63 + * Behaves like fwrite
  64 + */
  65 + function write($sData) {
  66 + $this->sString .= $sData;
  67 + return true;
  68 + }
  69 +
  70 + function get_contents() {
  71 + return $this->sString;
  72 + }
  73 +
  74 + function put_contents($sData) {
  75 + $this->sString = $sData;
  76 + return true;
  77 + }
  78 +
  79 + function eof() {
  80 + if ($this->iPos >= $this->iLen) {
  81 + return true;
  82 + }
  83 + return false;
  84 + }
  85 +
  86 + function filesize() {
  87 + return strlen($this->sString);
  88 + }
  89 +}
  90 +
  91 +?>
... ...
tests/filelike/testStringFileLike.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once(dirname(__FILE__) . "/../test.php");
  4 +require_once(KT_LIB_DIR . '/filelike/stringfilelike.inc.php');
  5 +
  6 +class StringFileLikeTestCase extends KTUnitTestCase {
  7 + function testRead() {
  8 + $sContents = "asdf";
  9 + $f = new KTStringFileLike($sContents);
  10 + $sBack = $f->read(64);
  11 + }
  12 +
  13 + function testEof() {
  14 + $sContents = "asdf";
  15 + $f = new KTStringFileLike($sContents);
  16 + $sBack = $f->read(64);
  17 + $this->assertEqual($f->eof(), true);
  18 + }
  19 +
  20 + function testEof2() {
  21 + $sContents = "asdf";
  22 + $f = new KTStringFileLike($sContents);
  23 + $sBack = $f->read(3);
  24 + $this->assertEqual($f->eof(), false);
  25 + }
  26 +
  27 + function testEof3() {
  28 + $sContents = "asdf";
  29 + $f = new KTStringFileLike($sContents);
  30 + $sBack = $f->read(4);
  31 + $this->assertEqual($f->eof(), true);
  32 + }
  33 +
  34 + function testShortRead() {
  35 + $sContents = "asdf";
  36 + $f = new KTStringFileLike($sContents);
  37 + $sBack = $f->read(3);
  38 + $this->assertEqual($sBack, 'asd');
  39 + }
  40 +
  41 + function testGetContents() {
  42 + $sContents = "asdf";
  43 + $f = new KTStringFileLike($sContents);
  44 + $sBack = $f->get_contents();
  45 + $this->assertEqual($sBack, $sContents);
  46 + }
  47 +
  48 + function testCheckPos() {
  49 + $f = new KTStringFileLike($sContents);
  50 + $sBack = $f->get_contents();
  51 + $this->assertEqual($sBack, $sContents);
  52 + }
  53 +}
  54 +
  55 +?>
... ...