3 * Classes, which help reading streams of data from files.
4 * Based on the classes from Danilo Segan <danilo@kvota.net>
6 * @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $
11 if ( ! class_exists( 'POMO_Reader', false ) ):
14 var $endian = 'little';
20 function __construct() {
21 $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
28 public function POMO_Reader() {
33 * Sets the endianness of the file.
35 * @param $endian string 'big' or 'little'
37 function setEndian($endian) {
38 $this->endian = $endian;
42 * Reads a 32bit Integer from the Stream
44 * @return mixed The integer, corresponding to the next 32 bits from
45 * the stream of false if there are not enough bytes or on error
47 function readint32() {
48 $bytes = $this->read(4);
49 if (4 != $this->strlen($bytes))
51 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
52 $int = unpack($endian_letter, $bytes);
57 * Reads an array of 32-bit Integers from the Stream
59 * @param integer count How many elements should be read
60 * @return mixed Array of integers or false if there isn't
61 * enough data or on error
63 function readint32array($count) {
64 $bytes = $this->read(4 * $count);
65 if (4*$count != $this->strlen($bytes))
67 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
68 return unpack($endian_letter.$count, $bytes);
72 * @param string $string
77 function substr($string, $start, $length) {
78 if ($this->is_overloaded) {
79 return mb_substr($string, $start, $length, 'ascii');
81 return substr($string, $start, $length);
86 * @param string $string
89 function strlen($string) {
90 if ($this->is_overloaded) {
91 return mb_strlen($string, 'ascii');
93 return strlen($string);
98 * @param string $string
99 * @param int $chunk_size
102 function str_split($string, $chunk_size) {
103 if (!function_exists('str_split')) {
104 $length = $this->strlen($string);
106 for ($i = 0; $i < $length; $i += $chunk_size)
107 $out[] = $this->substr($string, $i, $chunk_size);
110 return str_split( $string, $chunk_size );
124 function is_resource() {
137 if ( ! class_exists( 'POMO_FileReader', false ) ):
138 class POMO_FileReader extends POMO_Reader {
141 * @param string $filename
143 function __construct( $filename ) {
144 parent::POMO_Reader();
145 $this->_f = fopen($filename, 'rb');
151 public function POMO_FileReader( $filename ) {
152 self::__construct( $filename );
158 function read($bytes) {
159 return fread($this->_f, $bytes);
166 function seekto($pos) {
167 if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
177 function is_resource() {
178 return is_resource($this->_f);
185 return feof($this->_f);
192 return fclose($this->_f);
198 function read_all() {
200 while ( !$this->feof() )
201 $all .= $this->read(4096);
207 if ( ! class_exists( 'POMO_StringReader', false ) ):
209 * Provides file-like methods for manipulating a string instead
210 * of a physical file.
212 class POMO_StringReader extends POMO_Reader {
219 function __construct( $str = '' ) {
220 parent::POMO_Reader();
228 public function POMO_StringReader( $str = '' ) {
229 self::__construct( $str );
233 * @param string $bytes
236 function read($bytes) {
237 $data = $this->substr($this->_str, $this->_pos, $bytes);
238 $this->_pos += $bytes;
239 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
247 function seekto($pos) {
249 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
257 return $this->strlen($this->_str);
263 function read_all() {
264 return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
270 if ( ! class_exists( 'POMO_CachedFileReader', false ) ):
272 * Reads the contents of the file in the beginning.
274 class POMO_CachedFileReader extends POMO_StringReader {
278 function __construct( $filename ) {
279 parent::POMO_StringReader();
280 $this->_str = file_get_contents($filename);
281 if (false === $this->_str)
289 public function POMO_CachedFileReader( $filename ) {
290 self::__construct( $filename );
295 if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ):
297 * Reads the contents of the file in the beginning.
299 class POMO_CachedIntFileReader extends POMO_CachedFileReader {
303 public function __construct( $filename ) {
304 parent::POMO_CachedFileReader($filename);
310 function POMO_CachedIntFileReader( $filename ) {
311 self::__construct( $filename );