]> git.mxchange.org Git - core.git/blob - framework/main/classes/iterator/file/class_FileIterator.php
6e0f1957772cde1b7a489014027190fd55536d8a
[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
8 /**
9  * A file iterator
10  *
11  * @author              Roland Haeder <webmaster@ship-simu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
30 class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
31         /**
32          * Protected constructor
33          *
34          * @return      void
35          */
36         protected function __construct () {
37                 // Call parent constructor
38                 parent::__construct(__CLASS__);
39         }
40
41         /**
42          * Creates an instance of this class
43          *
44          * @param       $pointerInstance        An instance of a Block class
45          * @return      $iteratorInstance       An instance of a Iterator class
46          */
47         public final static function createFileIterator (Block $blockInstance) {
48                 // Get new instance
49                 $iteratorInstance = new FileIterator();
50
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);
54
55                 // Return the prepared instance
56                 return $iteratorInstance;
57         }
58
59         /**
60          * Gets currently read data
61          *
62          * @return      $current        Currently read data
63          */
64         public function current () {
65                 // Call block instance
66                 return $this->getBlockInstance()->current();
67         }
68
69         /**
70          * Gets current seek position ("key").
71          *
72          * @return      $key    Current key in iteration
73          */
74         public function key () {
75                 // Return it
76                 return $this->getBlockInstance()->determineSeekPosition();
77         }
78
79         /**
80          * Advances to next "block" of bytes
81          *
82          * @return      void
83          */
84         public function next () {
85                 // Call block instance
86                 $this->getBlockInstance()->next();
87         }
88
89         /**
90          * Rewinds to the beginning of the file
91          *
92          * @return      $status         Status of this operation
93          */
94         public function rewind () {
95                 // Call block instance
96                 return $this->getBlockInstance()->rewind();
97         }
98
99         /**
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.
102          *
103          * @return      $isValid        Whether the next entry is valid
104          */
105         public function valid () {
106                 // Call block instance
107                 return $this->getBlockInstance()->valid();
108         }
109
110         /**
111          * Seeks to given position
112          *
113          * @param       $seekPosition   Seek position in file
114          * @return      $status                 Status of this operation
115          */
116         public function seek ($seekPosition) {
117                 // Call block instance
118                 return $this->getBlockInstance()->seek($seekPosition);
119         }
120
121         /**
122          * Size of file stack
123          *
124          * @return      $size   Size (in bytes) of file
125          */
126         public function size () {
127                 // Call the block object
128                 $size = $this->getBlockInstance()->size();
129
130                 // Return result
131                 return $size;
132         }
133
134         /**
135          * Reads given amount of bytes from file.
136          *
137          * @param       $bytes  Amount of bytes to read
138          * @return      $data   Data read from file
139          */
140         public function read ($bytes = NULL) {
141                 // Call block instance
142                 return $this->getBlockInstance()->read($bytes);
143         }
144
145         /**
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).
149          *
150          * @return      void
151          */
152         public function analyzeFile () {
153                 // Just call the block instance
154                 $this->getBlockInstance()->analyzeFile();
155         }
156
157         /**
158          * Checks whether the file header is initialized
159          *
160          * @return      $isInitialized  Whether the file header is initialized
161          */
162         public function isFileHeaderInitialized () {
163                 // Just call the block instance
164                 return $this->getBlockInstance()->isFileHeaderInitialized();
165         }
166
167         /**
168          * Creates the assigned file
169          *
170          * @return      void
171          */
172         public function createFileHeader () {
173                 // Just call the block instance
174                 $this->getBlockInstance()->createFileHeader();
175         }
176
177         /**
178          * Pre-allocates file (if enabled) with some space for later faster write access.
179          *
180          * @param       $type   Type of the file
181          * @return      void
182          */
183         public function preAllocateFile ($type) {
184                 // Just call the block instance
185                 $this->getBlockInstance()->preAllocateFile($type);
186         }
187
188         /**
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.
193          *
194          * @return      void
195          */
196         public function initCountersGapsArray () {
197                 // Call block instance
198                 $this->getBlockInstance()->initCountersGapsArray();
199         }
200
201         /**
202          * Getter for header size
203          *
204          * @return      $totalEntries   Size of file header
205          */
206         public final function getHeaderSize () {
207                 // Call block instance
208                 return $this->getBlockInstance()->getHeaderSize();
209         }
210
211         /**
212          * Setter for header size
213          *
214          * @param       $headerSize             Size of file header
215          * @return      void
216          */
217         public final function setHeaderSize ($headerSize) {
218                 // Call block instance
219                 $this->getBlockInstance()->setHeaderSize($headerSize);
220         }
221
222         /**
223          * Getter for header array
224          *
225          * @return      $totalEntries   Size of file header
226          */
227         public final function getHeader () {
228                 // Call block instance
229                 return $this->getBlockInstance()->getHeader();
230         }
231
232         /**
233          * Setter for header
234          *
235          * @param       $header         Array for a file header
236          * @return      void
237          */
238         public final function setHeader (array $header) {
239                 // Call block instance
240                 $this->getBlockInstance()->setHeader($header);
241         }
242
243         /**
244          * Updates seekPosition attribute from file to avoid to much access on file.
245          *
246          * @return      void
247          */
248         public function updateSeekPosition () {
249                 // Call block instance
250                 $this->getBlockInstance()->updateSeekPosition();
251         }
252
253         /**
254          * Getter for total entries
255          *
256          * @return      $totalEntries   Total entries in this file
257          */
258         public final function getCounter () {
259                 // Call block instance
260                 return $this->getBlockInstance()->getCounter();
261         }
262
263         /**
264          * "Getter" for file size
265          *
266          * @return      $fileSize       Size of currently loaded file
267          */
268         public function getFileSize () {
269                 // Call block instance
270                 return $this->getBlockInstance()->getFileSize();
271         }
272
273         /**
274          * Writes data at given position
275          *
276          * @param       $seekPosition   Seek position
277          * @param       $data                   Data to be written
278          * @param       $flushHeader    Whether to flush the header (default: flush)
279          * @return      void
280          */
281         public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
282                 // Call block instance
283                 $this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
284         }
285
286         /**
287          * Getter for seek position
288          *
289          * @return      $seekPosition   Current seek position (stored here in object)
290          */
291         public function getSeekPosition () {
292                 // Call block instance
293                 return $this->getBlockInstance()->getSeekPosition();
294         }
295
296         /**
297          * Writes given value to the file and returns a hash and gap position for it
298          *
299          * @param       $groupId        Group identifier
300          * @param       $value          Value to be added to the stack
301          * @return      $data           Hash and gap position
302          */
303         public function writeValueToFile ($groupId, $value) {
304                 // Call block instance
305                 return $this->getBlockInstance()->writeValueToFile($groupId, $value);
306         }
307
308         /**
309          * Writes given raw data to the file and returns a gap position and length
310          *
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
315          */
316         public function writeDataToFreeGap ($groupId, $hash, $encoded) {
317                 // Call block instance
318                 return $this->getBlockInstance()->writeDataToFreeGap($groupId, $hash, $encoded);
319         }
320
321         /**
322          * Searches for next suitable gap the given length of data can fit in
323          * including padding bytes.
324          *
325          * @param       $length                 Length of raw data
326          * @return      $seekPosition   Found next gap's seek position
327          */
328         public function searchNextGap ($length) {
329                 // Call block instance
330                 print $this->getBlockInstance()->__toString() . PHP_EOL;
331                 return $this->getBlockInstance()->searchNextGap($length);
332         }
333
334 }