3 namespace CoreFramework\Iterator\File;
5 // Import framework stuff
6 use CoreFramework\Filesystem\Block;
11 * @author Roland Haeder <webmaster@ship-simu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.ship-simu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
32 * Protected constructor
36 protected function __construct () {
37 // Call parent constructor
38 parent::__construct(__CLASS__);
42 * Creates an instance of this class
44 * @param $pointerInstance An instance of a Block class
45 * @return $iteratorInstance An instance of a Iterator class
47 public final static function createFileIterator (Block $blockInstance) {
49 $iteratorInstance = new FileIterator();
51 // Set the instance here
52 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
53 $iteratorInstance->setBlockInstance($blockInstance);
55 // Return the prepared instance
56 return $iteratorInstance;
60 * Gets currently read data
62 * @return $current Currently read data
64 public function current () {
65 // Call block instance
66 return $this->getBlockInstance()->current();
70 * Gets current seek position ("key").
72 * @return $key Current key in iteration
74 public function key () {
76 return $this->getBlockInstance()->determineSeekPosition();
80 * Advances to next "block" of bytes
84 public function next () {
85 // Call block instance
86 $this->getBlockInstance()->next();
90 * Rewinds to the beginning of the file
92 * @return $status Status of this operation
94 public function rewind () {
95 // Call block instance
96 return $this->getBlockInstance()->rewind();
100 * Checks wether the current entry is valid (not at the end of the file).
101 * This method will return TRUE if an emptied (nulled) entry has been found.
103 * @return $isValid Whether the next entry is valid
105 public function valid () {
106 // Call block instance
107 return $this->getBlockInstance()->valid();
111 * Seeks to given position
113 * @param $seekPosition Seek position in file
114 * @return $status Status of this operation
116 public function seek ($seekPosition) {
117 // Call block instance
118 return $this->getBlockInstance()->seek($seekPosition);
124 * @return $size Size (in bytes) of file
126 public function size () {
127 // Call the block object
128 $size = $this->getBlockInstance()->size();
135 * Reads given amount of bytes from file.
137 * @param $bytes Amount of bytes to read
138 * @return $data Data read from file
140 public function read ($bytes = NULL) {
141 // Call block instance
142 return $this->getBlockInstance()->read($bytes);
146 * Analyzes entries in index file. This will count all found (and valid)
147 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
148 * only gaps are found, the file is considered as "virgin" (no entries).
152 public function analyzeFile () {
153 // Just call the block instance
154 $this->getBlockInstance()->analyzeFile();
158 * Checks whether the file header is initialized
160 * @return $isInitialized Whether the file header is initialized
162 public function isFileHeaderInitialized () {
163 // Just call the block instance
164 return $this->getBlockInstance()->isFileHeaderInitialized();
168 * Creates the assigned file
172 public function createFileHeader () {
173 // Just call the block instance
174 $this->getBlockInstance()->createFileHeader();
178 * Pre-allocates file (if enabled) with some space for later faster write access.
180 * @param $type Type of the file
183 public function preAllocateFile ($type) {
184 // Just call the block instance
185 $this->getBlockInstance()->preAllocateFile($type);
189 * Initializes counter for valid entries, arrays for damaged entries and
190 * an array for gap seek positions. If you call this method on your own,
191 * please re-analyze the file structure. So you are better to call
192 * analyzeFile() instead of this method.
196 public function initCountersGapsArray () {
197 // Call block instance
198 $this->getBlockInstance()->initCountersGapsArray();
202 * Getter for header size
204 * @return $totalEntries Size of file header
206 public final function getHeaderSize () {
207 // Call block instance
208 return $this->getBlockInstance()->getHeaderSize();
212 * Setter for header size
214 * @param $headerSize Size of file header
217 public final function setHeaderSize ($headerSize) {
218 // Call block instance
219 $this->getBlockInstance()->setHeaderSize($headerSize);
223 * Getter for header array
225 * @return $totalEntries Size of file header
227 public final function getHeader () {
228 // Call block instance
229 return $this->getBlockInstance()->getHeader();
235 * @param $header Array for a file header
238 public final function setHeader (array $header) {
239 // Call block instance
240 $this->getBlockInstance()->setHeader($header);
244 * Updates seekPosition attribute from file to avoid to much access on file.
248 public function updateSeekPosition () {
249 // Call block instance
250 $this->getBlockInstance()->updateSeekPosition();
254 * Getter for total entries
256 * @return $totalEntries Total entries in this file
258 public final function getCounter () {
259 // Call block instance
260 return $this->getBlockInstance()->getCounter();
264 * "Getter" for file size
266 * @return $fileSize Size of currently loaded file
268 public function getFileSize () {
269 // Call block instance
270 return $this->getBlockInstance()->getFileSize();
274 * Writes data at given position
276 * @param $seekPosition Seek position
277 * @param $data Data to be written
278 * @param $flushHeader Whether to flush the header (default: flush)
281 public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
282 // Call block instance
283 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
287 * Getter for seek position
289 * @return $seekPosition Current seek position (stored here in object)
291 public function getSeekPosition () {
292 // Call block instance
293 return $this->getBlockInstance()->getSeekPosition();
297 * Writes given value to the file and returns a hash and gap position for it
299 * @param $groupId Group identifier
300 * @param $value Value to be added to the stack
301 * @return $data Hash and gap position
303 public function writeValueToFile ($groupId, $value) {
304 // Call block instance
305 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
309 * Writes given raw data to the file and returns a gap position and length
311 * @param $groupId Group identifier
312 * @param $hash Hash from encoded value
313 * @param $encoded Encoded value to be written to the file
314 * @return $data Gap position and length of the raw data
316 public function writeDataToFreeGap ($groupId, $hash, $encoded) {
317 // Call block instance
318 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
322 * Searches for next suitable gap the given length of data can fit in
323 * including padding bytes.
325 * @param $length Length of raw data
326 * @return $seekPosition Found next gap's seek position
328 public function searchNextGap ($length) {
329 // Call block instance
330 print $this->getBlockInstance()->__toString() . PHP_EOL;
331 return $this->getBlockInstance()->searchNextGap($length);