]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/pomo/streams.php
WordPress 4.1
[autoinstalls/wordpress.git] / wp-includes / pomo / streams.php
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  *
6  * @version $Id: streams.php 718 2012-10-31 00:32:02Z nbachiyski $
7  * @package pomo
8  * @subpackage streams
9  */
10
11 if ( !class_exists( 'POMO_Reader' ) ):
12 class POMO_Reader {
13
14         var $endian = 'little';
15         var $_post = '';
16
17         function POMO_Reader() {
18                 $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
19                 $this->_pos = 0;
20         }
21
22         /**
23          * Sets the endianness of the file.
24          *
25          * @param $endian string 'big' or 'little'
26          */
27         function setEndian($endian) {
28                 $this->endian = $endian;
29         }
30
31         /**
32          * Reads a 32bit Integer from the Stream
33          *
34          * @return mixed The integer, corresponding to the next 32 bits from
35          *      the stream of false if there are not enough bytes or on error
36          */
37         function readint32() {
38                 $bytes = $this->read(4);
39                 if (4 != $this->strlen($bytes))
40                         return false;
41                 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
42                 $int = unpack($endian_letter, $bytes);
43                 return array_shift($int);
44         }
45
46         /**
47          * Reads an array of 32-bit Integers from the Stream
48          *
49          * @param integer count How many elements should be read
50          * @return mixed Array of integers or false if there isn't
51          *      enough data or on error
52          */
53         function readint32array($count) {
54                 $bytes = $this->read(4 * $count);
55                 if (4*$count != $this->strlen($bytes))
56                         return false;
57                 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
58                 return unpack($endian_letter.$count, $bytes);
59         }
60
61         /**
62          * @param string $string
63          * @param int    $start
64          * @param int    $length
65          * @return string
66          */
67         function substr($string, $start, $length) {
68                 if ($this->is_overloaded) {
69                         return mb_substr($string, $start, $length, 'ascii');
70                 } else {
71                         return substr($string, $start, $length);
72                 }
73         }
74
75         /**
76          * @param string $string
77          * @return int
78          */
79         function strlen($string) {
80                 if ($this->is_overloaded) {
81                         return mb_strlen($string, 'ascii');
82                 } else {
83                         return strlen($string);
84                 }
85         }
86
87         /**
88          * @param string $string
89          * @param int    $chunk_size
90          * @return array
91          */
92         function str_split($string, $chunk_size) {
93                 if (!function_exists('str_split')) {
94                         $length = $this->strlen($string);
95                         $out = array();
96                         for ($i = 0; $i < $length; $i += $chunk_size)
97                                 $out[] = $this->substr($string, $i, $chunk_size);
98                         return $out;
99                 } else {
100                         return str_split( $string, $chunk_size );
101                 }
102         }
103
104
105         function pos() {
106                 return $this->_pos;
107         }
108
109         function is_resource() {
110                 return true;
111         }
112
113         function close() {
114                 return true;
115         }
116 }
117 endif;
118
119 if ( !class_exists( 'POMO_FileReader' ) ):
120 class POMO_FileReader extends POMO_Reader {
121
122         /**
123          * @param string $filename
124          */
125         function POMO_FileReader($filename) {
126                 parent::POMO_Reader();
127                 $this->_f = fopen($filename, 'rb');
128         }
129
130         /**
131          * @param int $bytes
132          */
133         function read($bytes) {
134                 return fread($this->_f, $bytes);
135         }
136
137         /**
138          * @param int $pos
139          * @return boolean
140          */
141         function seekto($pos) {
142                 if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
143                         return false;
144                 }
145                 $this->_pos = $pos;
146                 return true;
147         }
148
149         function is_resource() {
150                 return is_resource($this->_f);
151         }
152
153         function feof() {
154                 return feof($this->_f);
155         }
156
157         function close() {
158                 return fclose($this->_f);
159         }
160
161         function read_all() {
162                 $all = '';
163                 while ( !$this->feof() )
164                         $all .= $this->read(4096);
165                 return $all;
166         }
167 }
168 endif;
169
170 if ( !class_exists( 'POMO_StringReader' ) ):
171 /**
172  * Provides file-like methods for manipulating a string instead
173  * of a physical file.
174  */
175 class POMO_StringReader extends POMO_Reader {
176
177         var $_str = '';
178
179         function POMO_StringReader($str = '') {
180                 parent::POMO_Reader();
181                 $this->_str = $str;
182                 $this->_pos = 0;
183         }
184
185         /**
186          * @param string $bytes
187          * @return string
188          */
189         function read($bytes) {
190                 $data = $this->substr($this->_str, $this->_pos, $bytes);
191                 $this->_pos += $bytes;
192                 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
193                 return $data;
194         }
195
196         /**
197          * @param int $pos
198          * @return int
199          */
200         function seekto($pos) {
201                 $this->_pos = $pos;
202                 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
203                 return $this->_pos;
204         }
205
206         function length() {
207                 return $this->strlen($this->_str);
208         }
209
210         function read_all() {
211                 return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
212         }
213
214 }
215 endif;
216
217 if ( !class_exists( 'POMO_CachedFileReader' ) ):
218 /**
219  * Reads the contents of the file in the beginning.
220  */
221 class POMO_CachedFileReader extends POMO_StringReader {
222         function POMO_CachedFileReader($filename) {
223                 parent::POMO_StringReader();
224                 $this->_str = file_get_contents($filename);
225                 if (false === $this->_str)
226                         return false;
227                 $this->_pos = 0;
228         }
229 }
230 endif;
231
232 if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
233 /**
234  * Reads the contents of the file in the beginning.
235  */
236 class POMO_CachedIntFileReader extends POMO_CachedFileReader {
237         function POMO_CachedIntFileReader($filename) {
238                 parent::POMO_CachedFileReader($filename);
239         }
240 }
241 endif;