PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Websites/webkit.org/blog/wp-includes/streams.php

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