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