[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 1157 2015-11-20 04:30:11Z dd32 $ 7 * @package pomo 8 * @subpackage streams 9 */ 10 11 if ( ! class_exists( 'POMO_Reader', false ) ) : 12 class POMO_Reader { 13 14 public $endian = 'little'; 15 public $_post = ''; 16 17 /** 18 * PHP5 constructor. 19 */ 20 function __construct() { 21 $this->is_overloaded = ( ( ini_get( 'mbstring.func_overload' ) & 2 ) != 0 ) && function_exists( 'mb_substr' ); // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated 22 $this->_pos = 0; 23 } 24 25 /** 26 * PHP4 constructor. 27 * 28 * @deprecated 5.4.0 Use __construct() instead. 29 * 30 * @see POMO_Reader::__construct() 31 */ 32 public function POMO_Reader() { 33 _deprecated_constructor( self::class, '5.4.0', static::class ); 34 self::__construct(); 35 } 36 37 /** 38 * Sets the endianness of the file. 39 * 40 * @param string $endian Set the endianness of the file. Accepts 'big', or 'little'. 41 */ 42 function setEndian( $endian ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid 43 $this->endian = $endian; 44 } 45 46 /** 47 * Reads a 32bit Integer from the Stream 48 * 49 * @return mixed The integer, corresponding to the next 32 bits from 50 * the stream of false if there are not enough bytes or on error 51 */ 52 function readint32() { 53 $bytes = $this->read( 4 ); 54 if ( 4 != $this->strlen( $bytes ) ) { 55 return false; 56 } 57 $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V'; 58 $int = unpack( $endian_letter, $bytes ); 59 return reset( $int ); 60 } 61 62 /** 63 * Reads an array of 32-bit Integers from the Stream 64 * 65 * @param int $count How many elements should be read 66 * @return mixed Array of integers or false if there isn't 67 * enough data or on error 68 */ 69 function readint32array( $count ) { 70 $bytes = $this->read( 4 * $count ); 71 if ( 4 * $count != $this->strlen( $bytes ) ) { 72 return false; 73 } 74 $endian_letter = ( 'big' === $this->endian ) ? 'N' : 'V'; 75 return unpack( $endian_letter . $count, $bytes ); 76 } 77 78 /** 79 * @param string $string 80 * @param int $start 81 * @param int $length 82 * @return string 83 */ 84 function substr( $string, $start, $length ) { 85 if ( $this->is_overloaded ) { 86 return mb_substr( $string, $start, $length, 'ascii' ); 87 } else { 88 return substr( $string, $start, $length ); 89 } 90 } 91 92 /** 93 * @param string $string 94 * @return int 95 */ 96 function strlen( $string ) { 97 if ( $this->is_overloaded ) { 98 return mb_strlen( $string, 'ascii' ); 99 } else { 100 return strlen( $string ); 101 } 102 } 103 104 /** 105 * @param string $string 106 * @param int $chunk_size 107 * @return array 108 */ 109 function str_split( $string, $chunk_size ) { 110 if ( ! function_exists( 'str_split' ) ) { 111 $length = $this->strlen( $string ); 112 $out = array(); 113 for ( $i = 0; $i < $length; $i += $chunk_size ) { 114 $out[] = $this->substr( $string, $i, $chunk_size ); 115 } 116 return $out; 117 } else { 118 return str_split( $string, $chunk_size ); 119 } 120 } 121 122 /** 123 * @return int 124 */ 125 function pos() { 126 return $this->_pos; 127 } 128 129 /** 130 * @return true 131 */ 132 function is_resource() { 133 return true; 134 } 135 136 /** 137 * @return true 138 */ 139 function close() { 140 return true; 141 } 142 } 143 endif; 144 145 if ( ! class_exists( 'POMO_FileReader', false ) ) : 146 class POMO_FileReader extends POMO_Reader { 147 148 /** 149 * @param string $filename 150 */ 151 function __construct( $filename ) { 152 parent::__construct(); 153 $this->_f = fopen( $filename, 'rb' ); 154 } 155 156 /** 157 * PHP4 constructor. 158 * 159 * @deprecated 5.4.0 Use __construct() instead. 160 * 161 * @see POMO_FileReader::__construct() 162 */ 163 public function POMO_FileReader( $filename ) { 164 _deprecated_constructor( self::class, '5.4.0', static::class ); 165 self::__construct( $filename ); 166 } 167 168 /** 169 * @param int $bytes 170 * @return string|false Returns read string, otherwise false. 171 */ 172 function read( $bytes ) { 173 return fread( $this->_f, $bytes ); 174 } 175 176 /** 177 * @param int $pos 178 * @return bool 179 */ 180 function seekto( $pos ) { 181 if ( -1 == fseek( $this->_f, $pos, SEEK_SET ) ) { 182 return false; 183 } 184 $this->_pos = $pos; 185 return true; 186 } 187 188 /** 189 * @return bool 190 */ 191 function is_resource() { 192 return is_resource( $this->_f ); 193 } 194 195 /** 196 * @return bool 197 */ 198 function feof() { 199 return feof( $this->_f ); 200 } 201 202 /** 203 * @return bool 204 */ 205 function close() { 206 return fclose( $this->_f ); 207 } 208 209 /** 210 * @return string 211 */ 212 function read_all() { 213 $all = ''; 214 while ( ! $this->feof() ) { 215 $all .= $this->read( 4096 ); 216 } 217 return $all; 218 } 219 } 220 endif; 221 222 if ( ! class_exists( 'POMO_StringReader', false ) ) : 223 /** 224 * Provides file-like methods for manipulating a string instead 225 * of a physical file. 226 */ 227 class POMO_StringReader extends POMO_Reader { 228 229 public $_str = ''; 230 231 /** 232 * PHP5 constructor. 233 */ 234 function __construct( $str = '' ) { 235 parent::__construct(); 236 $this->_str = $str; 237 $this->_pos = 0; 238 } 239 240 /** 241 * PHP4 constructor. 242 * 243 * @deprecated 5.4.0 Use __construct() instead. 244 * 245 * @see POMO_StringReader::__construct() 246 */ 247 public function POMO_StringReader( $str = '' ) { 248 _deprecated_constructor( self::class, '5.4.0', static::class ); 249 self::__construct( $str ); 250 } 251 252 /** 253 * @param string $bytes 254 * @return string 255 */ 256 function read( $bytes ) { 257 $data = $this->substr( $this->_str, $this->_pos, $bytes ); 258 $this->_pos += $bytes; 259 if ( $this->strlen( $this->_str ) < $this->_pos ) { 260 $this->_pos = $this->strlen( $this->_str ); 261 } 262 return $data; 263 } 264 265 /** 266 * @param int $pos 267 * @return int 268 */ 269 function seekto( $pos ) { 270 $this->_pos = $pos; 271 if ( $this->strlen( $this->_str ) < $this->_pos ) { 272 $this->_pos = $this->strlen( $this->_str ); 273 } 274 return $this->_pos; 275 } 276 277 /** 278 * @return int 279 */ 280 function length() { 281 return $this->strlen( $this->_str ); 282 } 283 284 /** 285 * @return string 286 */ 287 function read_all() { 288 return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) ); 289 } 290 291 } 292 endif; 293 294 if ( ! class_exists( 'POMO_CachedFileReader', false ) ) : 295 /** 296 * Reads the contents of the file in the beginning. 297 */ 298 class POMO_CachedFileReader extends POMO_StringReader { 299 /** 300 * PHP5 constructor. 301 */ 302 function __construct( $filename ) { 303 parent::__construct(); 304 $this->_str = file_get_contents( $filename ); 305 if ( false === $this->_str ) { 306 return false; 307 } 308 $this->_pos = 0; 309 } 310 311 /** 312 * PHP4 constructor. 313 * 314 * @deprecated 5.4.0 Use __construct() instead. 315 * 316 * @see POMO_CachedFileReader::__construct() 317 */ 318 public function POMO_CachedFileReader( $filename ) { 319 _deprecated_constructor( self::class, '5.4.0', static::class ); 320 self::__construct( $filename ); 321 } 322 } 323 endif; 324 325 if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) : 326 /** 327 * Reads the contents of the file in the beginning. 328 */ 329 class POMO_CachedIntFileReader extends POMO_CachedFileReader { 330 /** 331 * PHP5 constructor. 332 */ 333 public function __construct( $filename ) { 334 parent::__construct( $filename ); 335 } 336 337 /** 338 * PHP4 constructor. 339 * 340 * @deprecated 5.4.0 Use __construct() instead. 341 * 342 * @see POMO_CachedIntFileReader::__construct() 343 */ 344 function POMO_CachedIntFileReader( $filename ) { 345 _deprecated_constructor( self::class, '5.4.0', static::class ); 346 self::__construct( $filename ); 347 } 348 } 349 endif; 350
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Apr 21 01:00:05 2021 | Cross-referenced by PHPXref 0.7.1 |