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.1 2005/02/20 00:34:08 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 all the files of
  36.  * 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, $stat=array(), $mime=null)
  47.     {
  48.         $this->source =& $source;
  49.         $this->filename = $filename;
  50.         $this->stat = $stat;
  51.         $this->mime = $mime;
  52.  
  53.         //Compute the total length
  54.         $this->stat[7] = 0;
  55.         while($source->next())
  56.         {
  57.             $sourceStat = $source->getStat();
  58.             $this->stat[7] += $sourceStat[7];
  59.         }
  60.         $source->close();
  61.     }
  62.  
  63.     /**
  64.      * @see File_Archive_Reader::next()
  65.      */
  66.     function next()
  67.     {
  68.         if(!$this->opened) {
  69.             $this->opened = true;
  70.             $this->source->next();
  71.             return true;
  72.         } else {
  73.             return false;
  74.         }
  75.     }
  76.     /**
  77.      * @see File_Archive_Reader::getFilename()
  78.      */
  79.     function getFilename() { return $this->filename; }
  80.     /**
  81.      * @see File_Archive_Reader::getStat()
  82.      */
  83.     function getStat() { return $this->stat; }
  84.     /**
  85.      * @see File_Archive_Reader::getMime()
  86.      */
  87.     function getMime() { return $this->mime==null ? parent::getMime() : $this->mime; }
  88.     /**
  89.      * @see File_Archive_Reader::getData()
  90.      */
  91.     function getData($length = -1)
  92.     {
  93.         if($length == 0) {
  94.             return '';
  95.         }
  96.  
  97.         $result = '';
  98.         while($length == -1 || strlen($result)<$length)
  99.         {
  100.             $sourceData = $this->source->getData($length==-1 ? -1 : $length - strlen($result));
  101.  
  102.             if(PEAR::isError($sourceData)) {
  103.                 return $sourceData;
  104.             }
  105.  
  106.             if($sourceData == null) {
  107.                 $error = $this->source->next();
  108.                 if(PEAR::isError($error)) {
  109.                     return $error;
  110.                 }
  111.                 if(!$error) {
  112.                     break;
  113.                 }
  114.             } else {
  115.                 $result .= $sourceData;
  116.             }
  117.         }
  118.         return $result=='' ? null : $result;
  119.     }
  120.     /**
  121.      * @see File_Archive_Reader::skip()
  122.      */
  123.     function skip($length)
  124.     {
  125.         $skipped = 0;
  126.         while($skipped < $length)
  127.         {
  128.             $sourceSkipped = $this->source->skip($length);
  129.             if(PEAR::isError($sourceSkipped)) {
  130.                 return $skipped;
  131.             }
  132.             $skipped += $sourceSkipped;
  133.         }
  134.         return $skipped;
  135.     }
  136.     /**
  137.      * @see File_Archive_Reader::close()
  138.      */
  139.     function close()
  140.     {
  141.         $this->source->close();
  142.         $this->opened = false;
  143.     }
  144.  
  145. }
  146.  
  147. ?>

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