streams.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. #[AllowDynamicProperties]
  12. class POMO_Reader {
  13. public $endian = 'little';
  14. public $_pos;
  15. public $is_overloaded;
  16. /**
  17. * PHP5 constructor.
  18. */
  19. public function __construct() {
  20. if ( function_exists( 'mb_substr' )
  21. && ( (int) ini_get( 'mbstring.func_overload' ) & 2 ) // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated
  22. ) {
  23. $this->is_overloaded = true;
  24. } else {
  25. $this->is_overloaded = false;
  26. }
  27. $this->_pos = 0;
  28. }
  29. /**
  30. * PHP4 constructor.
  31. *
  32. * @deprecated 5.4.0 Use __construct() instead.
  33. *
  34. * @see POMO_Reader::__construct()
  35. */
  36. public function POMO_Reader() {
  37. _deprecated_constructor( self::class, '5.4.0', static::class );
  38. self::__construct();
  39. }
  40. /**
  41. * Sets the endianness of the file.
  42. *
  43. * @param string $endian Set the endianness of the file. Accepts 'big', or 'little'.
  44. */
  45. public function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
  46. $this->endian = $endian;
  47. }
  48. /**
  49. * Reads a 32bit Integer from the Stream
  50. *
  51. * @return mixed The integer, corresponding to the next 32 bits from
  52. * the stream of false if there are not enough bytes or on error
  53. */
  54. public function readint32() {
  55. $bytes = $this->read( 4 );
  56. if ( 4 != $this->strlen( $bytes ) ) {
  57. return false;
  58. }
  59. $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V';
  60. $int = unpack( $endian_letter, $bytes );
  61. return reset( $int );
  62. }
  63. /**
  64. * Reads an array of 32-bit Integers from the Stream
  65. *
  66. * @param int $count How many elements should be read
  67. * @return mixed Array of integers or false if there isn't
  68. * enough data or on error
  69. */
  70. public function readint32array( $count ) {
  71. $bytes = $this->read( 4 * $count );
  72. if ( 4 * $count != $this->strlen( $bytes ) ) {
  73. return false;
  74. }
  75. $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V';
  76. return unpack( $endian_letter . $count, $bytes );
  77. }
  78. /**
  79. * @param string $string
  80. * @param int $start
  81. * @param int $length
  82. * @return string
  83. */
  84. public function substr( $string, $start, $length ) {
  85. if ( $this->is_overloaded ) {
  86. return mb_substr( $string, $start, $length, 'ascii' );
  87. } else {
  88. return substr( $string, $start, $length );
  89. }
  90. }
  91. /**
  92. * @param string $string
  93. * @return int
  94. */
  95. public function strlen( $string ) {
  96. if ( $this->is_overloaded ) {
  97. return mb_strlen( $string, 'ascii' );
  98. } else {
  99. return strlen( $string );
  100. }
  101. }
  102. /**
  103. * @param string $string
  104. * @param int $chunk_size
  105. * @return array
  106. */
  107. public function str_split( $string, $chunk_size ) {
  108. if ( ! function_exists( 'str_split' ) ) {
  109. $length = $this->strlen( $string );
  110. $out = array();
  111. for ( $i = 0; $i < $length; $i += $chunk_size ) {
  112. $out[] = $this->substr( $string, $i, $chunk_size );
  113. }
  114. return $out;
  115. } else {
  116. return str_split( $string, $chunk_size );
  117. }
  118. }
  119. /**
  120. * @return int
  121. */
  122. public function pos() {
  123. return $this->_pos;
  124. }
  125. /**
  126. * @return true
  127. */
  128. public function is_resource() {
  129. return true;
  130. }
  131. /**
  132. * @return true
  133. */
  134. public function close() {
  135. return true;
  136. }
  137. }
  138. endif;
  139. if ( ! class_exists( 'POMO_FileReader', false ) ) :
  140. class POMO_FileReader extends POMO_Reader {
  141. /**
  142. * File pointer resource.
  143. *
  144. * @var resource|false
  145. */
  146. public $_f;
  147. /**
  148. * @param string $filename
  149. */
  150. public function __construct( $filename ) {
  151. parent::__construct();
  152. $this->_f = fopen( $filename, 'rb' );
  153. }
  154. /**
  155. * PHP4 constructor.
  156. *
  157. * @deprecated 5.4.0 Use __construct() instead.
  158. *
  159. * @see POMO_FileReader::__construct()
  160. */
  161. public function POMO_FileReader( $filename ) {
  162. _deprecated_constructor( self::class, '5.4.0', static::class );
  163. self::__construct( $filename );
  164. }
  165. /**
  166. * @param int $bytes
  167. * @return string|false Returns read string, otherwise false.
  168. */
  169. public function read( $bytes ) {
  170. return fread( $this->_f, $bytes );
  171. }
  172. /**
  173. * @param int $pos
  174. * @return bool
  175. */
  176. public function seekto( $pos ) {
  177. if ( -1 == fseek( $this->_f, $pos, SEEK_SET ) ) {
  178. return false;
  179. }
  180. $this->_pos = $pos;
  181. return true;
  182. }
  183. /**
  184. * @return bool
  185. */
  186. public function is_resource() {
  187. return is_resource( $this->_f );
  188. }
  189. /**
  190. * @return bool
  191. */
  192. public function feof() {
  193. return feof( $this->_f );
  194. }
  195. /**
  196. * @return bool
  197. */
  198. public function close() {
  199. return fclose( $this->_f );
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function read_all() {
  205. return stream_get_contents( $this->_f );
  206. }
  207. }
  208. endif;
  209. if ( ! class_exists( 'POMO_StringReader', false ) ) :
  210. /**
  211. * Provides file-like methods for manipulating a string instead
  212. * of a physical file.
  213. */
  214. class POMO_StringReader extends POMO_Reader {
  215. public $_str = '';
  216. /**
  217. * PHP5 constructor.
  218. */
  219. public function __construct( $str = '' ) {
  220. parent::__construct();
  221. $this->_str = $str;
  222. $this->_pos = 0;
  223. }
  224. /**
  225. * PHP4 constructor.
  226. *
  227. * @deprecated 5.4.0 Use __construct() instead.
  228. *
  229. * @see POMO_StringReader::__construct()
  230. */
  231. public function POMO_StringReader( $str = '' ) {
  232. _deprecated_constructor( self::class, '5.4.0', static::class );
  233. self::__construct( $str );
  234. }
  235. /**
  236. * @param string $bytes
  237. * @return string
  238. */
  239. public function read( $bytes ) {
  240. $data = $this->substr( $this->_str, $this->_pos, $bytes );
  241. $this->_pos += $bytes;
  242. if ( $this->strlen( $this->_str ) < $this->_pos ) {
  243. $this->_pos = $this->strlen( $this->_str );
  244. }
  245. return $data;
  246. }
  247. /**
  248. * @param int $pos
  249. * @return int
  250. */
  251. public function seekto( $pos ) {
  252. $this->_pos = $pos;
  253. if ( $this->strlen( $this->_str ) < $this->_pos ) {
  254. $this->_pos = $this->strlen( $this->_str );
  255. }
  256. return $this->_pos;
  257. }
  258. /**
  259. * @return int
  260. */
  261. public function length() {
  262. return $this->strlen( $this->_str );
  263. }
  264. /**
  265. * @return string
  266. */
  267. public function read_all() {
  268. return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) );
  269. }
  270. }
  271. endif;
  272. if ( ! class_exists( 'POMO_CachedFileReader', false ) ) :
  273. /**
  274. * Reads the contents of the file in the beginning.
  275. */
  276. class POMO_CachedFileReader extends POMO_StringReader {
  277. /**
  278. * PHP5 constructor.
  279. */
  280. public function __construct( $filename ) {
  281. parent::__construct();
  282. $this->_str = file_get_contents( $filename );
  283. if ( false === $this->_str ) {
  284. return false;
  285. }
  286. $this->_pos = 0;
  287. }
  288. /**
  289. * PHP4 constructor.
  290. *
  291. * @deprecated 5.4.0 Use __construct() instead.
  292. *
  293. * @see POMO_CachedFileReader::__construct()
  294. */
  295. public function POMO_CachedFileReader( $filename ) {
  296. _deprecated_constructor( self::class, '5.4.0', static::class );
  297. self::__construct( $filename );
  298. }
  299. }
  300. endif;
  301. if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) :
  302. /**
  303. * Reads the contents of the file in the beginning.
  304. */
  305. class POMO_CachedIntFileReader extends POMO_CachedFileReader {
  306. /**
  307. * PHP5 constructor.
  308. */
  309. public function __construct( $filename ) {
  310. parent::__construct( $filename );
  311. }
  312. /**
  313. * PHP4 constructor.
  314. *
  315. * @deprecated 5.4.0 Use __construct() instead.
  316. *
  317. * @see POMO_CachedIntFileReader::__construct()
  318. */
  319. public function POMO_CachedIntFileReader( $filename ) {
  320. _deprecated_constructor( self::class, '5.4.0', static::class );
  321. self::__construct( $filename );
  322. }
  323. }
  324. endif;