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