home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-includes / pomo / streams.php < prev    next >
Encoding:
PHP Script  |  2015-11-19  |  5.8 KB  |  316 lines

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