source: trunk/www.guidonia.net/wp/wp-includes/streams.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 4.3 KB
Line 
1<?php
2/**
3 * PHP-Gettext External Library: StreamReader classes
4 *
5 * @package External
6 * @subpackage PHP-gettext
7 *
8 * @internal
9 Copyright (c) 2003, 2005 Danilo Segan <danilo@kvota.net>.
10
11 This file is part of PHP-gettext.
12
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.
17
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.
22
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
26
27 */
28
29
30// Simple class to wrap file streams, string streams, etc.
31// seek is essential, and it should be byte stream
32class StreamReader {
33 // should return a string [FIXME: perhaps return array of bytes?]
34 function read($bytes) {
35 return false;
36 }
37
38 // should return new position
39 function seekto($position) {
40 return false;
41 }
42
43 // returns current position
44 function currentpos() {
45 return false;
46 }
47
48 // returns length of entire stream (limit for seekto()s)
49 function length() {
50 return false;
51 }
52}
53
54class StringReader {
55 var $_pos;
56 var $_str;
57
58 function StringReader($str='') {
59 $this->_str = $str;
60 $this->_pos = 0;
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');
63 }
64
65 function _substr($string, $start, $length) {
66 if ($this->is_overloaded) {
67 return mb_substr($string,$start,$length,'ascii');
68 } else {
69 return substr($string,$start,$length);
70 }
71 }
72
73 function _strlen($string) {
74 if ($this->is_overloaded) {
75 return mb_strlen($string,'ascii');
76 } else {
77 return strlen($string);
78 }
79 }
80
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);
86
87 return $data;
88 }
89
90 function seekto($pos) {
91 $this->_pos = $pos;
92 if ($this->_strlen($this->_str)<$this->_pos)
93 $this->_pos = $this->_strlen($this->_str);
94 return $this->_pos;
95 }
96
97 function currentpos() {
98 return $this->_pos;
99 }
100
101 function length() {
102 return $this->_strlen($this->_str);
103 }
104}
105
106
107class FileReader {
108 var $_pos;
109 var $_fd;
110 var $_length;
111
112 function FileReader($filename) {
113 if (file_exists($filename)) {
114
115 $this->_length=filesize($filename);
116 $this->_pos = 0;
117 $this->_fd = fopen($filename,'rb');
118 if (!$this->_fd) {
119 $this->error = 3; // Cannot read file, probably permissions
120 return false;
121 }
122 } else {
123 $this->error = 2; // File doesn't exist
124 return false;
125 }
126 }
127
128 function read($bytes) {
129 if ($bytes) {
130 fseek($this->_fd, $this->_pos);
131
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
134 while ($bytes > 0) {
135 $chunk = fread($this->_fd, $bytes);
136 $data .= $chunk;
137 $bytes -= strlen($chunk);
138 }
139 $this->_pos = ftell($this->_fd);
140
141 return $data;
142 } else return '';
143 }
144
145 function seekto($pos) {
146 fseek($this->_fd, $pos);
147 $this->_pos = ftell($this->_fd);
148 return $this->_pos;
149 }
150
151 function currentpos() {
152 return $this->_pos;
153 }
154
155 function length() {
156 return $this->_length;
157 }
158
159 function close() {
160 fclose($this->_fd);
161 }
162
163}
164
165// Preloads entire file in memory first, then creates a StringReader
166// over it (it assumes knowledge of StringReader internals)
167class CachedFileReader extends StringReader {
168 function CachedFileReader($filename) {
169 parent::StringReader();
170
171 if (file_exists($filename)) {
172
173 $length=filesize($filename);
174 $fd = fopen($filename,'rb');
175
176 if (!$fd) {
177 $this->error = 3; // Cannot read file, probably permissions
178 return false;
179 }
180 $this->_str = fread($fd, $length);
181 fclose($fd);
182
183 } else {
184 $this->error = 2; // File doesn't exist
185 return false;
186 }
187 }
188}
189
190
191?>
Note: See TracBrowser for help on using the repository browser.