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


Generated: Sun Sep 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1