/wp-includes/pomo/streams.php

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