5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class FileIoIterator extends BaseIterator implements SeekableWritableFileIterator {
28 private $backBuffer = '';
31 * Currently loaded block (will be returned by current())
33 private $currentBlock = '';
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates an instance of this class
48 * @param $pointerInstance An instance of a InputOutputPointer class
49 * @param $blockInstance An instance of a CalculatableBlock class
50 * @return $iteratorInstance An instance of a Iterator class
52 public final static function createFileIoIterator (InputOutputPointer $pointerInstance, CalculatableBlock $blockInstance) {
54 $iteratorInstance = new FileIoIterator();
56 // Set the instance here
57 $iteratorInstance->setPointerInstance($pointerInstance);
59 // Set the block instance here
60 $iteratorInstance->setBlockInstance($blockInstance);
62 // Return the prepared instance
63 return $iteratorInstance;
67 * Initializes the back-buffer by setting it to an empty string.
71 private function initBackBuffer () {
72 // Simply call the setter
73 $this->setBackBuffer('');
77 * Setter for backBuffer field
79 * @param $backBuffer Characters to "store" in back-buffer
82 private function setBackBuffer ($backBuffer) {
83 // Cast to string (so no arrays or objects)
84 $backBuffer = (string) $backBuffer;
87 $this->backBuffer = $backBuffer;
91 * Getter for backBuffer field
93 * @return $backBuffer Characters "stored" in back-buffer
95 private function getBackBuffer () {
96 return $this->backBuffer;
100 * Setter for currentBlock field
102 * @param $currentBlock Characters to set a currently loaded block
105 private function setCurrentBlock ($currentBlock) {
106 // Cast to string (so no arrays or objects)
107 $currentBlock = (string) $currentBlock;
110 $this->currentBlock = $currentBlock;
114 * Gets currently read data
116 * @return $current Currently read data
118 public function current () {
120 return $this->currentBlock;
124 * Gets current seek position ("key").
126 * @return $key Current key in iteration
128 public function key () {
130 return $this->getPointerInstance()->determineSeekPosition();
134 * Advances to next "block" of bytes
138 public function next () {
139 // Is there nothing to read?
140 if (!$this->valid()) {
145 // First calculate minimum block length
146 $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
148 // Short be more than zero!
151 // Wait until a entry/block separator has been found
152 $data = $this->getBackBuffer();
153 while ((!$this->getPointerInstance()->isEndOfFileReached()) && (!$this->getBlockInstance()->isBlockSeparatorFound($data))) {
154 // Then read the block
155 $data .= $this->read($length);
156 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data));
160 if ($this->getPointerInstance()->isEndOfFileReached()) {
161 // Set whole data as current block
162 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
163 $this->setCurrentBlock($data);
165 // Then abort here silently
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
171 * Init back-buffer which is the data that has been found beyond the
174 $this->initBackBuffer();
177 $dataArray = explode(self::getBlockSeparator(), $data);
179 // This array must contain two elements
180 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
181 assert(count($dataArray) == 2);
183 // Left part is the actual block, right one the back-buffer data
184 $this->setCurrentBlock($dataArray[0]);
185 $this->setBackBuffer($dataArray[1]);
189 * Rewinds to the beginning of the file
191 * @return $status Status of this operation
193 public function rewind () {
194 // Call pointer instance
195 return $this->getPointerInstance()->rewind();
199 * Checks wether the current entry is valid (not at the end of the file).
200 * This method will return TRUE if an emptied (nulled) entry has been found.
202 * @return $isValid Whether the next entry is valid
204 public function valid () {
205 // First calculate minimum block length
206 $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
208 // Short be more than zero!
211 // Get current seek position
212 $seekPosition = $this->key();
214 // Then try to read it
215 $data = $this->read($length);
217 // If some bytes could be read, all is fine
218 $isValid = ((is_string($data)) && (strlen($data) > 0));
221 $headerSize = $this->getBlockInstance()->getHeaderSize();
223 // Is the seek position at or beyond the header?
224 if ($seekPosition >= $headerSize) {
225 // Seek back to old position
226 $this->seek($seekPosition);
228 // Seek directly behind the header
229 $this->seek($headerSize);
237 * Seeks to given position
239 * @param $seekPosition Seek position in file
240 * @return $status Status of this operation
242 public function seek ($seekPosition) {
243 // Call pointer instance
244 return $this->getPointerInstance()->seek($seekPosition);
248 * Writes at given position by seeking to it.
250 * @param $seekPosition Seek position in file
251 * @param $data Data to be written
254 public function writeAtPosition ($seekPosition, $data) {
256 $this->seek($seekPosition);
258 // Then write the data at that position
259 $this->getPointerInstance()->writeToFile($data);
265 * @return $size Size (in bytes) of file
267 public function size () {
268 // Call the pointer object
269 $size = $this->getPointerInstance()->size();
276 * Reads given amount of bytes from file.
278 * @param $bytes Amount of bytes to read
279 * @return $data Data read from file
281 public function read ($bytes) {
282 // Call pointer instance
283 return $this->getPointerInstance()->read($bytes);