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

Source for file Concat.php

Documentation is available at Concat.php

  1. <?
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A reader that concatene the data of the files of a source
  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: Concat.php,v 1.5 2005/02/24 10:48:56 vincentlascaux Exp $
  29.  * @link       http://pear.php.net/package/File_Archive
  30.  */
  31.  
  32. require_once "Relay.php";
  33.  
  34. /**
  35.  * This reader provides one single file that is the concatenation of the data of
  36.  * all the files of another reader
  37.  */
  38. class File_Archive_Reader_Concat extends File_Archive_Reader
  39. {
  40.     var $source;
  41.     var $filename;
  42.     var $stat;
  43.     var $mime;
  44.     var $opened = false;
  45.  
  46.     function File_Archive_Reader_Concat(&$source, $filename,
  47.                                         $stat=array(), $mime=null)
  48.     {
  49.         $this->source =& $source;
  50.         $this->filename = $filename;
  51.         $this->stat = $stat;
  52.         $this->mime = $mime;
  53.  
  54.         //Compute the total length
  55.         $this->stat[7] = 0;
  56.         while (($error = $source->next()) === true) {
  57.             $sourceStat = $source->getStat();
  58.             $this->stat[7] += $sourceStat[7];
  59.         }
  60.         if (PEAR::isError($error) || PEAR::isError($source->close())) {
  61.             die("Error in File_Archive_Reader_Concat constructor ".
  62.                 "({$error->getMessage()}), cannot continue");
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * @see File_Archive_Reader::next()
  68.      */
  69.     function next()
  70.     {
  71.         if (!$this->opened) {
  72.             return $this->opened = $this->source->next();
  73.         } else {
  74.             return false;
  75.         }
  76.     }
  77.     /**
  78.      * @see File_Archive_Reader::getFilename()
  79.      */
  80.     function getFilename() { return $this->filename; }
  81.     /**
  82.      * @see File_Archive_Reader::getStat()
  83.      */
  84.     function getStat() { return $this->stat; }
  85.     /**
  86.      * @see File_Archive_Reader::getMime()
  87.      */
  88.     function getMime()
  89.     {
  90.         return $this->mime==null ? parent::getMime() : $this->mime;
  91.     }
  92.     /**
  93.      * @see File_Archive_Reader::getData()
  94.      */
  95.     function getData($length = -1)
  96.     {
  97.         if ($length == 0) {
  98.             return '';
  99.         }
  100.  
  101.         $result = '';
  102.         while ($length == -1 || strlen($result)<$length) {
  103.             $sourceData = $this->source->getData(
  104.                 $length==-1 ? -1 : $length - strlen($result)
  105.             );
  106.  
  107.             if (PEAR::isError($sourceData)) {
  108.                 return $sourceData;
  109.             }
  110.  
  111.             if ($sourceData == null) {
  112.                 $error = $this->source->next();
  113.                 if (PEAR::isError($error)) {
  114.                     return $error;
  115.                 }
  116.                 if (!$error) {
  117.                     break;
  118.                 }
  119.             } else {
  120.                 $result .= $sourceData;
  121.             }
  122.         }
  123.         return $result == '' ? null : $result;
  124.     }
  125.     /**
  126.      * @see File_Archive_Reader::skip()
  127.      */
  128.     function skip($length)
  129.     {
  130.         $skipped = 0;
  131.         while ($skipped < $length) {
  132.             $sourceSkipped = $this->source->skip($length);
  133.             if (PEAR::isError($sourceSkipped)) {
  134.                 return $skipped;
  135.             }
  136.             $skipped += $sourceSkipped;
  137.         }
  138.         return $skipped;
  139.     }
  140.     /**
  141.      * @see File_Archive_Reader::close()
  142.      */
  143.     function close()
  144.     {
  145.         $error = $this->source->close();
  146.         $this->opened = false;
  147.  
  148.         if (PEAR::isError($error)) {
  149.             return $error;
  150.         }
  151.     }
  152.  
  153. }
  154.  
  155. ?>

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