Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: BufferedReader.php 227 2007-08-28 02:17:00Z hans $
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: 2007-08-28 04:17:00 +0200 (Tue, 28 Aug 2007) $
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 a chunk of data.
57
     * @param int $len Number of bytes to read.  Default is to read configured buffer size number of bytes.
58
     * @return mixed buffer or -1 if EOF.
59
     */
60
    function read($len = null) {
61
 
62
    	// if $len is specified, we'll use that; otherwise, use the configured buffer size.
63
    	if ($len === null) $len = $this->bufferSize;
64
 
65
        if ( ($data = $this->in->read($len)) !== -1 ) {
66
 
67
			// not all files end with a newline character, so we also need to check EOF
68
			if (!$this->in->eof()) {
69
 
70
	            $notValidPart = strrchr($data, "\n");
71
	            $notValidPartSize = strlen($notValidPart);
72
 
73
	            if ( $notValidPartSize > 1 ) {
74
	                // Block doesn't finish on a EOL
75
	                // Find the last EOL and forget all following stuff
76
	                $dataSize = strlen($data);
77
	                $validSize = $dataSize - $notValidPartSize + 1;
78
 
79
	                $data = substr($data, 0, $validSize);
80
 
81
	                // Rewind to the begining of the forgotten stuff.
82
	                $this->in->skip(-$notValidPartSize+1);
83
	            }
84
 
85
			} // if !EOF
86
        }
87
        return $data;
88
    }
89
 
90
    function skip($n) {
91
        return $this->in->skip($n);
92
    }
93
 
94
    function reset() {
95
        return $this->in->reset();
96
    }
97
 
98
    function close() {
99
        return $this->in->close();
100
    }
101
 
102
    function open() {
103
        return $this->in->open();
104
    }
105
 
106
    /**
107
     * Read a line from input stream.
108
     */
109
    function readLine() {
110
        $line = null;
111
        while ( ($ch = $this->readChar()) !== -1 ) {
112
            if ( $ch === "\n" ) {
113
                break;
114
            }
115
            $line .= $ch;
116
        }
117
 
118
        // Warning : Not considering an empty line as an EOF
119
        if ( $line === null && $ch !== -1 )
120
            return "";
121
 
122
        return $line;
123
    }
124
 
125
    /**
126
     * Reads a single char from the reader.
127
     * @return string single char or -1 if EOF.
128
     */
129
    function readChar() {
130
 
131
        if ( $this->buffer === null ) {
132
            // Buffer is empty, fill it ...
133
            $read = $this->in->read($this->bufferSize);
134
            if ($read === -1) {
135
                $ch = -1;
136
            } else {
137
                $this->buffer = $read;
138
                return $this->readChar(); // recurse
139
            }
140
        } else {
141
            // Get next buffered char ...
142
            // handle case where buffer is read-in, but is empty.  The next readChar() will return -1 EOF,
143
            // so we just return empty string (char) at this point.  (Probably could also return -1 ...?)
144
            $ch = ($this->buffer !== "") ? $this->buffer{$this->bufferPos} : '';
145
            $this->bufferPos++;
146
            if ( $this->bufferPos >= strlen($this->buffer) ) {
147
                $this->buffer = null;
148
                $this->bufferPos = 0;
149
            }
150
        }
151
 
152
        return $ch;
153
    }
154
 
155
    /**
156
     * Returns whether eof has been reached in stream.
157
     * This is important, because filters may want to know if the end of the file (and not just buffer)
158
     * has been reached.
159
     * @return boolean
160
     */
161
    function eof() {
162
        return $this->in->eof();
163
    }
164
 
165
    function getResource() {
166
        return $this->in->getResource();
167
    }
168
}