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