web/wp-includes/pomo/streams.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Classes, which help reading streams of data from files.
     3  * Classes, which help reading streams of data from files.
     4  * Based on the classes from Danilo Segan <danilo@kvota.net>
     4  * Based on the classes from Danilo Segan <danilo@kvota.net>
     5  *
     5  *
     6  * @version $Id: streams.php 597 2011-01-16 20:14:36Z nbachiyski $
     6  * @version $Id: streams.php 718 2012-10-31 00:32:02Z nbachiyski $
     7  * @package pomo
     7  * @package pomo
     8  * @subpackage streams
     8  * @subpackage streams
     9  */
     9  */
    10 
    10 
    11 if ( !class_exists( 'POMO_Reader' ) ):
    11 if ( !class_exists( 'POMO_Reader' ) ):
    12 class POMO_Reader {
    12 class POMO_Reader {
    13 	
    13 
    14 	var $endian = 'little';
    14 	var $endian = 'little';
    15 	var $_post = '';
    15 	var $_post = '';
    16 	
    16 
    17 	function POMO_Reader() {
    17 	function POMO_Reader() {
    18 		$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
    18 		$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
    19 		$this->_pos = 0;
    19 		$this->_pos = 0;
    20 	}
    20 	}
    21 	
    21 
    22 	/**
    22 	/**
    23 	 * Sets the endianness of the file.
    23 	 * Sets the endianness of the file.
    24 	 *
    24 	 *
    25 	 * @param $endian string 'big' or 'little'
    25 	 * @param $endian string 'big' or 'little'
    26 	 */
    26 	 */
    55 		if (4*$count != $this->strlen($bytes))
    55 		if (4*$count != $this->strlen($bytes))
    56 			return false;
    56 			return false;
    57 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
    57 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
    58 		return unpack($endian_letter.$count, $bytes);
    58 		return unpack($endian_letter.$count, $bytes);
    59 	}
    59 	}
    60 	
    60 
    61 	
    61 
    62 	function substr($string, $start, $length) {
    62 	function substr($string, $start, $length) {
    63 		if ($this->is_overloaded) {
    63 		if ($this->is_overloaded) {
    64 			return mb_substr($string, $start, $length, 'ascii');
    64 			return mb_substr($string, $start, $length, 'ascii');
    65 		} else {
    65 		} else {
    66 			return substr($string, $start, $length);
    66 			return substr($string, $start, $length);
    67 		}
    67 		}
    68 	}
    68 	}
    69 	
    69 
    70 	function strlen($string) {
    70 	function strlen($string) {
    71 		if ($this->is_overloaded) {
    71 		if ($this->is_overloaded) {
    72 			return mb_strlen($string, 'ascii');
    72 			return mb_strlen($string, 'ascii');
    73 		} else {
    73 		} else {
    74 			return strlen($string);
    74 			return strlen($string);
    75 		}
    75 		}
    76 	}
    76 	}
    77 	
    77 
    78 	function str_split($string, $chunk_size) {
    78 	function str_split($string, $chunk_size) {
    79 		if (!function_exists('str_split')) {
    79 		if (!function_exists('str_split')) {
    80 			$length = $this->strlen($string);
    80 			$length = $this->strlen($string);
    81 			$out = array();
    81 			$out = array();
    82 			for ($i = 0; $i < $length; $i += $chunk_size)
    82 			for ($i = 0; $i < $length; $i += $chunk_size)
    84 			return $out;
    84 			return $out;
    85 		} else {
    85 		} else {
    86 			return str_split( $string, $chunk_size );
    86 			return str_split( $string, $chunk_size );
    87 		}
    87 		}
    88 	}
    88 	}
    89 	
    89 
    90 		
    90 
    91 	function pos() {
    91 	function pos() {
    92 		return $this->_pos;
    92 		return $this->_pos;
    93 	}
    93 	}
    94 
    94 
    95 	function is_resource() {
    95 	function is_resource() {
    96 		return true;
    96 		return true;
    97 	}
    97 	}
    98 	
    98 
    99 	function close() {
    99 	function close() {
   100 		return true;
   100 		return true;
   101 	}
   101 	}
   102 }
   102 }
   103 endif;
   103 endif;
   106 class POMO_FileReader extends POMO_Reader {
   106 class POMO_FileReader extends POMO_Reader {
   107 	function POMO_FileReader($filename) {
   107 	function POMO_FileReader($filename) {
   108 		parent::POMO_Reader();
   108 		parent::POMO_Reader();
   109 		$this->_f = fopen($filename, 'rb');
   109 		$this->_f = fopen($filename, 'rb');
   110 	}
   110 	}
   111 	
   111 
   112 	function read($bytes) {
   112 	function read($bytes) {
   113 		return fread($this->_f, $bytes);
   113 		return fread($this->_f, $bytes);
   114 	}
   114 	}
   115 	
   115 
   116 	function seekto($pos) {
   116 	function seekto($pos) {
   117 		if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
   117 		if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
   118 			return false;
   118 			return false;
   119 		}
   119 		}
   120 		$this->_pos = $pos;
   120 		$this->_pos = $pos;
   121 		return true;
   121 		return true;
   122 	}
   122 	}
   123 	
   123 
   124 	function is_resource() {
   124 	function is_resource() {
   125 		return is_resource($this->_f);
   125 		return is_resource($this->_f);
   126 	}
   126 	}
   127 	
   127 
   128 	function feof() {
   128 	function feof() {
   129 		return feof($this->_f);
   129 		return feof($this->_f);
   130 	}
   130 	}
   131 	
   131 
   132 	function close() {
   132 	function close() {
   133 		return fclose($this->_f);
   133 		return fclose($this->_f);
   134 	}
   134 	}
   135 	
   135 
   136 	function read_all() {
   136 	function read_all() {
   137 		$all = '';
   137 		$all = '';
   138 		while ( !$this->feof() )
   138 		while ( !$this->feof() )
   139 			$all .= $this->read(4096);
   139 			$all .= $this->read(4096);
   140 		return $all;
   140 		return $all;
   146 /**
   146 /**
   147  * Provides file-like methods for manipulating a string instead
   147  * Provides file-like methods for manipulating a string instead
   148  * of a physical file.
   148  * of a physical file.
   149  */
   149  */
   150 class POMO_StringReader extends POMO_Reader {
   150 class POMO_StringReader extends POMO_Reader {
   151 	
   151 
   152 	var $_str = '';
   152 	var $_str = '';
   153 	
   153 
   154 	function POMO_StringReader($str = '') {
   154 	function POMO_StringReader($str = '') {
   155 		parent::POMO_Reader();
   155 		parent::POMO_Reader();
   156 		$this->_str = $str;
   156 		$this->_str = $str;
   157 		$this->_pos = 0;
   157 		$this->_pos = 0;
   158 	}
   158 	}
   176 	}
   176 	}
   177 
   177 
   178 	function read_all() {
   178 	function read_all() {
   179 		return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
   179 		return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
   180 	}
   180 	}
   181 	
   181 
   182 }
   182 }
   183 endif;
   183 endif;
   184 
   184 
   185 if ( !class_exists( 'POMO_CachedFileReader' ) ):
   185 if ( !class_exists( 'POMO_CachedFileReader' ) ):
   186 /**
   186 /**