]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/streams.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / streams.php
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
32 class 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
54 class StringReader {
55   var $_pos;
56   var $_str;
57
58   function StringReader($str='') {
59     $this->_str = $str;
60     $this->_pos = 0;
61   }
62
63   function read($bytes) {
64     $data = substr($this->_str, $this->_pos, $bytes);
65     $this->_pos += $bytes;
66     if (strlen($this->_str)<$this->_pos)
67       $this->_pos = strlen($this->_str);
68
69     return $data;
70   }
71
72   function seekto($pos) {
73     $this->_pos = $pos;
74     if (strlen($this->_str)<$this->_pos)
75       $this->_pos = strlen($this->_str);
76     return $this->_pos;
77   }
78
79   function currentpos() {
80     return $this->_pos;
81   }
82
83   function length() {
84     return strlen($this->_str);
85   }
86
87 }
88
89
90 class FileReader {
91   var $_pos;
92   var $_fd;
93   var $_length;
94
95   function FileReader($filename) {
96     if (file_exists($filename)) {
97
98       $this->_length=filesize($filename);
99       $this->_pos = 0;
100       $this->_fd = fopen($filename,'rb');
101       if (!$this->_fd) {
102         $this->error = 3; // Cannot read file, probably permissions
103         return false;
104       }
105     } else {
106       $this->error = 2; // File doesn't exist
107       return false;
108     }
109   }
110
111   function read($bytes) {
112     if ($bytes) {
113       fseek($this->_fd, $this->_pos);
114
115       // PHP 5.1.1 does not read more than 8192 bytes in one fread()
116       // the discussions at PHP Bugs suggest it's the intended behaviour
117       while ($bytes > 0) {
118         $chunk  = fread($this->_fd, $bytes);
119         $data  .= $chunk;
120         $bytes -= strlen($chunk);
121       }
122       $this->_pos = ftell($this->_fd);
123
124       return $data;
125     } else return '';
126   }
127
128   function seekto($pos) {
129     fseek($this->_fd, $pos);
130     $this->_pos = ftell($this->_fd);
131     return $this->_pos;
132   }
133
134   function currentpos() {
135     return $this->_pos;
136   }
137
138   function length() {
139     return $this->_length;
140   }
141
142   function close() {
143     fclose($this->_fd);
144   }
145
146 }
147
148 // Preloads entire file in memory first, then creates a StringReader
149 // over it (it assumes knowledge of StringReader internals)
150 class CachedFileReader extends StringReader {
151   function CachedFileReader($filename) {
152     if (file_exists($filename)) {
153
154       $length=filesize($filename);
155       $fd = fopen($filename,'rb');
156
157       if (!$fd) {
158         $this->error = 3; // Cannot read file, probably permissions
159         return false;
160       }
161       $this->_str = fread($fd, $length);
162       fclose($fd);
163
164     } else {
165       $this->error = 2; // File doesn't exist
166       return false;
167     }
168   }
169 }
170
171
172 ?>