3
* PHP-Gettext External Library: StreamReader classes
6
* @subpackage PHP-gettext
9
Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>.
11
This file is part of PHP-gettext.
13
PHP-gettext is free software; you can redistribute it and/or modify
14
it under the terms of the GNU General Public License as published by
15
the Free Software Foundation; either version 2 of the License, or
16
(at your option) any later version.
18
PHP-gettext is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
23
You should have received a copy of the GNU General Public License
24
along with PHP-gettext; if not, write to the Free Software
25
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30
// Simple class to wrap file streams, string streams, etc.
31
// seek is essential, and it should be byte stream
33
// should return a string [FIXME: perhaps return array of bytes?]
34
function read($bytes) {
38
// should return new position
39
function seekto($position) {
43
// returns current position
44
function currentpos() {
48
// returns length of entire stream (limit for seekto()s)
58
function StringReader($str='') {
61
// If string functions are overloaded, we need to use the mb versions
62
$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
65
function _substr($string, $start, $length) {
66
if ($this->is_overloaded) {
67
return mb_substr($string,$start,$length,'ascii');
69
return substr($string,$start,$length);
73
function _strlen($string) {
74
if ($this->is_overloaded) {
75
return mb_strlen($string,'ascii');
77
return strlen($string);
81
function read($bytes) {
82
$data = $this->_substr($this->_str, $this->_pos, $bytes);
83
$this->_pos += $bytes;
84
if ($this->_strlen($this->_str)<$this->_pos)
85
$this->_pos = $this->_strlen($this->_str);
90
function seekto($pos) {
92
if ($this->_strlen($this->_str)<$this->_pos)
93
$this->_pos = $this->_strlen($this->_str);
97
function currentpos() {
102
return $this->_strlen($this->_str);
112
function FileReader($filename) {
113
if (file_exists($filename)) {
115
$this->_length=filesize($filename);
117
$this->_fd = fopen($filename,'rb');
119
$this->error = 3; // Cannot read file, probably permissions
123
$this->error = 2; // File doesn't exist
128
function read($bytes) {
130
fseek($this->_fd, $this->_pos);
132
// PHP 5.1.1 does not read more than 8192 bytes in one fread()
133
// the discussions at PHP Bugs suggest it's the intended behaviour
135
$chunk = fread($this->_fd, $bytes);
137
$bytes -= strlen($chunk);
139
$this->_pos = ftell($this->_fd);
145
function seekto($pos) {
146
fseek($this->_fd, $pos);
147
$this->_pos = ftell($this->_fd);
151
function currentpos() {
156
return $this->_length;
165
// Preloads entire file in memory first, then creates a StringReader
166
// over it (it assumes knowledge of StringReader internals)
167
class CachedFileReader extends StringReader {
168
function CachedFileReader($filename) {
169
parent::StringReader();
171
if (file_exists($filename)) {
173
$length=filesize($filename);
174
$fd = fopen($filename,'rb');
177
$this->error = 3; // Cannot read file, probably permissions
180
$this->_str = fread($fd, $length);
184
$this->error = 2; // File doesn't exist