PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/pear/phing/system/io/BufferedReader.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 192 lines | 101 code | 26 blank | 65 comment | 11 complexity | bcbcd96d5d8a75436f321a7c95ed1b85 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /*
  3. * $Id: BufferedReader.php 557 2009-08-29 13:54:38Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/system/io/Reader.php';
  22. /**
  23. * Convenience class for reading files.
  24. *
  25. * @author <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
  26. * @version $Revision: 557 $ $Date: 2009-08-29 15:54:38 +0200 (Sat, 29 Aug 2009) $
  27. * @access public
  28. * @see FilterReader
  29. * @package phing.system.io
  30. */
  31. class BufferedReader extends Reader
  32. {
  33. private $bufferSize = 0;
  34. private $buffer = null;
  35. private $bufferPos = 0;
  36. /**
  37. * The Reader we are buffering for.
  38. */
  39. private $in;
  40. /**
  41. *
  42. * @param object $reader The reader (e.g. FileReader).
  43. * @param integer $buffsize The size of the buffer we should use for reading files.
  44. * A large buffer ensures that most files (all scripts?) are parsed in 1 buffer.
  45. */
  46. function __construct(Reader $reader, $buffsize = 65536)
  47. {
  48. $this->in = $reader;
  49. $this->bufferSize = $buffsize;
  50. }
  51. /**
  52. * Reads and returns a chunk of data.
  53. * @param int $len Number of bytes to read. Default is to read configured buffer size number of bytes.
  54. * @return mixed buffer or -1 if EOF.
  55. */
  56. function read($len = null)
  57. {
  58. // if $len is specified, we'll use that; otherwise, use the configured buffer size.
  59. if ($len === null)
  60. $len = $this->bufferSize;
  61. if (($data = $this->in->read($len)) !== - 1)
  62. {
  63. // not all files end with a newline character, so we also need to check EOF
  64. if (! $this->in->eof())
  65. {
  66. $notValidPart = strrchr($data, "\n");
  67. $notValidPartSize = strlen($notValidPart);
  68. if ($notValidPartSize > 1)
  69. {
  70. // Block doesn't finish on a EOL
  71. // Find the last EOL and forget all following stuff
  72. $dataSize = strlen($data);
  73. $validSize = $dataSize - $notValidPartSize + 1;
  74. $data = substr($data, 0, $validSize);
  75. // Rewind to the begining of the forgotten stuff.
  76. $this->in->skip(- $notValidPartSize + 1);
  77. }
  78. } // if !EOF
  79. }
  80. return $data;
  81. }
  82. function skip($n)
  83. {
  84. return $this->in->skip($n);
  85. }
  86. function reset()
  87. {
  88. return $this->in->reset();
  89. }
  90. function close()
  91. {
  92. return $this->in->close();
  93. }
  94. function open()
  95. {
  96. return $this->in->open();
  97. }
  98. /**
  99. * Read a line from input stream.
  100. */
  101. function readLine()
  102. {
  103. $line = null;
  104. while (($ch = $this->readChar()) !== - 1)
  105. {
  106. if ($ch === "\n")
  107. {
  108. break;
  109. }
  110. $line .= $ch;
  111. }
  112. // Warning : Not considering an empty line as an EOF
  113. if ($line === null && $ch !== - 1)
  114. return "";
  115. return $line;
  116. }
  117. /**
  118. * Reads a single char from the reader.
  119. * @return string single char or -1 if EOF.
  120. */
  121. function readChar()
  122. {
  123. if ($this->buffer === null)
  124. {
  125. // Buffer is empty, fill it ...
  126. $read = $this->in->read($this->bufferSize);
  127. if ($read === - 1)
  128. {
  129. $ch = - 1;
  130. }
  131. else
  132. {
  133. $this->buffer = $read;
  134. return $this->readChar(); // recurse
  135. }
  136. }
  137. else
  138. {
  139. // Get next buffered char ...
  140. // handle case where buffer is read-in, but is empty. The next readChar() will return -1 EOF,
  141. // so we just return empty string (char) at this point. (Probably could also return -1 ...?)
  142. $ch = ($this->buffer !== "") ? $this->buffer{$this->bufferPos} : '';
  143. $this->bufferPos ++;
  144. if ($this->bufferPos >= strlen($this->buffer))
  145. {
  146. $this->buffer = null;
  147. $this->bufferPos = 0;
  148. }
  149. }
  150. return $ch;
  151. }
  152. /**
  153. * Returns whether eof has been reached in stream.
  154. * This is important, because filters may want to know if the end of the file (and not just buffer)
  155. * has been reached.
  156. * @return boolean
  157. */
  158. function eof()
  159. {
  160. return $this->in->eof();
  161. }
  162. function getResource()
  163. {
  164. return $this->in->getResource();
  165. }
  166. }