3 Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
5 This file is part of PHP-gettext.
7 PHP-gettext is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 PHP-gettext is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with PHP-gettext; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // Simple class to wrap file streams, string streams, etc.
25 // seek is essential, and it should be byte stream
27 // should return a string [FIXME: perhaps return array of bytes?]
28 function read($bytes) {
32 // should return new position
33 function seekto($position) {
37 // returns current position
38 function currentpos() {
42 // returns length of entire stream (limit for seekto()s)
52 function StringReader($str='') {
57 function read($bytes) {
58 $data = substr($this->_str, $this->_pos, $bytes);
59 $this->_pos += $bytes;
60 if (strlen($this->_str)<$this->_pos)
61 $this->_pos = strlen($this->_str);
66 function seekto($pos) {
68 if (strlen($this->_str)<$this->_pos)
69 $this->_pos = strlen($this->_str);
73 function currentpos() {
78 return strlen($this->_str);
89 function FileReader($filename) {
90 if (file_exists($filename)) {
92 $this->_length=filesize($filename);
94 $this->_fd = fopen($filename,'rb');
96 $this->error = 3; // Cannot read file, probably permissions
100 $this->error = 2; // File doesn't exist
105 function read($bytes) {
107 fseek($this->_fd, $this->_pos);
109 // PHP 5.1.1 does not read more than 8192 bytes in one fread()
110 // the discussions at PHP Bugs suggest it's the intended behaviour
113 $chunk = fread($this->_fd, $bytes);
115 $bytes -= strlen($chunk);
117 $this->_pos = ftell($this->_fd);
123 function seekto($pos) {
124 fseek($this->_fd, $pos);
125 $this->_pos = ftell($this->_fd);
129 function currentpos() {
134 return $this->_length;
143 // Preloads entire file in memory first, then creates a StringReader
144 // over it (it assumes knowledge of StringReader internals)
145 class CachedFileReader extends StringReader {
146 function CachedFileReader($filename) {
147 if (file_exists($filename)) {
149 $length=filesize($filename);
150 $fd = fopen($filename,'rb');
153 $this->error = 3; // Cannot read file, probably permissions
156 $this->_str = fread($fd, $length);
160 $this->error = 2; // File doesn't exist