3
* Classes, which help reading streams of data from files.
4
* Based on the classes from Danilo Segan <danilo@kvota.net>
6
* @version $Id: streams.php 718 2012-10-31 00:32:02Z nbachiyski $
11
if ( !class_exists( 'POMO_Reader' ) ):
14
var $endian = 'little';
17
function POMO_Reader() {
18
$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
23
* Sets the endianness of the file.
25
* @param $endian string 'big' or 'little'
27
function setEndian($endian) {
28
$this->endian = $endian;
32
* Reads a 32bit Integer from the Stream
34
* @return mixed The integer, corresponding to the next 32 bits from
35
* the stream of false if there are not enough bytes or on error
37
function readint32() {
38
$bytes = $this->read(4);
39
if (4 != $this->strlen($bytes))
41
$endian_letter = ('big' == $this->endian)? 'N' : 'V';
42
$int = unpack($endian_letter, $bytes);
43
return array_shift($int);
47
* Reads an array of 32-bit Integers from the Stream
49
* @param integer count How many elements should be read
50
* @return mixed Array of integers or false if there isn't
51
* enough data or on error
53
function readint32array($count) {
54
$bytes = $this->read(4 * $count);
55
if (4*$count != $this->strlen($bytes))
57
$endian_letter = ('big' == $this->endian)? 'N' : 'V';
58
return unpack($endian_letter.$count, $bytes);
62
function substr($string, $start, $length) {
63
if ($this->is_overloaded) {
64
return mb_substr($string, $start, $length, 'ascii');
66
return substr($string, $start, $length);
70
function strlen($string) {
71
if ($this->is_overloaded) {
72
return mb_strlen($string, 'ascii');
74
return strlen($string);
78
function str_split($string, $chunk_size) {
79
if (!function_exists('str_split')) {
80
$length = $this->strlen($string);
82
for ($i = 0; $i < $length; $i += $chunk_size)
83
$out[] = $this->substr($string, $i, $chunk_size);
86
return str_split( $string, $chunk_size );
95
function is_resource() {
105
if ( !class_exists( 'POMO_FileReader' ) ):
106
class POMO_FileReader extends POMO_Reader {
107
function POMO_FileReader($filename) {
108
parent::POMO_Reader();
109
$this->_f = fopen($filename, 'rb');
112
function read($bytes) {
113
return fread($this->_f, $bytes);
116
function seekto($pos) {
117
if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
124
function is_resource() {
125
return is_resource($this->_f);
129
return feof($this->_f);
133
return fclose($this->_f);
136
function read_all() {
138
while ( !$this->feof() )
139
$all .= $this->read(4096);
145
if ( !class_exists( 'POMO_StringReader' ) ):
147
* Provides file-like methods for manipulating a string instead
148
* of a physical file.
150
class POMO_StringReader extends POMO_Reader {
154
function POMO_StringReader($str = '') {
155
parent::POMO_Reader();
161
function read($bytes) {
162
$data = $this->substr($this->_str, $this->_pos, $bytes);
163
$this->_pos += $bytes;
164
if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
168
function seekto($pos) {
170
if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
175
return $this->strlen($this->_str);
178
function read_all() {
179
return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
185
if ( !class_exists( 'POMO_CachedFileReader' ) ):
187
* Reads the contents of the file in the beginning.
189
class POMO_CachedFileReader extends POMO_StringReader {
190
function POMO_CachedFileReader($filename) {
191
parent::POMO_StringReader();
192
$this->_str = file_get_contents($filename);
193
if (false === $this->_str)
200
if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
202
* Reads the contents of the file in the beginning.
204
class POMO_CachedIntFileReader extends POMO_CachedFileReader {
205
function POMO_CachedIntFileReader($filename) {
206
parent::POMO_CachedFileReader($filename);
b'\\ No newline at end of file'