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