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