File_Archive
[ class tree: File_Archive ] [ index: File_Archive ] [ all elements ]

Source for file ChangeName.php

Documentation is available at ChangeName.php

  1. <?
  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.10 2005/02/26 10:17:06 vincentlascaux Exp $
  29.  * @link       http://pear.php.net/package/File_Archive
  30.  */
  31.  
  32. require_once "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.  */
  45. class 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.      * @see File_Archive_Reader::getFilename()
  56.      */
  57.     function getFilename()
  58.     {
  59.         $name = parent::getFilename();
  60.         return $this->baseName.
  61.                (empty($this->baseName) || empty($name) ? '': '/').
  62.                $name;
  63.     }
  64. }
  65.  
  66. /**
  67.  * Change a directory name to another
  68.  *
  69.  * Example:
  70.  *  If archive.tar is a file archive containing files a.txt and foo/b.txt
  71.  *  new File_Archive_Reader_ChangeBaseName('foo', 'bar'
  72.  *     new File_Archive_Reader_Tar(
  73.  *         new File_Archive_Reader_File('archive.tar')
  74.  *     )
  75.  *  ) is a reader containing files a.txt and bar/b.txt
  76.  */
  77. class File_Archive_Reader_ChangeBaseName extends File_Archive_Reader_Relay
  78. {
  79.     var $oldBaseName;
  80.     var $newBaseName;
  81.  
  82.     function File_Archive_Reader_ChangeBaseName
  83.                         ($oldBaseName, $newBaseName, &$source)
  84.     {
  85.         parent::File_Archive_Reader_Relay($source);
  86.         $this->oldBaseName = $this->getStandardURL($oldBaseName);
  87.         if (substr($this->oldBaseName, -1) == '/') {
  88.             $this->oldBaseName = substr($this->oldBaseName, 0, -1);
  89.         }
  90.  
  91.         $this->newBaseName = $this->getStandardURL($newBaseName);
  92.         if (substr($this->newBaseName, -1) == '/') {
  93.             $this->newBaseName = substr($this->newBaseName, 0, -1);
  94.         }
  95.     }
  96.  
  97.     /**
  98.      * @see File_Archive_Reader::getFilename()
  99.      */
  100.     function getFilename()
  101.     {
  102.         $name = parent::getFilename();
  103.         if (empty($this->oldBaseName) ||
  104.           !strncmp($name, $this->oldBaseName.'/', strlen($this->oldBaseName)+1) ||
  105.            strcmp($name, $this->oldBaseName) == 0) {
  106.             return $this->newBaseName.
  107.                    (
  108.                     empty($this->newBaseName) ||
  109.                     strlen($name)<=strlen($this->oldBaseName)+1 ?
  110.                     '' : '/'
  111.                    ).
  112.                    substr($name, strlen($this->oldBaseName)+1);
  113.         } else {
  114.             return $name;
  115.         }
  116.     }
  117. }
  118.  
  119. ?>

Documentation generated on Mon, 11 Mar 2019 14:21:14 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.