5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BaseFile extends BaseFrameworkSystem {
26 * Separator for header data
28 const SEPARATOR_HEADER_DATA = 0x01;
31 * Separator header->entries
33 const SEPARATOR_HEADER_ENTRIES = 0x02;
36 * Separator hash->name
38 const SEPARATOR_HASH_NAME = 0x03;
41 * Separator entry->entry
43 const SEPARATOR_ENTRIES = 0x04;
46 * Separator type->position
48 const SEPARATOR_TYPE_POSITION = 0x05;
53 const LENGTH_COUNT = 20;
58 const LENGTH_POSITION = 20;
63 const LENGTH_NAME = 10;
66 * Maximum length of entry type
68 const LENGTH_TYPE = 20;
71 * Length of output from hash()
73 private static $hashLength = NULL;
76 * Counter for total entries
78 private $totalEntries = 0;
81 * Current seek position
83 private $seekPosition = 0;
88 private $headerSize = 0;
93 private $header = array();
96 * Seek positions for gaps ("fragmentation")
98 private $gaps = array();
101 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
103 private $damagedEntries = array();
106 * The current file we are working in
108 private $fileName = '';
113 private $backBuffer = '';
116 * Currently loaded block (will be returned by current())
118 private $currentBlock = '';
121 * Protected constructor
123 * @param $className Name of the class
126 protected function __construct ($className) {
127 // Call parent constructor
128 parent::__construct($className);
130 // Init counters and gaps array
131 $this->initCountersGapsArray();
135 * Destructor for cleaning purposes, etc
139 public final function __destruct() {
140 // Try to close a file
143 // Call the parent destructor
144 parent::__destruct();
148 * Initializes counter for valid entries, arrays for damaged entries and
149 * an array for gap seek positions. If you call this method on your own,
150 * please re-analyze the file structure. So you are better to call
151 * analyzeFile() instead of this method.
155 protected function initCountersGapsArray () {
156 // Init counter and seek position
157 $this->setCounter(0);
158 $this->setSeekPosition(0);
161 $this->gaps = array();
162 $this->damagedEntries = array();
166 * Getter for total entries
168 * @return $totalEntries Total entries in this file
170 protected final function getCounter () {
172 return $this->totalEntries;
176 * Setter for total entries
178 * @param $totalEntries Total entries in this file
181 protected final function setCounter ($counter) {
183 $this->totalEntries = $counter;
191 protected final function incrementCounter () {
193 $this->totalEntries++;
197 * Getter for header size
199 * @return $totalEntries Size of file header
201 public final function getHeaderSize () {
203 return $this->headerSize;
207 * Setter for header size
209 * @param $headerSize Size of file header
212 protected final function setHeaderSize ($headerSize) {
214 $this->headerSize = $headerSize;
218 * Getter for header array
220 * @return $totalEntries Size of file header
222 protected final function getHeade () {
224 return $this->header;
230 * @param $header Array for a file header
233 protected final function setHeader (array $header) {
235 $this->header = $header;
239 * Getter for seek position
241 * @return $seekPosition Current seek position (stored here in object)
243 protected final function getSeekPosition () {
245 return $this->seekPosition;
249 * Setter for seek position
251 * @param $seekPosition Current seek position (stored here in object)
254 protected final function setSeekPosition ($seekPosition) {
256 $this->seekPosition = $seekPosition;
260 * Updates seekPosition attribute from file to avoid to much access on file.
264 protected function updateSeekPosition () {
265 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
267 // Get key (= seek position)
268 $seekPosition = $this->getIteratorInstance()->key();
269 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
272 $this->setSeekPosition($seekPosition);
274 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
278 * Seeks to beginning of file, updates seek position in this object and
279 * flushes the header.
283 protected function rewineUpdateSeekPosition () {
284 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
286 // flushFileHeader must be callable
287 assert(is_callable(array($this, 'flushFileHeader')));
289 // Seek to beginning of file
290 $this->getIteratorInstance()->rewind();
292 // And update seek position ...
293 $this->updateSeekPosition();
295 // ... to write it back into the file
296 $this->flushFileHeader();
298 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
302 * Seeks to old position
306 protected function seekToOldPosition () {
307 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
309 // Seek to currently ("old") saved position
310 $this->getIteratorInstance()->seek($this->getSeekPosition());
312 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
316 * Checks whether the block separator has been found
318 * @param $str String to look in
319 * @return $isFound Whether the block separator has been found
321 public static function isBlockSeparatorFound ($str) {
323 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
330 * Getter for the file pointer
332 * @return $filePointer The file pointer which shall be a valid file resource
333 * @throws UnsupportedOperationException If this method is called
335 public final function getPointer () {
336 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
340 * Setter for file name
342 * @param $fileName The new file name
345 protected final function setFileName ($fileName) {
346 $fileName = (string) $fileName;
347 $this->fileName = $fileName;
351 * Getter for file name
353 * @return $fileName The current file name
355 public final function getFileName () {
356 return $this->fileName;
360 * Initializes the back-buffer by setting it to an empty string.
364 private function initBackBuffer () {
365 // Simply call the setter
366 $this->setBackBuffer('');
370 * Setter for backBuffer field
372 * @param $backBuffer Characters to "store" in back-buffer
375 private function setBackBuffer ($backBuffer) {
376 // Cast to string (so no arrays or objects)
377 $backBuffer = (string) $backBuffer;
380 $this->backBuffer = $backBuffer;
384 * Getter for backBuffer field
386 * @return $backBuffer Characters "stored" in back-buffer
388 private function getBackBuffer () {
389 return $this->backBuffer;
393 * Setter for currentBlock field
395 * @param $currentBlock Characters to set a currently loaded block
398 private function setCurrentBlock ($currentBlock) {
399 // Cast to string (so no arrays or objects)
400 $currentBlock = (string) $currentBlock;
403 $this->currentBlock = $currentBlock;
407 * Gets currently read data
409 * @return $current Currently read data
411 public function getCurrentBlock () {
413 return $this->currentBlock;
417 * Initializes this file class
419 * @param $fileName Name of this abstract file
422 protected function initFile ($fileName) {
423 // Get a file i/o pointer instance
424 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
426 // ... and set it here
427 $this->setPointerInstance($pointerInstance);
431 * Writes data at given position
433 * @param $seekPosition Seek position
434 * @param $data Data to be written
435 * @param $flushHeader Whether to flush the header (default: flush)
438 protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
439 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
441 // Write data at given position
442 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
444 // Update seek position
445 $this->updateSeekPosition();
448 if ($flushHeader === TRUE) {
450 $this->flushFileHeader();
452 // Seek to old position
453 $this->seekToOldPosition();
456 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
460 * Checks whether the file header is initialized
462 * @return $isInitialized Whether the file header is initialized
464 public function isFileHeaderInitialized () {
465 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
467 // Default is not initialized
468 $isInitialized = FALSE;
470 // Is the file initialized?
471 if ($this->isFileInitialized()) {
472 // Some bytes has been written, so rewind to start of it.
473 $rewindStatus = $this->rewind();
474 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
476 // Is the rewind() call successfull?
477 if ($rewindStatus != 1) {
478 // Something bad happened
479 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
483 $this->readFileHeader();
485 // The above method does already check the header
486 $isInitialized = TRUE;
490 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
491 return $isInitialized;
495 * Checks whether the assigned file has been initialized
497 * @return $isInitialized Whether the file's size is zero
499 public function isFileInitialized () {
500 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
502 // Get it from iterator which holds the pointer instance. If FALSE is returned
503 $fileSize = $this->size();
504 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
507 * The returned file size should not be FALSE or NULL as this means
508 * that the pointer class does not work correctly.
510 assert(is_int($fileSize));
512 // Is more than 0 returned?
513 $isInitialized = ($fileSize > 0);
516 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
517 return $isInitialized;
521 * Creates the assigned file
525 public function createFileHeader () {
526 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
528 // The file's header should not be initialized here
529 assert(!$this->isFileHeaderInitialized());
531 // Simple flush file header which will create it.
532 $this->flushFileHeader();
534 // Rewind seek position (to beginning of file) and update/flush file header
535 $this->rewineUpdateSeekPosition();
537 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
541 * Pre-allocates file (if enabled) with some space for later faster write access.
543 * @param $type Type of the file
546 public function preAllocateFile ($type) {
547 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
550 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
552 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
554 // Don't continue here.
559 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
561 // Calculate minimum length for one entry
562 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
563 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
565 // Calulcate seek position
566 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
567 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
569 // Now simply write a NUL there. This will pre-allocate the file.
570 $this->writeData($seekPosition, chr(0));
572 // Rewind seek position (to beginning of file) and update/flush file header
573 $this->rewineUpdateSeekPosition();
575 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
579 * Close a file source and set it's instance to null and the file name
585 public function closeFile () {
586 $this->partialStub('Unfinished method.');
589 $this->setFileName('');
593 * Determines seek position
595 * @return $seekPosition Current seek position
597 public function determineSeekPosition () {
598 // Call pointer instance
599 return $this->getPointerInstance()->determineSeekPosition();
603 * Seek to given offset (default) or other possibilities as fseek() gives.
605 * @param $offset Offset to seek to (or used as "base" for other seeks)
606 * @param $whence Added to offset (default: only use offset to seek to)
607 * @return $status Status of file seek: 0 = success, -1 = failed
609 public function seek ($offset, $whence = SEEK_SET) {
610 // Call pointer instance
611 return $this->getPointerInstance()->seek($offset, $whence);
617 * @return $size Size (in bytes) of file
618 * @todo Handle seekStatus
620 public function size () {
621 // Call pointer instance
622 return $this->getPointerInstance()->size();
626 * Read data a file pointer
628 * @return mixed The result of fread()
629 * @throws NullPointerException If the file pointer instance
630 * is not set by setPointer()
631 * @throws InvalidResourceException If there is being set
633 public function readFromFile () {
634 // Call pointer instance
635 return $this->getPointerInstance()->readFromFile();
639 * Reads given amount of bytes from file.
641 * @param $bytes Amount of bytes to read
642 * @return $data Data read from file
644 public function read ($bytes) {
645 // Call pointer instance
646 return $this->getPointerInstance()->read($bytes);
650 * Write data to a file pointer
652 * @param $dataStream The data stream we shall write to the file
653 * @return mixed Number of writes bytes or FALSE on error
654 * @throws NullPointerException If the file pointer instance
655 * is not set by setPointer()
656 * @throws InvalidResourceException If there is being set
657 * an invalid file resource
659 public function writeToFile ($dataStream) {
660 // Call pointer instance
661 return $this->getPointerInstance()->writeToFile($dataStream);
665 * Rewinds to the beginning of the file
667 * @return $status Status of this operation
669 public function rewind () {
670 // Call pointer instance
671 return $this->getPointerInstance()->rewind();
675 * Determines whether the EOF has been reached
677 * @return $isEndOfFileReached Whether the EOF has been reached
679 public final function isEndOfFileReached () {
680 // Call pointer instance
681 return $this->getPointerInstance()->isEndOfFileReached();
685 * Analyzes entries in index file. This will count all found (and valid)
686 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
687 * only gaps are found, the file is considered as "virgin" (no entries).
691 public function analyzeFile () {
692 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
694 // Make sure the file is initialized
695 assert($this->isFileInitialized());
697 // Init counters and gaps array
698 $this->initCountersGapsArray();
700 // Output message (as this may take some time)
701 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
703 // First rewind to the begining
706 // Then try to load all entries
707 while ($this->valid()) {
712 $current = $this->getCurrentBlock();
715 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
718 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
722 * Advances to next "block" of bytes
725 * @todo This method will load large but empty files in a whole
727 public function next () {
728 // Is there nothing to read?
729 if (!$this->valid()) {
734 // Make sure the block instance is set
735 assert($this->getBlockInstance() instanceof CalculatableBlock);
737 // First calculate minimum block length
738 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
740 // Short be more than zero!
743 // Wait until a entry/block separator has been found
744 $data = $this->getBackBuffer();
745 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
746 // Then read the block
747 $data .= $this->read($length);
748 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data));
752 if ($this->isEndOfFileReached()) {
753 // Set whole data as current block
754 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
755 $this->setCurrentBlock($data);
757 // Then abort here silently
758 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
763 * Init back-buffer which is the data that has been found beyond the
766 $this->initBackBuffer();
769 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
771 // This array must contain two elements
772 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
773 assert(count($dataArray) == 2);
775 // Left part is the actual block, right one the back-buffer data
776 $this->setCurrentBlock($dataArray[0]);
777 $this->setBackBuffer($dataArray[1]);
781 * Checks wether the current entry is valid (not at the end of the file).
782 * This method will return TRUE if an emptied (nulled) entry has been found.
784 * @return $isValid Whether the next entry is valid
786 public function valid () {
787 // Make sure the block instance is set
788 assert($this->getBlockInstance() instanceof Block);
790 // First calculate minimum block length
791 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
793 // Short be more than zero!
796 // Get current seek position
797 $seekPosition = $this->key();
799 // Then try to read it
800 $data = $this->read($length);
802 // If some bytes could be read, all is fine
803 $isValid = ((is_string($data)) && (strlen($data) > 0));
806 $headerSize = $this->getBlockInstance()->getHeaderSize();
808 // Is the seek position at or beyond the header?
809 if ($seekPosition >= $headerSize) {
810 // Seek back to old position
811 $this->seek($seekPosition);
813 // Seek directly behind the header
814 $this->seek($headerSize);
822 * Gets current seek position ("key").
824 * @return $key Current key in iteration
826 public function key () {
827 // Call pointer instance
828 return $this->getPointerInstance()->determineSeekPosition();
832 * Reads the file header
836 public function readFileHeader () {
837 // Make sure the block instance is set
838 assert($this->getBlockInstance() instanceof Block);
840 // Call block instance
841 $this->getBlockInstance()->readFileHeader();
845 * Flushes the file header
849 public function flushFileHeader () {
850 // Make sure the block instance is set
851 assert($this->getBlockInstance() instanceof Block);
853 // Call block instance
854 $this->getBlockInstance()->flushFileHeader();