44 /** |
44 /** |
45 * Sets the endianness of the file. |
45 * Sets the endianness of the file. |
46 * |
46 * |
47 * @param string $endian Set the endianness of the file. Accepts 'big', or 'little'. |
47 * @param string $endian Set the endianness of the file. Accepts 'big', or 'little'. |
48 */ |
48 */ |
49 function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
49 public function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
50 $this->endian = $endian; |
50 $this->endian = $endian; |
51 } |
51 } |
52 |
52 |
53 /** |
53 /** |
54 * Reads a 32bit Integer from the Stream |
54 * Reads a 32bit Integer from the Stream |
55 * |
55 * |
56 * @return mixed The integer, corresponding to the next 32 bits from |
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 |
57 * the stream of false if there are not enough bytes or on error |
58 */ |
58 */ |
59 function readint32() { |
59 public function readint32() { |
60 $bytes = $this->read( 4 ); |
60 $bytes = $this->read( 4 ); |
61 if ( 4 != $this->strlen( $bytes ) ) { |
61 if ( 4 != $this->strlen( $bytes ) ) { |
62 return false; |
62 return false; |
63 } |
63 } |
64 $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V'; |
64 $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V'; |
71 * |
71 * |
72 * @param int $count How many elements should be read |
72 * @param int $count How many elements should be read |
73 * @return mixed Array of integers or false if there isn't |
73 * @return mixed Array of integers or false if there isn't |
74 * enough data or on error |
74 * enough data or on error |
75 */ |
75 */ |
76 function readint32array( $count ) { |
76 public function readint32array( $count ) { |
77 $bytes = $this->read( 4 * $count ); |
77 $bytes = $this->read( 4 * $count ); |
78 if ( 4 * $count != $this->strlen( $bytes ) ) { |
78 if ( 4 * $count != $this->strlen( $bytes ) ) { |
79 return false; |
79 return false; |
80 } |
80 } |
81 $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V'; |
81 $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V'; |
86 * @param string $string |
86 * @param string $string |
87 * @param int $start |
87 * @param int $start |
88 * @param int $length |
88 * @param int $length |
89 * @return string |
89 * @return string |
90 */ |
90 */ |
91 function substr( $string, $start, $length ) { |
91 public function substr( $string, $start, $length ) { |
92 if ( $this->is_overloaded ) { |
92 if ( $this->is_overloaded ) { |
93 return mb_substr( $string, $start, $length, 'ascii' ); |
93 return mb_substr( $string, $start, $length, 'ascii' ); |
94 } else { |
94 } else { |
95 return substr( $string, $start, $length ); |
95 return substr( $string, $start, $length ); |
96 } |
96 } |
98 |
98 |
99 /** |
99 /** |
100 * @param string $string |
100 * @param string $string |
101 * @return int |
101 * @return int |
102 */ |
102 */ |
103 function strlen( $string ) { |
103 public function strlen( $string ) { |
104 if ( $this->is_overloaded ) { |
104 if ( $this->is_overloaded ) { |
105 return mb_strlen( $string, 'ascii' ); |
105 return mb_strlen( $string, 'ascii' ); |
106 } else { |
106 } else { |
107 return strlen( $string ); |
107 return strlen( $string ); |
108 } |
108 } |
111 /** |
111 /** |
112 * @param string $string |
112 * @param string $string |
113 * @param int $chunk_size |
113 * @param int $chunk_size |
114 * @return array |
114 * @return array |
115 */ |
115 */ |
116 function str_split( $string, $chunk_size ) { |
116 public function str_split( $string, $chunk_size ) { |
117 if ( ! function_exists( 'str_split' ) ) { |
117 if ( ! function_exists( 'str_split' ) ) { |
118 $length = $this->strlen( $string ); |
118 $length = $this->strlen( $string ); |
119 $out = array(); |
119 $out = array(); |
120 for ( $i = 0; $i < $length; $i += $chunk_size ) { |
120 for ( $i = 0; $i < $length; $i += $chunk_size ) { |
121 $out[] = $this->substr( $string, $i, $chunk_size ); |
121 $out[] = $this->substr( $string, $i, $chunk_size ); |
153 class POMO_FileReader extends POMO_Reader { |
153 class POMO_FileReader extends POMO_Reader { |
154 |
154 |
155 /** |
155 /** |
156 * @param string $filename |
156 * @param string $filename |
157 */ |
157 */ |
158 function __construct( $filename ) { |
158 public function __construct( $filename ) { |
159 parent::__construct(); |
159 parent::__construct(); |
160 $this->_f = fopen( $filename, 'rb' ); |
160 $this->_f = fopen( $filename, 'rb' ); |
161 } |
161 } |
162 |
162 |
163 /** |
163 /** |
174 |
174 |
175 /** |
175 /** |
176 * @param int $bytes |
176 * @param int $bytes |
177 * @return string|false Returns read string, otherwise false. |
177 * @return string|false Returns read string, otherwise false. |
178 */ |
178 */ |
179 function read( $bytes ) { |
179 public function read( $bytes ) { |
180 return fread( $this->_f, $bytes ); |
180 return fread( $this->_f, $bytes ); |
181 } |
181 } |
182 |
182 |
183 /** |
183 /** |
184 * @param int $pos |
184 * @param int $pos |
185 * @return bool |
185 * @return bool |
186 */ |
186 */ |
187 function seekto( $pos ) { |
187 public function seekto( $pos ) { |
188 if ( -1 == fseek( $this->_f, $pos, SEEK_SET ) ) { |
188 if ( -1 == fseek( $this->_f, $pos, SEEK_SET ) ) { |
189 return false; |
189 return false; |
190 } |
190 } |
191 $this->_pos = $pos; |
191 $this->_pos = $pos; |
192 return true; |
192 return true; |
193 } |
193 } |
194 |
194 |
195 /** |
195 /** |
196 * @return bool |
196 * @return bool |
197 */ |
197 */ |
198 function is_resource() { |
198 public function is_resource() { |
199 return is_resource( $this->_f ); |
199 return is_resource( $this->_f ); |
200 } |
200 } |
201 |
201 |
202 /** |
202 /** |
203 * @return bool |
203 * @return bool |
204 */ |
204 */ |
205 function feof() { |
205 public function feof() { |
206 return feof( $this->_f ); |
206 return feof( $this->_f ); |
207 } |
207 } |
208 |
208 |
209 /** |
209 /** |
210 * @return bool |
210 * @return bool |
211 */ |
211 */ |
212 function close() { |
212 public function close() { |
213 return fclose( $this->_f ); |
213 return fclose( $this->_f ); |
214 } |
214 } |
215 |
215 |
216 /** |
216 /** |
217 * @return string |
217 * @return string |
218 */ |
218 */ |
219 function read_all() { |
219 public function read_all() { |
220 $all = ''; |
220 return stream_get_contents( $this->_f ); |
221 while ( ! $this->feof() ) { |
|
222 $all .= $this->read( 4096 ); |
|
223 } |
|
224 return $all; |
|
225 } |
221 } |
226 } |
222 } |
227 endif; |
223 endif; |
228 |
224 |
229 if ( ! class_exists( 'POMO_StringReader', false ) ) : |
225 if ( ! class_exists( 'POMO_StringReader', false ) ) : |
258 |
254 |
259 /** |
255 /** |
260 * @param string $bytes |
256 * @param string $bytes |
261 * @return string |
257 * @return string |
262 */ |
258 */ |
263 function read( $bytes ) { |
259 public function read( $bytes ) { |
264 $data = $this->substr( $this->_str, $this->_pos, $bytes ); |
260 $data = $this->substr( $this->_str, $this->_pos, $bytes ); |
265 $this->_pos += $bytes; |
261 $this->_pos += $bytes; |
266 if ( $this->strlen( $this->_str ) < $this->_pos ) { |
262 if ( $this->strlen( $this->_str ) < $this->_pos ) { |
267 $this->_pos = $this->strlen( $this->_str ); |
263 $this->_pos = $this->strlen( $this->_str ); |
268 } |
264 } |
271 |
267 |
272 /** |
268 /** |
273 * @param int $pos |
269 * @param int $pos |
274 * @return int |
270 * @return int |
275 */ |
271 */ |
276 function seekto( $pos ) { |
272 public function seekto( $pos ) { |
277 $this->_pos = $pos; |
273 $this->_pos = $pos; |
278 if ( $this->strlen( $this->_str ) < $this->_pos ) { |
274 if ( $this->strlen( $this->_str ) < $this->_pos ) { |
279 $this->_pos = $this->strlen( $this->_str ); |
275 $this->_pos = $this->strlen( $this->_str ); |
280 } |
276 } |
281 return $this->_pos; |
277 return $this->_pos; |
282 } |
278 } |
283 |
279 |
284 /** |
280 /** |
285 * @return int |
281 * @return int |
286 */ |
282 */ |
287 function length() { |
283 public function length() { |
288 return $this->strlen( $this->_str ); |
284 return $this->strlen( $this->_str ); |
289 } |
285 } |
290 |
286 |
291 /** |
287 /** |
292 * @return string |
288 * @return string |
293 */ |
289 */ |
294 function read_all() { |
290 public function read_all() { |
295 return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) ); |
291 return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) ); |
296 } |
292 } |
297 |
293 |
298 } |
294 } |
299 endif; |
295 endif; |
304 */ |
300 */ |
305 class POMO_CachedFileReader extends POMO_StringReader { |
301 class POMO_CachedFileReader extends POMO_StringReader { |
306 /** |
302 /** |
307 * PHP5 constructor. |
303 * PHP5 constructor. |
308 */ |
304 */ |
309 function __construct( $filename ) { |
305 public function __construct( $filename ) { |
310 parent::__construct(); |
306 parent::__construct(); |
311 $this->_str = file_get_contents( $filename ); |
307 $this->_str = file_get_contents( $filename ); |
312 if ( false === $this->_str ) { |
308 if ( false === $this->_str ) { |
313 return false; |
309 return false; |
314 } |
310 } |
346 * |
342 * |
347 * @deprecated 5.4.0 Use __construct() instead. |
343 * @deprecated 5.4.0 Use __construct() instead. |
348 * |
344 * |
349 * @see POMO_CachedIntFileReader::__construct() |
345 * @see POMO_CachedIntFileReader::__construct() |
350 */ |
346 */ |
351 function POMO_CachedIntFileReader( $filename ) { |
347 public function POMO_CachedIntFileReader( $filename ) { |
352 _deprecated_constructor( self::class, '5.4.0', static::class ); |
348 _deprecated_constructor( self::class, '5.4.0', static::class ); |
353 self::__construct( $filename ); |
349 self::__construct( $filename ); |
354 } |
350 } |
355 } |
351 } |
356 endif; |
352 endif; |