[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: FileReader.php 3076 2006-12-18 08:52:12Z fabien $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://phing.info>. 20 */ 21 22 include_once 'phing/system/io/PhingFile.php'; 23 include_once 'phing/system/io/Reader.php'; 24 25 /** 26 * Convenience class for reading files. The constructor of this 27 * @package phing.system.io 28 */ 29 30 class FileReader extends Reader { 31 32 protected $file; 33 protected $fd; 34 35 protected $currentPosition = 0; 36 protected $mark = 0; 37 38 function __construct($file, $exclusive = false) { 39 40 if ($file instanceof PhingFile) { 41 $this->file = $file; 42 } elseif (is_string($file)) { 43 $this->file = new PhingFile($file); 44 } else { 45 throw new Exception("Illegal argument type to " . __METHOD__); 46 } 47 } 48 49 function skip($n) { 50 $this->open(); 51 52 $start = $this->currentPosition; 53 54 $ret = @fseek($this->fd, $n, SEEK_CUR); 55 if ( $ret === -1 ) 56 return -1; 57 58 $this->currentPosition = ftell($this->fd); 59 60 if ( $start > $this->currentPosition ) 61 $skipped = $start - $this->currentPosition; 62 else 63 $skipped = $this->currentPosition - $start; 64 65 return $skipped; 66 } 67 68 /** 69 * Read data from file. 70 * @param int $len Num chars to read. 71 * @return string chars read or -1 if eof. 72 */ 73 function read($len = null) { 74 $this->open(); 75 if (feof($this->fd)) { 76 return -1; 77 } 78 79 // Compute length to read 80 // possible that filesize($this->file) will be larger than 81 // available bytes to read, but that's fine -- better to err on high end 82 $length = ($len === null) ? filesize($this->file->getAbsolutePath()) : $len; 83 84 // Read data 85 $out = fread($this->fd, $length + 1); // adding 1 seems to ensure that next call to read() will return EOF (-1) 86 $this->currentPosition = ftell($this->fd); 87 88 return $out; 89 } 90 91 function mark($n = null) { 92 $this->mark = $this->currentPosition; 93 } 94 95 function reset() { 96 // goes back to last mark, by default this would be 0 (i.e. rewind file). 97 fseek($this->fd, SEEK_SET, $this->mark); 98 $this->mark = 0; 99 } 100 101 function close() { 102 if ($this->fd === null) { 103 return true; 104 } 105 106 if (false === @fclose($this->fd)) { 107 // FAILED. 108 $msg = "Cannot fclose " . $this->file->__toString() . " $php_errormsg"; 109 throw new IOException($msg); 110 } else { 111 $this->fd = null; 112 return true; 113 } 114 } 115 116 function open() { 117 global $php_errormsg; 118 119 if ($this->fd === null) { 120 $this->fd = @fopen($this->file->getAbsolutePath(), "rb"); 121 } 122 123 if ($this->fd === false) { 124 // fopen FAILED. 125 // Add error from php to end of log message. $php_errormsg. 126 $msg = "Cannot fopen ".$this->file->getAbsolutePath().". $php_errormsg"; 127 throw new IOException($msg); 128 } 129 130 if (false) { 131 // Locks don't seem to work on windows??? HELP!!!!!!!!! 132 // if (FALSE === @flock($fp, LOCK_EX)) { // FAILED. 133 $msg = "Cannot acquire flock on $file. $php_errormsg"; 134 throw new IOException($msg); 135 } 136 137 return true; 138 } 139 140 /** 141 * Whether eof has been reached with stream. 142 * @return boolean 143 */ 144 function eof() { 145 return feof($this->fd); 146 } 147 148 /** 149 * Reads a entire file and stores the data in the variable 150 * passed by reference. 151 * 152 * @param string $file String. Path and/or name of file to read. 153 * @param object &$rBuffer Reference. Variable of where to put contents. 154 * 155 * @return TRUE on success. Err object on failure. 156 * @author Charlie Killian, charlie@tizac.com 157 */ 158 function readInto(&$rBuffer) { 159 160 $this->open(); 161 162 $fileSize = $this->file->length(); 163 if ($fileSize === false) { 164 $msg = "Cannot get filesize of " . $this->file->__toString() . " $php_errormsg"; 165 throw new IOException($msg); 166 } 167 $rBuffer = fread($this->fd, $fileSize); 168 $this->close(); 169 } 170 171 /** 172 * Returns path to file we are reading. 173 * @return string 174 */ 175 function getResource() { 176 return $this->file->toString(); 177 } 178 } 179 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |