wp/wp-includes/pomo/streams.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
     6  * @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $
     6  * @version $Id: streams.php 1157 2015-11-20 04:30:11Z dd32 $
     7  * @package pomo
     7  * @package pomo
     8  * @subpackage streams
     8  * @subpackage streams
     9  */
     9  */
    10 
    10 
    11 if ( ! class_exists( 'POMO_Reader', false ) ):
    11 if ( ! class_exists( 'POMO_Reader', false ) ) :
    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 		/**
       
    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 ) {
       
    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 ) ) :
    17 	/**
   213 	/**
    18 	 * PHP5 constructor.
   214 	 * Provides file-like methods for manipulating a string instead
       
   215 	 * of a physical file.
    19 	 */
   216 	 */
    20 	function __construct() {
   217 	class POMO_StringReader extends POMO_Reader {
    21 		$this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
   218 
    22 		$this->_pos = 0;
   219 		var $_str = '';
    23 	}
   220 
    24 
   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 ) ) :
    25 	/**
   280 	/**
    26 	 * PHP4 constructor.
   281 	 * Reads the contents of the file in the beginning.
    27 	 */
   282 	 */
    28 	public function POMO_Reader() {
   283 	class POMO_CachedFileReader extends POMO_StringReader {
    29 		self::__construct();
   284 		/**
    30 	}
   285 		 * PHP5 constructor.
    31 
   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 ) ) :
    32 	/**
   306 	/**
    33 	 * Sets the endianness of the file.
   307 	 * Reads the contents of the file in the beginning.
    34 	 *
       
    35 	 * @param $endian string 'big' or 'little'
       
    36 	 */
   308 	 */
    37 	function setEndian($endian) {
   309 	class POMO_CachedIntFileReader extends POMO_CachedFileReader {
    38 		$this->endian = $endian;
   310 		/**
    39 	}
   311 		 * PHP5 constructor.
    40 
   312 		 */
    41 	/**
   313 		public function __construct( $filename ) {
    42 	 * Reads a 32bit Integer from the Stream
   314 			parent::POMO_CachedFileReader( $filename );
    43 	 *
   315 		}
    44 	 * @return mixed The integer, corresponding to the next 32 bits from
   316 
    45 	 * 	the stream of false if there are not enough bytes or on error
   317 		/**
    46 	 */
   318 		 * PHP4 constructor.
    47 	function readint32() {
   319 		 */
    48 		$bytes = $this->read(4);
   320 		function POMO_CachedIntFileReader( $filename ) {
    49 		if (4 != $this->strlen($bytes))
   321 			self::__construct( $filename );
    50 			return false;
   322 		}
    51 		$endian_letter = ('big' == $this->endian)? 'N' : 'V';
   323 	}
    52 		$int = unpack($endian_letter, $bytes);
   324 endif;
    53 		return reset( $int );
   325 
    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