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