b958b52b5468c7b4c2797d273132868107d0a125
[core.git] / inc / classes / main / iterator / file / class_FileIterator.php
1 <?php
2 /**
3  * A file iterator
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Creates an instance of this class
37          *
38          * @param       $pointerInstance        An instance of a Block class
39          * @return      $iteratorInstance       An instance of a Iterator class
40          */
41         public final static function createFileIterator (Block $blockInstance) {
42                 // Get new instance
43                 $iteratorInstance = new FileIterator();
44
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);
48
49                 // Return the prepared instance
50                 return $iteratorInstance;
51         }
52
53         /**
54          * Gets currently read data
55          *
56          * @return      $current        Currently read data
57          */
58         public function current () {
59                 // Call block instance
60                 return $this->getBlockInstance()->current();
61         }
62
63         /**
64          * Gets current seek position ("key").
65          *
66          * @return      $key    Current key in iteration
67          */
68         public function key () {
69                 // Return it
70                 return $this->getBlockInstance()->determineSeekPosition();
71         }
72
73         /**
74          * Advances to next "block" of bytes
75          *
76          * @return      void
77          */
78         public function next () {
79                 // Call block instance
80                 $this->getBlockInstance()->next();
81         }
82
83         /**
84          * Rewinds to the beginning of the file
85          *
86          * @return      $status         Status of this operation
87          */
88         public function rewind () {
89                 // Call block instance
90                 return $this->getBlockInstance()->rewind();
91         }
92
93         /**
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.
96          *
97          * @return      $isValid        Whether the next entry is valid
98          */
99         public function valid () {
100                 // Call block instance
101                 return $this->getBlockInstance()->valid();
102         }
103
104         /**
105          * Seeks to given position
106          *
107          * @param       $seekPosition   Seek position in file
108          * @return      $status                 Status of this operation
109          */
110         public function seek ($seekPosition) {
111                 // Call block instance
112                 return $this->getBlockInstance()->seek($seekPosition);
113         }
114
115         /**
116          * Size of file stack
117          *
118          * @return      $size   Size (in bytes) of file
119          */
120         public function size () {
121                 // Call the block object
122                 $size = $this->getBlockInstance()->size();
123
124                 // Return result
125                 return $size;
126         }
127
128         /**
129          * Reads given amount of bytes from file.
130          *
131          * @param       $bytes  Amount of bytes to read
132          * @return      $data   Data read from file
133          */
134         public function read ($bytes = NULL) {
135                 // Call block instance
136                 return $this->getBlockInstance()->read($bytes);
137         }
138
139         /**
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).
143          *
144          * @return      void
145          */
146         public function analyzeFile () {
147                 // Just call the block instance
148                 $this->getBlockInstance()->analyzeFile();
149         }
150
151         /**
152          * Checks whether the file header is initialized
153          *
154          * @return      $isInitialized  Whether the file header is initialized
155          */
156         public function isFileHeaderInitialized () {
157                 // Just call the block instance
158                 return $this->getBlockInstance()->isFileHeaderInitialized();
159         }
160
161         /**
162          * Creates the assigned file
163          *
164          * @return      void
165          */
166         public function createFileHeader () {
167                 // Just call the block instance
168                 $this->getBlockInstance()->createFileHeader();
169         }
170
171         /**
172          * Pre-allocates file (if enabled) with some space for later faster write access.
173          *
174          * @param       $type   Type of the file
175          * @return      void
176          */
177         public function preAllocateFile ($type) {
178                 // Just call the block instance
179                 $this->getBlockInstance()->preAllocateFile($type);
180         }
181
182         /**
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.
187          *
188          * @return      void
189          */
190         public function initCountersGapsArray () {
191                 // Call block instance
192                 $this->getBlockInstance()->initCountersGapsArray();
193         }
194
195         /**
196          * Getter for header size
197          *
198          * @return      $totalEntries   Size of file header
199          */
200         public final function getHeaderSize () {
201                 // Call block instance
202                 return $this->getBlockInstance()->getHeaderSize();
203         }
204
205         /**
206          * Setter for header size
207          *
208          * @param       $headerSize             Size of file header
209          * @return      void
210          */
211         public final function setHeaderSize ($headerSize) {
212                 // Call block instance
213                 $this->getBlockInstance()->setHeaderSize($headerSize);
214         }
215
216         /**
217          * Getter for header array
218          *
219          * @return      $totalEntries   Size of file header
220          */
221         public final function getHeader () {
222                 // Call block instance
223                 return $this->getBlockInstance()->getHeader();
224         }
225
226         /**
227          * Setter for header
228          *
229          * @param       $header         Array for a file header
230          * @return      void
231          */
232         public final function setHeader (array $header) {
233                 // Call block instance
234                 $this->getBlockInstance()->setHeader($header);
235         }
236
237         /**
238          * Updates seekPosition attribute from file to avoid to much access on file.
239          *
240          * @return      void
241          */
242         public function updateSeekPosition () {
243                 // Call block instance
244                 $this->getBlockInstance()->updateSeekPosition();
245         }
246
247         /**
248          * Getter for total entries
249          *
250          * @return      $totalEntries   Total entries in this file
251          */
252         public final function getCounter () {
253                 // Call block instance
254                 return $this->getBlockInstance()->getCounter();
255         }
256
257         /**
258          * "Getter" for file size
259          *
260          * @return      $fileSize       Size of currently loaded file
261          */
262         public function getFileSize () {
263                 // Call block instance
264                 return $this->getBlockInstance()->getFileSize();
265         }
266
267         /**
268          * Writes data at given position
269          *
270          * @param       $seekPosition   Seek position
271          * @param       $data                   Data to be written
272          * @param       $flushHeader    Whether to flush the header (default: flush)
273          * @return      void
274          */
275         public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
276                 // Call block instance
277                 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
278         }
279
280         /**
281          * Getter for seek position
282          *
283          * @return      $seekPosition   Current seek position (stored here in object)
284          */
285         public function getSeekPosition () {
286                 // Call block instance
287                 return $this->getBlockInstance()->getSeekPosition();
288         }
289 }
290
291 // [EOF]
292 ?>