cvs: pear /File_Archive/Archive/Reader Ar.php

From: Date: Sun, 22 May 2005 20:38:08 +0000
Subject: cvs: pear /File_Archive/Archive/Reader Ar.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-32012@lists.php.net to get a copy of this message
vincentlascaux Sun May 22 16:38:08 2005 EDT Modified files: /pear/File_Archive/Archive/Reader Ar.php Log: Don't read data in next but in getData http://cvs.php.net/diff.php/pear/File_Archive/Archive/Reader/Ar.php?r1=1.1&r2=1.2&ty=u Index: pear/File_Archive/Archive/Reader/Ar.php diff -u pear/File_Archive/Archive/Reader/Ar.php:1.1 pear/File_Archive/Archive/Reader/Ar.php:1.2 --- pear/File_Archive/Archive/Reader/Ar.php:1.1 Sat May 21 14:54:46 2005 +++ pear/File_Archive/Archive/Reader/Ar.php Sun May 22 16:38:07 2005 @@ -23,7 +23,7 @@ * @author Pablo Fischer <pablo@pablo.com.mx> * @copyright 1997-2005 The PHP Group * @license http://www.gnu.org/copyleft/lesser.html LGPL - * @version CVS: $Id: + * @version CVS: $Id: * @link http://pear.php.net/package/File_Archive */ @@ -36,19 +36,23 @@ { /** - * @var array Array that has a registry of the files thas has been asked - * for their data. - * - * The files inside an ar file come with their data, each 'block' - * of an ar file has the filename, size, uid, gid and the data (content). - * So the data of the file should be asked only once (they not come in pieces) + * @var int The number of files to read to reach the end of the + * current ar file * * @access private */ - var $_readedFiles = array (); - + var $_nbBytesLeft = 0; + /** - * @var boolean Flag that has tell us if we have read the header of the + * @var boolean Flag set if their is a 1 byte footer after the data + * of the current ar file + * + * @access private + */ + var $_footer = false; + + /** + * @var boolean Flag that has tell us if we have read the header of the * current file * @access private */ @@ -65,16 +69,16 @@ * It has: name, utime, uid, gid, mode, size and data * @access private */ - var $_currentStat = null; + var $_currentStat = null; /** * @see File_Archive_Reader::getFilename() */ - function getFilename() + function getFilename() { - return $this->_currentFilename; + return $this->_currentFilename; } - + /** * @see File_Archive_Reader::close() */ @@ -82,18 +86,17 @@ { $this->_currentFilename = null; $this->_currentStat = null; - $this->_readedFiles = array (); return parent::close(); } - + /** * @see File_Archive_Reader::getStat() */ - function getStat() + function getStat() { - return $this->_currentStat; + return $this->_currentStat; } - + /** * @see File_Archive_Reader::next() */ @@ -102,8 +105,12 @@ $error = parent::next(); if ($error !== true) { return $error; - } - + } + + $this->source->skip( + $this->_nbBytesLefts + ($this->_footer ? 1 : 0) + ); + $filename = $this->source->getDataFilename(); if (!$this->_alreadyRead) { @@ -114,7 +121,7 @@ $this->_alreadyRead = true; } - + $name = $this->source->getData(16); $mtime = $this->source->getData(12); $uid = $this->source->getData(6); @@ -122,7 +129,7 @@ $mode = $this->source->getData(8); $size = $this->source->getData(10); $delim = $this->source->getData(2); - + if ($delim == null) { return false; } @@ -130,59 +137,50 @@ if ($size < 0) { return PEAR::raiseError("Files must be at least one byte long"); } - if (!$data = $this->source->getData($size)) { - return PEAR::raiseError("There was a problem reading files inside {$filename}}"); - } - - if ($size % 2 == 1) { - $this->source->skip(1); //Dunno if this is the equivalent of fgetc($fp); - } - + + $this->_footer = ($size % 2 == 1); + // if the filename starts with a length, then just read the bytes of it if (preg_match("/\#1\/(\d+)/", $name, $matches)) { - $name = substr($data, 0, $matches[1]); - $data = substr($data, $matches[1]); + $name = $this->source->getData($matches[1]); $size -= $matches[1]; } else { // strip trailing spaces in name, so we can distinguish spaces in a filename with padding. $name = preg_replace ("/\s+$/", "", $name); - } - $this->_leftLength = $size; - if (!empty($name) && !empty($mtime) && !empty($uid) && + } + + $this->_nbBytesLeft = $size; + if (!empty($name) && !empty($mtime) && !empty($uid) && !empty($gid) && !empty($mode) && !empty($size)) { - $this->_currentFilename = $this->getStandardURL($name); - $this->_currentStat = array('name' => $name, - 'mtime' => $mtime, - 'uid' => $uid, - 'gid' => $gid, - 'mode' => $mode, - 'size' => $size, - 'data' => $data); + return PEAR::raiseError("An ar field is empty"); } - + + $this->_currentFilename = $this->getStandardURL($name); + $this->_currentStat = array('name' => $name, + 'mtime' => $mtime, + 'uid' => $uid, + 'gid' => $gid, + 'mode' => $mode, + 'size' => $size); + return true; - } + } /** * @see File_Archive_Reader::getData() */ function getData($length = -1) - { - /** - * We need to verify if the file has already returned the data. - * - * The data can only be returned in one call, the data comes in a unique - * block and not in chunks or little pieces of data. - */ - if (isset($this->_currentStat["name"])) { - if (!in_array($this->_currentStat["name"], $this->_readedFiles)) { - $this->_readedFiles[$this->_currentStat["name"]] = $this->_currentStat["name"]; - return $this->_currentStat["data"]; - } else { - return null; - } + { + if ($length == -1) { + $length = $this->_nbBytesLeft; } else { + $length = min($length, $this->_nbBytesLeft); + } + if ($length == 0) { return null; + } else { + $this->nbBytesLeft -= $length; + return $this->source->getData($length); } } }

« previous php.pear.cvs (#32012) next »