streams.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 string $endian Set the endianness of the file. Accepts 'big', or 'little'.
  31. */
  32. function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  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. }
  46. $endian_letter = ( 'big' == $this->endian ) ? 'N' : 'V';
  47. $int = unpack( $endian_letter, $bytes );
  48. return reset( $int );
  49. }
  50. /**
  51. * Reads an array of 32-bit Integers from the Stream
  52. *
  53. * @param integer $count How many elements should be read
  54. * @return mixed Array of integers or false if there isn't
  55. * enough data or on error
  56. */
  57. function readint32array( $count ) {
  58. $bytes = $this->read( 4 * $count );
  59. if ( 4 * $count != $this->strlen( $bytes ) ) {
  60. return false;
  61. }
  62. $endian_letter = ( 'big' == $this->endian ) ? 'N' : 'V';
  63. return unpack( $endian_letter . $count, $bytes );
  64. }
  65. /**
  66. * @param string $string
  67. * @param int $start
  68. * @param int $length
  69. * @return string
  70. */
  71. function substr( $string, $start, $length ) {
  72. if ( $this->is_overloaded ) {
  73. return mb_substr( $string, $start, $length, 'ascii' );
  74. } else {
  75. return substr( $string, $start, $length );
  76. }
  77. }
  78. /**
  79. * @param string $string
  80. * @return int
  81. */
  82. function strlen( $string ) {
  83. if ( $this->is_overloaded ) {
  84. return mb_strlen( $string, 'ascii' );
  85. } else {
  86. return strlen( $string );
  87. }
  88. }
  89. /**
  90. * @param string $string
  91. * @param int $chunk_size
  92. * @return array
  93. */
  94. function str_split( $string, $chunk_size ) {
  95. if ( ! function_exists( 'str_split' ) ) {
  96. $length = $this->strlen( $string );
  97. $out = array();
  98. for ( $i = 0; $i < $length; $i += $chunk_size ) {
  99. $out[] = $this->substr( $string, $i, $chunk_size );
  100. }
  101. return $out;
  102. } else {
  103. return str_split( $string, $chunk_size );
  104. }
  105. }
  106. /**
  107. * @return int
  108. */
  109. function pos() {
  110. return $this->_pos;
  111. }
  112. /**
  113. * @return true
  114. */
  115. function is_resource() {
  116. return true;
  117. }
  118. /**
  119. * @return true
  120. */
  121. function close() {
  122. return true;
  123. }
  124. }
  125. endif;
  126. if ( ! class_exists( 'POMO_FileReader', false ) ) :
  127. class POMO_FileReader extends POMO_Reader {
  128. /**
  129. * @param string $filename
  130. */
  131. function __construct( $filename ) {
  132. parent::POMO_Reader();
  133. $this->_f = fopen( $filename, 'rb' );
  134. }
  135. /**
  136. * PHP4 constructor.
  137. */
  138. public function POMO_FileReader( $filename ) {
  139. self::__construct( $filename );
  140. }
  141. /**
  142. * @param int $bytes
  143. * @return string|false Returns read string, otherwise false.
  144. */
  145. function read( $bytes ) {
  146. return fread( $this->_f, $bytes );
  147. }
  148. /**
  149. * @param int $pos
  150. * @return boolean
  151. */
  152. function seekto( $pos ) {
  153. if ( -1 == fseek( $this->_f, $pos, SEEK_SET ) ) {
  154. return false;
  155. }
  156. $this->_pos = $pos;
  157. return true;
  158. }
  159. /**
  160. * @return bool
  161. */
  162. function is_resource() {
  163. return is_resource( $this->_f );
  164. }
  165. /**
  166. * @return bool
  167. */
  168. function feof() {
  169. return feof( $this->_f );
  170. }
  171. /**
  172. * @return bool
  173. */
  174. function close() {
  175. return fclose( $this->_f );
  176. }
  177. /**
  178. * @return string
  179. */
  180. function read_all() {
  181. $all = '';
  182. while ( ! $this->feof() ) {
  183. $all .= $this->read( 4096 );
  184. }
  185. return $all;
  186. }
  187. }
  188. endif;
  189. if ( ! class_exists( 'POMO_StringReader', false ) ) :
  190. /**
  191. * Provides file-like methods for manipulating a string instead
  192. * of a physical file.
  193. */
  194. class POMO_StringReader extends POMO_Reader {
  195. var $_str = '';
  196. /**
  197. * PHP5 constructor.
  198. */
  199. function __construct( $str = '' ) {
  200. parent::POMO_Reader();
  201. $this->_str = $str;
  202. $this->_pos = 0;
  203. }
  204. /**
  205. * PHP4 constructor.
  206. */
  207. public function POMO_StringReader( $str = '' ) {
  208. self::__construct( $str );
  209. }
  210. /**
  211. * @param string $bytes
  212. * @return string
  213. */
  214. function read( $bytes ) {
  215. $data = $this->substr( $this->_str, $this->_pos, $bytes );
  216. $this->_pos += $bytes;
  217. if ( $this->strlen( $this->_str ) < $this->_pos ) {
  218. $this->_pos = $this->strlen( $this->_str );
  219. }
  220. return $data;
  221. }
  222. /**
  223. * @param int $pos
  224. * @return int
  225. */
  226. function seekto( $pos ) {
  227. $this->_pos = $pos;
  228. if ( $this->strlen( $this->_str ) < $this->_pos ) {
  229. $this->_pos = $this->strlen( $this->_str );
  230. }
  231. return $this->_pos;
  232. }
  233. /**
  234. * @return int
  235. */
  236. function length() {
  237. return $this->strlen( $this->_str );
  238. }
  239. /**
  240. * @return string
  241. */
  242. function read_all() {
  243. return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) );
  244. }
  245. }
  246. endif;
  247. if ( ! class_exists( 'POMO_CachedFileReader', false ) ) :
  248. /**
  249. * Reads the contents of the file in the beginning.
  250. */
  251. class POMO_CachedFileReader extends POMO_StringReader {
  252. /**
  253. * PHP5 constructor.
  254. */
  255. function __construct( $filename ) {
  256. parent::POMO_StringReader();
  257. $this->_str = file_get_contents( $filename );
  258. if ( false === $this->_str ) {
  259. return false;
  260. }
  261. $this->_pos = 0;
  262. }
  263. /**
  264. * PHP4 constructor.
  265. */
  266. public function POMO_CachedFileReader( $filename ) {
  267. self::__construct( $filename );
  268. }
  269. }
  270. endif;
  271. if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) :
  272. /**
  273. * Reads the contents of the file in the beginning.
  274. */
  275. class POMO_CachedIntFileReader extends POMO_CachedFileReader {
  276. /**
  277. * PHP5 constructor.
  278. */
  279. public function __construct( $filename ) {
  280. parent::POMO_CachedFileReader( $filename );
  281. }
  282. /**
  283. * PHP4 constructor.
  284. */
  285. function POMO_CachedIntFileReader( $filename ) {
  286. self::__construct( $filename );
  287. }
  288. }
  289. endif;