Make WordPress Core

Ticket #14207: streams.php

File streams.php, 4.7 KB (added by bueltge, 16 years ago)
Line 
1<?php
2/**
3 * Classes, which help reading streams of data from files.
4 * Based on the classes from Danilo Segan <danilo@kvota.net>
5 *
6 * @version $Id: streams.php 406 2010-02-07 11:10:24Z nbachiyski $
7 * @package pomo
8 * @subpackage streams
9 */
10
11if ( !class_exists( 'POMO_Reader' ) ):
12class POMO_Reader {
13
14 var $endian = 'little';
15 var $_post = '';
16
17 function POMO_Reader() {
18 $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
19 $this->_pos = 0;
20 }
21
22 /**
23 * Sets the endianness of the file.
24 *
25 * @param $endian string 'big' or 'little'
26 */
27 function setEndian($endian) {
28 $this->endian = $endian;
29 }
30
31 /**
32 * Reads a 32bit Integer from the Stream
33 *
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
36 */
37 function readint32() {
38 $bytes = $this->read(4);
39 if (4 != $this->strlen($bytes))
40 return false;
41 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
42 $int = unpack($endian_letter, $bytes);
43 return array_shift($int);
44 }
45
46 /**
47 * Reads an array of 32-bit Integers from the Stream
48 *
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
52 */
53 function readint32array($count) {
54 $bytes = $this->read(4 * $count);
55 if (4*$count != $this->strlen($bytes))
56 return false;
57 $endian_letter = ('big' == $this->endian)? 'N' : 'V';
58 return unpack($endian_letter.$count, $bytes);
59 }
60
61
62 function substr($string, $start, $length) {
63 if ($this->is_overloaded) {
64 return mb_substr($string, $start, $length, 'ascii');
65 } else {
66 return substr($string, $start, $length);
67 }
68 }
69
70 function strlen($string) {
71 if ($this->is_overloaded) {
72 return mb_strlen($string, 'ascii');
73 } else {
74 return strlen($string);
75 }
76 }
77
78 function str_split($string, $chunk_size) {
79 if (!function_exists('str_split')) {
80 $length = $this->strlen($string);
81 $out = array();
82 for ($i = 0; $i < $length; $i += $chunk_size)
83 $out[] = $this->substr($string, $i, $chunk_size);
84 return $out;
85 } else {
86 return str_split( $string, $chunk_size );
87 }
88 }
89
90
91 function pos() {
92 return $this->_pos;
93 }
94
95 function is_resource() {
96 return true;
97 }
98
99 function close() {
100 return true;
101 }
102}
103endif;
104
105if ( !class_exists( 'POMO_FileReader' ) ):
106class POMO_FileReader extends POMO_Reader {
107 function POMO_FileReader($filename) {
108 parent::POMO_Reader();
109 $this->_f = fopen($filename, 'r');
110 }
111
112 function read($bytes) {
113 if ( empty($bytes) )
114 return fread($this->_f, $bytes);
115 else
116 return;
117 }
118
119 function seekto($pos) {
120 if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
121 return false;
122 }
123 $this->_pos = $pos;
124 return true;
125 }
126
127 function is_resource() {
128 return is_resource($this->_f);
129 }
130
131 function feof() {
132 return feof($this->_f);
133 }
134
135 function close() {
136 return fclose($this->_f);
137 }
138
139 function read_all() {
140 $all = '';
141 while ( !$this->feof() )
142 $all .= $this->read(4096);
143 return $all;
144 }
145}
146endif;
147
148if ( !class_exists( 'POMO_StringReader' ) ):
149/**
150 * Provides file-like methods for manipulating a string instead
151 * of a physical file.
152 */
153class POMO_StringReader extends POMO_Reader {
154
155 var $_str = '';
156
157 function POMO_StringReader($str = '') {
158 parent::POMO_Reader();
159 $this->_str = $str;
160 $this->_pos = 0;
161 }
162
163
164 function read($bytes) {
165 $data = $this->substr($this->_str, $this->_pos, $bytes);
166 $this->_pos += $bytes;
167 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
168 return $data;
169 }
170
171 function seekto($pos) {
172 $this->_pos = $pos;
173 if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
174 return $this->_pos;
175 }
176
177 function length() {
178 return $this->strlen($this->_str);
179 }
180
181 function read_all() {
182 return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
183 }
184
185}
186endif;
187
188if ( !class_exists( 'POMO_CachedFileReader' ) ):
189/**
190 * Reads the contents of the file in the beginning.
191 */
192class POMO_CachedFileReader extends POMO_StringReader {
193 function POMO_CachedFileReader($filename) {
194 parent::POMO_StringReader();
195 $this->_str = file_get_contents($filename);
196 if (false === $this->_str)
197 return false;
198 $this->_pos = 0;
199 }
200}
201endif;
202
203if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
204/**
205 * Reads the contents of the file in the beginning.
206 */
207class POMO_CachedIntFileReader extends POMO_CachedFileReader {
208 function POMO_CachedIntFileReader($filename) {
209 parent::POMO_CachedFileReader($filename);
210 }
211}
212endif;