3 namespace CoreFramework\Iterator\File;
8 * @author Roland Haeder <webmaster@ship-simu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.ship-simu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
39 * Creates an instance of this class
41 * @param $pointerInstance An instance of a Block class
42 * @return $iteratorInstance An instance of a Iterator class
44 public final static function createFileIterator (Block $blockInstance) {
46 $iteratorInstance = new FileIterator();
48 // Set the instance here
49 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
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 () {
62 // Call block instance
63 return $this->getBlockInstance()->current();
67 * Gets current seek position ("key").
69 * @return $key Current key in iteration
71 public function key () {
73 return $this->getBlockInstance()->determineSeekPosition();
77 * Advances to next "block" of bytes
81 public function next () {
82 // Call block instance
83 $this->getBlockInstance()->next();
87 * Rewinds to the beginning of the file
89 * @return $status Status of this operation
91 public function rewind () {
92 // Call block instance
93 return $this->getBlockInstance()->rewind();
97 * Checks wether the current entry is valid (not at the end of the file).
98 * This method will return TRUE if an emptied (nulled) entry has been found.
100 * @return $isValid Whether the next entry is valid
102 public function valid () {
103 // Call block instance
104 return $this->getBlockInstance()->valid();
108 * Seeks to given position
110 * @param $seekPosition Seek position in file
111 * @return $status Status of this operation
113 public function seek ($seekPosition) {
114 // Call block instance
115 return $this->getBlockInstance()->seek($seekPosition);
121 * @return $size Size (in bytes) of file
123 public function size () {
124 // Call the block object
125 $size = $this->getBlockInstance()->size();
132 * Reads given amount of bytes from file.
134 * @param $bytes Amount of bytes to read
135 * @return $data Data read from file
137 public function read ($bytes = NULL) {
138 // Call block instance
139 return $this->getBlockInstance()->read($bytes);
143 * Analyzes entries in index file. This will count all found (and valid)
144 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
145 * only gaps are found, the file is considered as "virgin" (no entries).
149 public function analyzeFile () {
150 // Just call the block instance
151 $this->getBlockInstance()->analyzeFile();
155 * Checks whether the file header is initialized
157 * @return $isInitialized Whether the file header is initialized
159 public function isFileHeaderInitialized () {
160 // Just call the block instance
161 return $this->getBlockInstance()->isFileHeaderInitialized();
165 * Creates the assigned file
169 public function createFileHeader () {
170 // Just call the block instance
171 $this->getBlockInstance()->createFileHeader();
175 * Pre-allocates file (if enabled) with some space for later faster write access.
177 * @param $type Type of the file
180 public function preAllocateFile ($type) {
181 // Just call the block instance
182 $this->getBlockInstance()->preAllocateFile($type);
186 * Initializes counter for valid entries, arrays for damaged entries and
187 * an array for gap seek positions. If you call this method on your own,
188 * please re-analyze the file structure. So you are better to call
189 * analyzeFile() instead of this method.
193 public function initCountersGapsArray () {
194 // Call block instance
195 $this->getBlockInstance()->initCountersGapsArray();
199 * Getter for header size
201 * @return $totalEntries Size of file header
203 public final function getHeaderSize () {
204 // Call block instance
205 return $this->getBlockInstance()->getHeaderSize();
209 * Setter for header size
211 * @param $headerSize Size of file header
214 public final function setHeaderSize ($headerSize) {
215 // Call block instance
216 $this->getBlockInstance()->setHeaderSize($headerSize);
220 * Getter for header array
222 * @return $totalEntries Size of file header
224 public final function getHeader () {
225 // Call block instance
226 return $this->getBlockInstance()->getHeader();
232 * @param $header Array for a file header
235 public final function setHeader (array $header) {
236 // Call block instance
237 $this->getBlockInstance()->setHeader($header);
241 * Updates seekPosition attribute from file to avoid to much access on file.
245 public function updateSeekPosition () {
246 // Call block instance
247 $this->getBlockInstance()->updateSeekPosition();
251 * Getter for total entries
253 * @return $totalEntries Total entries in this file
255 public final function getCounter () {
256 // Call block instance
257 return $this->getBlockInstance()->getCounter();
261 * "Getter" for file size
263 * @return $fileSize Size of currently loaded file
265 public function getFileSize () {
266 // Call block instance
267 return $this->getBlockInstance()->getFileSize();
271 * Writes data at given position
273 * @param $seekPosition Seek position
274 * @param $data Data to be written
275 * @param $flushHeader Whether to flush the header (default: flush)
278 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
279 // Call block instance
280 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
284 * Getter for seek position
286 * @return $seekPosition Current seek position (stored here in object)
288 public function getSeekPosition () {
289 // Call block instance
290 return $this->getBlockInstance()->getSeekPosition();
294 * Writes given value to the file and returns a hash and gap position for it
296 * @param $groupId Group identifier
297 * @param $value Value to be added to the stack
298 * @return $data Hash and gap position
300 public function writeValueToFile ($groupId, $value) {
301 // Call block instance
302 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
306 * Writes given raw data to the file and returns a gap position and length
308 * @param $groupId Group identifier
309 * @param $hash Hash from encoded value
310 * @param $encoded Encoded value to be written to the file
311 * @return $data Gap position and length of the raw data
313 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
314 // Call block instance
315 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
319 * Searches for next suitable gap the given length of data can fit in
320 * including padding bytes.
322 * @param $length Length of raw data
323 * @return $seekPosition Found next gap's seek position
325 public function searchNextGap ($length) {
326 // Call block instance
327 print $this->getBlockInstance()->__toString() . PHP_EOL;
328 return $this->getBlockInstance()->searchNextGap($length);