Read.php 6.0 KB

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