]> git.mxchange.org Git - core.git/commitdiff
Moved some code to BaseFrameworkSystem, and yes: getBlockSeparator() looks stupid...
authorRoland Haeder <roland@mxchange.org>
Fri, 23 May 2014 23:25:04 +0000 (01:25 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 23 May 2014 23:25:04 +0000 (01:25 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/interfaces/calculatable/class_CalculatableBlock.php
inc/classes/main/class_BaseFrameworkSystem.php
inc/classes/main/index/class_BaseIndex.php
inc/classes/main/iterator/io/class_FileIoIterator.php
inc/classes/main/stacker/file/class_BaseFileStack.php

index ba98085729329e0a60a8b3d3de6175c3f9ed720c..6d472687ce2bfe1a73c1d4429ee40041131627aa 100644 (file)
@@ -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]
index 707f5600a6ae6b8bccfa74d914905939973003dd..f9b63b4ff2df46d80e95f12f4f3776dc2132c504 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 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]
index a427c5b7c74632a34ff501afb147d06ab6b7bb7f..de0e5629ab411569ec73b1897d225f32e89919ec 100644 (file)
@@ -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]
index 736280a3db49ab13ac4dd14c089d71fd3e1e4393..7f30fb514f1577c829b7d57eeb01f53512bf98d2 100644 (file)
@@ -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;
index f6819a02deb9b5b2f8870409e121d0e72682adbb..1ceda761f28f0522a590fce0aa5ba5c8c381b0b6 100644 (file)
@@ -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]