Continued:
[core.git] / framework / main / classes / iterator / file / class_FileIterator.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Iterator\File;
4
5 // Import framework stuff
6 use CoreFramework\Filesystem\Block;
7 use CoreFramework\Iterator\BaseIterator;
8 use CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
9
10 /**
11  * A file iterator
12  *
13  * @author              Roland Haeder <webmaster@ship-simu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.ship-simu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31  */
32 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of this class
45          *
46          * @param       $pointerInstance        An instance of a Block class
47          * @return      $iteratorInstance       An instance of a Iterator class
48          */
49         public final static function createFileIterator (Block $blockInstance) {
50                 // Get new instance
51                 $iteratorInstance = new FileIterator();
52
53                 // Set the instance here
54                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d] Setting blockInstance=%s ...', __METHOD__, __LINE__, $blockInstance->__toString()));
55                 $iteratorInstance->setBlockInstance($blockInstance);
56
57                 // Return the prepared instance
58                 return $iteratorInstance;
59         }
60
61         /**
62          * Gets currently read data
63          *
64          * @return      $current        Currently read data
65          */
66         public function current () {
67                 // Call block instance
68                 return $this->getBlockInstance()->current();
69         }
70
71         /**
72          * Gets current seek position ("key").
73          *
74          * @return      $key    Current key in iteration
75          */
76         public function key () {
77                 // Return it
78                 return $this->getBlockInstance()->determineSeekPosition();
79         }
80
81         /**
82          * Advances to next "block" of bytes
83          *
84          * @return      void
85          */
86         public function next () {
87                 // Call block instance
88                 $this->getBlockInstance()->next();
89         }
90
91         /**
92          * Rewinds to the beginning of the file
93          *
94          * @return      $status         Status of this operation
95          */
96         public function rewind () {
97                 // Call block instance
98                 return $this->getBlockInstance()->rewind();
99         }
100
101         /**
102          * Checks wether the current entry is valid (not at the end of the file).
103          * This method will return TRUE if an emptied (nulled) entry has been found.
104          *
105          * @return      $isValid        Whether the next entry is valid
106          */
107         public function valid () {
108                 // Call block instance
109                 return $this->getBlockInstance()->valid();
110         }
111
112         /**
113          * Seeks to given position
114          *
115          * @param       $seekPosition   Seek position in file
116          * @return      $status                 Status of this operation
117          */
118         public function seek ($seekPosition) {
119                 // Call block instance
120                 return $this->getBlockInstance()->seek($seekPosition);
121         }
122
123         /**
124          * Size of file stack
125          *
126          * @return      $size   Size (in bytes) of file
127          */
128         public function size () {
129                 // Call the block object
130                 $size = $this->getBlockInstance()->size();
131
132                 // Return result
133                 return $size;
134         }
135
136         /**
137          * Reads given amount of bytes from file.
138          *
139          * @param       $bytes  Amount of bytes to read
140          * @return      $data   Data read from file
141          */
142         public function read ($bytes = NULL) {
143                 // Call block instance
144                 return $this->getBlockInstance()->read($bytes);
145         }
146
147         /**
148          * Analyzes entries in index file. This will count all found (and valid)
149          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
150          * only gaps are found, the file is considered as "virgin" (no entries).
151          *
152          * @return      void
153          */
154         public function analyzeFile () {
155                 // Just call the block instance
156                 $this->getBlockInstance()->analyzeFile();
157         }
158
159         /**
160          * Checks whether the file header is initialized
161          *
162          * @return      $isInitialized  Whether the file header is initialized
163          */
164         public function isFileHeaderInitialized () {
165                 // Just call the block instance
166                 return $this->getBlockInstance()->isFileHeaderInitialized();
167         }
168
169         /**
170          * Creates the assigned file
171          *
172          * @return      void
173          */
174         public function createFileHeader () {
175                 // Just call the block instance
176                 $this->getBlockInstance()->createFileHeader();
177         }
178
179         /**
180          * Pre-allocates file (if enabled) with some space for later faster write access.
181          *
182          * @param       $type   Type of the file
183          * @return      void
184          */
185         public function preAllocateFile ($type) {
186                 // Just call the block instance
187                 $this->getBlockInstance()->preAllocateFile($type);
188         }
189
190         /**
191          * Initializes counter for valid entries, arrays for damaged entries and
192          * an array for gap seek positions. If you call this method on your own,
193          * please re-analyze the file structure. So you are better to call
194          * analyzeFile() instead of this method.
195          *
196          * @return      void
197          */
198         public function initCountersGapsArray () {
199                 // Call block instance
200                 $this->getBlockInstance()->initCountersGapsArray();
201         }
202
203         /**
204          * Getter for header size
205          *
206          * @return      $totalEntries   Size of file header
207          */
208         public final function getHeaderSize () {
209                 // Call block instance
210                 return $this->getBlockInstance()->getHeaderSize();
211         }
212
213         /**
214          * Setter for header size
215          *
216          * @param       $headerSize             Size of file header
217          * @return      void
218          */
219         public final function setHeaderSize ($headerSize) {
220                 // Call block instance
221                 $this->getBlockInstance()->setHeaderSize($headerSize);
222         }
223
224         /**
225          * Getter for header array
226          *
227          * @return      $totalEntries   Size of file header
228          */
229         public final function getHeader () {
230                 // Call block instance
231                 return $this->getBlockInstance()->getHeader();
232         }
233
234         /**
235          * Setter for header
236          *
237          * @param       $header         Array for a file header
238          * @return      void
239          */
240         public final function setHeader (array $header) {
241                 // Call block instance
242                 $this->getBlockInstance()->setHeader($header);
243         }
244
245         /**
246          * Updates seekPosition attribute from file to avoid to much access on file.
247          *
248          * @return      void
249          */
250         public function updateSeekPosition () {
251                 // Call block instance
252                 $this->getBlockInstance()->updateSeekPosition();
253         }
254
255         /**
256          * Getter for total entries
257          *
258          * @return      $totalEntries   Total entries in this file
259          */
260         public final function getCounter () {
261                 // Call block instance
262                 return $this->getBlockInstance()->getCounter();
263         }
264
265         /**
266          * "Getter" for file size
267          *
268          * @return      $fileSize       Size of currently loaded file
269          */
270         public function getFileSize () {
271                 // Call block instance
272                 return $this->getBlockInstance()->getFileSize();
273         }
274
275         /**
276          * Writes data at given position
277          *
278          * @param       $seekPosition   Seek position
279          * @param       $data                   Data to be written
280          * @param       $flushHeader    Whether to flush the header (default: flush)
281          * @return      void
282          */
283         public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
284                 // Call block instance
285                 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
286         }
287
288         /**
289          * Getter for seek position
290          *
291          * @return      $seekPosition   Current seek position (stored here in object)
292          */
293         public function getSeekPosition () {
294                 // Call block instance
295                 return $this->getBlockInstance()->getSeekPosition();
296         }
297
298         /**
299          * Writes given value to the file and returns a hash and gap position for it
300          *
301          * @param       $groupId        Group identifier
302          * @param       $value          Value to be added to the stack
303          * @return      $data           Hash and gap position
304          */
305         public function writeValueToFile ($groupId, $value) {
306                 // Call block instance
307                 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
308         }
309
310         /**
311          * Writes given raw data to the file and returns a gap position and length
312          *
313          * @param       $groupId        Group identifier
314          * @param       $hash           Hash from encoded value
315          * @param       $encoded        Encoded value to be written to the file
316          * @return      $data           Gap position and length of the raw data
317          */
318         public function writeDataToFreeGap ($groupId, $hash, $encoded) {
319                 // Call block instance
320                 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
321         }
322
323         /**
324          * Searches for next suitable gap the given length of data can fit in
325          * including padding bytes.
326          *
327          * @param       $length                 Length of raw data
328          * @return      $seekPosition   Found next gap's seek position
329          */
330         public function searchNextGap ($length) {
331                 // Call block instance
332                 print $this->getBlockInstance()->__toString() . PHP_EOL;
333                 return $this->getBlockInstance()->searchNextGap($length);
334         }
335
336 }