| 1 | <?php |
| 2 | /** |
| 3 | * PHP-Gettext External Library: StreamReader classes |
| 4 | * |
| 5 | * @package External |
| 6 | * @subpackage PHP-gettext |
| 7 | * @version 1.0.7-WordPress.2.8.5 |
| 8 | * |
| 9 | * @internal |
| 10 | Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>. |
| 11 | |
| 12 | This file is part of PHP-gettext. |
| 13 | |
| 14 | PHP-gettext is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License as published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | PHP-gettext is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with PHP-gettext; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | |
| 28 | */ |
| 29 | |
| 30 | |
| 31 | // Simple class to wrap file streams, string streams, etc. |
| 32 | // seek is essential, and it should be byte stream |
| 33 | class StreamReader { |
| 34 | // should return a string [FIXME: perhaps return array of bytes?] |
| 35 | function read($bytes) { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | // should return new position |
| 40 | function seekto($position) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | // returns current position |
| 45 | function currentpos() { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | // returns length of entire stream (limit for seekto()s) |
| 50 | function length() { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | class StringReader { |
| 56 | var $_pos; |
| 57 | var $_str; |
| 58 | |
| 59 | function StringReader($str='') { |
| 60 | $this->_str = $str; |
| 61 | $this->_pos = 0; |
| 62 | // If string functions are overloaded, we need to use the mb versions |
| 63 | $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr'); |
| 64 | } |
| 65 | |
| 66 | function _substr($string, $start, $length) { |
| 67 | if ($this->is_overloaded) { |
| 68 | return mb_substr($string,$start,$length,'ascii'); |
| 69 | } else { |
| 70 | return substr($string,$start,$length); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | function _strlen($string) { |
| 75 | if ($this->is_overloaded) { |
| 76 | return mb_strlen($string,'ascii'); |
| 77 | } else { |
| 78 | return strlen($string); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function read($bytes) { |
| 83 | $data = $this->_substr($this->_str, $this->_pos, $bytes); |
| 84 | $this->_pos += $bytes; |
| 85 | if ($this->_strlen($this->_str)<$this->_pos) |
| 86 | $this->_pos = $this->_strlen($this->_str); |
| 87 | |
| 88 | return $data; |
| 89 | } |
| 90 | |
| 91 | function seekto($pos) { |
| 92 | $this->_pos = $pos; |
| 93 | if ($this->_strlen($this->_str)<$this->_pos) |
| 94 | $this->_pos = $this->_strlen($this->_str); |
| 95 | return $this->_pos; |
| 96 | } |
| 97 | |
| 98 | function currentpos() { |
| 99 | return $this->_pos; |
| 100 | } |
| 101 | |
| 102 | function length() { |
| 103 | return $this->_strlen($this->_str); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | class FileReader { |
| 109 | var $_pos; |
| 110 | var $_fd; |
| 111 | var $_length; |
| 112 | |
| 113 | function FileReader($filename) { |
| 114 | if (file_exists($filename)) { |
| 115 | |
| 116 | $this->_length=filesize($filename); |
| 117 | $this->_pos = 0; |
| 118 | $this->_fd = fopen($filename,'rb'); |
| 119 | if (!$this->_fd) { |
| 120 | $this->error = 3; // Cannot read file, probably permissions |
| 121 | return false; |
| 122 | } |
| 123 | } else { |
| 124 | $this->error = 2; // File doesn't exist |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | function read($bytes) { |
| 130 | if ($bytes) { |
| 131 | fseek($this->_fd, $this->_pos); |
| 132 | |
| 133 | // PHP 5.1.1 does not read more than 8192 bytes in one fread() |
| 134 | // the discussions at PHP Bugs suggest it's the intended behaviour |
| 135 | while ($bytes > 0) { |
| 136 | $chunk = fread($this->_fd, $bytes); |
| 137 | $data .= $chunk; |
| 138 | $bytes -= strlen($chunk); |
| 139 | } |
| 140 | $this->_pos = ftell($this->_fd); |
| 141 | |
| 142 | return $data; |
| 143 | } else return ''; |
| 144 | } |
| 145 | |
| 146 | function seekto($pos) { |
| 147 | fseek($this->_fd, $pos); |
| 148 | $this->_pos = ftell($this->_fd); |
| 149 | return $this->_pos; |
| 150 | } |
| 151 | |
| 152 | function currentpos() { |
| 153 | return $this->_pos; |
| 154 | } |
| 155 | |
| 156 | function length() { |
| 157 | return $this->_length; |
| 158 | } |
| 159 | |
| 160 | function close() { |
| 161 | fclose($this->_fd); |
| 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | // Preloads entire file in memory first, then creates a StringReader |
| 167 | // over it (it assumes knowledge of StringReader internals) |
| 168 | class CachedFileReader extends StringReader { |
| 169 | function CachedFileReader($filename) { |
| 170 | parent::StringReader(); |
| 171 | |
| 172 | if (file_exists($filename)) { |
| 173 | |
| 174 | $length=filesize($filename); |
| 175 | $fd = fopen($filename,'rb'); |
| 176 | |
| 177 | if (!$fd) { |
| 178 | $this->error = 3; // Cannot read file, probably permissions |
| 179 | return false; |
| 180 | } |
| 181 | $this->_str = fread($fd, $length); |
| 182 | fclose($fd); |
| 183 | |
| 184 | } else { |
| 185 | $this->error = 2; // File doesn't exist |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | ?> |
| 193 | |