A tumblelog CMS built on AJAX, PHP and MySQL.

streams.class.php 3.9KB

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