summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/system/io/BufferedReader.php
blob: 4946985ce6141d58a682cd48dd7c453f5181ad34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
 *  $Id: BufferedReader.php,v 1.6 2005/12/27 19:12:13 hlellelid Exp $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
*/

include_once 'phing/system/io/Reader.php';

/*
 * Convenience class for reading files.
 *
 * @author    <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
 * @version   $Revision: 1.6 $ $Date: 2005/12/27 19:12:13 $
 * @access    public
 * @see       FilterReader
 * @package   phing.system.io
*/
class BufferedReader extends Reader {

    private $bufferSize = 0;
    private $buffer     = null;
    private $bufferPos  = 0;
    
    /**
     * The Reader we are buffering for.
     */
    private $in;
    
    /**
     * 
     * @param object $reader The reader (e.g. FileReader).
     * @param integer $buffsize The size of the buffer we should use for reading files.
     *                             A large buffer ensures that most files (all scripts?) are parsed in 1 buffer.
     */     
    function __construct(Reader $reader, $buffsize = 65536) {
        $this->in = $reader;
        $this->bufferSize = $buffsize;
    }

    /**
     * Reads and returns $_bufferSize chunk of data.
     * @return mixed buffer or -1 if EOF.
     */
    function read($len = null) {
        // ignore $len param, not sure how to hanlde it, since 
        // this should only read bufferSize amount of data.
        if ($len !== null) {
            $this->currentPosition = ftell($this->fd);
        }
        
        if ( ($data = $this->in->read($this->bufferSize)) !== -1 ) {
		
			// not all files end with a newline character, so we also need to check EOF
			if (!$this->in->eof()) {
			
	            $notValidPart = strrchr($data, "\n");
	            $notValidPartSize = strlen($notValidPart);
	        
	            if ( $notValidPartSize > 1 ) {
	                // Block doesn't finish on a EOL
	                // Find the last EOL and forgot all following stuff
	                $dataSize = strlen($data);
	                $validSize = $dataSize - $notValidPartSize + 1;
	            
	                $data = substr($data, 0, $validSize);
	
	                // Rewind to the begining of the forgotten stuff.
	                $this->in->skip(-$notValidPartSize+1);
	            }
				
			} // if !EOF
        }
        return $data;
    }
    
    function skip($n) {
        return $this->in->skip($n);
    }
    
    function reset() {
        return $this->in->reset();
    }
    
    function close() {
        return $this->in->close();
    }
    
    function open() {
        return $this->in->open();
    }
    
    /**
     * Read a line from input stream.
     */
    function readLine() {
        $line = null;
        while ( ($ch = $this->readChar()) !== -1 ) {
            if ( $ch === "\n" ) {
                break;
            }
            $line .= $ch;
        }

        // Warning : Not considering an empty line as an EOF
        if ( $line === null && $ch !== -1 )
            return "";

        return $line;
    }
    
    /**
     * Reads a single char from the reader.
     * @return string single char or -1 if EOF.
     */
    function readChar() {        

        if ( $this->buffer === null ) {
            // Buffer is empty, fill it ...
            $read = $this->in->read($this->bufferSize);
            if ($read === -1) {
                $ch = -1;
            } else {
                $this->buffer = $read;
                return $this->readChar(); // recurse
            }
        } else {            
            // Get next buffered char ...
            // handle case where buffer is read-in, but is empty.  The next readChar() will return -1 EOF,
            // so we just return empty string (char) at this point.  (Probably could also return -1 ...?)
            $ch = ($this->buffer !== "") ? $this->buffer{$this->bufferPos} : '';
            $this->bufferPos++;
            if ( $this->bufferPos >= strlen($this->buffer) ) {
                $this->buffer = null;
                $this->bufferPos = 0;
            }
        }

        return $ch;
    }
    
    /**
     * Returns whether eof has been reached in stream.
     * This is important, because filters may want to know if the end of the file (and not just buffer)
     * has been reached.
     * @return boolean
     */ 
    function eof() {
        return $this->in->eof();
    }

    function getResource() {
        return $this->in->getResource();
    }    
}
?>