author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
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 |
*/ |
|
20 |
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 |
*/ |
|
16 | 49 |
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 |
*/ |
|
59 |
function readint32() { |
|
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 |
*/ |
|
76 |
function readint32array( $count ) { |
|
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 |
*/ |
|
91 |
function substr( $string, $start, $length ) { |
|
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 |
*/ |
|
103 |
function strlen( $string ) { |
|
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 |
*/ |
|
116 |
function str_split( $string, $chunk_size ) { |
|
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 |
*/ |
|
132 |
function pos() { |
|
133 |
return $this->_pos; |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* @return true |
|
138 |
*/ |
|
139 |
function is_resource() { |
|
140 |
return true; |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* @return true |
|
145 |
*/ |
|
146 |
function close() { |
|
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 |
*/ |
|
158 |
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 |
*/ |
|
179 |
function read( $bytes ) { |
|
180 |
return fread( $this->_f, $bytes ); |
|
0 | 181 |
} |
182 |
||
9 | 183 |
/** |
184 |
* @param int $pos |
|
18 | 185 |
* @return bool |
9 | 186 |
*/ |
187 |
function seekto( $pos ) { |
|
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 |
*/ |
|
198 |
function is_resource() { |
|
199 |
return is_resource( $this->_f ); |
|
200 |
} |
|
0 | 201 |
|
9 | 202 |
/** |
203 |
* @return bool |
|
204 |
*/ |
|
205 |
function feof() { |
|
206 |
return feof( $this->_f ); |
|
207 |
} |
|
0 | 208 |
|
9 | 209 |
/** |
210 |
* @return bool |
|
211 |
*/ |
|
212 |
function close() { |
|
213 |
return fclose( $this->_f ); |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* @return string |
|
218 |
*/ |
|
219 |
function read_all() { |
|
220 |
$all = ''; |
|
221 |
while ( ! $this->feof() ) { |
|
222 |
$all .= $this->read( 4096 ); |
|
223 |
} |
|
224 |
return $all; |
|
225 |
} |
|
0 | 226 |
} |
227 |
endif; |
|
228 |
||
9 | 229 |
if ( ! class_exists( 'POMO_StringReader', false ) ) : |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
/** |
9 | 231 |
* Provides file-like methods for manipulating a string instead |
232 |
* of a physical file. |
|
5 | 233 |
*/ |
9 | 234 |
class POMO_StringReader extends POMO_Reader { |
235 |
||
18 | 236 |
public $_str = ''; |
9 | 237 |
|
238 |
/** |
|
239 |
* PHP5 constructor. |
|
240 |
*/ |
|
241 |
function __construct( $str = '' ) { |
|
16 | 242 |
parent::__construct(); |
9 | 243 |
$this->_str = $str; |
244 |
$this->_pos = 0; |
|
245 |
} |
|
246 |
||
247 |
/** |
|
248 |
* PHP4 constructor. |
|
16 | 249 |
* |
250 |
* @deprecated 5.4.0 Use __construct() instead. |
|
251 |
* |
|
252 |
* @see POMO_StringReader::__construct() |
|
9 | 253 |
*/ |
254 |
public function POMO_StringReader( $str = '' ) { |
|
16 | 255 |
_deprecated_constructor( self::class, '5.4.0', static::class ); |
9 | 256 |
self::__construct( $str ); |
257 |
} |
|
0 | 258 |
|
9 | 259 |
/** |
260 |
* @param string $bytes |
|
261 |
* @return string |
|
262 |
*/ |
|
263 |
function read( $bytes ) { |
|
264 |
$data = $this->substr( $this->_str, $this->_pos, $bytes ); |
|
265 |
$this->_pos += $bytes; |
|
266 |
if ( $this->strlen( $this->_str ) < $this->_pos ) { |
|
267 |
$this->_pos = $this->strlen( $this->_str ); |
|
268 |
} |
|
269 |
return $data; |
|
270 |
} |
|
271 |
||
272 |
/** |
|
273 |
* @param int $pos |
|
274 |
* @return int |
|
275 |
*/ |
|
276 |
function seekto( $pos ) { |
|
277 |
$this->_pos = $pos; |
|
278 |
if ( $this->strlen( $this->_str ) < $this->_pos ) { |
|
279 |
$this->_pos = $this->strlen( $this->_str ); |
|
280 |
} |
|
281 |
return $this->_pos; |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* @return int |
|
286 |
*/ |
|
287 |
function length() { |
|
288 |
return $this->strlen( $this->_str ); |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* @return string |
|
293 |
*/ |
|
294 |
function read_all() { |
|
295 |
return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) ); |
|
296 |
} |
|
297 |
||
0 | 298 |
} |
299 |
endif; |
|
300 |
||
9 | 301 |
if ( ! class_exists( 'POMO_CachedFileReader', false ) ) : |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
/** |
9 | 303 |
* 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
|
304 |
*/ |
9 | 305 |
class POMO_CachedFileReader extends POMO_StringReader { |
306 |
/** |
|
307 |
* PHP5 constructor. |
|
308 |
*/ |
|
309 |
function __construct( $filename ) { |
|
16 | 310 |
parent::__construct(); |
9 | 311 |
$this->_str = file_get_contents( $filename ); |
312 |
if ( false === $this->_str ) { |
|
313 |
return false; |
|
314 |
} |
|
315 |
$this->_pos = 0; |
|
316 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
|
9 | 318 |
/** |
319 |
* PHP4 constructor. |
|
16 | 320 |
* |
321 |
* @deprecated 5.4.0 Use __construct() instead. |
|
322 |
* |
|
323 |
* @see POMO_CachedFileReader::__construct() |
|
9 | 324 |
*/ |
325 |
public function POMO_CachedFileReader( $filename ) { |
|
16 | 326 |
_deprecated_constructor( self::class, '5.4.0', static::class ); |
9 | 327 |
self::__construct( $filename ); |
328 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
} |
0 | 330 |
endif; |
331 |
||
9 | 332 |
if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) : |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
/** |
9 | 334 |
* 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
|
335 |
*/ |
9 | 336 |
class POMO_CachedIntFileReader extends POMO_CachedFileReader { |
337 |
/** |
|
338 |
* PHP5 constructor. |
|
339 |
*/ |
|
340 |
public function __construct( $filename ) { |
|
16 | 341 |
parent::__construct( $filename ); |
9 | 342 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
|
9 | 344 |
/** |
345 |
* PHP4 constructor. |
|
16 | 346 |
* |
347 |
* @deprecated 5.4.0 Use __construct() instead. |
|
348 |
* |
|
349 |
* @see POMO_CachedIntFileReader::__construct() |
|
9 | 350 |
*/ |
351 |
function POMO_CachedIntFileReader( $filename ) { |
|
16 | 352 |
_deprecated_constructor( self::class, '5.4.0', static::class ); |
9 | 353 |
self::__construct( $filename ); |
354 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
endif; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |