From: Roland Haeder Date: Fri, 23 May 2014 23:25:04 +0000 (+0200) Subject: Moved some code to BaseFrameworkSystem, and yes: getBlockSeparator() looks stupid... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ba393ad8425e00880c34f129980de3d4fd697b25;p=core.git Moved some code to BaseFrameworkSystem, and yes: getBlockSeparator() looks stupid. ;-) Signed-off-by: Roland Häder --- diff --git a/inc/classes/interfaces/calculatable/class_CalculatableBlock.php b/inc/classes/interfaces/calculatable/class_CalculatableBlock.php index ba980857..6d472687 100644 --- a/inc/classes/interfaces/calculatable/class_CalculatableBlock.php +++ b/inc/classes/interfaces/calculatable/class_CalculatableBlock.php @@ -28,6 +28,14 @@ interface CalculatableBlock extends FrameworkInterface { * @return $length Minimum length for one entry/block */ function caluclateMinimumBlockLength (); + + /** + * Checks whether the block separator has been found + * + * @param $str String to look in + * @return $isFound Whether the block separator has been found + */ + function isBlockSeparatorFound ($str); } // [EOF] diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 707f5600..f9b63b4f 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -23,6 +23,26 @@ * along with this program. If not, see . */ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { + /** + * 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; + /** * Length of count */ @@ -33,6 +53,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { */ const LENGTH_POSITION = 20; + /** + * Length of name + */ + const LENGTH_NAME = 10; + /** * The real class name */ @@ -2921,7 +2946,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { * * @return $totalEntries Size of file header */ - protected final function getHeaderSize () { + public final function getHeaderSize () { // Get it return $this->headerSize; } @@ -3188,6 +3213,29 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { //* 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 function isBlockSeparatorFound ($str) { + // Determine it + $isFound = (strpos($str, self::getBlockSeparator()) !== FALSE); + + // Return result + return $isFound; + } + + /** + * Getter for block separator character(s) + * + * @return $blockSeparator A separator for blocks + */ + protected static final function getBlockSeparator () { + return chr(self::SEPARATOR_ENTRIES); + } } // [EOF] diff --git a/inc/classes/main/index/class_BaseIndex.php b/inc/classes/main/index/class_BaseIndex.php index a427c5b7..de0e5629 100644 --- a/inc/classes/main/index/class_BaseIndex.php +++ b/inc/classes/main/index/class_BaseIndex.php @@ -27,16 +27,6 @@ class BaseIndex extends BaseFrameworkSystem { */ const INDEX_MAGIC = 'INDEXv0.1'; - /** - * Separator for header data - */ - const SEPARATOR_HEADER_DATA = 0x01; - - /** - * Separator header->entries - */ - const SEPARATOR_HEADER_ENTRIES = 0x02; - /** * Protected constructor * @@ -185,20 +175,6 @@ class BaseIndex extends BaseFrameworkSystem { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); } - /** - * Calculates minimum length for one entry/block - * - * @return $length Minimum length for one entry/block - */ - public function caluclateMinimumBlockLength () { - // Calulcate it - // @TODO Not finished yet - $length = 0; - - // Return it - return $length; - } - /** * Initializes this index * @@ -237,6 +213,20 @@ class BaseIndex extends BaseFrameworkSystem { // Count all entries in file $this->analyzeFile(); } + + /** + * Calculates minimum length for one entry/block + * + * @return $length Minimum length for one entry/block + */ + public function caluclateMinimumBlockLength () { + // Calulcate it + // @TODO Not finished yet + $length = 0; + + // Return it + return $length; + } } // [EOF] diff --git a/inc/classes/main/iterator/io/class_FileIoIterator.php b/inc/classes/main/iterator/io/class_FileIoIterator.php index 736280a3..7f30fb51 100644 --- a/inc/classes/main/iterator/io/class_FileIoIterator.php +++ b/inc/classes/main/iterator/io/class_FileIoIterator.php @@ -162,7 +162,7 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato $this->initBackBuffer(); // Separate data - $dataArray = explode($this->getBlockInstance()->getBlockSeparator(), $data); + $dataArray = explode(self::getBlockSeparator(), $data); // Left part is the actual block, right one the back-buffer data $this->setCurrentBlock($dataArray[0]); @@ -201,8 +201,17 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato // If some bytes could be read, all is fine $isValid = ((is_string($data)) && (strlen($data) > 0)); - // Seek back to old position - $this->seek($seekPosition); + // 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; diff --git a/inc/classes/main/stacker/file/class_BaseFileStack.php b/inc/classes/main/stacker/file/class_BaseFileStack.php index f6819a02..1ceda761 100644 --- a/inc/classes/main/stacker/file/class_BaseFileStack.php +++ b/inc/classes/main/stacker/file/class_BaseFileStack.php @@ -27,26 +27,6 @@ class BaseFileStack extends BaseStacker { */ const STACK_MAGIC = 'STACKv0.1'; - /** - * 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; - - /** - * Length of name - */ - const LENGTH_NAME = 10; - /** * Protected constructor * @@ -253,19 +233,6 @@ class BaseFileStack extends BaseStacker { $this->setIndexInstance($indexInstance); } - /** - * Calculates minimum length for one entry/block - * - * @return $length Minimum length for one entry/block - */ - public function caluclateMinimumBlockLength () { - // Calulcate it - $length = self::getHashLength() + strlen(self::SEPARATOR_HASH_NAME) + self::LENGTH_NAME + 1; - - // Return it - return $length; - } - /** * Adds a value to given stack * @@ -440,6 +407,19 @@ class BaseFileStack extends BaseStacker { // Now, simply return the found count value, this must be up-to-date then! return $this->getCounter(); } + + /** + * Calculates minimum length for one entry/block + * + * @return $length Minimum length for one entry/block + */ + public function caluclateMinimumBlockLength () { + // Calulcate it + $length = self::getHashLength() + strlen(chr(self::SEPARATOR_HASH_NAME)) + self::LENGTH_NAME + 1 + strlen(self::getBlockSeparator()); + + // Return it + return $length; + } } // [EOF]