X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ffile_directories%2Fclass_BaseFile.php;h=0d1bb418ea9be169b0357cf5b607718cbaaa46af;hp=c596fcb4a31f4ce8891b9cb9cc9b12b00fae5e29;hb=c77663981dbaefb7d8ff039967509aa1204ee556;hpb=b79b70f64580d944cbde60cd7a08716ea70b3e11 diff --git a/inc/classes/main/file_directories/class_BaseFile.php b/inc/classes/main/file_directories/class_BaseFile.php index c596fcb4..0d1bb418 100644 --- a/inc/classes/main/file_directories/class_BaseFile.php +++ b/inc/classes/main/file_directories/class_BaseFile.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -23,32 +23,24 @@ */ class BaseFile extends BaseFrameworkSystem { /** - * The current file we are working in - */ - private $fileName = ''; - - /** - * Back-buffer + * Counter for total entries */ - private $backBuffer = ''; + private $totalEntries = 0; /** - * Currently loaded block (will be returned by current()) + * The current file we are working in */ - private $currentBlock = ''; + private $fileName = ''; /** * 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(); } /** @@ -65,252 +57,74 @@ y * @return void } /** - * 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); - } - - /** - * Setter for file name - * - * @param $fileName The new file name - * @return void - */ - protected final function setFileName ($fileName) { - $fileName = (string) $fileName; - $this->fileName = $fileName; - } - - /** - * Getter for file name - * - * @return $fileName The current file name - */ - public final function getFileName () { - 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 + * "Getter" for abstracted file size * - * @param $backBuffer Characters to "store" in back-buffer - * @return void + * @return $fileSize Size of abstracted file */ - private function setBackBuffer ($backBuffer) { - // Cast to string (so no arrays or objects) - $backBuffer = (string) $backBuffer; - - // ... and set it - $this->backBuffer = $backBuffer; + public function getFileSize () { + // Call pointer instanze + return $this->getPointerInstance()->getFileSize(); } /** - * Getter for backBuffer field + * Getter for total entries * - * @return $backBuffer Characters "stored" in back-buffer + * @return $totalEntries Total entries in this file */ - private function getBackBuffer () { - return $this->backBuffer; + public final function getCounter () { + // Get it + return $this->totalEntries; } /** - * Setter for currentBlock field + * Setter for total entries * - * @param $currentBlock Characters to set a currently loaded block + * @param $totalEntries Total entries in this file * @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; + protected final function setCounter ($counter) { + // Set it + $this->totalEntries = $counter; } /** - * Initializes this file class + * Increment counter * - * @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); + protected final function incrementCounter () { + // Get it + $this->totalEntries++; } /** - * 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__)); - } - - /** - * 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->getIteratorInstance()->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->getBlockInstance()->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 + * Getter for the file pointer * - * @return $isInitialized Whether the file's size is zero + * @return $filePointer The file pointer which shall be a valid file resource + * @throws UnsupportedOperationException If this method is called */ - 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; + public final function getPointer () { + throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } /** - * Creates the assigned file + * Setter for file name * + * @param $fileName The new file name * @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->getBlockInstance()->flushFileHeader(); - - // Rewind seek position (to beginning of file) and update/flush file header - $this->rewineUpdateSeekPosition(); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__)); + protected final function setFileName ($fileName) { + $fileName = (string) $fileName; + $this->fileName = $fileName; } /** - * Pre-allocates file (if enabled) with some space for later faster write access. + * Getter for file name * - * @param $type Type of the file - * @return void + * @return $fileName The current file name */ - 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->rewineUpdateSeekPosition(); - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); + public final function getFileName () { + return $this->fileName; } /** @@ -327,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 * @@ -373,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 * @@ -399,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 * @@ -418,153 +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(); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current))); - } // END - while - - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); - } - - /** - * Advances to next "block" of bytes - * - * @return void - * @todo This method will load large but empty files in a whole - */ - public function next () { - // Is there nothing to read? - if (!$this->valid()) { - // Nothing to read - return; - } // END - if - - // Make sure the block instance is set - assert($this->getBlockInstance() instanceof CalculatableBlock); - - // First calculate minimum block length - $length = $this->getBlockInstance()->calculateMinimumBlockLength(); - - // Short be more than zero! - assert($length > 0); - - // Wait until a entry/block separator has been found - $data = $this->getBackBuffer(); - while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) { - // Then read the block - $data .= $this->read($length); - //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data)); - } // END - if - - // EOF reached? - if ($this->isEndOfFileReached()) { - // Set whole data as current 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(self::getBlockSeparator(), $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 CalculatableBlock); - - // 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->getBlockInstance()->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(); - } } // [EOF]