Read.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\Temporary;
  36. use Hoa\File;
  37. use Hoa\Stream;
  38. /**
  39. * Class \Hoa\File\Temporary\Read.
  40. *
  41. * Read a temporary file.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Read extends Temporary implements Stream\IStream\In
  47. {
  48. /**
  49. * Open a file.
  50. *
  51. * @param string $streamName Stream name.
  52. * @param string $mode Open mode, see the parent::MODE_* constants.
  53. * @param string $context Context ID (please, see the
  54. * \Hoa\Stream\Context class).
  55. * @param bool $wait Differ opening or not.
  56. */
  57. public function __construct(
  58. $streamName,
  59. $mode = parent::MODE_READ,
  60. $context = null,
  61. $wait = false
  62. ) {
  63. parent::__construct($streamName, $mode, $context, $wait);
  64. return;
  65. }
  66. /**
  67. * Open the stream and return the associated resource.
  68. *
  69. * @param string $streamName Stream name (e.g. path or URL).
  70. * @param \Hoa\Stream\Context $context Context.
  71. * @return resource
  72. * @throws \Hoa\File\Exception\FileDoesNotExist
  73. * @throws \Hoa\File\Exception
  74. */
  75. protected function &_open($streamName, Stream\Context $context = null)
  76. {
  77. static $createModes = [
  78. parent::MODE_READ
  79. ];
  80. if (!in_array($this->getMode(), $createModes)) {
  81. throw new File\Exception(
  82. 'Open mode are not supported; given %d. Only %s are supported.',
  83. 0,
  84. [$this->getMode(), implode(', ', $createModes)]
  85. );
  86. }
  87. preg_match('#^(\w+)://#', $streamName, $match);
  88. if (((isset($match[1]) && $match[1] == 'file') || !isset($match[1])) &&
  89. !file_exists($streamName)) {
  90. throw new File\Exception\FileDoesNotExist(
  91. 'File %s does not exist.',
  92. 1,
  93. $streamName
  94. );
  95. }
  96. $out = parent::_open($streamName, $context);
  97. return $out;
  98. }
  99. /**
  100. * Test for end-of-file.
  101. *
  102. * @return bool
  103. */
  104. public function eof()
  105. {
  106. return feof($this->getStream());
  107. }
  108. /**
  109. * Read n characters.
  110. *
  111. * @param int $length Length.
  112. * @return string
  113. * @throws \Hoa\File\Exception
  114. */
  115. public function read($length)
  116. {
  117. if (0 > $length) {
  118. throw new File\Exception(
  119. 'Length must be greater than 0, given %d.',
  120. 2,
  121. $length
  122. );
  123. }
  124. return fread($this->getStream(), $length);
  125. }
  126. /**
  127. * Alias of $this->read().
  128. *
  129. * @param int $length Length.
  130. * @return string
  131. */
  132. public function readString($length)
  133. {
  134. return $this->read($length);
  135. }
  136. /**
  137. * Read a character.
  138. *
  139. * @return string
  140. */
  141. public function readCharacter()
  142. {
  143. return fgetc($this->getStream());
  144. }
  145. /**
  146. * Read a boolean.
  147. *
  148. * @return bool
  149. */
  150. public function readBoolean()
  151. {
  152. return (bool) $this->read(1);
  153. }
  154. /**
  155. * Read an integer.
  156. *
  157. * @param int $length Length.
  158. * @return int
  159. */
  160. public function readInteger($length = 1)
  161. {
  162. return (int) $this->read($length);
  163. }
  164. /**
  165. * Read a float.
  166. *
  167. * @param int $length Length.
  168. * @return float
  169. */
  170. public function readFloat($length = 1)
  171. {
  172. return (float) $this->read($length);
  173. }
  174. /**
  175. * Read an array.
  176. * Alias of the $this->scanf() method.
  177. *
  178. * @param string $format Format (see printf's formats).
  179. * @return array
  180. */
  181. public function readArray($format = null)
  182. {
  183. return $this->scanf($format);
  184. }
  185. /**
  186. * Read a line.
  187. *
  188. * @return string
  189. */
  190. public function readLine()
  191. {
  192. return fgets($this->getStream());
  193. }
  194. /**
  195. * Read all, i.e. read as much as possible.
  196. *
  197. * @param int $offset Offset.
  198. * @return string
  199. */
  200. public function readAll($offset = 0)
  201. {
  202. return stream_get_contents($this->getStream(), -1, $offset);
  203. }
  204. /**
  205. * Parse input from a stream according to a format.
  206. *
  207. * @param string $format Format (see printf's formats).
  208. * @return array
  209. */
  210. public function scanf($format)
  211. {
  212. return fscanf($this->getStream(), $format);
  213. }
  214. }