[ Index ]

PHP Cross Reference of BackPress

title

Body

[close]

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


Generated: Sun Apr 28 01:01:03 2024 Cross-referenced by PHPXref 0.7.1