1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Write the concatenation of the files in a 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.14 2005/06/02 12:24:43 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "File/Archive/Writer.php";
33
34/**
35 * Write the concatenation of the files in a buffer
36 */
37class File_Archive_Writer_Memory extends File_Archive_Writer
38{
39    /**
40     * @var string $data The buffer
41     * @access private
42     */
43    var $data = "";
44    /**
45     * Information about the file being written into this writer
46     * @access private
47     */
48    var $filename;
49    var $stat;
50    var $mime;
51
52    /**
53     * @param reference $data If provided, the data will be output in this
54     *        variable. Any existent data in $data will be overwritten by the
55     *        actual data of the writer. You should not modify manually this
56     *        variable while using this writer (you can safely use all the
57     *        functions of the archive, like clear for example)
58     * @param int keptData is the offset from where to start writing in $data
59     *        Any data located after $seek will be erased
60     *        The default value is 0
61     */
62    function File_Archive_Writer_Memory(&$data, $seek = 0)
63    {
64        $this->data =& $data;
65        $this->data = substr($data, 0, $seek);
66    }
67
68    function writeData($d) { $this->data .= $d; }
69
70    /**
71     * @see File_Archive_Writer::newFile()
72     */
73    function newFile($filename, $stat, $mime = "application/octet-stream")
74    {
75        $this->filename = $filename;
76        $this->stat = $stat;
77        $this->mime = $mime;
78    }
79    /**
80     * @see File_Archive_Writer::newFileNeedsMIME
81     */
82    function newFileNeedsMIME()
83    {
84        return true;
85    }
86
87    /**
88     * Retrieve the concatenated data
89     * The value is returned by reference for performance problems, but you
90     * should not manually modify it
91     *
92     * @return string buffer
93     */
94    function &getData() { return $this->data; }
95
96    /**
97     * Clear the buffer
98     */
99    function clear() { $this->data = ""; }
100
101    /**
102     * Returns true iif the buffer is empty
103     */
104    function isEmpty() { return empty($this->data); }
105
106    /**
107     * Create a reader from this writer
108     *
109     * @param string $filename Name of the file provided by the reader
110     * @param array $stat Statistics of the file provided by the reader
111     * @param string $mime Mime type of the file provided by the reader
112     *
113     * Any unspecified parameter will be set to the value of the last file
114     * written in this writer
115     */
116    function makeReader($filename = null, $stat = null, $mime = null)
117    {
118        require_once "File/Archive/Reader/Memory.php";
119        return new File_Archive_Reader_Memory(
120            $this->data,
121            $filename === null ? $this->filename : $filename,
122            $stat     === null ? $this->stat     : $stat,
123            $mime     === null ? $this->mime     : $mime);
124    }
125}
126
127?>