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