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