1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * A reader that takes its input from a memory buffer
6 *
7 * PHP versions 4 and 5
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library 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 GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
22 *
23 * @category   File Formats
24 * @package    File_Archive
25 * @author     Vincent Lascaux <vincentlascaux@php.net>
26 * @copyright  1997-2005 The PHP Group
27 * @license    http://www.gnu.org/copyleft/lesser.html  LGPL
28 * @version    CVS: $Id: Memory.php,v 1.19 2005/06/19 20:09:57 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "File/Archive/Reader.php";
33
34/**
35 * A reader that takes its input from a memory buffer
36 */
37class File_Archive_Reader_Memory extends File_Archive_Reader
38{
39    /**
40     * @var String Name of the file exported by this reader
41     * @access private
42     */
43    var $filename;
44    /**
45     * @var Array Stat of the file exported by this reader
46     * @access private
47     */
48    var $stat;
49    /**
50     * @var String MIME type of the file exported by this reader
51     * @access private
52     */
53    var $mime;
54    /**
55     * @var String Memory buffer that contains the data of the file
56     * @access private
57     */
58    var $memory;
59    /**
60     * @var Int Current position in the file
61     * @access private
62     */
63    var $offset = 0;
64    /**
65     * @var Boolean Has the file already been read
66     * @access private
67     */
68    var $alreadyRead = false;
69
70    /**
71     * @param string $memory is the content of the file.
72     *        This parameter is passed as a reference for performance
73     *        reasons. The content should not be changer after the constructor
74     * @param string $filename is the name of the file
75     * @param array  $stat are the statistics of the file. The size will be
76     *        recomputed from $memory
77     * @param string $mime is the mime type of the file
78     */
79    function File_Archive_Reader_Memory(&$memory, $filename,
80                                        $stat=array(), $mime=null)
81    {
82        $this->memory = &$memory;
83        $this->filename = $this->getStandardURL($filename);
84        $this->stat = $stat;
85        $this->stat[7] = $this->stat['size'] = strlen($this->memory);
86        $this->mime = $mime;
87    }
88
89    /**
90     * The subclass should overwrite this function to change the filename, stat
91     * and memory
92     */
93    function next()
94    {
95        if ($this->alreadyRead) {
96            return false;
97        } else {
98            $this->alreadyRead = true;
99            return true;
100        }
101    }
102
103    /**
104     * @see File_Archive_Reader::getFilename()
105     */
106    function getFilename() { return $this->filename; }
107    /**
108     * @see File_Archive_Reader::getStat()
109     */
110    function getStat() { return $this->stat; }
111    /**
112     * @see File_Archive_Reader::getMime()
113     */
114    function getMime()
115    {
116        return $this->mime==null ? parent::getMime() : $this->mime;
117    }
118
119    /**
120     * @see File_Archive_Reader::getData()
121     */
122    function getData($length = -1)
123    {
124        if ($this->offset == strlen($this->memory)) {
125            return null;
126        }
127        if ($length == -1) {
128            $actualLength = strlen($this->memory) - $this->offset;
129        } else {
130            $actualLength = min($length, strlen($this->memory) - $this->offset);
131        }
132        $result = substr($this->memory, $this->offset, $actualLength);
133        $this->offset += $actualLength;
134        return $result;
135    }
136
137    /**
138     * @see File_Archive_Reader::skip()
139     */
140    function skip($length = -1)
141    {
142        if ($length == -1) {
143            $length = strlen($this->memory) - $this->offset;
144        } else {
145            $length = min($length, strlen($this->memory) - $this->offset);
146        }
147        $this->offset += $length;
148        return $length;
149    }
150
151    /**
152     * @see File_Archive_Reader::rewind()
153     */
154    function rewind($length = -1)
155    {
156        if ($length == -1) {
157            $tmp = $this->offset;
158            $this->offset = 0;
159            return $tmp;
160        } else {
161            $length = min($length, $this->offset);
162            $this->offset -= $length;
163            return $length;
164        }
165    }
166
167    /**
168     * @see File_Archive_Reader::tell()
169     */
170    function tell()
171    {
172        return $this->offset;
173    }
174
175    /**
176     * @see File_Archive_Reader::close()
177     */
178    function close()
179    {
180        $this->offset = 0;
181        $this->alreadyRead = false;
182    }
183
184    /**
185     * @see File_Archive_Reader::makeAppendWriter()
186     */
187    function makeAppendWriter()
188    {
189        return PEAR::raiseError('Unable to append files to a memory archive');
190    }
191
192    /**
193     * @see File_Archive_Reader::makeWriterRemoveFiles()
194     */
195    function makeWriterRemoveFiles($pred)
196    {
197        return PEAR::raiseError('Unable to remove files from a memory archive');
198    }
199
200    /**
201     * @see File_Archive_Reader::makeWriterRemoveBlocks()
202     */
203    function makeWriterRemoveBlocks($blocks, $seek = 0)
204    {
205        require_once "File/Archive/Writer/Memory.php";
206        $data = substr($this->memory, 0, $this->offset + $seek);
207        $this->memory = substr($this->memory, $this->offset + $seek);
208
209        $keep = false;
210        foreach ($blocks as $length) {
211            if ($keep) {
212                $data .= substr($this->memory, 0, $length);
213            }
214            $this->memory = substr($this->memory, $length);
215            $keep = !$keep;
216        }
217        if ($keep) {
218            $this->memory = $data . $this->memory;
219        } else {
220            $this->memory = $data;
221        }
222        $this->close();
223        return new File_Archive_Writer_Memory($this->memory, strlen($this->memory));
224    }
225}
226
227?>