PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/adapters/ffmpeg-php/php-reader/src/Reader.php

https://github.com/mariuz/firetube
PHP | 215 lines | 75 code | 15 blank | 125 comment | 14 complexity | 19da6c9b47fb88552c920868ff747fb7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * PHP Reader Library
  4. *
  5. * Copyright (c) 2006-2008 The PHP Reader Project Workgroup. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * - Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * - Neither the name of the project workgroup nor the names of its
  17. * contributors may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * @package php-reader
  33. * @copyright Copyright (c) 2006-2008 The PHP Reader Project Workgroup
  34. * @license http://code.google.com/p/php-reader/wiki/License New BSD License
  35. * @version $Id: Reader.php 39 2008-03-26 17:27:22Z svollbehr $
  36. */
  37. /**#@+ @ignore */
  38. require_once("Reader/Exception.php");
  39. require_once("Transform.php");
  40. /**#@-*/
  41. /**
  42. * The Reader class encapsulates a file. It is hence responsible of upkeeping
  43. * the connection to the file, keeping track of the cursor position and reading
  44. * data from it.
  45. *
  46. * @package php-reader
  47. * @author Sven Vollbehr <svollbehr@gmail.com>
  48. * @copyright Copyright (c) 2006-2008 The PHP Reader Project Workgroup
  49. * @license http://code.google.com/p/php-reader/wiki/License New BSD License
  50. * @version $Rev: 39 $
  51. */
  52. class Reader
  53. {
  54. /** @var resource */
  55. private $_fd;
  56. /** @var integer */
  57. private $_size;
  58. /**
  59. * Constructs the Reader class with given file.
  60. *
  61. * @param string $filename The path to the file.
  62. * @param string $mode The type of access.
  63. * @throws Reader_Exception if the file cannot be read.
  64. */
  65. public function __construct($filename, $mode = "rb")
  66. {
  67. if (($this->_fd = fopen($filename, $mode)) === false)
  68. throw new Reader_Exception("Unable to open file:" . $filename);
  69. fseek($this->_fd, 0, SEEK_END);
  70. $this->_size = ftell($this->_fd);
  71. fseek($this->_fd, 0);
  72. }
  73. /**
  74. * Closes the file.
  75. */
  76. public function __destruct()
  77. {
  78. @fclose($this->_fd);
  79. }
  80. /**
  81. * Checks whether there is more to be read in the file. Returns
  82. * <var>true</var> if the end of the file has not yet been reached;
  83. * <var>false</var> otherwise.
  84. *
  85. * @return boolean
  86. */
  87. public function available()
  88. {
  89. return $this->getOffset() < $this->_size;
  90. }
  91. /**
  92. * Jumps <var>size</var> amount of bytes in the file stream.
  93. *
  94. * @param integer $size The amount of bytes.
  95. * @return void
  96. * @throws Reader_Exception if <var>size</var> attribute is negative.
  97. */
  98. public function skip($size)
  99. {
  100. if ($size < 0)
  101. throw new Reader_Exception("Invalid argument");
  102. if ($size == 0)
  103. return;
  104. fseek($this->_fd, $size, SEEK_CUR);
  105. }
  106. /**
  107. * Reads <var>length</var> amount of bytes from the file stream.
  108. *
  109. * @param integer $length The amount of bytes.
  110. * @return string
  111. * @throws Reader_Exception if <var>length</var> attribute is negative.
  112. */
  113. public function read($length)
  114. {
  115. if ($length < 0)
  116. throw new Reader_Exception("Invalid argument");
  117. if ($length == 0)
  118. return "";
  119. return fread($this->_fd, $length);
  120. }
  121. /**
  122. * Returns the current point of operation.
  123. *
  124. * @return integer
  125. */
  126. public function getOffset()
  127. {
  128. return ftell($this->_fd);
  129. }
  130. /**
  131. * Sets the point of operation, ie the cursor offset value. The offset can
  132. * also be set to a negative value when it is interpreted as an offset from
  133. * the end of the file instead of the beginning.
  134. *
  135. * @param integer $offset The new point of operation.
  136. * @return void
  137. */
  138. public function setOffset($offset)
  139. {
  140. fseek($this->_fd, $offset < 0 ? $this->_size + $offset : $offset);
  141. }
  142. /**
  143. * Returns the file size in bytes.
  144. *
  145. * @return integer
  146. */
  147. public function getSize()
  148. {
  149. return $this->_size;
  150. }
  151. /**
  152. * Magic function so that $obj->value will work.
  153. *
  154. * @param string $name The field name.
  155. * @return mixed
  156. */
  157. public function __get($name) {
  158. if (method_exists($this, "get" . ucfirst(strtolower($name))))
  159. return call_user_func(array($this, "get" . ucfirst(strtolower($name))));
  160. else throw new Reader_Exception("Unknown field: " . $name);
  161. }
  162. /**
  163. * Magic function so that assignments with $obj->value will work.
  164. *
  165. * @param string $name The field name.
  166. * @param string $value The field value.
  167. * @return mixed
  168. */
  169. public function __set($name, $value) {
  170. if (method_exists($this, "set" . ucfirst(strtolower($name))))
  171. call_user_func
  172. (array($this, "set" . ucfirst(strtolower($name))), $value);
  173. else throw new Reader_Exception("Unknown field: " . $name);
  174. }
  175. /**
  176. * Magic function to delegate the call to helper methods of
  177. * <var>Transform</var> class to transform read data in another format.
  178. *
  179. * The read data length is determined from the helper method name. For methods
  180. * where arbitrary data lengths are accepted a parameter can be used to
  181. * specify the length.
  182. *
  183. * @param string $method The method to be called.
  184. * @param string $params The parameters should the function accept them.
  185. * @return mixed
  186. * @throws Reader_Exception if no such transformer is implemented
  187. */
  188. public function __call($method, $params) {
  189. $chunks = array();
  190. if (preg_match
  191. ("/read([a-z]{3,6})?(\d{1,2})?(?:LE|BE)?/i", $method, $chunks) &&
  192. method_exists("Transform", preg_replace("/^read/", "from", $method))) {
  193. return call_user_func
  194. (array("Transform", preg_replace("/^read/", "from", $method)),
  195. $this->read(preg_match("/String|(?:H|L)Hex/", $chunks[1]) ?
  196. (isset($params[0]) ? $params[0] : 1) :
  197. ($chunks[1] == "GUID" ? 16 : $chunks[2] / 8)));
  198. } else throw new Reader_Exception("Unknown method: " . $method);
  199. }
  200. }