Splitted 'binary specific' methods/attributes from generic file class and created...
[core.git] / inc / classes / main / file_directories / binary / class_BaseBinaryFile.php
1 <?php
2 /**
3  * A general binary file class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 BaseBinaryFile extends BaseFile {
25         /**
26          * Separator for header data
27          */
28         const SEPARATOR_HEADER_DATA = 0x01;
29
30         /**
31          * Separator header->entries
32          */
33         const SEPARATOR_HEADER_ENTRIES = 0x02;
34
35         /**
36          * Separator hash->name
37          */
38         const SEPARATOR_HASH_NAME = 0x03;
39
40         /**
41          * Separator entry->entry
42          */
43         const SEPARATOR_ENTRIES = 0x04;
44
45         /**
46          * Separator type->position
47          */
48         const SEPARATOR_TYPE_POSITION = 0x05;
49
50         /**
51          * Length of count
52          */
53         const LENGTH_COUNT = 20;
54
55         /**
56          * Length of position
57          */
58         const LENGTH_POSITION = 20;
59
60         /**
61          * Length of name
62          */
63         const LENGTH_NAME = 10;
64
65         /**
66          * Maximum length of entry type
67          */
68         const LENGTH_TYPE = 20;
69
70         //***** Array elements for 'gaps' array *****
71
72         /**
73          * Start of gap
74          */
75         const GAPS_INDEX_START = 'start';
76
77         /**
78          * End of gap
79          */
80         const GAPS_INDEX_END = 'end';
81
82         /**
83          * Current seek position
84          */
85         private $seekPosition = 0;
86
87         /**
88          * Size of header
89          */
90         private $headerSize = 0;
91
92         /**
93          * File header
94          */
95         private $header = array();
96
97         /**
98          * Seek positions for gaps ("fragmentation")
99          */
100         private $gaps = array();
101
102         /**
103          * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
104          */
105         private $damagedEntries = array();
106
107         /**
108          * Back-buffer
109          */
110         private $backBuffer = '';
111
112         /**
113          * Currently loaded block (will be returned by current())
114          */
115         private $currentBlock = '';
116
117         /**
118          * Protected constructor
119          *
120          * @param       $className      Name of the class
121          * @return      void
122          */
123         protected function __construct ($className) {
124                 // Call parent constructor
125                 parent::__construct($className);
126
127                 // Init counters and gaps array
128                 $this->initCountersGapsArray();
129         }
130
131         /**
132          * Checks whether the abstracted file only contains gaps by counting all
133          * gaps' bytes together and compare it to total length.
134          *
135          * @return      $isGapsOnly             Whether the abstracted file only contains gaps
136          */
137         private function isFileOnlyGaps () {
138                 // First/last gap found?
139                 /* Only for debugging
140                 if (isset($this->gaps[0])) {
141                         // Output first and last gap
142                         self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', __METHOD__, __LINE__, print_r($this->gaps[0], TRUE), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], TRUE)));
143                 } // END - if
144                 */
145
146                 // Now count every gap
147                 $gapsSize = 0;
148                 foreach ($this->gaps as $gap) {
149                         // Calculate size of found gap: end-start including both
150                         $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
151                 } // END - if
152
153                 // Debug output
154                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize()));
155
156                 // Total gap size + header size must be same as file size
157                 $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
158
159                 // Return status
160                 return $isGapsOnly;
161         }
162
163         /**
164          * Initializes counter for valid entries, arrays for damaged entries and
165          * an array for gap seek positions. If you call this method on your own,
166          * please re-analyze the file structure. So you are better to call
167          * analyzeFile() instead of this method.
168          *
169          * @return      void
170          */
171         public function initCountersGapsArray () {
172                 // Init counter and seek position
173                 $this->setCounter(0);
174                 $this->setSeekPosition(0);
175
176                 // Init arrays
177                 $this->gaps = array();
178                 $this->damagedEntries = array();
179         }
180
181         /**
182          * Getter for header size
183          *
184          * @return      $totalEntries   Size of file header
185          */
186         public final function getHeaderSize () {
187                 // Get it
188                 return $this->headerSize;
189         }
190
191         /**
192          * Setter for header size
193          *
194          * @param       $headerSize             Size of file header
195          * @return      void
196          */
197         public final function setHeaderSize ($headerSize) {
198                 // Set it
199                 $this->headerSize = $headerSize;
200         }
201
202         /**
203          * Getter for header array
204          *
205          * @return      $totalEntries   Size of file header
206          */
207         public final function getHeader () {
208                 // Get it
209                 return $this->header;
210         }
211
212         /**
213          * Setter for header
214          *
215          * @param       $header         Array for a file header
216          * @return      void
217          */
218         public final function setHeader (array $header) {
219                 // Set it
220                 $this->header = $header;
221         }
222
223         /**
224          * Getter for seek position
225          *
226          * @return      $seekPosition   Current seek position (stored here in object)
227          */
228         protected final function getSeekPosition () {
229                 // Get it
230                 return $this->seekPosition;
231         }
232
233         /**
234          * Setter for seek position
235          *
236          * @param       $seekPosition   Current seek position (stored here in object)
237          * @return      void
238          */
239         protected final function setSeekPosition ($seekPosition) {
240                 // And set it
241                 $this->seekPosition = $seekPosition;
242         }
243
244         /**
245          * Updates seekPosition attribute from file to avoid to much access on file.
246          *
247          * @return      void
248          */
249         public function updateSeekPosition () {
250                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
251
252                 // Get key (= seek position)
253                 $seekPosition = $this->key();
254                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
255
256                 // And set it here
257                 $this->setSeekPosition($seekPosition);
258
259                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
260         }
261
262         /**
263          * Seeks to beginning of file, updates seek position in this object and
264          * flushes the header.
265          *
266          * @return      void
267          */
268         protected function rewindUpdateSeekPosition () {
269                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
270
271                 // flushFileHeader must be callable
272                 assert(is_callable(array($this, 'flushFileHeader')));
273
274                 // Seek to beginning of file
275                 $this->rewind();
276
277                 // And update seek position ...
278                 $this->updateSeekPosition();
279
280                 // ... to write it back into the file
281                 $this->flushFileHeader();
282
283                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
284         }
285
286         /**
287          * Seeks to old position
288          *
289          * @return      void
290          */
291         protected function seekToOldPosition () {
292                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
293
294                 // Seek to currently ("old") saved position
295                 $this->seek($this->getSeekPosition());
296
297                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
298         }
299
300         /**
301          * Checks whether the block separator has been found
302          *
303          * @param       $str            String to look in
304          * @return      $isFound        Whether the block separator has been found
305          */
306         public static function isBlockSeparatorFound ($str) {
307                 // Determine it
308                 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
309
310                 // Return result
311                 return $isFound;
312         }
313
314         /**
315          * Getter for the file pointer
316          *
317          * @return      $filePointer    The file pointer which shall be a valid file resource
318          * @throws      UnsupportedOperationException   If this method is called
319          */
320         public final function getPointer () {
321                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
322         }
323
324         /**
325          * Initializes the back-buffer by setting it to an empty string.
326          *
327          * @return      void
328          */
329         private function initBackBuffer () {
330                 // Simply call the setter
331                 $this->setBackBuffer('');
332         }
333
334         /**
335          * Setter for backBuffer field
336          *
337          * @param       $backBuffer             Characters to "store" in back-buffer
338          * @return      void
339          */
340         private function setBackBuffer ($backBuffer) {
341                 // Cast to string (so no arrays or objects)
342                 $backBuffer = (string) $backBuffer;
343
344                 // ... and set it
345                 $this->backBuffer = $backBuffer;
346         }
347
348         /**
349          * Getter for backBuffer field
350          *
351          * @return      $backBuffer             Characters "stored" in back-buffer
352          */
353         private function getBackBuffer () {
354                 return $this->backBuffer;
355         }
356
357         /**
358          * Setter for currentBlock field
359          *
360          * @param       $currentBlock           Characters to set a currently loaded block
361          * @return      void
362          */
363         private function setCurrentBlock ($currentBlock) {
364                 // Cast to string (so no arrays or objects)
365                 $currentBlock = (string) $currentBlock;
366
367                 // ... and set it
368                 $this->currentBlock = $currentBlock;
369         }
370
371         /**
372          * Gets currently read data
373          *
374          * @return      $current        Currently read data
375          */
376         public function getCurrentBlock () {
377                 // Return it
378                 return $this->currentBlock;
379         }
380
381         /**
382          * Initializes this file class
383          *
384          * @param       $fileName       Name of this abstract file
385          * @return      void
386          */
387         protected function initFile ($fileName) {
388                 // Get a file i/o pointer instance
389                 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
390
391                 // ... and set it here
392                 $this->setPointerInstance($pointerInstance);
393         }
394
395         /**
396          * Writes data at given position
397          *
398          * @param       $seekPosition   Seek position
399          * @param       $data                   Data to be written
400          * @param       $flushHeader    Whether to flush the header (default: flush)
401          * @return      void
402          */
403         protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
404                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
405
406                 // Write data at given position
407                 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
408
409                 // Update seek position
410                 $this->updateSeekPosition();
411
412                 // Flush the header?
413                 if ($flushHeader === TRUE) {
414                         // Flush header
415                         $this->flushFileHeader();
416
417                         // Seek to old position
418                         $this->seekToOldPosition();
419                 } // END - if
420
421                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
422         }
423
424         /**
425          * Marks the currently loaded block as empty (with length of the block)
426          *
427          * @param       $length         Length of the block
428          * @return      void
429          */
430         protected function markCurrentBlockAsEmpty ($length) {
431                 // Get current seek position
432                 $currentPosition = $this->key();
433
434                 // Now add it as gap entry
435                 array_push($this->gaps, array(
436                         self::GAPS_INDEX_START  => ($currentPosition - $length),
437                         self::GAPS_INDEX_END    => $currentPosition,
438                 ));
439         }
440
441         /**
442          * Checks whether the file header is initialized
443          *
444          * @return      $isInitialized  Whether the file header is initialized
445          */
446         public function isFileHeaderInitialized () {
447                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
448
449                 // Default is not initialized
450                 $isInitialized = FALSE;
451
452                 // Is the file initialized?
453                 if ($this->isFileInitialized()) {
454                         // Some bytes has been written, so rewind to start of it.
455                         $rewindStatus = $this->rewind();
456                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
457
458                         // Is the rewind() call successfull?
459                         if ($rewindStatus != 1) {
460                                 // Something bad happened
461                                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
462                         } // END - if
463
464                         // Read file header
465                         $this->readFileHeader();
466
467                         // The above method does already check the header
468                         $isInitialized = TRUE;
469                 } // END - if
470
471                 // Return result
472                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
473                 return $isInitialized;
474         }
475
476         /**
477          * Checks whether the assigned file has been initialized
478          *
479          * @return      $isInitialized          Whether the file's size is zero
480          */
481         public function isFileInitialized () {
482                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
483
484                 // Get it from iterator which holds the pointer instance. If FALSE is returned
485                 $fileSize = $this->size();
486                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
487
488                 /*
489                  * The returned file size should not be FALSE or NULL as this means
490                  * that the pointer class does not work correctly.
491                  */
492                 assert(is_int($fileSize));
493
494                 // Is more than 0 returned?
495                 $isInitialized = ($fileSize > 0);
496
497                 // Return result
498                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
499                 return $isInitialized;
500         }
501
502         /**
503          * Creates the assigned file
504          *
505          * @return      void
506          */
507         public function createFileHeader () {
508                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
509
510                 // The file's header should not be initialized here
511                 assert(!$this->isFileHeaderInitialized());
512
513                 // Simple flush file header which will create it.
514                 $this->flushFileHeader();
515
516                 // Rewind seek position (to beginning of file) and update/flush file header
517                 $this->rewindUpdateSeekPosition();
518
519                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
520         }
521
522         /**
523          * Pre-allocates file (if enabled) with some space for later faster write access.
524          *
525          * @param       $type   Type of the file
526          * @return      void
527          */
528         public function preAllocateFile ($type) {
529                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
530
531                 // Is it enabled?
532                 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
533                         // Not enabled
534                         self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
535
536                         // Don't continue here.
537                         return;
538                 } // END - if
539
540                 // Message to user
541                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
542
543                 // Calculate minimum length for one entry
544                 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
545                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
546
547                 // Calulcate seek position
548                 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
549                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
550
551                 // Now simply write a NUL there. This will pre-allocate the file.
552                 $this->writeData($seekPosition, chr(0));
553
554                 // Rewind seek position (to beginning of file) and update/flush file header
555                 $this->rewindUpdateSeekPosition();
556
557                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
558         }
559
560         /**
561          * Determines seek position
562          *
563          * @return      $seekPosition   Current seek position
564          */
565         public function determineSeekPosition () {
566                 // Call pointer instance
567                 return $this->getPointerInstance()->determineSeekPosition();
568         }
569
570         /**
571          * Seek to given offset (default) or other possibilities as fseek() gives.
572          *
573          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
574          * @param       $whence         Added to offset (default: only use offset to seek to)
575          * @return      $status         Status of file seek: 0 = success, -1 = failed
576          */
577         public function seek ($offset, $whence = SEEK_SET) {
578                 // Call pointer instance
579                 return $this->getPointerInstance()->seek($offset, $whence);
580         }
581
582         /**
583          * Reads given amount of bytes from file.
584          *
585          * @param       $bytes  Amount of bytes to read
586          * @return      $data   Data read from file
587          */
588         public function read ($bytes) {
589                 // Call pointer instance
590                 return $this->getPointerInstance()->read($bytes);
591         }
592
593         /**
594          * Rewinds to the beginning of the file
595          *
596          * @return      $status         Status of this operation
597          */
598         public function rewind () {
599                 // Call pointer instance
600                 return $this->getPointerInstance()->rewind();
601         }
602
603         /**
604          * Analyzes entries in index file. This will count all found (and valid)
605          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
606          * only gaps are found, the file is considered as "virgin" (no entries).
607          *
608          * @return      void
609          */
610         public function analyzeFile () {
611                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
612
613                 // Make sure the file is initialized
614                 assert($this->isFileInitialized());
615
616                 // Init counters and gaps array
617                 $this->initCountersGapsArray();
618
619                 // Output message (as this may take some time)
620                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
621
622                 // First rewind to the begining
623                 $this->rewind();
624
625                 // Then try to load all entries
626                 while ($this->valid()) {
627                         // Go to next entry
628                         $this->next();
629
630                         // Get current entry
631                         $current = $this->getCurrentBlock();
632
633                         /*
634                          * If the block is empty, maybe the whole file is? This could mean
635                          * that the file has been pre-allocated.
636                          */
637                         if (empty($current)) {
638                                 // Then skip this part
639                                 continue;
640                         } // END - if
641
642                         // Debug message
643                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
644                 } // END - while
645
646                 // If the last read block is empty, check gaps
647                 if (empty($current)) {
648                         // Output message
649                         self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps)));
650
651                         // Check gaps, if the whole file is empty.
652                         if ($this->isFileOnlyGaps()) {
653                                 // Only gaps, so don't continue here.
654                                 return;
655                         } // END - if
656
657                         /*
658                          * The above call has calculated a total size of all gaps. If the
659                          * percentage of gaps passes a "soft" limit and last
660                          * defragmentation is to far in the past, or if a "hard" limit has
661                          * reached, run defragmentation.
662                          */
663                         if ($this->isDefragmentationNeeded()) {
664                                 // Run "defragmentation"
665                                 $this->doRunDefragmentation();
666                         } // END - if
667                 } // END - if
668                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
669         }
670
671         /**
672          * Advances to next "block" of bytes
673          *
674          * @return      void
675          */
676         public function next () {
677                 // Is there nothing to read?
678                 if (!$this->valid()) {
679                         // Nothing to read
680                         return;
681                 } // END - if
682
683                 // Debug message
684                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%s', __FUNCTION__, __LINE__, $this->key()));
685
686                 // Make sure the block instance is set
687                 assert($this->getBlockInstance() instanceof CalculatableBlock);
688
689                 // First calculate minimum block length
690                 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
691                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length));
692
693                 // Short be more than zero!
694                 assert($length > 0);
695
696                 // Read possibly back-buffered bytes from previous call of next().
697                 $data = $this->getBackBuffer();
698
699                 /*
700                  * Read until a entry/block separator has been found. The next read
701                  * "block" may not fit, so this loop will continue until the EOB or EOF
702                  * has been reached whatever comes first.
703                  */
704                 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
705                         // Then read the next possible block
706                         $block = $this->read($length);
707
708                         // Debug message
709                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%s,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
710
711                         // Is it all empty?
712                         if (strlen(trim($block)) == 0) {
713                                 // Mark this block as empty
714                                 $this->markCurrentBlockAsEmpty(strlen($block));
715
716                                 // Skip to next block
717                                 continue;
718                         } // END - if
719
720                         // At this block then
721                         $data .= $block;
722
723                         // A debug message
724                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data)));
725                 } // END - while
726
727                 // EOF reached?
728                 if ($this->isEndOfFileReached()) {
729                         // Set whole data as current read block
730                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
731                         $this->setCurrentBlock($data);
732
733                         // Then abort here silently
734                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
735                         return;
736                 } // END - if
737
738                 /*
739                  * Init back-buffer which is the data that has been found beyond the
740                  * separator.
741                  */
742                 $this->initBackBuffer();
743
744                 // Separate data
745                 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
746
747                 // This array must contain two elements
748                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
749                 assert(count($dataArray) == 2);
750
751                 // Left part is the actual block, right one the back-buffer data
752                 $this->setCurrentBlock($dataArray[0]);
753                 $this->setBackBuffer($dataArray[1]);
754         }
755
756         /**
757          * Checks wether the current entry is valid (not at the end of the file).
758          * This method will return TRUE if an emptied (nulled) entry has been found.
759          *
760          * @return      $isValid        Whether the next entry is valid
761          */
762         public function valid () {
763                 // Make sure the block instance is set
764                 assert($this->getBlockInstance() instanceof Block);
765
766                 // First calculate minimum block length
767                 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
768
769                 // Short be more than zero!
770                 assert($length > 0);
771
772                 // Get current seek position
773                 $seekPosition = $this->key();
774
775                 // Then try to read it
776                 $data = $this->read($length);
777
778                 // If some bytes could be read, all is fine
779                 $isValid = ((is_string($data)) && (strlen($data) > 0));
780
781                 // Get header size
782                 $headerSize = $this->getHeaderSize();
783
784                 // Is the seek position at or beyond the header?
785                 if ($seekPosition >= $headerSize) {
786                         // Seek back to old position
787                         $this->seek($seekPosition);
788                 } else {
789                         // Seek directly behind the header
790                         $this->seek($headerSize);
791                 }
792
793                 // Return result
794                 return $isValid;
795         }
796
797         /**
798          * Gets current seek position ("key").
799          *
800          * @return      $key    Current key in iteration
801          */
802         public function key () {
803                 // Call pointer instance
804                 return $this->getPointerInstance()->determineSeekPosition();
805         }
806
807         /**
808          * Reads the file header
809          *
810          * @return      void
811          */
812         public function readFileHeader () {
813                 // Make sure the block instance is set
814                 assert($this->getBlockInstance() instanceof Block);
815
816                 // Call block instance
817                 $this->getBlockInstance()->readFileHeader();
818         }
819
820         /**
821          * Flushes the file header
822          *
823          * @return      void
824          */
825         public function flushFileHeader () {
826                 // Make sure the block instance is set
827                 assert($this->getBlockInstance() instanceof Block);
828
829                 // Call block instance
830                 $this->getBlockInstance()->flushFileHeader();
831         }
832 }
833
834 // [EOF]
835 ?>