[ 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              return stream_get_contents( $this->_f );
 221          }
 222      }
 223  endif;
 224  
 225  if ( ! class_exists( 'POMO_StringReader', false ) ) :
 226      /**
 227       * Provides file-like methods for manipulating a string instead
 228       * of a physical file.
 229       */
 230      class POMO_StringReader extends POMO_Reader {
 231  
 232          public $_str = '';
 233  
 234          /**
 235           * PHP5 constructor.
 236           */
 237  		public function __construct( $str = '' ) {
 238              parent::__construct();
 239              $this->_str = $str;
 240              $this->_pos = 0;
 241          }
 242  
 243          /**
 244           * PHP4 constructor.
 245           *
 246           * @deprecated 5.4.0 Use __construct() instead.
 247           *
 248           * @see POMO_StringReader::__construct()
 249           */
 250  		public function POMO_StringReader( $str = '' ) {
 251              _deprecated_constructor( self::class, '5.4.0', static::class );
 252              self::__construct( $str );
 253          }
 254  
 255          /**
 256           * @param string $bytes
 257           * @return string
 258           */
 259  		public function read( $bytes ) {
 260              $data        = $this->substr( $this->_str, $this->_pos, $bytes );
 261              $this->_pos += $bytes;
 262              if ( $this->strlen( $this->_str ) < $this->_pos ) {
 263                  $this->_pos = $this->strlen( $this->_str );
 264              }
 265              return $data;
 266          }
 267  
 268          /**
 269           * @param int $pos
 270           * @return int
 271           */
 272  		public function seekto( $pos ) {
 273              $this->_pos = $pos;
 274              if ( $this->strlen( $this->_str ) < $this->_pos ) {
 275                  $this->_pos = $this->strlen( $this->_str );
 276              }
 277              return $this->_pos;
 278          }
 279  
 280          /**
 281           * @return int
 282           */
 283  		public function length() {
 284              return $this->strlen( $this->_str );
 285          }
 286  
 287          /**
 288           * @return string
 289           */
 290  		public function read_all() {
 291              return $this->substr( $this->_str, $this->_pos, $this->strlen( $this->_str ) );
 292          }
 293  
 294      }
 295  endif;
 296  
 297  if ( ! class_exists( 'POMO_CachedFileReader', false ) ) :
 298      /**
 299       * Reads the contents of the file in the beginning.
 300       */
 301      class POMO_CachedFileReader extends POMO_StringReader {
 302          /**
 303           * PHP5 constructor.
 304           */
 305  		public function __construct( $filename ) {
 306              parent::__construct();
 307              $this->_str = file_get_contents( $filename );
 308              if ( false === $this->_str ) {
 309                  return false;
 310              }
 311              $this->_pos = 0;
 312          }
 313  
 314          /**
 315           * PHP4 constructor.
 316           *
 317           * @deprecated 5.4.0 Use __construct() instead.
 318           *
 319           * @see POMO_CachedFileReader::__construct()
 320           */
 321  		public function POMO_CachedFileReader( $filename ) {
 322              _deprecated_constructor( self::class, '5.4.0', static::class );
 323              self::__construct( $filename );
 324          }
 325      }
 326  endif;
 327  
 328  if ( ! class_exists( 'POMO_CachedIntFileReader', false ) ) :
 329      /**
 330       * Reads the contents of the file in the beginning.
 331       */
 332      class POMO_CachedIntFileReader extends POMO_CachedFileReader {
 333          /**
 334           * PHP5 constructor.
 335           */
 336  		public function __construct( $filename ) {
 337              parent::__construct( $filename );
 338          }
 339  
 340          /**
 341           * PHP4 constructor.
 342           *
 343           * @deprecated 5.4.0 Use __construct() instead.
 344           *
 345           * @see POMO_CachedIntFileReader::__construct()
 346           */
 347  		public function POMO_CachedIntFileReader( $filename ) {
 348              _deprecated_constructor( self::class, '5.4.0', static::class );
 349              self::__construct( $filename );
 350          }
 351      }
 352  endif;
 353  


Generated: Sat Nov 23 01:00:02 2024 Cross-referenced by PHPXref 0.7.1