streams.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // should return a string [FIXME: perhaps return array of bytes?]
  21. function read($bytes) {
  22. return false;
  23. }
  24. // should return new position
  25. function seekto($position) {
  26. return false;
  27. }
  28. // returns current position
  29. function currentpos() {
  30. return false;
  31. }
  32. // returns length of entire stream (limit for seekto()s)
  33. function length() {
  34. return false;
  35. }
  36. };
  37. class StringReader {
  38. var $_pos;
  39. var $_str;
  40. function StringReader($str='') {
  41. $this->_str = $str;
  42. $this->_pos = 0;
  43. }
  44. function read($bytes) {
  45. $data = substr($this->_str, $this->_pos, $bytes);
  46. $this->_pos += $bytes;
  47. if (strlen($this->_str)<$this->_pos)
  48. $this->_pos = strlen($this->_str);
  49. return $data;
  50. }
  51. function seekto($pos) {
  52. $this->_pos = $pos;
  53. if (strlen($this->_str)<$this->_pos)
  54. $this->_pos = strlen($this->_str);
  55. return $this->_pos;
  56. }
  57. function currentpos() {
  58. return $this->_pos;
  59. }
  60. function length() {
  61. return strlen($this->_str);
  62. }
  63. };
  64. class FileReader {
  65. var $_pos;
  66. var $_fd;
  67. var $_length;
  68. function FileReader($filename) {
  69. if (file_exists($filename)) {
  70. $this->_length=filesize($filename);
  71. $this->_pos = 0;
  72. $this->_fd = fopen($filename,'rb');
  73. if (!$this->_fd) {
  74. $this->error = 3; // Cannot read file, probably permissions
  75. return false;
  76. }
  77. } else {
  78. $this->error = 2; // File doesn't exist
  79. return false;
  80. }
  81. }
  82. function read($bytes) {
  83. if ($bytes) {
  84. fseek($this->_fd, $this->_pos);
  85. // PHP 5.1.1 does not read more than 8192 bytes in one fread()
  86. // the discussions at PHP Bugs suggest it's the intended behaviour
  87. $data = '';
  88. while ($bytes > 0) {
  89. $chunk = fread($this->_fd, $bytes);
  90. $data .= $chunk;
  91. $bytes -= strlen($chunk);
  92. }
  93. $this->_pos = ftell($this->_fd);
  94. return $data;
  95. } else return '';
  96. }
  97. function seekto($pos) {
  98. fseek($this->_fd, $pos);
  99. $this->_pos = ftell($this->_fd);
  100. return $this->_pos;
  101. }
  102. function currentpos() {
  103. return $this->_pos;
  104. }
  105. function length() {
  106. return $this->_length;
  107. }
  108. function close() {
  109. fclose($this->_fd);
  110. }
  111. };
  112. // Preloads entire file in memory first, then creates a StringReader
  113. // over it (it assumes knowledge of StringReader internals)
  114. class CachedFileReader extends StringReader {
  115. function CachedFileReader($filename) {
  116. if (file_exists($filename)) {
  117. $length=filesize($filename);
  118. $fd = fopen($filename,'rb');
  119. if (!$fd) {
  120. $this->error = 3; // Cannot read file, probably permissions
  121. return false;
  122. }
  123. $this->_str = fread($fd, $length);
  124. fclose($fd);
  125. } else {
  126. $this->error = 2; // File doesn't exist
  127. return false;
  128. }
  129. }
  130. };
  131. ?>