1<?php
2/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4/**
5 * Add a directory to the public name of all the files of a reader
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: ChangeName.php,v 1.19 2005/07/09 12:54:35 vincentlascaux Exp $
29 * @link       http://pear.php.net/package/File_Archive
30 */
31
32require_once "File/Archive/Reader/Relay.php";
33
34/**
35 * Add a directory to the public name of all the files of a reader
36 *
37 * Example:
38 *  If archive.tar is a file archive containing files a.txt and foo/b.txt
39 *  new File_Archive_Reader_AddBaseName('bar',
40 *     new File_Archive_Reader_Tar(
41 *         new File_Archive_Reader_File('archive.tar')
42 *     )
43 *  ) is a reader containing files bar/a.txt and bar/foo/b.txt
44 */
45class File_Archive_Reader_AddBaseName extends File_Archive_Reader_Relay
46{
47    var $baseName;
48    function File_Archive_Reader_AddBaseName($baseName, &$source)
49    {
50        parent::File_Archive_Reader_Relay($source);
51        $this->baseName = $this->getStandardURL($baseName);
52    }
53
54    /**
55     * Modify the name by adding baseName to it
56     */
57    function modifyName($name)
58    {
59        return $this->baseName.
60               (empty($this->baseName) || empty($name) ? '': '/').
61               $name;
62    }
63
64    /**
65     * Remove baseName from the name
66     * Return false if the name doesn't start with baseName
67     */
68    function unmodifyName($name)
69    {
70        if (strncmp($name, $this->baseName.'/', strlen($this->baseName)+1) == 0) {
71            $res = substr($name, strlen($this->baseName)+1);
72            if ($res === false) {
73                return '';
74            } else {
75                return $res;
76            }
77        } else if (empty($this->baseName)) {
78            return $name;
79        } else if ($name == $this->baseName) {
80            return '';
81        } else {
82            return false;
83        }
84    }
85
86    /**
87     * @see File_Archive_Reader::getFilename()
88     */
89    function getFilename()
90    {
91        return $this->modifyName(parent::getFilename());
92    }
93    /**
94     * @see File_Archive_Reader::getFileList()
95     */
96    function getFileList()
97    {
98        $list = parent::getFileList();
99        $result = array();
100        foreach ($list as $name) {
101            $result[] = $this->modifyName($name);
102        }
103        return $result;
104    }
105    /**
106     * @see File_Archive_Reader::select()
107     */
108    function select($filename, $close = true)
109    {
110        $name = $this->unmodifyName($filename);
111        if ($name === false) {
112            return false;
113        } else {
114            return $this->source->select($name, $close);
115        }
116    }
117}
118
119/**
120 * Change a directory name to another
121 *
122 * Example:
123 *  If archive.tar is a file archive containing files a.txt and foo/b.txt
124 *  new File_Archive_Reader_ChangeBaseName('foo', 'bar'
125 *     new File_Archive_Reader_Tar(
126 *         new File_Archive_Reader_File('archive.tar')
127 *     )
128 *  ) is a reader containing files a.txt and bar/b.txt
129 */
130class File_Archive_Reader_ChangeBaseName extends File_Archive_Reader_Relay
131{
132    var $oldBaseName;
133    var $newBaseName;
134
135    function File_Archive_Reader_ChangeBaseName
136                        ($oldBaseName, $newBaseName, &$source)
137    {
138        parent::File_Archive_Reader_Relay($source);
139        $this->oldBaseName = $this->getStandardURL($oldBaseName);
140        if (substr($this->oldBaseName, -1) == '/') {
141            $this->oldBaseName = substr($this->oldBaseName, 0, -1);
142        }
143
144        $this->newBaseName = $this->getStandardURL($newBaseName);
145        if (substr($this->newBaseName, -1) == '/') {
146            $this->newBaseName = substr($this->newBaseName, 0, -1);
147        }
148    }
149
150    function modifyName($name)
151    {
152        if (empty($this->oldBaseName) ||
153          !strncmp($name, $this->oldBaseName.'/', strlen($this->oldBaseName)+1) ||
154           strcmp($name, $this->oldBaseName) == 0) {
155            return $this->newBaseName.
156                   (
157                    empty($this->newBaseName) ||
158                    strlen($name)<=strlen($this->oldBaseName)+1 ?
159                    '' : '/'
160                   ).
161                   substr($name, strlen($this->oldBaseName)+1);
162        } else {
163            return $name;
164        }
165    }
166    function unmodifyName($name)
167    {
168        if (empty($this->newBaseName) ||
169          !strncmp($name, $this->newBaseName.'/', strlen($this->newBaseName)+1) ||
170           strcmp($name, $this->newBaseName) == 0) {
171            return $this->oldBaseName.
172                   (
173                    empty($this->oldBaseName) ||
174                    strlen($name)<=strlen($this->newBaseName)+1 ?
175                    '' : '/'
176                   ).
177                   substr($name, strlen($this->newBaseName)+1);
178        } else {
179            return $name;
180        }
181    }
182
183    /**
184     * @see File_Archive_Reader::getFilename()
185     */
186    function getFilename()
187    {
188        return $this->modifyName(parent::getFilename());
189    }
190    /**
191     * @see File_Archive_Reader::getFileList()
192     */
193    function getFileList()
194    {
195        $list = parent::getFileList();
196        $result = array();
197        foreach ($list as $name) {
198            $result[] = $this->modifyName($name);
199        }
200        return $result;
201    }
202    /**
203     * @see File_Archive_Reader::select()
204     */
205    function select($filename, $close = true)
206    {
207        return $this->source->select($this->unmodifyName($filename));
208    }
209
210}
211
212?>