[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/pomo/ -> streams.php (source)

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


Generated: Sun Oct 24 01:00:03 2021 Cross-referenced by PHPXref 0.7.1