[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/phing/system/io/ -> BufferedReader.php (source)

   1  <?php
   2  /*
   3   *  $Id: BufferedReader.php 3076 2006-12-18 08:52:12Z fabien $
   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  
  22  include_once 'phing/system/io/Reader.php';
  23  
  24  /*
  25   * Convenience class for reading files.
  26   *
  27   * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
  28   * @version   $Revision: 1.6 $ $Date: 2005/12/27 19:12:13 $
  29   * @access    public
  30   * @see       FilterReader
  31   * @package   phing.system.io
  32  */
  33  class BufferedReader extends Reader {
  34  
  35      private $bufferSize = 0;
  36      private $buffer     = null;
  37      private $bufferPos  = 0;
  38      
  39      /**
  40       * The Reader we are buffering for.
  41       */
  42      private $in;
  43      
  44      /**
  45       * 
  46       * @param object $reader The reader (e.g. FileReader).
  47       * @param integer $buffsize The size of the buffer we should use for reading files.
  48       *                             A large buffer ensures that most files (all scripts?) are parsed in 1 buffer.
  49       */     
  50      function __construct(Reader $reader, $buffsize = 65536) {
  51          $this->in = $reader;
  52          $this->bufferSize = $buffsize;
  53      }
  54  
  55      /**
  56       * Reads and returns $_bufferSize chunk of data.
  57       * @return mixed buffer or -1 if EOF.
  58       */
  59      function read($len = null) {
  60          // ignore $len param, not sure how to hanlde it, since 
  61          // this should only read bufferSize amount of data.
  62          if ($len !== null) {
  63              $this->currentPosition = ftell($this->fd);
  64          }
  65          
  66          if ( ($data = $this->in->read($this->bufferSize)) !== -1 ) {
  67          
  68              // not all files end with a newline character, so we also need to check EOF
  69              if (!$this->in->eof()) {
  70              
  71                  $notValidPart = strrchr($data, "\n");
  72                  $notValidPartSize = strlen($notValidPart);
  73              
  74                  if ( $notValidPartSize > 1 ) {
  75                      // Block doesn't finish on a EOL
  76                      // Find the last EOL and forgot all following stuff
  77                      $dataSize = strlen($data);
  78                      $validSize = $dataSize - $notValidPartSize + 1;
  79                  
  80                      $data = substr($data, 0, $validSize);
  81      
  82                      // Rewind to the begining of the forgotten stuff.
  83                      $this->in->skip(-$notValidPartSize+1);
  84                  }
  85                  
  86              } // if !EOF
  87          }
  88          return $data;
  89      }
  90      
  91      function skip($n) {
  92          return $this->in->skip($n);
  93      }
  94      
  95      function reset() {
  96          return $this->in->reset();
  97      }
  98      
  99      function close() {
 100          return $this->in->close();
 101      }
 102      
 103      function open() {
 104          return $this->in->open();
 105      }
 106      
 107      /**
 108       * Read a line from input stream.
 109       */
 110      function readLine() {
 111          $line = null;
 112          while ( ($ch = $this->readChar()) !== -1 ) {
 113              if ( $ch === "\n" ) {
 114                  break;
 115              }
 116              $line .= $ch;
 117          }
 118  
 119          // Warning : Not considering an empty line as an EOF
 120          if ( $line === null && $ch !== -1 )
 121              return "";
 122  
 123          return $line;
 124      }
 125      
 126      /**
 127       * Reads a single char from the reader.
 128       * @return string single char or -1 if EOF.
 129       */
 130      function readChar() {        
 131  
 132          if ( $this->buffer === null ) {
 133              // Buffer is empty, fill it ...
 134              $read = $this->in->read($this->bufferSize);
 135              if ($read === -1) {
 136                  $ch = -1;
 137              } else {
 138                  $this->buffer = $read;
 139                  return $this->readChar(); // recurse
 140              }
 141          } else {            
 142              // Get next buffered char ...
 143              // handle case where buffer is read-in, but is empty.  The next readChar() will return -1 EOF,
 144              // so we just return empty string (char) at this point.  (Probably could also return -1 ...?)
 145              $ch = ($this->buffer !== "") ? $this->buffer{$this->bufferPos} : '';
 146              $this->bufferPos++;
 147              if ( $this->bufferPos >= strlen($this->buffer) ) {
 148                  $this->buffer = null;
 149                  $this->bufferPos = 0;
 150              }
 151          }
 152  
 153          return $ch;
 154      }
 155      
 156      /**
 157       * Returns whether eof has been reached in stream.
 158       * This is important, because filters may want to know if the end of the file (and not just buffer)
 159       * has been reached.
 160       * @return boolean
 161       */ 
 162      function eof() {
 163          return $this->in->eof();
 164      }
 165  
 166      function getResource() {
 167          return $this->in->getResource();
 168      }    
 169  }
 170  ?>


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7