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);
118 * @return $size Size (in bytes) of file
120 public function size () {
121 // Call the block object
122 $size = $this->getBlockInstance()->size();
129 * Reads given amount of bytes from file.
131 * @param $bytes Amount of bytes to read
132 * @return $data Data read from file
134 public function read ($bytes) {
135 // Call block instance
136 return $this->getBlockInstance()->read($bytes);
140 * Analyzes entries in index file. This will count all found (and valid)
141 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
142 * only gaps are found, the file is considered as "virgin" (no entries).
146 public function analyzeFile () {
147 // Just call the block instance
148 $this->getBlockInstance()->analyzeFile();
152 * Checks whether the file header is initialized
154 * @return $isInitialized Whether the file header is initialized
156 public function isFileHeaderInitialized () {
157 // Just call the block instance
158 return $this->getBlockInstance()->isFileHeaderInitialized();
162 * Creates the assigned file
166 public function createFileHeader () {
167 // Just call the block instance
168 $this->getBlockInstance()->createFileHeader();
172 * Pre-allocates file (if enabled) with some space for later faster write access.
174 * @param $type Type of the file
177 public function preAllocateFile ($type) {
178 // Just call the block instance
179 $this->getBlockInstance()->preAllocateFile($type);
183 * Initializes counter for valid entries, arrays for damaged entries and
184 * an array for gap seek positions. If you call this method on your own,
185 * please re-analyze the file structure. So you are better to call
186 * analyzeFile() instead of this method.
190 public function initCountersGapsArray () {
191 // Call block instance
192 $this->getBlockInstance()->initCountersGapsArray();
196 * Getter for header size
198 * @return $totalEntries Size of file header
200 public final function getHeaderSize () {
201 // Call block instance
202 return $this->getBlockInstance()->getHeaderSize();
206 * Setter for header size
208 * @param $headerSize Size of file header
211 public final function setHeaderSize ($headerSize) {
212 // Call block instance
213 $this->getBlockInstance()->setHeaderSize($headerSize);
217 * Getter for header array
219 * @return $totalEntries Size of file header
221 public final function getHeader () {
222 // Call block instance
223 return $this->getBlockInstance()->getHeader();
229 * @param $header Array for a file header
232 public final function setHeader (array $header) {
233 // Call block instance
234 $this->getBlockInstance()->setHeader($header);
238 * Updates seekPosition attribute from file to avoid to much access on file.
242 public function updateSeekPosition () {
243 // Call block instance
244 $this->getBlockInstance()->updateSeekPosition();
248 * Getter for total entries
250 * @return $totalEntries Total entries in this file
252 public final function getCounter () {
253 // Call block instance
254 return $this->getBlockInstance()->getCounter();
258 * "Getter" for file size
260 * @return $fileSize Size of currently loaded file
262 public function getFileSize () {
263 // Call block instance
264 return $this->getBlockInstance()->getFileSize();
268 * Writes data at given position
270 * @param $seekPosition Seek position
271 * @param $data Data to be written
272 * @param $flushHeader Whether to flush the header (default: flush)
275 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
276 // Call block instance
277 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
281 * Getter for seek position
283 * @return $seekPosition Current seek position (stored here in object)
285 public function getSeekPosition () {
286 // Call block instance
287 return $this->getBlockInstance()->getSeekPosition();