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