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 FileIterator 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 Block class
39 * @return $iteratorInstance An instance of a Iterator class
41 public final static function createFileIterator (Block $blockInstance) {
43 $iteratorInstance = new FileIterator();
45 // Set the instance here
46 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
47 $iteratorInstance->setBlockInstance($blockInstance);
49 // Return the prepared instance
50 return $iteratorInstance;
54 * Gets currently read data
56 * @return $current Currently read data
58 public function current () {
59 // Call block instance
60 return $this->getBlockInstance()->current();
64 * Gets current seek position ("key").
66 * @return $key Current key in iteration
68 public function key () {
70 return $this->getBlockInstance()->determineSeekPosition();
74 * Advances to next "block" of bytes
78 public function next () {
79 // Call block instance
80 $this->getBlockInstance()->next();
84 * Rewinds to the beginning of the file
86 * @return $status Status of this operation
88 public function rewind () {
89 // Call block instance
90 return $this->getBlockInstance()->rewind();
94 * Checks wether the current entry is valid (not at the end of the file).
95 * This method will return TRUE if an emptied (nulled) entry has been found.
97 * @return $isValid Whether the next entry is valid
99 public function valid () {
100 // Call block instance
101 return $this->getBlockInstance()->valid();
105 * Seeks to given position
107 * @param $seekPosition Seek position in file
108 * @return $status Status of this operation
110 public function seek ($seekPosition) {
111 // Call block instance
112 return $this->getBlockInstance()->seek($seekPosition);
116 * Writes at given position by seeking to it.
118 * @param $seekPosition Seek position in file
119 * @param $data Data to be written
122 public function writeAtPosition ($seekPosition, $data) {
124 $this->seek($seekPosition);
126 // Then write the data at that position
127 $this->getBlockInstance()->writeToFile($data);
133 * @return $size Size (in bytes) of file
135 public function size () {
136 // Call the block object
137 $size = $this->getBlockInstance()->size();
144 * Reads given amount of bytes from file.
146 * @param $bytes Amount of bytes to read
147 * @return $data Data read from file
149 public function read ($bytes) {
150 // Call block instance
151 return $this->getBlockInstance()->read($bytes);
155 * Analyzes entries in index file. This will count all found (and valid)
156 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
157 * only gaps are found, the file is considered as "virgin" (no entries).
161 public function analyzeFile () {
162 // Just call the block instance
163 $this->getBlockInstance()->analyzeFile();
167 * Checks whether the file header is initialized
169 * @return $isInitialized Whether the file header is initialized
171 public function isFileHeaderInitialized () {
172 // Just call the block instance
173 return $this->getBlockInstance()->isFileHeaderInitialized();
177 * Creates the assigned file
181 public function createFileHeader () {
182 // Just call the block instance
183 $this->getBlockInstance()->createFileHeader();
187 * Pre-allocates file (if enabled) with some space for later faster write access.
189 * @param $type Type of the file
192 public function preAllocateFile ($type) {
193 // Just call the block instance
194 $this->getBlockInstance()->preAllocateFile($type);
198 * Initializes counter for valid entries, arrays for damaged entries and
199 * an array for gap seek positions. If you call this method on your own,
200 * please re-analyze the file structure. So you are better to call
201 * analyzeFile() instead of this method.
205 public function initCountersGapsArray () {
206 // Call block instance
207 $this->getBlockInstance()->initCountersGapsArray();
211 * Getter for header size
213 * @return $totalEntries Size of file header
215 public final function getHeaderSize () {
216 // Call block instance
217 return $this->getBlockInstance()->getHeaderSize();
221 * Setter for header size
223 * @param $headerSize Size of file header
226 public final function setHeaderSize ($headerSize) {
227 // Call block instance
228 $this->getBlockInstance()->setHeaderSize($headerSize);
232 * Getter for header array
234 * @return $totalEntries Size of file header
236 public final function getHeader () {
237 // Call block instance
238 return $this->getBlockInstance()->getHeader();
244 * @param $header Array for a file header
247 public final function setHeader (array $header) {
248 // Call block instance
249 $this->getBlockInstance()->setHeader($header);
253 * Updates seekPosition attribute from file to avoid to much access on file.
257 public function updateSeekPosition () {
258 // Call block instance
259 $this->getBlockInstance()->updateSeekPosition();
263 * Getter for total entries
265 * @return $totalEntries Total entries in this file
267 public final function getCounter () {
268 // Call block instance
269 return $this->getBlockInstance()->getCounter();