1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Base class for any writer
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: Writer.php,v 1.12 2005/05/31 21:22:02 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "PEAR.php";
33
34/**
35 * Base class for any writer
36 */
37class File_Archive_Writer
38{
39    /**
40     * Create a new file in the writer
41     *
42     * @param string $filename Name of the file, eventually including a path
43     * @param array $stat Its Statistics. None of the indexes are required
44     * @param string $mime MIME type of the file
45     */
46    function newFile($filename, $stat = array(), $mime = "application/octet-stream")
47    {
48    }
49
50    /**
51     * Create a new file in the writer with the content of the physical file $filename
52     * and then unlink $filename.
53     * newFromTempFile($tmpfile, $filename, $stat, $mime) is equivalent to
54     * newFile($filename, $stat, $mime); writeFile($tmpfile); unlink($tmpfile);
55     * but can be more efficient.
56     * A typical use is for File_Archive_Writer_Files: it renames the temporary
57     * file instead of copy/delete
58     *
59     * @param string $tmpfile Name of the physical file that contains data and will be unlinked
60     * @param string $filename Name of the file, eventually including a path
61     * @param array $stat Its Statistics. None of the indexes are required
62     * @param string $mime MIME type of the file
63     */
64    function newFromTempFile($tmpfile, $filename, $stat = array(), $mime = "application/octet-stream")
65    {
66        $this->newFile($filename, $stat, $mime);
67        $this->writeFile($tmpfile);
68        unlink($tmpfile);
69    }
70
71    /**
72     * Returns whether the writer newFile function needs the $mime parameter
73     * Default is false
74     */
75    function newFileNeedsMIME()
76    {
77        return false;
78    }
79
80    /**
81     * Append the specified data to the writer
82     *
83     * @param String $data the data to append to the writer
84     */
85    function writeData($data)
86    {
87    }
88
89    /**
90     * Append the content of the physical file $filename to the writer
91     * writeFile($filename) must be equivalent to
92     * writeData(file_get_contents($filename)) but can be more efficient
93     *
94     * @param string $filename Name of the file which content must be appended
95     *        to the writer
96     */
97    function writeFile($filename)
98    {
99        $handle = fopen($filename, "r");
100        if (!is_resource($handle)) {
101            return PEAR::raiseError("Unable to write to $filename");
102        }
103        while (!feof($handle)) {
104            $error = $this->writeData(fread($handle, 102400));
105            if (PEAR::isError($error)) {
106                return $error;
107            }
108        }
109        fclose($handle);
110    }
111
112    /**
113     * Close the writer, eventually flush the data, write the footer...
114     * This function must be called before the end of the script
115     */
116    function close() { }
117}
118
119?>