From: Roland Haeder Date: Thu, 5 Jun 2014 20:43:32 +0000 (+0200) Subject: Splitted 'binary specific' methods/attributes from generic file class and created... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f724cf4b97b80ee1c886d6d4d01ab05b4d3341bf;p=core.git Splitted 'binary specific' methods/attributes from generic file class and created general text/binary classes. Signed-off-by: Roland Häder --- diff --git a/inc/classes/main/file_directories/binary/.htaccess b/inc/classes/main/file_directories/binary/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/file_directories/binary/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/file_directories/binary/class_BaseBinaryFile.php b/inc/classes/main/file_directories/binary/class_BaseBinaryFile.php new file mode 100644 index 00000000..230b4cbb --- /dev/null +++ b/inc/classes/main/file_directories/binary/class_BaseBinaryFile.php @@ -0,0 +1,835 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class BaseBinaryFile extends BaseFile { + /** + * Separator for header data + */ + const SEPARATOR_HEADER_DATA = 0x01; + + /** + * Separator header->entries + */ + const SEPARATOR_HEADER_ENTRIES = 0x02; + + /** + * Separator hash->name + */ + const SEPARATOR_HASH_NAME = 0x03; + + /** + * Separator entry->entry + */ + const SEPARATOR_ENTRIES = 0x04; + + /** + * Separator type->position + */ + const SEPARATOR_TYPE_POSITION = 0x05; + + /** + * Length of count + */ + const LENGTH_COUNT = 20; + + /** + * Length of position + */ + const LENGTH_POSITION = 20; + + /** + * Length of name + */ + const LENGTH_NAME = 10; + + /** + * Maximum length of entry type + */ + const LENGTH_TYPE = 20; + + //***** Array elements for 'gaps' array ***** + + /** + * Start of gap + */ + const GAPS_INDEX_START = 'start'; + + /** + * End of gap + */ + const GAPS_INDEX_END = 'end'; + + /** + * Current seek position + */ + private $seekPosition = 0; + + /** + * Size of header + */ + private $headerSize = 0; + + /** + * File header + */ + private $header = array(); + + /** + * Seek positions for gaps ("fragmentation") + */ + private $gaps = array(); + + /** + * Seek positions for damaged entries (e.g. mismatching hash sum, ...) + */ + private $damagedEntries = array(); + + /** + * Back-buffer + */ + private $backBuffer = ''; + + /** + * Currently loaded block (will be returned by current()) + */ + private $currentBlock = ''; + + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + + // Init counters and gaps array + $this->initCountersGapsArray(); + } + + /** + * Checks whether the abstracted file only contains gaps by counting all + * gaps' bytes together and compare it to total length. + * + * @return $isGapsOnly Whether the abstracted file only contains gaps + */ + private function isFileOnlyGaps () { + // First/last gap found? + /* Only for debugging + if (isset($this->gaps[0])) { + // Output first and last gap + 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))); + } // END - if + */ + + // Now count every gap + $gapsSize = 0; + foreach ($this->gaps as $gap) { + // Calculate size of found gap: end-start including both + $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]); + } // END - if + + // Debug output + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize())); + + // Total gap size + header size must be same as file size + $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize()); + + // Return status + return $isGapsOnly; + } + + /** + * Initializes counter for valid entries, arrays for damaged entries and + * an array for gap seek positions. If you call this method on your own, + * please re-analyze the file structure. So you are better to call + * analyzeFile() instead of this method. + * + * @return void + */ + public function initCountersGapsArray () { + // Init counter and seek position + $this->setCounter(0); + $this->setSeekPosition(0); + + // Init arrays + $this->gaps = array(); + $this->damagedEntries = array(); + } + + /** + * Getter for header size + * + * @return $totalEntries Size of file header + */ + public final function getHeaderSize () { + // Get it + return $this->headerSize; + } + + /** + * Setter for header size + * + * @param $headerSize Size of file header + * @return void + */ + public final function setHeaderSize ($headerSize) { + // Set it + $this->headerSize = $headerSize; + } + + /** + * Getter for header array + * + * @return $totalEntries Size of file header + */ + public final function getHeader () { + // Get it + return $this->header; + } + + /** + * Setter for header + * + * @param $header Array for a file header + * @return void + */ + public final function setHeader (array $header) { + // Set it + $this->header = $header; + } + + /** + * Getter for seek position + * + * @return $seekPosition Current seek position (stored here in object) + */ + protected final function getSeekPosition () { + // Get it + return $this->seekPosition; + } + + /** + * Setter for seek position + * + * @param $seekPosition Current seek position (stored here in object) + * @return void + */ + protected final function setSeekPosition ($seekPosition) { + // And set it + $this->seekPosition = $seekPosition; + } + + /** + * Updates seekPosition attribute from file to avoid to much access on file. + * + * @return void + */ + public function updateSeekPosition () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Get key (= seek position) + $seekPosition = $this->key(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); + + // And set it here + $this->setSeekPosition($seekPosition); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + + /** + * Seeks to beginning of file, updates seek position in this object and + * flushes the header. + * + * @return void + */ + protected function rewindUpdateSeekPosition () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // flushFileHeader must be callable + assert(is_callable(array($this, 'flushFileHeader'))); + + // Seek to beginning of file + $this->rewind(); + + // And update seek position ... + $this->updateSeekPosition(); + + // ... to write it back into the file + $this->flushFileHeader(); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); + } + + /** + * Seeks to old position + * + * @return void + */ + protected function seekToOldPosition () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Seek to currently ("old") saved position + $this->seek($this->getSeekPosition()); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); + } + + /** + * Checks whether the block separator has been found + * + * @param $str String to look in + * @return $isFound Whether the block separator has been found + */ + public static function isBlockSeparatorFound ($str) { + // Determine it + $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE); + + // Return result + return $isFound; + } + + /** + * Getter for the file pointer + * + * @return $filePointer The file pointer which shall be a valid file resource + * @throws UnsupportedOperationException If this method is called + */ + public final function getPointer () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); + } + + /** + * Initializes the back-buffer by setting it to an empty string. + * + * @return void + */ + private function initBackBuffer () { + // Simply call the setter + $this->setBackBuffer(''); + } + + /** + * Setter for backBuffer field + * + * @param $backBuffer Characters to "store" in back-buffer + * @return void + */ + private function setBackBuffer ($backBuffer) { + // Cast to string (so no arrays or objects) + $backBuffer = (string) $backBuffer; + + // ... and set it + $this->backBuffer = $backBuffer; + } + + /** + * Getter for backBuffer field + * + * @return $backBuffer Characters "stored" in back-buffer + */ + private function getBackBuffer () { + return $this->backBuffer; + } + + /** + * Setter for currentBlock field + * + * @param $currentBlock Characters to set a currently loaded block + * @return void + */ + private function setCurrentBlock ($currentBlock) { + // Cast to string (so no arrays or objects) + $currentBlock = (string) $currentBlock; + + // ... and set it + $this->currentBlock = $currentBlock; + } + + /** + * Gets currently read data + * + * @return $current Currently read data + */ + public function getCurrentBlock () { + // Return it + return $this->currentBlock; + } + + /** + * Initializes this file class + * + * @param $fileName Name of this abstract file + * @return void + */ + protected function initFile ($fileName) { + // Get a file i/o pointer instance + $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName)); + + // ... and set it here + $this->setPointerInstance($pointerInstance); + } + + /** + * Writes data at given position + * + * @param $seekPosition Seek position + * @param $data Data to be written + * @param $flushHeader Whether to flush the header (default: flush) + * @return void + */ + protected function writeData ($seekPosition, $data, $flushHeader = TRUE) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data))); + + // Write data at given position + $this->getPointerInstance()->writeAtPosition($seekPosition, $data); + + // Update seek position + $this->updateSeekPosition(); + + // Flush the header? + if ($flushHeader === TRUE) { + // Flush header + $this->flushFileHeader(); + + // Seek to old position + $this->seekToOldPosition(); + } // END - if + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + + /** + * Marks the currently loaded block as empty (with length of the block) + * + * @param $length Length of the block + * @return void + */ + protected function markCurrentBlockAsEmpty ($length) { + // Get current seek position + $currentPosition = $this->key(); + + // Now add it as gap entry + array_push($this->gaps, array( + self::GAPS_INDEX_START => ($currentPosition - $length), + self::GAPS_INDEX_END => $currentPosition, + )); + } + + /** + * Checks whether the file header is initialized + * + * @return $isInitialized Whether the file header is initialized + */ + public function isFileHeaderInitialized () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Default is not initialized + $isInitialized = FALSE; + + // Is the file initialized? + if ($this->isFileInitialized()) { + // Some bytes has been written, so rewind to start of it. + $rewindStatus = $this->rewind(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus)); + + // Is the rewind() call successfull? + if ($rewindStatus != 1) { + // Something bad happened + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__)); + } // END - if + + // Read file header + $this->readFileHeader(); + + // The above method does already check the header + $isInitialized = TRUE; + } // END - if + + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); + return $isInitialized; + } + + /** + * Checks whether the assigned file has been initialized + * + * @return $isInitialized Whether the file's size is zero + */ + public function isFileInitialized () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Get it from iterator which holds the pointer instance. If FALSE is returned + $fileSize = $this->size(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize)); + + /* + * The returned file size should not be FALSE or NULL as this means + * that the pointer class does not work correctly. + */ + assert(is_int($fileSize)); + + // Is more than 0 returned? + $isInitialized = ($fileSize > 0); + + // Return result + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); + return $isInitialized; + } + + /** + * Creates the assigned file + * + * @return void + */ + public function createFileHeader () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // The file's header should not be initialized here + assert(!$this->isFileHeaderInitialized()); + + // Simple flush file header which will create it. + $this->flushFileHeader(); + + // Rewind seek position (to beginning of file) and update/flush file header + $this->rewindUpdateSeekPosition(); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); + } + + /** + * Pre-allocates file (if enabled) with some space for later faster write access. + * + * @param $type Type of the file + * @return void + */ + public function preAllocateFile ($type) { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Is it enabled? + if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') { + // Not enabled + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__)); + + // Don't continue here. + return; + } // END - if + + // Message to user + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__)); + + // Calculate minimum length for one entry + $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry)); + + // Calulcate seek position + $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count'); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); + + // Now simply write a NUL there. This will pre-allocate the file. + $this->writeData($seekPosition, chr(0)); + + // Rewind seek position (to beginning of file) and update/flush file header + $this->rewindUpdateSeekPosition(); + + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + + /** + * Determines seek position + * + * @return $seekPosition Current seek position + */ + public function determineSeekPosition () { + // Call pointer instance + return $this->getPointerInstance()->determineSeekPosition(); + } + + /** + * Seek to given offset (default) or other possibilities as fseek() gives. + * + * @param $offset Offset to seek to (or used as "base" for other seeks) + * @param $whence Added to offset (default: only use offset to seek to) + * @return $status Status of file seek: 0 = success, -1 = failed + */ + public function seek ($offset, $whence = SEEK_SET) { + // Call pointer instance + return $this->getPointerInstance()->seek($offset, $whence); + } + + /** + * Reads given amount of bytes from file. + * + * @param $bytes Amount of bytes to read + * @return $data Data read from file + */ + public function read ($bytes) { + // Call pointer instance + return $this->getPointerInstance()->read($bytes); + } + + /** + * Rewinds to the beginning of the file + * + * @return $status Status of this operation + */ + public function rewind () { + // Call pointer instance + return $this->getPointerInstance()->rewind(); + } + + /** + * Analyzes entries in index file. This will count all found (and valid) + * entries, mark invalid as damaged and count gaps ("fragmentation"). If + * only gaps are found, the file is considered as "virgin" (no entries). + * + * @return void + */ + public function analyzeFile () { + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); + + // Make sure the file is initialized + assert($this->isFileInitialized()); + + // Init counters and gaps array + $this->initCountersGapsArray(); + + // Output message (as this may take some time) + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__)); + + // First rewind to the begining + $this->rewind(); + + // Then try to load all entries + while ($this->valid()) { + // Go to next entry + $this->next(); + + // Get current entry + $current = $this->getCurrentBlock(); + + /* + * If the block is empty, maybe the whole file is? This could mean + * that the file has been pre-allocated. + */ + if (empty($current)) { + // Then skip this part + continue; + } // END - if + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current))); + } // END - while + + // If the last read block is empty, check gaps + if (empty($current)) { + // Output message + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps))); + + // Check gaps, if the whole file is empty. + if ($this->isFileOnlyGaps()) { + // Only gaps, so don't continue here. + return; + } // END - if + + /* + * The above call has calculated a total size of all gaps. If the + * percentage of gaps passes a "soft" limit and last + * defragmentation is to far in the past, or if a "hard" limit has + * reached, run defragmentation. + */ + if ($this->isDefragmentationNeeded()) { + // Run "defragmentation" + $this->doRunDefragmentation(); + } // END - if + } // END - if + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + } + + /** + * Advances to next "block" of bytes + * + * @return void + */ + public function next () { + // Is there nothing to read? + if (!$this->valid()) { + // Nothing to read + return; + } // END - if + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%s', __FUNCTION__, __LINE__, $this->key())); + + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof CalculatableBlock); + + // First calculate minimum block length + $length = $this->getBlockInstance()->calculateMinimumBlockLength(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length)); + + // Short be more than zero! + assert($length > 0); + + // Read possibly back-buffered bytes from previous call of next(). + $data = $this->getBackBuffer(); + + /* + * Read until a entry/block separator has been found. The next read + * "block" may not fit, so this loop will continue until the EOB or EOF + * has been reached whatever comes first. + */ + while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) { + // Then read the next possible block + $block = $this->read($length); + + // Debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%s,length=%s', __FUNCTION__, __LINE__, strlen($block), $length)); + + // Is it all empty? + if (strlen(trim($block)) == 0) { + // Mark this block as empty + $this->markCurrentBlockAsEmpty(strlen($block)); + + // Skip to next block + continue; + } // END - if + + // At this block then + $data .= $block; + + // A debug message + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data))); + } // END - while + + // EOF reached? + if ($this->isEndOfFileReached()) { + // Set whole data as current read block + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...'); + $this->setCurrentBlock($data); + + // Then abort here silently + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.'); + return; + } // END - if + + /* + * Init back-buffer which is the data that has been found beyond the + * separator. + */ + $this->initBackBuffer(); + + // Separate data + $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data); + + // This array must contain two elements + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE)); + assert(count($dataArray) == 2); + + // Left part is the actual block, right one the back-buffer data + $this->setCurrentBlock($dataArray[0]); + $this->setBackBuffer($dataArray[1]); + } + + /** + * Checks wether the current entry is valid (not at the end of the file). + * This method will return TRUE if an emptied (nulled) entry has been found. + * + * @return $isValid Whether the next entry is valid + */ + public function valid () { + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof Block); + + // First calculate minimum block length + $length = $this->getBlockInstance()->calculateMinimumBlockLength(); + + // Short be more than zero! + assert($length > 0); + + // Get current seek position + $seekPosition = $this->key(); + + // Then try to read it + $data = $this->read($length); + + // If some bytes could be read, all is fine + $isValid = ((is_string($data)) && (strlen($data) > 0)); + + // Get header size + $headerSize = $this->getHeaderSize(); + + // Is the seek position at or beyond the header? + if ($seekPosition >= $headerSize) { + // Seek back to old position + $this->seek($seekPosition); + } else { + // Seek directly behind the header + $this->seek($headerSize); + } + + // Return result + return $isValid; + } + + /** + * Gets current seek position ("key"). + * + * @return $key Current key in iteration + */ + public function key () { + // Call pointer instance + return $this->getPointerInstance()->determineSeekPosition(); + } + + /** + * Reads the file header + * + * @return void + */ + public function readFileHeader () { + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof Block); + + // Call block instance + $this->getBlockInstance()->readFileHeader(); + } + + /** + * Flushes the file header + * + * @return void + */ + public function flushFileHeader () { + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof Block); + + // Call block instance + $this->getBlockInstance()->flushFileHeader(); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/file_directories/binary/index/.htaccess b/inc/classes/main/file_directories/binary/index/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/file_directories/binary/index/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/file_directories/binary/index/class_IndexFile.php b/inc/classes/main/file_directories/binary/index/class_IndexFile.php new file mode 100644 index 00000000..ab1da24a --- /dev/null +++ b/inc/classes/main/file_directories/binary/index/class_IndexFile.php @@ -0,0 +1,58 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class IndexFile extends BaseBinaryFile implements Block { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this File class and prepares it for usage + * + * @param $fileName Name of the index file + * @param $blockInstance An instance of a Block class + * @return $fileInstance An instance of this File class + */ + public final static function createIndexFile ($fileName, Block $blockInstance) { + // Get a new instance + $fileInstance = new IndexFile(); + + // Set block instance here for callbacks + $fileInstance->setBlockInstance($blockInstance); + + // Init this abstract file + $fileInstance->initFile($fileName); + + // Return the prepared instance + return $fileInstance; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/file_directories/binary/stack/.htaccess b/inc/classes/main/file_directories/binary/stack/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/file_directories/binary/stack/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/file_directories/binary/stack/class_StackFile.php b/inc/classes/main/file_directories/binary/stack/class_StackFile.php new file mode 100644 index 00000000..68d5e127 --- /dev/null +++ b/inc/classes/main/file_directories/binary/stack/class_StackFile.php @@ -0,0 +1,58 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class StackFile extends BaseBinaryFile implements Block { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this File class and prepares it for usage + * + * @param $fileName Name of the stack file + * @param $blockInstance An instance of a Block class + * @return $fileInstance An instance of this File class + */ + public final static function createStackFile ($fileName, Block $blockInstance) { + // Get a new instance + $fileInstance = new StackFile(); + + // Set block instance here for callbacks + $fileInstance->setBlockInstance($blockInstance); + + // Init this abstract file + $fileInstance->initFile($fileName); + + // Return the prepared instance + return $fileInstance; + } +} + +// [EOF] +?> diff --git a/inc/classes/main/file_directories/class_BaseFile.php b/inc/classes/main/file_directories/class_BaseFile.php index 7c96e34c..cf1bc402 100644 --- a/inc/classes/main/file_directories/class_BaseFile.php +++ b/inc/classes/main/file_directories/class_BaseFile.php @@ -22,120 +22,25 @@ * along with this program. If not, see . */ class BaseFile extends BaseFrameworkSystem { - /** - * Separator for header data - */ - const SEPARATOR_HEADER_DATA = 0x01; - - /** - * Separator header->entries - */ - const SEPARATOR_HEADER_ENTRIES = 0x02; - - /** - * Separator hash->name - */ - const SEPARATOR_HASH_NAME = 0x03; - - /** - * Separator entry->entry - */ - const SEPARATOR_ENTRIES = 0x04; - - /** - * Separator type->position - */ - const SEPARATOR_TYPE_POSITION = 0x05; - - /** - * Length of count - */ - const LENGTH_COUNT = 20; - - /** - * Length of position - */ - const LENGTH_POSITION = 20; - - /** - * Length of name - */ - const LENGTH_NAME = 10; - - /** - * Maximum length of entry type - */ - const LENGTH_TYPE = 20; - - //***** Array elements for 'gaps' array ***** - - /** - * Start of gap - */ - const GAPS_INDEX_START = 'start'; - - /** - * End of gap - */ - const GAPS_INDEX_END = 'end'; - /** * Counter for total entries */ private $totalEntries = 0; - /** - * Current seek position - */ - private $seekPosition = 0; - - /** - * Size of header - */ - private $headerSize = 0; - - /** - * File header - */ - private $header = array(); - - /** - * Seek positions for gaps ("fragmentation") - */ - private $gaps = array(); - - /** - * Seek positions for damaged entries (e.g. mismatching hash sum, ...) - */ - private $damagedEntries = array(); - /** * The current file we are working in */ private $fileName = ''; - /** - * Back-buffer - */ - private $backBuffer = ''; - - /** - * Currently loaded block (will be returned by current()) - */ - private $currentBlock = ''; - /** * Protected constructor * * @param $className Name of the class -y * @return void + * @return void */ protected function __construct ($className) { // Call parent constructor parent::__construct($className); - - // Init counters and gaps array - $this->initCountersGapsArray(); } /** @@ -151,56 +56,6 @@ y * @return void parent::__destruct(); } - /** - * Checks whether the abstracted file only contains gaps by counting all - * gaps' bytes together and compare it to total length. - * - * @return $isGapsOnly Whether the abstracted file only contains gaps - */ - private function isFileOnlyGaps () { - // First/last gap found? - /* Only for debugging - if (isset($this->gaps[0])) { - // Output first and last gap - 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))); - } // END - if - */ - - // Now count every gap - $gapsSize = 0; - foreach ($this->gaps as $gap) { - // Calculate size of found gap: end-start including both - $gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]); - } // END - if - - // Debug output - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', __METHOD__, __LINE__, $gapsSize, $this->getHeaderSize())); - - // Total gap size + header size must be same as file size - $isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize()); - - // Return status - return $isGapsOnly; - } - - /** - * Initializes counter for valid entries, arrays for damaged entries and - * an array for gap seek positions. If you call this method on your own, - * please re-analyze the file structure. So you are better to call - * analyzeFile() instead of this method. - * - * @return void - */ - public function initCountersGapsArray () { - // Init counter and seek position - $this->setCounter(0); - $this->setSeekPosition(0); - - // Init arrays - $this->gaps = array(); - $this->damagedEntries = array(); - } - /** * "Getter" for abstracted file size * @@ -242,139 +97,6 @@ y * @return void $this->totalEntries++; } - /** - * Getter for header size - * - * @return $totalEntries Size of file header - */ - public final function getHeaderSize () { - // Get it - return $this->headerSize; - } - - /** - * Setter for header size - * - * @param $headerSize Size of file header - * @return void - */ - public final function setHeaderSize ($headerSize) { - // Set it - $this->headerSize = $headerSize; - } - - /** - * Getter for header array - * - * @return $totalEntries Size of file header - */ - public final function getHeader () { - // Get it - return $this->header; - } - - /** - * Setter for header - * - * @param $header Array for a file header - * @return void - */ - public final function setHeader (array $header) { - // Set it - $this->header = $header; - } - - /** - * Getter for seek position - * - * @return $seekPosition Current seek position (stored here in object) - */ - protected final function getSeekPosition () { - // Get it - return $this->seekPosition; - } - - /** - * Setter for seek position - * - * @param $seekPosition Current seek position (stored here in object) - * @return void - */ - protected final function setSeekPosition ($seekPosition) { - // And set it - $this->seekPosition = $seekPosition; - } - - /** - * Updates seekPosition attribute from file to avoid to much access on file. - * - * @return void - */ - public function updateSeekPosition () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Get key (= seek position) - $seekPosition = $this->key(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); - - // And set it here - $this->setSeekPosition($seekPosition); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - - /** - * Seeks to beginning of file, updates seek position in this object and - * flushes the header. - * - * @return void - */ - protected function rewindUpdateSeekPosition () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // flushFileHeader must be callable - assert(is_callable(array($this, 'flushFileHeader'))); - - // Seek to beginning of file - $this->rewind(); - - // And update seek position ... - $this->updateSeekPosition(); - - // ... to write it back into the file - $this->flushFileHeader(); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); - } - - /** - * Seeks to old position - * - * @return void - */ - protected function seekToOldPosition () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Seek to currently ("old") saved position - $this->seek($this->getSeekPosition()); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); - } - - /** - * Checks whether the block separator has been found - * - * @param $str String to look in - * @return $isFound Whether the block separator has been found - */ - public static function isBlockSeparatorFound ($str) { - // Determine it - $isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== FALSE); - - // Return result - return $isFound; - } - /** * Getter for the file pointer * @@ -405,242 +127,6 @@ y * @return void return $this->fileName; } - /** - * Initializes the back-buffer by setting it to an empty string. - * - * @return void - */ - private function initBackBuffer () { - // Simply call the setter - $this->setBackBuffer(''); - } - - /** - * Setter for backBuffer field - * - * @param $backBuffer Characters to "store" in back-buffer - * @return void - */ - private function setBackBuffer ($backBuffer) { - // Cast to string (so no arrays or objects) - $backBuffer = (string) $backBuffer; - - // ... and set it - $this->backBuffer = $backBuffer; - } - - /** - * Getter for backBuffer field - * - * @return $backBuffer Characters "stored" in back-buffer - */ - private function getBackBuffer () { - return $this->backBuffer; - } - - /** - * Setter for currentBlock field - * - * @param $currentBlock Characters to set a currently loaded block - * @return void - */ - private function setCurrentBlock ($currentBlock) { - // Cast to string (so no arrays or objects) - $currentBlock = (string) $currentBlock; - - // ... and set it - $this->currentBlock = $currentBlock; - } - - /** - * Gets currently read data - * - * @return $current Currently read data - */ - public function getCurrentBlock () { - // Return it - return $this->currentBlock; - } - - /** - * Initializes this file class - * - * @param $fileName Name of this abstract file - * @return void - */ - protected function initFile ($fileName) { - // Get a file i/o pointer instance - $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName)); - - // ... and set it here - $this->setPointerInstance($pointerInstance); - } - - /** - * Writes data at given position - * - * @param $seekPosition Seek position - * @param $data Data to be written - * @param $flushHeader Whether to flush the header (default: flush) - * @return void - */ - protected function writeData ($seekPosition, $data, $flushHeader = TRUE) { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data))); - - // Write data at given position - $this->getPointerInstance()->writeAtPosition($seekPosition, $data); - - // Update seek position - $this->updateSeekPosition(); - - // Flush the header? - if ($flushHeader === TRUE) { - // Flush header - $this->flushFileHeader(); - - // Seek to old position - $this->seekToOldPosition(); - } // END - if - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - - /** - * Marks the currently loaded block as empty (with length of the block) - * - * @param $length Length of the block - * @return void - */ - protected function markCurrentBlockAsEmpty ($length) { - // Get current seek position - $currentPosition = $this->key(); - - // Now add it as gap entry - array_push($this->gaps, array( - self::GAPS_INDEX_START => ($currentPosition - $length), - self::GAPS_INDEX_END => $currentPosition, - )); - } - - /** - * Checks whether the file header is initialized - * - * @return $isInitialized Whether the file header is initialized - */ - public function isFileHeaderInitialized () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Default is not initialized - $isInitialized = FALSE; - - // Is the file initialized? - if ($this->isFileInitialized()) { - // Some bytes has been written, so rewind to start of it. - $rewindStatus = $this->rewind(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] rewindStatus=%s', __METHOD__, __LINE__, $rewindStatus)); - - // Is the rewind() call successfull? - if ($rewindStatus != 1) { - // Something bad happened - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Could not rewind().', __METHOD__, __LINE__)); - } // END - if - - // Read file header - $this->readFileHeader(); - - // The above method does already check the header - $isInitialized = TRUE; - } // END - if - - // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); - return $isInitialized; - } - - /** - * Checks whether the assigned file has been initialized - * - * @return $isInitialized Whether the file's size is zero - */ - public function isFileInitialized () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Get it from iterator which holds the pointer instance. If FALSE is returned - $fileSize = $this->size(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] fileSize=%s', __METHOD__, __LINE__, $fileSize)); - - /* - * The returned file size should not be FALSE or NULL as this means - * that the pointer class does not work correctly. - */ - assert(is_int($fileSize)); - - // Is more than 0 returned? - $isInitialized = ($fileSize > 0); - - // Return result - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] isInitialized=%d - EXIT!', __METHOD__, __LINE__, intval($isInitialized))); - return $isInitialized; - } - - /** - * Creates the assigned file - * - * @return void - */ - public function createFileHeader () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // The file's header should not be initialized here - assert(!$this->isFileHeaderInitialized()); - - // Simple flush file header which will create it. - $this->flushFileHeader(); - - // Rewind seek position (to beginning of file) and update/flush file header - $this->rewindUpdateSeekPosition(); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); - } - - /** - * Pre-allocates file (if enabled) with some space for later faster write access. - * - * @param $type Type of the file - * @return void - */ - public function preAllocateFile ($type) { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Is it enabled? - if ($this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_enabled') != 'Y') { - // Not enabled - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Not pre-allocating file.', __METHOD__, __LINE__)); - - // Don't continue here. - return; - } // END - if - - // Message to user - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Pre-allocating file ...', __METHOD__, __LINE__)); - - // Calculate minimum length for one entry - $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] minLengthEntry=%s', __METHOD__, __LINE__, $minLengthEntry)); - - // Calulcate seek position - $seekPosition = $minLengthEntry * $this->getConfigInstance()->getConfigEntry($type . '_pre_allocate_count'); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s', __METHOD__, __LINE__, $seekPosition)); - - // Now simply write a NUL there. This will pre-allocate the file. - $this->writeData($seekPosition, chr(0)); - - // Rewind seek position (to beginning of file) and update/flush file header - $this->rewindUpdateSeekPosition(); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - /** * Close a file source and set it's instance to null and the file name * to empty @@ -655,28 +141,6 @@ y * @return void $this->setFileName(''); } - /** - * Determines seek position - * - * @return $seekPosition Current seek position - */ - public function determineSeekPosition () { - // Call pointer instance - return $this->getPointerInstance()->determineSeekPosition(); - } - - /** - * Seek to given offset (default) or other possibilities as fseek() gives. - * - * @param $offset Offset to seek to (or used as "base" for other seeks) - * @param $whence Added to offset (default: only use offset to seek to) - * @return $status Status of file seek: 0 = success, -1 = failed - */ - public function seek ($offset, $whence = SEEK_SET) { - // Call pointer instance - return $this->getPointerInstance()->seek($offset, $whence); - } - /** * Size of this file * @@ -701,17 +165,6 @@ y * @return void return $this->getPointerInstance()->readFromFile(); } - /** - * Reads given amount of bytes from file. - * - * @param $bytes Amount of bytes to read - * @return $data Data read from file - */ - public function read ($bytes) { - // Call pointer instance - return $this->getPointerInstance()->read($bytes); - } - /** * Write data to a file pointer * @@ -727,16 +180,6 @@ y * @return void return $this->getPointerInstance()->writeToFile($dataStream); } - /** - * Rewinds to the beginning of the file - * - * @return $status Status of this operation - */ - public function rewind () { - // Call pointer instance - return $this->getPointerInstance()->rewind(); - } - /** * Determines whether the EOF has been reached * @@ -746,236 +189,6 @@ y * @return void // Call pointer instance return $this->getPointerInstance()->isEndOfFileReached(); } - - /** - * Analyzes entries in index file. This will count all found (and valid) - * entries, mark invalid as damaged and count gaps ("fragmentation"). If - * only gaps are found, the file is considered as "virgin" (no entries). - * - * @return void - */ - public function analyzeFile () { - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__)); - - // Make sure the file is initialized - assert($this->isFileInitialized()); - - // Init counters and gaps array - $this->initCountersGapsArray(); - - // Output message (as this may take some time) - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__)); - - // First rewind to the begining - $this->rewind(); - - // Then try to load all entries - while ($this->valid()) { - // Go to next entry - $this->next(); - - // Get current entry - $current = $this->getCurrentBlock(); - - /* - * If the block is empty, maybe the whole file is? This could mean - * that the file has been pre-allocated. - */ - if (empty($current)) { - // Then skip this part - continue; - } // END - if - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current))); - } // END - while - - // If the last read block is empty, check gaps - if (empty($current)) { - // Output message - self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Found a total of %s gaps.', __METHOD__, __LINE__, count($this->gaps))); - - // Check gaps, if the whole file is empty. - if ($this->isFileOnlyGaps()) { - // Only gaps, so don't continue here. - return; - } // END - if - - /* - * The above call has calculated a total size of all gaps. If the - * percentage of gaps passes a "soft" limit and last - * defragmentation is to far in the past, or if a "hard" limit has - * reached, run defragmentation. - */ - if ($this->isDefragmentationNeeded()) { - // Run "defragmentation" - $this->doRunDefragmentation(); - } // END - if - } // END - if - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - - /** - * Advances to next "block" of bytes - * - * @return void - */ - public function next () { - // Is there nothing to read? - if (!$this->valid()) { - // Nothing to read - return; - } // END - if - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%s', __FUNCTION__, __LINE__, $this->key())); - - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof CalculatableBlock); - - // First calculate minimum block length - $length = $this->getBlockInstance()->calculateMinimumBlockLength(); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] length=%s', __FUNCTION__, __LINE__, $length)); - - // Short be more than zero! - assert($length > 0); - - // Read possibly back-buffered bytes from previous call of next(). - $data = $this->getBackBuffer(); - - /* - * Read until a entry/block separator has been found. The next read - * "block" may not fit, so this loop will continue until the EOB or EOF - * has been reached whatever comes first. - */ - while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) { - // Then read the next possible block - $block = $this->read($length); - - // Debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%s,length=%s', __FUNCTION__, __LINE__, strlen($block), $length)); - - // Is it all empty? - if (strlen(trim($block)) == 0) { - // Mark this block as empty - $this->markCurrentBlockAsEmpty(strlen($block)); - - // Skip to next block - continue; - } // END - if - - // At this block then - $data .= $block; - - // A debug message - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data))); - } // END - while - - // EOF reached? - if ($this->isEndOfFileReached()) { - // Set whole data as current read block - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...'); - $this->setCurrentBlock($data); - - // Then abort here silently - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.'); - return; - } // END - if - - /* - * Init back-buffer which is the data that has been found beyond the - * separator. - */ - $this->initBackBuffer(); - - // Separate data - $dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data); - - // This array must contain two elements - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE)); - assert(count($dataArray) == 2); - - // Left part is the actual block, right one the back-buffer data - $this->setCurrentBlock($dataArray[0]); - $this->setBackBuffer($dataArray[1]); - } - - /** - * Checks wether the current entry is valid (not at the end of the file). - * This method will return TRUE if an emptied (nulled) entry has been found. - * - * @return $isValid Whether the next entry is valid - */ - public function valid () { - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof Block); - - // First calculate minimum block length - $length = $this->getBlockInstance()->calculateMinimumBlockLength(); - - // Short be more than zero! - assert($length > 0); - - // Get current seek position - $seekPosition = $this->key(); - - // Then try to read it - $data = $this->read($length); - - // If some bytes could be read, all is fine - $isValid = ((is_string($data)) && (strlen($data) > 0)); - - // Get header size - $headerSize = $this->getHeaderSize(); - - // Is the seek position at or beyond the header? - if ($seekPosition >= $headerSize) { - // Seek back to old position - $this->seek($seekPosition); - } else { - // Seek directly behind the header - $this->seek($headerSize); - } - - // Return result - return $isValid; - } - - /** - * Gets current seek position ("key"). - * - * @return $key Current key in iteration - */ - public function key () { - // Call pointer instance - return $this->getPointerInstance()->determineSeekPosition(); - } - - /** - * Reads the file header - * - * @return void - */ - public function readFileHeader () { - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof Block); - - // Call block instance - $this->getBlockInstance()->readFileHeader(); - } - - /** - * Flushes the file header - * - * @return void - */ - public function flushFileHeader () { - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof Block); - - // Call block instance - $this->getBlockInstance()->flushFileHeader(); - } } // [EOF] diff --git a/inc/classes/main/file_directories/index/.htaccess b/inc/classes/main/file_directories/index/.htaccess deleted file mode 100644 index 3a428827..00000000 --- a/inc/classes/main/file_directories/index/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/inc/classes/main/file_directories/index/class_IndexFile.php b/inc/classes/main/file_directories/index/class_IndexFile.php deleted file mode 100644 index c3190a1d..00000000 --- a/inc/classes/main/file_directories/index/class_IndexFile.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class IndexFile extends BaseFile implements Block { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this File class and prepares it for usage - * - * @param $fileName Name of the index file - * @param $blockInstance An instance of a Block class - * @return $fileInstance An instance of this File class - */ - public final static function createIndexFile ($fileName, Block $blockInstance) { - // Get a new instance - $fileInstance = new IndexFile(); - - // Set block instance here for callbacks - $fileInstance->setBlockInstance($blockInstance); - - // Init this abstract file - $fileInstance->initFile($fileName); - - // Return the prepared instance - return $fileInstance; - } -} - -// [EOF] -?> diff --git a/inc/classes/main/file_directories/stack/.htaccess b/inc/classes/main/file_directories/stack/.htaccess deleted file mode 100644 index 3a428827..00000000 --- a/inc/classes/main/file_directories/stack/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all diff --git a/inc/classes/main/file_directories/stack/class_StackFile.php b/inc/classes/main/file_directories/stack/class_StackFile.php deleted file mode 100644 index 863362c3..00000000 --- a/inc/classes/main/file_directories/stack/class_StackFile.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team - * @license GNU GPL 3.0 or any newer version - * @link http://www.ship-simu.org - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -class StackFile extends BaseFile implements Block { - /** - * Protected constructor - * - * @return void - */ - protected function __construct () { - // Call parent constructor - parent::__construct(__CLASS__); - } - - /** - * Creates an instance of this File class and prepares it for usage - * - * @param $fileName Name of the stack file - * @param $blockInstance An instance of a Block class - * @return $fileInstance An instance of this File class - */ - public final static function createStackFile ($fileName, Block $blockInstance) { - // Get a new instance - $fileInstance = new StackFile(); - - // Set block instance here for callbacks - $fileInstance->setBlockInstance($blockInstance); - - // Init this abstract file - $fileInstance->initFile($fileName); - - // Return the prepared instance - return $fileInstance; - } -} - -// [EOF] -?> diff --git a/inc/classes/main/file_directories/text/.htaccess b/inc/classes/main/file_directories/text/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/file_directories/text/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/file_directories/text/class_BaseTextFile.php b/inc/classes/main/file_directories/text/class_BaseTextFile.php new file mode 100644 index 00000000..8f2afc9f --- /dev/null +++ b/inc/classes/main/file_directories/text/class_BaseTextFile.php @@ -0,0 +1,38 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class BaseTextFile extends BaseFile { + /** + * Protected constructor + * + * @param $className Name of the class + * @return void + */ + protected function __construct ($className) { + // Call parent constructor + parent::__construct($className); + } +} + +// [EOF] +?>