1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Classes, which help reading streams of data from files. |
3 * Classes, which help reading streams of data from files. |
4 * Based on the classes from Danilo Segan <danilo@kvota.net> |
4 * Based on the classes from Danilo Segan <danilo@kvota.net> |
5 * |
5 * |
6 * @version $Id: streams.php 718 2012-10-31 00:32:02Z nbachiyski $ |
6 * @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $ |
7 * @package pomo |
7 * @package pomo |
8 * @subpackage streams |
8 * @subpackage streams |
9 */ |
9 */ |
10 |
10 |
11 if ( !class_exists( 'POMO_Reader' ) ): |
11 if ( ! class_exists( 'POMO_Reader', false ) ): |
12 class POMO_Reader { |
12 class POMO_Reader { |
13 |
13 |
14 var $endian = 'little'; |
14 var $endian = 'little'; |
15 var $_post = ''; |
15 var $_post = ''; |
16 |
16 |
17 function POMO_Reader() { |
17 /** |
|
18 * PHP5 constructor. |
|
19 */ |
|
20 function __construct() { |
18 $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr'); |
21 $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr'); |
19 $this->_pos = 0; |
22 $this->_pos = 0; |
|
23 } |
|
24 |
|
25 /** |
|
26 * PHP4 constructor. |
|
27 */ |
|
28 public function POMO_Reader() { |
|
29 self::__construct(); |
20 } |
30 } |
21 |
31 |
22 /** |
32 /** |
23 * Sets the endianness of the file. |
33 * Sets the endianness of the file. |
24 * |
34 * |
99 } else { |
109 } else { |
100 return str_split( $string, $chunk_size ); |
110 return str_split( $string, $chunk_size ); |
101 } |
111 } |
102 } |
112 } |
103 |
113 |
104 |
114 /** |
|
115 * @return int |
|
116 */ |
105 function pos() { |
117 function pos() { |
106 return $this->_pos; |
118 return $this->_pos; |
107 } |
119 } |
108 |
120 |
|
121 /** |
|
122 * @return true |
|
123 */ |
109 function is_resource() { |
124 function is_resource() { |
110 return true; |
125 return true; |
111 } |
126 } |
112 |
127 |
|
128 /** |
|
129 * @return true |
|
130 */ |
113 function close() { |
131 function close() { |
114 return true; |
132 return true; |
115 } |
133 } |
116 } |
134 } |
117 endif; |
135 endif; |
118 |
136 |
119 if ( !class_exists( 'POMO_FileReader' ) ): |
137 if ( ! class_exists( 'POMO_FileReader', false ) ): |
120 class POMO_FileReader extends POMO_Reader { |
138 class POMO_FileReader extends POMO_Reader { |
121 |
139 |
122 /** |
140 /** |
123 * @param string $filename |
141 * @param string $filename |
124 */ |
142 */ |
125 function POMO_FileReader($filename) { |
143 function __construct( $filename ) { |
126 parent::POMO_Reader(); |
144 parent::POMO_Reader(); |
127 $this->_f = fopen($filename, 'rb'); |
145 $this->_f = fopen($filename, 'rb'); |
|
146 } |
|
147 |
|
148 /** |
|
149 * PHP4 constructor. |
|
150 */ |
|
151 public function POMO_FileReader( $filename ) { |
|
152 self::__construct( $filename ); |
128 } |
153 } |
129 |
154 |
130 /** |
155 /** |
131 * @param int $bytes |
156 * @param int $bytes |
132 */ |
157 */ |
144 } |
169 } |
145 $this->_pos = $pos; |
170 $this->_pos = $pos; |
146 return true; |
171 return true; |
147 } |
172 } |
148 |
173 |
|
174 /** |
|
175 * @return bool |
|
176 */ |
149 function is_resource() { |
177 function is_resource() { |
150 return is_resource($this->_f); |
178 return is_resource($this->_f); |
151 } |
179 } |
152 |
180 |
|
181 /** |
|
182 * @return bool |
|
183 */ |
153 function feof() { |
184 function feof() { |
154 return feof($this->_f); |
185 return feof($this->_f); |
155 } |
186 } |
156 |
187 |
|
188 /** |
|
189 * @return bool |
|
190 */ |
157 function close() { |
191 function close() { |
158 return fclose($this->_f); |
192 return fclose($this->_f); |
159 } |
193 } |
160 |
194 |
|
195 /** |
|
196 * @return string |
|
197 */ |
161 function read_all() { |
198 function read_all() { |
162 $all = ''; |
199 $all = ''; |
163 while ( !$this->feof() ) |
200 while ( !$this->feof() ) |
164 $all .= $this->read(4096); |
201 $all .= $this->read(4096); |
165 return $all; |
202 return $all; |
166 } |
203 } |
167 } |
204 } |
168 endif; |
205 endif; |
169 |
206 |
170 if ( !class_exists( 'POMO_StringReader' ) ): |
207 if ( ! class_exists( 'POMO_StringReader', false ) ): |
171 /** |
208 /** |
172 * Provides file-like methods for manipulating a string instead |
209 * Provides file-like methods for manipulating a string instead |
173 * of a physical file. |
210 * of a physical file. |
174 */ |
211 */ |
175 class POMO_StringReader extends POMO_Reader { |
212 class POMO_StringReader extends POMO_Reader { |
176 |
213 |
177 var $_str = ''; |
214 var $_str = ''; |
178 |
215 |
179 function POMO_StringReader($str = '') { |
216 /** |
|
217 * PHP5 constructor. |
|
218 */ |
|
219 function __construct( $str = '' ) { |
180 parent::POMO_Reader(); |
220 parent::POMO_Reader(); |
181 $this->_str = $str; |
221 $this->_str = $str; |
182 $this->_pos = 0; |
222 $this->_pos = 0; |
|
223 } |
|
224 |
|
225 /** |
|
226 * PHP4 constructor. |
|
227 */ |
|
228 public function POMO_StringReader( $str = '' ) { |
|
229 self::__construct( $str ); |
183 } |
230 } |
184 |
231 |
185 /** |
232 /** |
186 * @param string $bytes |
233 * @param string $bytes |
187 * @return string |
234 * @return string |
201 $this->_pos = $pos; |
248 $this->_pos = $pos; |
202 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str); |
249 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str); |
203 return $this->_pos; |
250 return $this->_pos; |
204 } |
251 } |
205 |
252 |
|
253 /** |
|
254 * @return int |
|
255 */ |
206 function length() { |
256 function length() { |
207 return $this->strlen($this->_str); |
257 return $this->strlen($this->_str); |
208 } |
258 } |
209 |
259 |
|
260 /** |
|
261 * @return string |
|
262 */ |
210 function read_all() { |
263 function read_all() { |
211 return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str)); |
264 return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str)); |
212 } |
265 } |
213 |
266 |
214 } |
267 } |
215 endif; |
268 endif; |
216 |
269 |
217 if ( !class_exists( 'POMO_CachedFileReader' ) ): |
270 if ( ! class_exists( 'POMO_CachedFileReader', false ) ): |
218 /** |
271 /** |
219 * Reads the contents of the file in the beginning. |
272 * Reads the contents of the file in the beginning. |
220 */ |
273 */ |
221 class POMO_CachedFileReader extends POMO_StringReader { |
274 class POMO_CachedFileReader extends POMO_StringReader { |
222 function POMO_CachedFileReader($filename) { |
275 /** |
|
276 * PHP5 constructor. |
|
277 */ |
|
278 function __construct( $filename ) { |
223 parent::POMO_StringReader(); |
279 parent::POMO_StringReader(); |
224 $this->_str = file_get_contents($filename); |
280 $this->_str = file_get_contents($filename); |
225 if (false === $this->_str) |
281 if (false === $this->_str) |
226 return false; |
282 return false; |
227 $this->_pos = 0; |
283 $this->_pos = 0; |
228 } |
284 } |
229 } |
285 |
230 endif; |
286 /** |
231 |
287 * PHP4 constructor. |
232 if ( !class_exists( 'POMO_CachedIntFileReader' ) ): |
288 */ |
|
289 public function POMO_CachedFileReader( $filename ) { |
|
290 self::__construct( $filename ); |
|
291 } |
|
292 } |
|
293 endif; |
|
294 |
|
295 if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ): |
233 /** |
296 /** |
234 * Reads the contents of the file in the beginning. |
297 * Reads the contents of the file in the beginning. |
235 */ |
298 */ |
236 class POMO_CachedIntFileReader extends POMO_CachedFileReader { |
299 class POMO_CachedIntFileReader extends POMO_CachedFileReader { |
237 function POMO_CachedIntFileReader($filename) { |
300 /** |
|
301 * PHP5 constructor. |
|
302 */ |
|
303 public function __construct( $filename ) { |
238 parent::POMO_CachedFileReader($filename); |
304 parent::POMO_CachedFileReader($filename); |
239 } |
305 } |
240 } |
306 |
241 endif; |
307 /** |
|
308 * PHP4 constructor. |
|
309 */ |
|
310 function POMO_CachedIntFileReader( $filename ) { |
|
311 self::__construct( $filename ); |
|
312 } |
|
313 } |
|
314 endif; |
|
315 |