1<?php 2 3/** 4 * Hoa 5 * 6 * 7 * @license 8 * 9 * New BSD License 10 * 11 * Copyright © 2007-2017, Hoa community. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions are met: 15 * * Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * * Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * * Neither the name of the Hoa nor the names of its contributors may be 21 * used to endorse or promote products derived from this software without 22 * specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37namespace Hoa\File; 38 39use Hoa\Stream; 40 41/** 42 * Class \Hoa\File\Read. 43 * 44 * File handler. 45 * 46 * @copyright Copyright © 2007-2017 Hoa community 47 * @license New BSD License 48 */ 49class Read extends File implements Stream\IStream\In 50{ 51 /** 52 * Open a file. 53 * 54 * @param string $streamName Stream name. 55 * @param string $mode Open mode, see the self::MODE_* constants. 56 * @param string $context Context ID (please, see the 57 * \Hoa\Stream\Context class). 58 * @param bool $wait Differ opening or not. 59 */ 60 public function __construct( 61 $streamName, 62 $mode = parent::MODE_READ, 63 $context = null, 64 $wait = false 65 ) { 66 parent::__construct($streamName, $mode, $context, $wait); 67 68 return; 69 } 70 71 /** 72 * Open the stream and return the associated resource. 73 * 74 * @param string $streamName Stream name (e.g. path or URL). 75 * @param \Hoa\Stream\Context $context Context. 76 * @return resource 77 * @throws \Hoa\File\Exception\FileDoesNotExist 78 * @throws \Hoa\File\Exception 79 */ 80 protected function &_open($streamName, Stream\Context $context = null) 81 { 82 static $createModes = [ 83 parent::MODE_READ 84 ]; 85 86 if (!in_array($this->getMode(), $createModes)) { 87 throw new Exception( 88 'Open mode are not supported; given %d. Only %s are supported.', 89 0, 90 [$this->getMode(), implode(', ', $createModes)] 91 ); 92 } 93 94 preg_match('#^(\w+)://#', $streamName, $match); 95 96 if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) && 97 !file_exists($streamName)) { 98 throw new Exception\FileDoesNotExist( 99 'File %s does not exist.', 100 1, 101 $streamName 102 ); 103 } 104 105 $out = parent::_open($streamName, $context); 106 107 return $out; 108 } 109 110 /** 111 * Test for end-of-file. 112 * 113 * @return bool 114 */ 115 public function eof() 116 { 117 return feof($this->getStream()); 118 } 119 120 /** 121 * Read n characters. 122 * 123 * @param int $length Length. 124 * @return string 125 * @throws \Hoa\File\Exception 126 */ 127 public function read($length) 128 { 129 if (0 > $length) { 130 throw new Exception( 131 'Length must be greater than 0, given %d.', 132 2, 133 $length 134 ); 135 } 136 137 return fread($this->getStream(), $length); 138 } 139 140 /** 141 * Alias of $this->read(). 142 * 143 * @param int $length Length. 144 * @return string 145 */ 146 public function readString($length) 147 { 148 return $this->read($length); 149 } 150 151 /** 152 * Read a character. 153 * 154 * @return string 155 */ 156 public function readCharacter() 157 { 158 return fgetc($this->getStream()); 159 } 160 161 /** 162 * Read a boolean. 163 * 164 * @return bool 165 */ 166 public function readBoolean() 167 { 168 return (bool) $this->read(1); 169 } 170 171 /** 172 * Read an integer. 173 * 174 * @param int $length Length. 175 * @return int 176 */ 177 public function readInteger($length = 1) 178 { 179 return (int) $this->read($length); 180 } 181 182 /** 183 * Read a float. 184 * 185 * @param int $length Length. 186 * @return float 187 */ 188 public function readFloat($length = 1) 189 { 190 return (float) $this->read($length); 191 } 192 193 /** 194 * Read an array. 195 * Alias of the $this->scanf() method. 196 * 197 * @param string $format Format (see printf's formats). 198 * @return array 199 */ 200 public function readArray($format = null) 201 { 202 return $this->scanf($format); 203 } 204 205 /** 206 * Read a line. 207 * 208 * @return string 209 */ 210 public function readLine() 211 { 212 return fgets($this->getStream()); 213 } 214 215 /** 216 * Read all, i.e. read as much as possible. 217 * 218 * @param int $offset Offset. 219 * @return string 220 */ 221 public function readAll($offset = 0) 222 { 223 return stream_get_contents($this->getStream(), -1, $offset); 224 } 225 226 /** 227 * Parse input from a stream according to a format. 228 * 229 * @param string $format Format (see printf's formats). 230 * @return array 231 */ 232 public function scanf($format) 233 { 234 return fscanf($this->getStream(), $format); 235 } 236} 237