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 ba98085..6d47268 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 707f560..f9b63b4 100644 (file)
  */
 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
         */
        const LENGTH_COUNT = 20;
@@ -34,6 +54,11 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        const LENGTH_POSITION = 20;
 
        /**
+        * Length of name
+        */
+       const LENGTH_NAME = 10;
+
+       /**
         * The real class name
         */
        private $realClass = 'BaseFrameworkSystem';
@@ -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 a427c5b..de0e562 100644 (file)
@@ -28,16 +28,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
         *
         * @param       $className      Name of the class
@@ -186,20 +176,6 @@ class BaseIndex extends BaseFrameworkSystem {
        }
 
        /**
-        * 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
         *
         * @param       $fileName       File name of 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 736280a..7f30fb5 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 f6819a0..1ceda76 100644 (file)
@@ -28,26 +28,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
         *
         * @param       $className      Name of the class
@@ -254,19 +234,6 @@ class BaseFileStack extends BaseStacker {
        }
 
        /**
-        * 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
         *
         * @param       $stackerName    Name of the 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]