streams.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <danilo@kvota.net>.
  4. This file is part of PHP-gettext.
  5. PHP-gettext is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. PHP-gettext is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with PHP-gettext; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. // Simple class to wrap file streams, string streams, etc.
  18. // seek is essential, and it should be byte stream
  19. class StreamReader
  20. {
  21. // should return a string [FIXME: perhaps return array of bytes?]
  22. public function read($bytes)
  23. {
  24. return false;
  25. }
  26. // should return new position
  27. public function seekto($position)
  28. {
  29. return false;
  30. }
  31. // returns current position
  32. public function currentpos()
  33. {
  34. return false;
  35. }
  36. // returns length of entire stream (limit for seekto()s)
  37. public function length()
  38. {
  39. return false;
  40. }
  41. };
  42. class StringReader
  43. {
  44. public $_pos;
  45. public $_str;
  46. public function StringReader($str='')
  47. {
  48. $this->_str = $str;
  49. $this->_pos = 0;
  50. }
  51. public function read($bytes)
  52. {
  53. $data = substr($this->_str, $this->_pos, $bytes);
  54. $this->_pos += $bytes;
  55. if (strlen($this->_str)<$this->_pos) {
  56. $this->_pos = strlen($this->_str);
  57. }
  58. return $data;
  59. }
  60. public function seekto($pos)
  61. {
  62. $this->_pos = $pos;
  63. if (strlen($this->_str)<$this->_pos) {
  64. $this->_pos = strlen($this->_str);
  65. }
  66. return $this->_pos;
  67. }
  68. public function currentpos()
  69. {
  70. return $this->_pos;
  71. }
  72. public function length()
  73. {
  74. return strlen($this->_str);
  75. }
  76. };
  77. class FileReader
  78. {
  79. public $_pos;
  80. public $_fd;
  81. public $_length;
  82. public function FileReader($filename)
  83. {
  84. if (file_exists($filename)) {
  85. $this->_length=filesize($filename);
  86. $this->_pos = 0;
  87. $this->_fd = fopen($filename, 'rb');
  88. if (!$this->_fd) {
  89. $this->error = 3; // Cannot read file, probably permissions
  90. return false;
  91. }
  92. } else {
  93. $this->error = 2; // File doesn't exist
  94. return false;
  95. }
  96. }
  97. public function read($bytes)
  98. {
  99. if ($bytes) {
  100. fseek($this->_fd, $this->_pos);
  101. // PHP 5.1.1 does not read more than 8192 bytes in one fread()
  102. // the discussions at PHP Bugs suggest it's the intended behaviour
  103. $data = '';
  104. while ($bytes > 0) {
  105. $chunk = fread($this->_fd, $bytes);
  106. $data .= $chunk;
  107. $bytes -= strlen($chunk);
  108. }
  109. $this->_pos = ftell($this->_fd);
  110. return $data;
  111. } else {
  112. return '';
  113. }
  114. }
  115. public function seekto($pos)
  116. {
  117. fseek($this->_fd, $pos);
  118. $this->_pos = ftell($this->_fd);
  119. return $this->_pos;
  120. }
  121. public function currentpos()
  122. {
  123. return $this->_pos;
  124. }
  125. public function length()
  126. {
  127. return $this->_length;
  128. }
  129. public function close()
  130. {
  131. fclose($this->_fd);
  132. }
  133. };
  134. // Preloads entire file in memory first, then creates a StringReader
  135. // over it (it assumes knowledge of StringReader internals)
  136. class CachedFileReader extends StringReader
  137. {
  138. public function CachedFileReader($filename)
  139. {
  140. if (file_exists($filename)) {
  141. $length=filesize($filename);
  142. $fd = fopen($filename, 'rb');
  143. if (!$fd) {
  144. $this->error = 3; // Cannot read file, probably permissions
  145. return false;
  146. }
  147. $this->_str = fread($fd, $length);
  148. fclose($fd);
  149. } else {
  150. $this->error = 2; // File doesn't exist
  151. return false;
  152. }
  153. }
  154. };