5 // Copyright 2004-2014 Setasign - Jan Slabon
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
28 * @var integer 0 = file | 1 = string
33 * @var resource|string
62 public function __construct(&$f)
65 if (is_string($this->file
))
72 * Get the position in the file stream
76 public function getPos()
78 if ($this->_mode
== 0) {
79 return ftell($this->file
);
86 * Reset the position in the file stream.
88 * Optionally move the file pointer to a new location and reset the buffered data.
93 public function reset($pos = null, $l = 100)
95 if ($this->_mode
== 0) {
97 fseek ($this->file
, $pos);
100 $this->buffer
= $l > 0 ?
fread($this->file
, $l) : '';
101 $this->length
= strlen($this->buffer
);
102 if ($this->length
< $l)
103 $this->increaseLength($l - $this->length
);
105 $this->buffer
= $this->file
;
106 $this->length
= strlen($this->buffer
);
109 $this->stack
= array();
113 * Make sure that there is at least one character beyond the current offset in the buffer.
115 * To prevent the tokenizer from attempting to access data that does not exist.
119 public function ensureContent()
121 if ($this->offset
>= $this->length
- 1) {
122 return $this->increaseLength();
129 * Forcefully read more data into the buffer
134 public function increaseLength($l = 100)
136 if ($this->_mode
== 0 && feof($this->file
)) {
138 } else if ($this->_mode
== 0) {
139 $totalLength = $this->length +
$l;
141 $toRead = $totalLength - $this->length
;
145 $this->buffer
.= fread($this->file
, $toRead);
146 } while ((($this->length
= strlen($this->buffer
)) != $totalLength) && !feof($this->file
));