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 {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Creates an instance of this class
38 * @param $pointerInstance An instance of a InputOutputPointer class
39 * @param $blockInstance An instance of a CalculatableBlock class
40 * @return $iteratorInstance An instance of a Iterator class
42 public final static function createFileIoIterator (InputOutputPointer $pointerInstance, CalculatableBlock $blockInstance) {
44 $iteratorInstance = new FileIoIterator();
46 // Set the instance here
47 $iteratorInstance->setPointerInstance($pointerInstance);
49 // Set the block instance here
50 $iteratorInstance->setBlockInstance($blockInstance);
52 // Return the prepared instance
53 return $iteratorInstance;
57 * Gets currently read data
59 * @return $current Currently read data
61 public function current () {
65 $this->partialStub('Please implement this method.');
72 * Gets current seek position ("key").
74 * @return $key Current key in iteration
76 public function key () {
78 return $this->getPointerInstance()->determineSeekPosition();
82 * Advances to next "block" of bytes
86 public function next () {
87 $this->partialStub('Please implement this method.');
91 * Rewinds to the beginning of the file
93 * @return $status Status of this operation
95 public function rewind () {
96 // Call pointer instance
97 return $this->getPointerInstance()->rewind();
101 * Checks wether the current entry is valid (not at the end of the file).
102 * This method will return TRUE if an emptied (nulled) entry has been found.
104 * @return $isValid Whether the next entry is valid
106 public function valid () {
107 // First calculate minimum block length
108 $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
110 // Short be more than zero!
113 // Get current seek position
114 $seekPosition = $this->key();
116 // Then try to read it
117 $data = $this->read($length);
119 // If some bytes could be read, all is fine
120 $isValid = ((is_string($data)) && (strlen($data) > 0));
122 // Seek back to old position
123 $this->seek($seekPosition);
130 * Seeks to given position
132 * @param $seekPosition Seek position in file
133 * @return $status Status of this operation
135 public function seek ($seekPosition) {
136 // Call pointer instance
137 return $this->getPointerInstance()->seek($seekPosition);
141 * Writes at given position by seeking to it.
143 * @param $seekPosition Seek position in file
144 * @param $data Data to be written
147 public function writeAtPosition ($seekPosition, $data) {
149 $this->seek($seekPosition);
151 // Then write the data at that position
152 $this->getPointerInstance()->writeToFile($data);
158 * @return $size Size (in bytes) of file
160 public function size () {
161 // Call the pointer object
162 $size = $this->getPointerInstance()->size();
169 * Reads given amount of bytes from file.
171 * @param $bytes Amount of bytes to read
172 * @return $data Data read from file
174 public function read ($bytes) {
175 // Call pointer instance
176 return $this->getPointerInstance()->read($bytes);