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