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;
70 //***** Array elements for 'gaps' array *****
75 const GAPS_INDEX_START = 'start';
80 const GAPS_INDEX_END = 'end';
83 * Counter for total entries
85 private $totalEntries = 0;
88 * Current seek position
90 private $seekPosition = 0;
95 private $headerSize = 0;
100 private $header = array();
103 * Seek positions for gaps ("fragmentation")
105 private $gaps = array();
108 * Seek positions for damaged entries (e.g. mismatching hash sum, ...)
110 private $damagedEntries = array();
113 * The current file we are working in
115 private $fileName = '';
120 private $backBuffer = '';
123 * Currently loaded block (will be returned by current())
125 private $currentBlock = '';
128 * Protected constructor
130 * @param $className Name of the class
133 protected function __construct ($className) {
134 // Call parent constructor
135 parent::__construct($className);
137 // Init counters and gaps array
138 $this->initCountersGapsArray();
142 * Destructor for cleaning purposes, etc
146 public final function __destruct() {
147 // Try to close a file
150 // Call the parent destructor
151 parent::__destruct();
155 * Initializes counter for valid entries, arrays for damaged entries and
156 * an array for gap seek positions. If you call this method on your own,
157 * please re-analyze the file structure. So you are better to call
158 * analyzeFile() instead of this method.
162 public function initCountersGapsArray () {
163 // Init counter and seek position
164 $this->setCounter(0);
165 $this->setSeekPosition(0);
168 $this->gaps = array();
169 $this->damagedEntries = array();
173 * Getter for total entries
175 * @return $totalEntries Total entries in this file
177 protected final function getCounter () {
179 return $this->totalEntries;
183 * Setter for total entries
185 * @param $totalEntries Total entries in this file
188 protected final function setCounter ($counter) {
190 $this->totalEntries = $counter;
198 protected final function incrementCounter () {
200 $this->totalEntries++;
204 * Getter for header size
206 * @return $totalEntries Size of file header
208 public final function getHeaderSize () {
210 return $this->headerSize;
214 * Setter for header size
216 * @param $headerSize Size of file header
219 public final function setHeaderSize ($headerSize) {
221 $this->headerSize = $headerSize;
225 * Getter for header array
227 * @return $totalEntries Size of file header
229 public final function getHeader () {
231 return $this->header;
237 * @param $header Array for a file header
240 public final function setHeader (array $header) {
242 $this->header = $header;
246 * Getter for seek position
248 * @return $seekPosition Current seek position (stored here in object)
250 protected final function getSeekPosition () {
252 return $this->seekPosition;
256 * Setter for seek position
258 * @param $seekPosition Current seek position (stored here in object)
261 protected final function setSeekPosition ($seekPosition) {
263 $this->seekPosition = $seekPosition;
267 * Updates seekPosition attribute from file to avoid to much access on file.
271 public function updateSeekPosition () {
272 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
274 // Get key (= seek position)
275 $seekPosition = $this->key();
276 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
279 $this->setSeekPosition($seekPosition);
281 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
285 * Seeks to beginning of file, updates seek position in this object and
286 * flushes the header.
290 protected function rewindUpdateSeekPosition () {
291 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
293 // flushFileHeader must be callable
294 assert(is_callable(array($this, 'flushFileHeader')));
296 // Seek to beginning of file
299 // And update seek position ...
300 $this->updateSeekPosition();
302 // ... to write it back into the file
303 $this->flushFileHeader();
305 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
309 * Seeks to old position
313 protected function seekToOldPosition () {
314 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
316 // Seek to currently ("old") saved position
317 $this->seek($this->getSeekPosition());
319 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
323 * Checks whether the block separator has been found
325 * @param $str String to look in
326 * @return $isFound Whether the block separator has been found
328 public static function isBlockSeparatorFound ($str) {
330 $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE);
337 * Getter for the file pointer
339 * @return $filePointer The file pointer which shall be a valid file resource
340 * @throws UnsupportedOperationException If this method is called
342 public final function getPointer () {
343 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
347 * Setter for file name
349 * @param $fileName The new file name
352 protected final function setFileName ($fileName) {
353 $fileName = (string) $fileName;
354 $this->fileName = $fileName;
358 * Getter for file name
360 * @return $fileName The current file name
362 public final function getFileName () {
363 return $this->fileName;
367 * Initializes the back-buffer by setting it to an empty string.
371 private function initBackBuffer () {
372 // Simply call the setter
373 $this->setBackBuffer('');
377 * Setter for backBuffer field
379 * @param $backBuffer Characters to "store" in back-buffer
382 private function setBackBuffer ($backBuffer) {
383 // Cast to string (so no arrays or objects)
384 $backBuffer = (string) $backBuffer;
387 $this->backBuffer = $backBuffer;
391 * Getter for backBuffer field
393 * @return $backBuffer Characters "stored" in back-buffer
395 private function getBackBuffer () {
396 return $this->backBuffer;
400 * Setter for currentBlock field
402 * @param $currentBlock Characters to set a currently loaded block
405 private function setCurrentBlock ($currentBlock) {
406 // Cast to string (so no arrays or objects)
407 $currentBlock = (string) $currentBlock;
410 $this->currentBlock = $currentBlock;
414 * Gets currently read data
416 * @return $current Currently read data
418 public function getCurrentBlock () {
420 return $this->currentBlock;
424 * Initializes this file class
426 * @param $fileName Name of this abstract file
429 protected function initFile ($fileName) {
430 // Get a file i/o pointer instance
431 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
433 // ... and set it here
434 $this->setPointerInstance($pointerInstance);
438 * Writes data at given position
440 * @param $seekPosition Seek position
441 * @param $data Data to be written
442 * @param $flushHeader Whether to flush the header (default: flush)
445 protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
446 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
448 // Write data at given position
449 $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
451 // Update seek position
452 $this->updateSeekPosition();
455 if ($flushHeader === TRUE) {
457 $this->flushFileHeader();
459 // Seek to old position
460 $this->seekToOldPosition();
463 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
467 * Marks the currently loaded block as empty (with length of the block)
469 * @param $length Length of the block
472 protected function markCurrentBlockAsEmpty ($length) {
473 // Get current seek position
474 $currentPosition = $this->key();
476 // Now add it as gap entry
477 array_push($this->gaps, array(
478 self::GAPS_INDEX_START => ($currentPosition - $length),
479 self::GAPS_INDEX_END => $currentPosition,
484 * Checks whether the file header is initialized
486 * @return $isInitialized Whether the file header is initialized
488 public function isFileHeaderInitialized () {
489 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
491 // Default is not initialized
492 $isInitialized = FALSE;
494 // Is the file initialized?
495 if ($this->isFileInitialized()) {
496 // Some bytes has been written, so rewind to start of it.
497 $rewindStatus = $this->rewind();
498 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus));
500 // Is the rewind() call successfull?
501 if ($rewindStatus != 1) {
502 // Something bad happened
503 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__));
507 $this->readFileHeader();
509 // The above method does already check the header
510 $isInitialized = TRUE;
514 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
515 return $isInitialized;
519 * Checks whether the assigned file has been initialized
521 * @return $isInitialized Whether the file's size is zero
523 public function isFileInitialized () {
524 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
526 // Get it from iterator which holds the pointer instance. If FALSE is returned
527 $fileSize = $this->size();
528 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize));
531 * The returned file size should not be FALSE or NULL as this means
532 * that the pointer class does not work correctly.
534 assert(is_int($fileSize));
536 // Is more than 0 returned?
537 $isInitialized = ($fileSize > 0);
540 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized)));
541 return $isInitialized;
545 * Creates the assigned file
549 public function createFileHeader () {
550 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
552 // The file's header should not be initialized here
553 assert(!$this->isFileHeaderInitialized());
555 // Simple flush file header which will create it.
556 $this->flushFileHeader();
558 // Rewind seek position (to beginning of file) and update/flush file header
559 $this->rewindUpdateSeekPosition();
561 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
565 * Pre-allocates file (if enabled) with some space for later faster write access.
567 * @param $type Type of the file
570 public function preAllocateFile ($type) {
571 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
574 if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') {
576 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__));
578 // Don't continue here.
583 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__));
585 // Calculate minimum length for one entry
586 $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
587 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry));
589 // Calulcate seek position
590 $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count');
591 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition));
593 // Now simply write a NUL there. This will pre-allocate the file.
594 $this->writeData($seekPosition, chr(0));
596 // Rewind seek position (to beginning of file) and update/flush file header
597 $this->rewindUpdateSeekPosition();
599 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
603 * Close a file source and set it's instance to null and the file name
609 public function closeFile () {
610 $this->partialStub('Unfinished method.');
613 $this->setFileName('');
617 * Determines seek position
619 * @return $seekPosition Current seek position
621 public function determineSeekPosition () {
622 // Call pointer instance
623 return $this->getPointerInstance()->determineSeekPosition();
627 * Seek to given offset (default) or other possibilities as fseek() gives.
629 * @param $offset Offset to seek to (or used as "base" for other seeks)
630 * @param $whence Added to offset (default: only use offset to seek to)
631 * @return $status Status of file seek: 0 = success, -1 = failed
633 public function seek ($offset, $whence = SEEK_SET) {
634 // Call pointer instance
635 return $this->getPointerInstance()->seek($offset, $whence);
641 * @return $size Size (in bytes) of file
642 * @todo Handle seekStatus
644 public function size () {
645 // Call pointer instance
646 return $this->getPointerInstance()->size();
650 * Read data a file pointer
652 * @return mixed The result of fread()
653 * @throws NullPointerException If the file pointer instance
654 * is not set by setPointer()
655 * @throws InvalidResourceException If there is being set
657 public function readFromFile () {
658 // Call pointer instance
659 return $this->getPointerInstance()->readFromFile();
663 * Reads given amount of bytes from file.
665 * @param $bytes Amount of bytes to read
666 * @return $data Data read from file
668 public function read ($bytes) {
669 // Call pointer instance
670 return $this->getPointerInstance()->read($bytes);
674 * Write data to a file pointer
676 * @param $dataStream The data stream we shall write to the file
677 * @return mixed Number of writes bytes or FALSE on error
678 * @throws NullPointerException If the file pointer instance
679 * is not set by setPointer()
680 * @throws InvalidResourceException If there is being set
681 * an invalid file resource
683 public function writeToFile ($dataStream) {
684 // Call pointer instance
685 return $this->getPointerInstance()->writeToFile($dataStream);
689 * Rewinds to the beginning of the file
691 * @return $status Status of this operation
693 public function rewind () {
694 // Call pointer instance
695 return $this->getPointerInstance()->rewind();
699 * Determines whether the EOF has been reached
701 * @return $isEndOfFileReached Whether the EOF has been reached
703 public final function isEndOfFileReached () {
704 // Call pointer instance
705 return $this->getPointerInstance()->isEndOfFileReached();
709 * Analyzes entries in index file. This will count all found (and valid)
710 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
711 * only gaps are found, the file is considered as "virgin" (no entries).
715 public function analyzeFile () {
716 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
718 // Make sure the file is initialized
719 assert($this->isFileInitialized());
721 // Init counters and gaps array
722 $this->initCountersGapsArray();
724 // Output message (as this may take some time)
725 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
727 // First rewind to the begining
730 // Then try to load all entries
731 while ($this->valid()) {
736 $current = $this->getCurrentBlock();
739 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
742 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
746 * Advances to next "block" of bytes
750 public function next () {
751 // Is there nothing to read?
752 if (!$this->valid()) {
757 // Make sure the block instance is set
758 assert($this->getBlockInstance() instanceof CalculatableBlock);
760 // First calculate minimum block length
761 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
763 // Short be more than zero!
766 // Read possibly back-buffered bytes from previous call of next().
767 $data = $this->getBackBuffer();
770 * Read until a entry/block separator has been found. The next read
771 * "block" may not fit, so this loop will continue until the EOB or EOF
772 * has been reached whatever comes first.
774 while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
775 // Then read the next possible block
776 $block = $this->read($length);
779 if (strlen(trim($block)) == 0) {
780 // Mark this block as empty
781 $this->markCurrentBlockAsEmpty($length);
783 // Skip to next block
787 // At this block then
791 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data)));
795 if ($this->isEndOfFileReached()) {
796 // Set whole data as current read block
797 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
798 $this->setCurrentBlock($data);
800 // Then abort here silently
801 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
806 * Init back-buffer which is the data that has been found beyond the
809 $this->initBackBuffer();
812 $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
814 // This array must contain two elements
815 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
816 assert(count($dataArray) == 2);
818 // Left part is the actual block, right one the back-buffer data
819 $this->setCurrentBlock($dataArray[0]);
820 $this->setBackBuffer($dataArray[1]);
824 * Checks wether the current entry is valid (not at the end of the file).
825 * This method will return TRUE if an emptied (nulled) entry has been found.
827 * @return $isValid Whether the next entry is valid
829 public function valid () {
830 // Make sure the block instance is set
831 assert($this->getBlockInstance() instanceof Block);
833 // First calculate minimum block length
834 $length = $this->getBlockInstance()->calculateMinimumBlockLength();
836 // Short be more than zero!
839 // Get current seek position
840 $seekPosition = $this->key();
842 // Then try to read it
843 $data = $this->read($length);
845 // If some bytes could be read, all is fine
846 $isValid = ((is_string($data)) && (strlen($data) > 0));
849 $headerSize = $this->getHeaderSize();
851 // Is the seek position at or beyond the header?
852 if ($seekPosition >= $headerSize) {
853 // Seek back to old position
854 $this->seek($seekPosition);
856 // Seek directly behind the header
857 $this->seek($headerSize);
865 * Gets current seek position ("key").
867 * @return $key Current key in iteration
869 public function key () {
870 // Call pointer instance
871 return $this->getPointerInstance()->determineSeekPosition();
875 * Reads the file header
879 public function readFileHeader () {
880 // Make sure the block instance is set
881 assert($this->getBlockInstance() instanceof Block);
883 // Call block instance
884 $this->getBlockInstance()->readFileHeader();
888 * Flushes the file header
892 public function flushFileHeader () {
893 // Make sure the block instance is set
894 assert($this->getBlockInstance() instanceof Block);
896 // Call block instance
897 $this->getBlockInstance()->flushFileHeader();