From 114c6b6044c2854142564a8f75009dede394ed2c Mon Sep 17 00:00:00 2001 From: Neil Blakey-Milner Date: Thu, 15 Sep 2005 08:53:34 +0000 Subject: [PATCH] Add File-like objects that can be handled as a file would, but might be backed by other things (databases, a URL, a subversion repository, and so forth). --- lib/filelike/filelike.inc.php | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/filelike/filelikeutil.inc.php | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/filelike/fsfilelike.inc.php | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 278 insertions(+), 0 deletions(-) create mode 100644 lib/filelike/filelike.inc.php create mode 100644 lib/filelike/filelikeutil.inc.php create mode 100644 lib/filelike/fsfilelike.inc.php diff --git a/lib/filelike/filelike.inc.php b/lib/filelike/filelike.inc.php new file mode 100644 index 0000000..930fd6f --- /dev/null +++ b/lib/filelike/filelike.inc.php @@ -0,0 +1,97 @@ + diff --git a/lib/filelike/filelikeutil.inc.php b/lib/filelike/filelikeutil.inc.php new file mode 100644 index 0000000..70fca5e --- /dev/null +++ b/lib/filelike/filelikeutil.inc.php @@ -0,0 +1,85 @@ +bSupportChunking && $oTo->bSupportChunking) { + $res = $oFrom->open("r"); + if (PEAR::isError($res)) { + return $res; + } + $res = $oTo->open("w"); + if (PEAR::isError($res)) { + return $res; + } + while (!$oFrom->eof()) { + $res = $oFrom->read(8192); + if (PEAR::isError($res)) { + return $res; + } + $res = $oTo->write($res); + if (PEAR::isError($res)) { + return $res; + } + } + $res = $oFrom->close(); + if (PEAR::isError($res)) { + return $res; + } + $res = $oTo->close(); + if (PEAR::isError($res)) { + return $res; + } + } else { + $oTo->put_contents($oFrom->get_contents()); + } + return; + } + + function send_contents($oFrom, $bChunking = true) { + if ($oFrom->bSupportChunking && $bChunking) { + $res = $oFrom->open("r"); + if (PEAR::isError($res)) { + return $res; + } + while (!$oFrom->eof()) { + $res = $oFrom->read(8192); + if (PEAR::isError($res)) { + return $res; + } + print $res; + } + $res = $oFrom->close(); + if (PEAR::isError($res)) { + return $res; + } + } else { + print $oFrom->get_contents(); + } + } +} + +?> diff --git a/lib/filelike/fsfilelike.inc.php b/lib/filelike/fsfilelike.inc.php new file mode 100644 index 0000000..639b687 --- /dev/null +++ b/lib/filelike/fsfilelike.inc.php @@ -0,0 +1,96 @@ +sFilename = $sFilename; + } + + function getFSPath() { + return $this->sFilename; + } + + /** + * Set up any resources needed to perform work. + */ + function open($mode = "r") { + $this->fh = fopen($this->sFilename, $mode); + if ($this->fh === false) { + $this->fh = null; + return PEAR::raiseError('Error opening file'); + } + } + + /** + * Take care of getting rid of any active resources. + */ + function close() { + if (is_null($this->fh)) { + return PEAR::raiseError('Not open'); + } + return fclose($this->fh); + } + + /** + * Behaves like fread + */ + function read($iBytes) { + return fread($this->fh, $iBytes); + } + + /** + * Behaves like fwrite + */ + function write($sData) { + return fwrite($this->fh, $sData); + } + + function get_contents() { + return file_get_contents($this->sFilename); + } + + function put_contents($sData) { + return file_put_contents($this->sFilename, $sData); + } + + function eof() { + return feof($this->fh); + } + + function filesize() { + return filesize($this->sFilename); + } +} + +?> -- libgit2 0.21.4