web/wp-includes/pomo/streams.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
     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 138 2009-06-23 13:22:09Z nbachiyski $
     6  * @version $Id: streams.php 293 2009-11-12 15:43:50Z nbachiyski $
     7  * @package pomo
     7  * @package pomo
     8  * @subpackage streams
     8  * @subpackage streams
     9  */
     9  */
    10 
    10 
    11 
    11 if ( !class_exists( 'POMO_Reader' ) ):
    12 /**
    12 class POMO_Reader {
    13  * Provides file-like methods for manipulating a string instead
    13 	
    14  * of a physical file.
    14 	var $endian = 'little';
    15  */
    15 	var $_post = '';
    16 class POMO_StringReader {
    16 	
    17   var $_pos;
    17 	function POMO_Reader() {
    18   var $_str;
    18 		$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
    19 
       
    20 	function POMO_StringReader($str = '') {
       
    21 		$this->_str = $str;
       
    22 		$this->_pos = 0;
    19 		$this->_pos = 0;
    23 		$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
    20 	}
    24 	}
    21 	
    25 
       
    26 	function _substr($string, $start, $length) {
       
    27 		if ($this->is_overloaded) {
       
    28 			return mb_substr($string,$start,$length,'ascii');
       
    29 		} else {
       
    30 			return substr($string,$start,$length);
       
    31 		}
       
    32 	}
       
    33 	
       
    34 	function _strlen($string) {
       
    35 		if ($this->is_overloaded) {
       
    36 			return mb_strlen($string,'ascii');
       
    37 		} else {
       
    38 			return strlen($string);
       
    39 		}
       
    40 	}
       
    41 
       
    42 	function read($bytes) {
       
    43 		$data = $this->_substr($this->_str, $this->_pos, $bytes);
       
    44 		$this->_pos += $bytes;
       
    45 		if ($this->_strlen($this->_str) < $this->_pos) $this->_pos = $this->_strlen($this->_str);
       
    46 		return $data;
       
    47 	}
       
    48 
       
    49 	function seekto($pos) {
       
    50 		$this->_pos = $pos;
       
    51 		if ($this->_strlen($this->_str) < $this->_pos) $this->_pos = $this->_strlen($this->_str);
       
    52 		return $this->_pos;
       
    53 	}
       
    54 
       
    55 	function pos() {
       
    56 		return $this->_pos;
       
    57 	}
       
    58 
       
    59 	function length() {
       
    60 		return $this->_strlen($this->_str);
       
    61 	}
       
    62 
       
    63 }
       
    64 
       
    65 /**
       
    66  * Reads the contents of the file in the beginning.
       
    67  */
       
    68 class POMO_CachedFileReader extends POMO_StringReader {
       
    69 	function POMO_CachedFileReader($filename) {
       
    70 		parent::POMO_StringReader();
       
    71 		$this->_str = file_get_contents($filename);
       
    72 		if (false === $this->_str)
       
    73 			return false;
       
    74 		$this->_pos = 0;
       
    75 	}
       
    76 }
       
    77 
       
    78 /**
       
    79  * Allows reading integers from a file.
       
    80  */
       
    81 class POMO_CachedIntFileReader extends POMO_CachedFileReader {
       
    82 
       
    83 	var $endian = 'little';
       
    84 
       
    85 	/**
       
    86 	 * Opens a file and caches it.
       
    87 	 *
       
    88 	 * @param $filename string name of the file to be opened
       
    89 	 * @param $endian string endianness of the words in the file, allowed
       
    90 	 * 	values are 'little' or 'big'. Default value is 'little'
       
    91 	 */
       
    92 	function POMO_CachedIntFileReader($filename, $endian = 'little') {
       
    93 		$this->endian = $endian;
       
    94 		parent::POMO_CachedFileReader($filename);
       
    95 	}
       
    96 
       
    97 	/**
    22 	/**
    98 	 * Sets the endianness of the file.
    23 	 * Sets the endianness of the file.
    99 	 *
    24 	 *
   100 	 * @param $endian string 'big' or 'little'
    25 	 * @param $endian string 'big' or 'little'
   101 	 */
    26 	 */
   109 	 * @return mixed The integer, corresponding to the next 32 bits from
    34 	 * @return mixed The integer, corresponding to the next 32 bits from
   110 	 * 	the stream of false if there are not enough bytes or on error
    35 	 * 	the stream of false if there are not enough bytes or on error
   111 	 */
    36 	 */
   112 	function readint32() {
    37 	function readint32() {
   113 		$bytes = $this->read(4);
    38 		$bytes = $this->read(4);
   114 		if (4 != $this->_strlen($bytes))
    39 		if (4 != $this->strlen($bytes))
   115 			return false;
    40 			return false;
   116 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
    41 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
   117 		$int = unpack($endian_letter, $bytes);
    42 		$int = unpack($endian_letter, $bytes);
   118 		return array_shift($int);
    43 		return array_shift($int);
   119 	}
    44 	}
   125 	 * @return mixed Array of integers or false if there isn't
    50 	 * @return mixed Array of integers or false if there isn't
   126 	 * 	enough data or on error
    51 	 * 	enough data or on error
   127 	 */
    52 	 */
   128 	function readint32array($count) {
    53 	function readint32array($count) {
   129 		$bytes = $this->read(4 * $count);
    54 		$bytes = $this->read(4 * $count);
   130 		if (4*$count != $this->_strlen($bytes))
    55 		if (4*$count != $this->strlen($bytes))
   131 			return false;
    56 			return false;
   132 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
    57 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
   133 		return unpack($endian_letter.$count, $bytes);
    58 		return unpack($endian_letter.$count, $bytes);
   134 	}
    59 	}
   135 }
    60 	
   136 
    61 	
   137 ?>
    62 	function substr($string, $start, $length) {
       
    63 		if ($this->is_overloaded) {
       
    64 			return mb_substr($string, $start, $length, 'ascii');
       
    65 		} else {
       
    66 			return substr($string, $start, $length);
       
    67 		}
       
    68 	}
       
    69 	
       
    70 	function strlen($string) {
       
    71 		if ($this->is_overloaded) {
       
    72 			return mb_strlen($string, 'ascii');
       
    73 		} else {
       
    74 			return strlen($string);
       
    75 		}
       
    76 	}
       
    77 	
       
    78 	function str_split($string, $chunk_size) {
       
    79 		if (!function_exists('str_split')) {
       
    80 			$length = $this->strlen($string);
       
    81 			$out = array();
       
    82 			for ($i = 0; $i < $length; $i += $chunk_size)
       
    83 				$out[] = $this->substr($string, $i, $chunk_size);
       
    84 			return $out;
       
    85 		} else {
       
    86 			return str_split( $string, $chunk_size );
       
    87 		}
       
    88 	}
       
    89 	
       
    90 		
       
    91 	function pos() {
       
    92 		return $this->_pos;
       
    93 	}
       
    94 
       
    95 	function is_resource() {
       
    96 		return true;
       
    97 	}
       
    98 	
       
    99 	function close() {
       
   100 		return true;
       
   101 	}
       
   102 }
       
   103 endif;
       
   104 
       
   105 if ( !class_exists( 'POMO_FileReader' ) ):
       
   106 class POMO_FileReader extends POMO_Reader {
       
   107 	function POMO_FileReader($filename) {
       
   108 		parent::POMO_Reader();
       
   109 		$this->_f = fopen($filename, 'r');
       
   110 	}
       
   111 	
       
   112 	function read($bytes) {
       
   113 		return fread($this->_f, $bytes);
       
   114 	}
       
   115 	
       
   116 	function seekto($pos) {
       
   117 		if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
       
   118 			return false;
       
   119 		}
       
   120 		$this->_pos = $pos;
       
   121 		return true;
       
   122 	}
       
   123 	
       
   124 	function is_resource() {
       
   125 		return is_resource($this->_f);
       
   126 	}
       
   127 	
       
   128 	function feof() {
       
   129 		return feof($this->_f);
       
   130 	}
       
   131 	
       
   132 	function close() {
       
   133 		return fclose($this->_f);
       
   134 	}
       
   135 	
       
   136 	function read_all() {
       
   137 		$all = '';
       
   138 		while ( !$this->feof() )
       
   139 			$all .= $this->read(4096);
       
   140 		return $all;
       
   141 	}
       
   142 }
       
   143 endif;
       
   144 
       
   145 if ( !class_exists( 'POMO_StringReader' ) ):
       
   146 /**
       
   147  * Provides file-like methods for manipulating a string instead
       
   148  * of a physical file.
       
   149  */
       
   150 class POMO_StringReader extends POMO_Reader {
       
   151 	
       
   152 	var $_str = '';
       
   153 	
       
   154 	function POMO_StringReader($str = '') {
       
   155 		parent::POMO_Reader();
       
   156 		$this->_str = $str;
       
   157 		$this->_pos = 0;
       
   158 	}
       
   159 
       
   160 
       
   161 	function read($bytes) {
       
   162 		$data = $this->substr($this->_str, $this->_pos, $bytes);
       
   163 		$this->_pos += $bytes;
       
   164 		if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
       
   165 		return $data;
       
   166 	}
       
   167 
       
   168 	function seekto($pos) {
       
   169 		$this->_pos = $pos;
       
   170 		if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
       
   171 		return $this->_pos;
       
   172 	}
       
   173 
       
   174 	function length() {
       
   175 		return $this->strlen($this->_str);
       
   176 	}
       
   177 
       
   178 	function read_all() {
       
   179 		return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
       
   180 	}
       
   181 	
       
   182 }
       
   183 endif;
       
   184 
       
   185 if ( !class_exists( 'POMO_CachedFileReader' ) ):
       
   186 /**
       
   187  * Reads the contents of the file in the beginning.
       
   188  */
       
   189 class POMO_CachedFileReader extends POMO_StringReader {
       
   190 	function POMO_CachedFileReader($filename) {
       
   191 		parent::POMO_StringReader();
       
   192 		$this->_str = file_get_contents($filename);
       
   193 		if (false === $this->_str)
       
   194 			return false;
       
   195 		$this->_pos = 0;
       
   196 	}
       
   197 }
       
   198 endif;
       
   199 
       
   200 if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
       
   201 /**
       
   202  * Reads the contents of the file in the beginning.
       
   203  */
       
   204 class POMO_CachedIntFileReader extends POMO_CachedFileReader {
       
   205 	function POMO_CachedIntFileReader($filename) {
       
   206 		parent::POMO_CachedFileReader($filename);
       
   207 	}
       
   208 }
       
   209 endif;