PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/pomo/streams.php

https://github.com/muskmelon/Greemo
PHP | 209 lines | 141 code | 32 blank | 36 comment | 26 complexity | 0bb91ff8188ed64f122f5b2d71e2e80d MD5 | raw file
  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 406 2010-02-07 11:10:24Z nbachiyski $
  7. * @package pomo
  8. * @subpackage streams
  9. */
  10. if ( !class_exists( 'POMO_Reader' ) ):
  11. class POMO_Reader {
  12. var $endian = 'little';
  13. var $_post = '';
  14. function POMO_Reader() {
  15. $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
  16. $this->_pos = 0;
  17. }
  18. /**
  19. * Sets the endianness of the file.
  20. *
  21. * @param string $endian 'big' or 'little'
  22. */
  23. function setEndian($endian) {
  24. $this->endian = $endian;
  25. }
  26. /**
  27. * Reads a 32bit Integer from the Stream
  28. *
  29. * @return mixed The integer, corresponding to the next 32 bits from
  30. * the stream of false if there are not enough bytes or on error
  31. */
  32. function readint32() {
  33. $bytes = $this->read(4);
  34. if (4 != $this->strlen($bytes))
  35. return false;
  36. $endian_letter = ('big' == $this->endian)? 'N' : 'V';
  37. $int = unpack($endian_letter, $bytes);
  38. return array_shift($int);
  39. }
  40. /**
  41. * Reads an array of 32-bit Integers from the Stream
  42. *
  43. * @param integer count How many elements should be read
  44. * @return mixed Array of integers or false if there isn't
  45. * enough data or on error
  46. */
  47. function readint32array($count) {
  48. $bytes = $this->read(4 * $count);
  49. if (4*$count != $this->strlen($bytes))
  50. return false;
  51. $endian_letter = ('big' == $this->endian)? 'N' : 'V';
  52. return unpack($endian_letter.$count, $bytes);
  53. }
  54. function substr($string, $start, $length) {
  55. if ($this->is_overloaded) {
  56. return mb_substr($string, $start, $length, 'ascii');
  57. } else {
  58. return substr($string, $start, $length);
  59. }
  60. }
  61. function strlen($string) {
  62. if ($this->is_overloaded) {
  63. return mb_strlen($string, 'ascii');
  64. } else {
  65. return strlen($string);
  66. }
  67. }
  68. function str_split($string, $chunk_size) {
  69. if (!function_exists('str_split')) {
  70. $length = $this->strlen($string);
  71. $out = array();
  72. for ($i = 0; $i < $length; $i += $chunk_size)
  73. $out[] = $this->substr($string, $i, $chunk_size);
  74. return $out;
  75. } else {
  76. return str_split( $string, $chunk_size );
  77. }
  78. }
  79. function pos() {
  80. return $this->_pos;
  81. }
  82. function is_resource() {
  83. return true;
  84. }
  85. function close() {
  86. return true;
  87. }
  88. }
  89. endif;
  90. if ( !class_exists( 'POMO_FileReader' ) ):
  91. class POMO_FileReader extends POMO_Reader {
  92. function POMO_FileReader($filename) {
  93. parent::POMO_Reader();
  94. $this->_f = fopen($filename, 'r');
  95. }
  96. function read($bytes) {
  97. return fread($this->_f, $bytes);
  98. }
  99. function seekto($pos) {
  100. if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
  101. return false;
  102. }
  103. $this->_pos = $pos;
  104. return true;
  105. }
  106. function is_resource() {
  107. return is_resource($this->_f);
  108. }
  109. function feof() {
  110. return feof($this->_f);
  111. }
  112. function close() {
  113. return fclose($this->_f);
  114. }
  115. function read_all() {
  116. $all = '';
  117. while ( !$this->feof() )
  118. $all .= $this->read(4096);
  119. return $all;
  120. }
  121. }
  122. endif;
  123. if ( !class_exists( 'POMO_StringReader' ) ):
  124. /**
  125. * Provides file-like methods for manipulating a string instead
  126. * of a physical file.
  127. */
  128. class POMO_StringReader extends POMO_Reader {
  129. var $_str = '';
  130. function POMO_StringReader($str = '') {
  131. parent::POMO_Reader();
  132. $this->_str = $str;
  133. $this->_pos = 0;
  134. }
  135. function read($bytes) {
  136. $data = $this->substr($this->_str, $this->_pos, $bytes);
  137. $this->_pos += $bytes;
  138. if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
  139. return $data;
  140. }
  141. function seekto($pos) {
  142. $this->_pos = $pos;
  143. if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
  144. return $this->_pos;
  145. }
  146. function length() {
  147. return $this->strlen($this->_str);
  148. }
  149. function read_all() {
  150. return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
  151. }
  152. }
  153. endif;
  154. if ( !class_exists( 'POMO_CachedFileReader' ) ):
  155. /**
  156. * Reads the contents of the file in the beginning.
  157. */
  158. class POMO_CachedFileReader extends POMO_StringReader {
  159. function POMO_CachedFileReader($filename) {
  160. parent::POMO_StringReader();
  161. $this->_str = file_get_contents($filename);
  162. if (false === $this->_str)
  163. return false;
  164. $this->_pos = 0;
  165. }
  166. }
  167. endif;
  168. if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
  169. /**
  170. * Reads the contents of the file in the beginning.
  171. */
  172. class POMO_CachedIntFileReader extends POMO_CachedFileReader {
  173. function POMO_CachedIntFileReader($filename) {
  174. parent::POMO_CachedFileReader($filename);
  175. }
  176. }
  177. endif;