You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

316 lines
5.8 KiB

  1. <?php
  2. /**
  3. * Classes, which help reading streams of data from files.
  4. * Based on the classes from Danilo Segan <danilo@kvota.net>
  5. *
  6. * @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $
  7. * @package pomo
  8. * @subpackage streams
  9. */
  10. if ( ! class_exists( 'POMO_Reader', false ) ):
  11. class POMO_Reader {
  12. var $endian = 'little';
  13. var $_post = '';
  14. /**
  15. * PHP5 constructor.
  16. */
  17. function __construct() {
  18. $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
  19. $this->_pos = 0;
  20. }
  21. /**
  22. * PHP4 constructor.
  23. */
  24. public function POMO_Reader() {
  25. self::__construct();
  26. }
  27. /**
  28. * Sets the endianness of the file.
  29. *
  30. * @param $endian string 'big' or 'little'
  31. */
  32. function setEndian($endian) {
  33. $this->endian = $endian;
  34. }
  35. /**
  36. * Reads a 32bit Integer from the Stream
  37. *
  38. * @return mixed The integer, corresponding to the next 32 bits from
  39. * the stream of false if there are not enough bytes or on error
  40. */
  41. function readint32() {
  42. $bytes = $this->read(4);
  43. if (4 != $this->strlen($bytes))
  44. return false;
  45. $endian_letter = ('big' == $this->endian)? 'N' : 'V';
  46. $int = unpack($endian_letter, $bytes);
  47. return reset( $int );
  48. }
  49. /**
  50. * Reads an array of 32-bit Integers from the Stream
  51. *
  52. * @param integer count How many elements should be read
  53. * @return mixed Array of integers or false if there isn't
  54. * enough data or on error
  55. */
  56. function readint32array($count) {
  57. $bytes = $this->read(4 * $count);
  58. if (4*$count != $this->strlen($bytes))
  59. return false;
  60. $endian_letter = ('big' == $this->endian)? 'N' : 'V';
  61. return unpack($endian_letter.$count, $bytes);
  62. }
  63. /**
  64. * @param string $string
  65. * @param int $start
  66. * @param int $length
  67. * @return string
  68. */
  69. function substr($string, $start, $length) {
  70. if ($this->is_overloaded) {
  71. return mb_substr($string, $start, $length, 'ascii');
  72. } else {
  73. return substr($string, $start, $length);
  74. }
  75. }
  76. /**
  77. * @param string $string
  78. * @return int
  79. */
  80. function strlen($string) {
  81. if ($this->is_overloaded) {
  82. return mb_strlen($string, 'ascii');
  83. } else {
  84. return strlen($string);
  85. }
  86. }
  87. /**
  88. * @param string $string
  89. * @param int $chunk_size
  90. * @return array
  91. */
  92. function str_split($string, $chunk_size) {
  93. if (!function_exists('str_split')) {
  94. $length = $this->strlen($string);
  95. $out = array();
  96. for ($i = 0; $i < $length; $i += $chunk_size)
  97. $out[] = $this->substr($string, $i, $chunk_size);
  98. return $out;
  99. } else {
  100. return str_split( $string, $chunk_size );
  101. }
  102. }
  103. /**
  104. * @return int
  105. */
  106. function pos() {
  107. return $this->_pos;
  108. }
  109. /**
  110. * @return true
  111. */
  112. function is_resource() {
  113. return true;
  114. }
  115. /**
  116. * @return true
  117. */
  118. function close() {
  119. return true;
  120. }
  121. }
  122. endif;
  123. if ( ! class_exists( 'POMO_FileReader', false ) ):
  124. class POMO_FileReader extends POMO_Reader {
  125. /**
  126. * @param string $filename
  127. */
  128. function __construct( $filename ) {
  129. parent::POMO_Reader();
  130. $this->_f = fopen($filename, 'rb');
  131. }
  132. /**
  133. * PHP4 constructor.
  134. */
  135. public function POMO_FileReader( $filename ) {
  136. self::__construct( $filename );
  137. }
  138. /**
  139. * @param int $bytes
  140. */
  141. function read($bytes) {
  142. return fread($this->_f, $bytes);
  143. }
  144. /**
  145. * @param int $pos
  146. * @return boolean
  147. */
  148. function seekto($pos) {
  149. if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
  150. return false;
  151. }
  152. $this->_pos = $pos;
  153. return true;
  154. }
  155. /**
  156. * @return bool
  157. */
  158. function is_resource() {
  159. return is_resource($this->_f);
  160. }
  161. /**
  162. * @return bool
  163. */
  164. function feof() {
  165. return feof($this->_f);
  166. }
  167. /**
  168. * @return bool
  169. */
  170. function close() {
  171. return fclose($this->_f);
  172. }
  173. /**
  174. * @return string
  175. */
  176. function read_all() {
  177. $all = '';
  178. while ( !$this->feof() )
  179. $all .= $this->read(4096);
  180. return $all;
  181. }
  182. }
  183. endif;
  184. if ( ! class_exists( 'POMO_StringReader', false ) ):
  185. /**
  186. * Provides file-like methods for manipulating a string instead
  187. * of a physical file.
  188. */
  189. class POMO_StringReader extends POMO_Reader {
  190. var $_str = '';
  191. /**
  192. * PHP5 constructor.
  193. */
  194. function __construct( $str = '' ) {
  195. parent::POMO_Reader();
  196. $this->_str = $str;
  197. $this->_pos = 0;
  198. }
  199. /**
  200. * PHP4 constructor.
  201. */
  202. public function POMO_StringReader( $str = '' ) {
  203. self::__construct( $str );
  204. }
  205. /**
  206. * @param string $bytes
  207. * @return string
  208. */
  209. function read($bytes) {
  210. $data = $this->substr($this->_str, $this->_pos, $bytes);
  211. $this->_pos += $bytes;
  212. if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
  213. return $data;
  214. }
  215. /**
  216. * @param int $pos
  217. * @return int
  218. */
  219. function seekto($pos) {
  220. $this->_pos = $pos;
  221. if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
  222. return $this->_pos;
  223. }
  224. /**
  225. * @return int
  226. */
  227. function length() {
  228. return $this->strlen($this->_str);
  229. }
  230. /**
  231. * @return string
  232. */
  233. function read_all() {
  234. return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
  235. }
  236. }
  237. endif;
  238. if ( ! class_exists( 'POMO_CachedFileReader', false ) ):
  239. /**
  240. * Reads the contents of the file in the beginning.
  241. */
  242. class POMO_CachedFileReader extends POMO_StringReader {
  243. /**
  244. * PHP5 constructor.
  245. */
  246. function __construct( $filename ) {
  247. parent::POMO_StringReader();
  248. $this->_str = file_get_contents($filename);
  249. if (false === $this->_str)
  250. return false;
  251. $this->_pos = 0;
  252. }
  253. /**
  254. * PHP4 constructor.
  255. */
  256. public function POMO_CachedFileReader( $filename ) {
  257. self::__construct( $filename );
  258. }
  259. }
  260. endif;
  261. if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ):
  262. /**
  263. * Reads the contents of the file in the beginning.
  264. */
  265. class POMO_CachedIntFileReader extends POMO_CachedFileReader {
  266. /**
  267. * PHP5 constructor.
  268. */
  269. public function __construct( $filename ) {
  270. parent::POMO_CachedFileReader($filename);
  271. }
  272. /**
  273. * PHP4 constructor.
  274. */
  275. function POMO_CachedIntFileReader( $filename ) {
  276. self::__construct( $filename );
  277. }
  278. }
  279. endif;