]> git.mxchange.org Git - core.git/commitdiff
Moved even more code to BaseFile where a much better place is for it (clear encapsula...
authorRoland Haeder <roland@mxchange.org>
Sat, 24 May 2014 11:52:47 +0000 (13:52 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 24 May 2014 11:53:13 +0000 (13:53 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/main/file_directories/class_BaseFile.php
inc/classes/main/file_directories/index/class_IndexFile.php
inc/classes/main/file_directories/stack/class_StackFile.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 fb60162627ffd747a23ac92c928ffebe15e152a7..632cbdcec73bce2dfe7dffaf79711c2de2178988 100644 (file)
@@ -27,11 +27,21 @@ class BaseFile extends BaseFrameworkSystem {
         */
        private $fileName = '';
 
         */
        private $fileName = '';
 
+       /**
+        * Back-buffer
+        */
+       private $backBuffer = '';
+
+       /**
+        * Currently loaded block (will be returned by current())
+        */
+       private $currentBlock = '';
+
        /**
         * Protected constructor
         *
         * @param       $className      Name of the class
        /**
         * Protected constructor
         *
         * @param       $className      Name of the class
-        * @return      void
+y       * @return      void
         */
        protected function __construct ($className) {
                // Call parent constructor
         */
        protected function __construct ($className) {
                // Call parent constructor
@@ -84,6 +94,53 @@ class BaseFile extends BaseFrameworkSystem {
                return $this->fileName;
        }
 
                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;
+       }
+
        /**
         * Initializes this file class
         *
        /**
         * Initializes this file class
         *
@@ -193,7 +250,7 @@ class BaseFile extends BaseFrameworkSystem {
         * @return      $isEndOfFileReached             Whether the EOF has been reached
         */
        public final function isEndOfFileReached () {
         * @return      $isEndOfFileReached             Whether the EOF has been reached
         */
        public final function isEndOfFileReached () {
-               return $this->getPointerInstance()->isEndOfFileReached();
+               return $this->isEndOfFileReached();
        }
 
        /**
        }
 
        /**
@@ -232,6 +289,106 @@ class BaseFile extends BaseFrameworkSystem {
 
                //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
 
                //* 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()->caluclateMinimumBlockLength();
+
+               // Short be more than zero!
+               assert($length > 0);
+
+               // Wait until a entry/block separator has been found
+               $data = $this->getBackBuffer();
+               while ((!$this->isEndOfFileReached()) && (!$this->getBlockInstance()->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()->caluclateMinimumBlockLength();
+
+               // 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;
+       }
 }
 
 // [EOF]
 }
 
 // [EOF]
index d416b240b14a771fcb63909fb26e0a90fe14dec5..f8e6be927e19cb557bd787d185cccaa3b6b623fc 100644 (file)
@@ -36,12 +36,16 @@ class IndexFile extends BaseFile implements InputOutputPointer {
         * Creates an instance of this File class and prepares it for usage
         *
         * @param       $fileName               Name of the index file
         * 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 CalculatableBlock class
         * @return      $fileInstance   An instance of this File class
         */
         * @return      $fileInstance   An instance of this File class
         */
-       public final static function createIndexFile ($fileName) {
+       public final static function createIndexFile ($fileName, CalculatableBlock $blockInstance) {
                // Get a new instance
                $fileInstance = new IndexFile();
 
                // Get a new instance
                $fileInstance = new IndexFile();
 
+               // Set block instance here for callbacks
+               $fileInstance->setBlockInstance($blockInstance);
+
                // Init this abstract file
                $fileInstance->initFile($fileName);
 
                // Init this abstract file
                $fileInstance->initFile($fileName);
 
index 4cd1f386210b6c4932bc3e5ff5c7de16463b45fa..299b004276422805c7d681ff1e71ebbe423768f6 100644 (file)
@@ -36,12 +36,16 @@ class StackFile extends BaseFile implements InputOutputPointer {
         * Creates an instance of this File class and prepares it for usage
         *
         * @param       $fileName               Name of the stack file
         * 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 CalculatableBlock class
         * @return      $fileInstance   An instance of this File class
         */
         * @return      $fileInstance   An instance of this File class
         */
-       public final static function createStackFile ($fileName) {
+       public final static function createStackFile ($fileName, CalculatableBlock $blockInstance) {
                // Get a new instance
                $fileInstance = new StackFile();
 
                // Get a new instance
                $fileInstance = new StackFile();
 
+               // Set block instance here for callbacks
+               $fileInstance->setBlockInstance($blockInstance);
+
                // Init this abstract file
                $fileInstance->initFile($fileName);
 
                // Init this abstract file
                $fileInstance->initFile($fileName);
 
index 0ee7b77c251cdeb08c49dda69acdf71bee129de5..fd57724131a179bf518a159ad54e7d141684ef75 100644 (file)
@@ -150,10 +150,10 @@ class BaseIndex extends BaseFrameworkSystem {
                $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
 
                // Get a file i/o pointer instance for index file
                $fileName .= $this->getConfigInstance()->getConfigEntry('index_extension');
 
                // Get a file i/o pointer instance for index file
-               $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName));
+               $fileInstance = ObjectFactory::createObjectByConfiguredName('index_file_class', array($fileName, $this));
 
                // Get iterator instance
 
                // Get iterator instance
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance, $this));
+               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance));
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);
index f2158c63eaf94a790caed9ebbb7ab3a08cc3ce4b..5be1266347c17bf9fac6252a198e914e6e06f07a 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class FileIoIterator extends BaseIterator implements SeekableWritableFileIterator {
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class FileIoIterator extends BaseIterator implements SeekableWritableFileIterator {
-       /**
-        * Back-buffer
-        */
-       private $backBuffer = '';
-
-       /**
-        * Currently loaded block (will be returned by current())
-        */
-       private $currentBlock = '';
-
        /**
         * Protected constructor
         *
        /**
         * Protected constructor
         *
@@ -46,78 +36,27 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * Creates an instance of this class
         *
         * @param       $pointerInstance        An instance of a InputOutputPointer class
         * Creates an instance of this class
         *
         * @param       $pointerInstance        An instance of a InputOutputPointer class
-        * @param       $blockInstance          An instance of a CalculatableBlock class
         * @return      $iteratorInstance       An instance of a Iterator class
         */
         * @return      $iteratorInstance       An instance of a Iterator class
         */
-       public final static function createFileIoIterator (InputOutputPointer $pointerInstance, CalculatableBlock $blockInstance) {
+       public final static function createFileIoIterator (InputOutputPointer $pointerInstance) {
                // Get new instance
                $iteratorInstance = new FileIoIterator();
 
                // Set the instance here
                $iteratorInstance->setPointerInstance($pointerInstance);
 
                // Get new instance
                $iteratorInstance = new FileIoIterator();
 
                // Set the instance here
                $iteratorInstance->setPointerInstance($pointerInstance);
 
-               // Set the block instance here
-               $iteratorInstance->setBlockInstance($blockInstance);
-
                // Return the prepared instance
                return $iteratorInstance;
        }
 
                // Return the prepared instance
                return $iteratorInstance;
        }
 
-       /**
-        * 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 current () {
        /**
         * Gets currently read data
         *
         * @return      $current        Currently read data
         */
        public function current () {
-               // Return it
-               return $this->currentBlock;
+               // Call pointer instance
+               return $this->getPointerInstance()->current();
        }
 
        /**
        }
 
        /**
@@ -134,56 +73,10 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * Advances to next "block" of bytes
         *
         * @return      void
         * Advances to next "block" of bytes
         *
         * @return      void
-        * @todo        This method will load large but empty files in a whole
         */
        public function next () {
         */
        public function next () {
-               // Is there nothing to read?
-               if (!$this->valid()) {
-                       // Nothing to read
-                       return;
-               } // END - if
-
-               // First calculate minimum block length
-               $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
-
-               // Short be more than zero!
-               assert($length > 0);
-
-               // Wait until a entry/block separator has been found
-               $data = $this->getBackBuffer();
-               while ((!$this->getPointerInstance()->isEndOfFileReached()) && (!$this->getBlockInstance()->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->getPointerInstance()->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]);
+               // Call pointer instance
+               $this->getPointerInstance()->next();
        }
 
        /**
        }
 
        /**
@@ -203,35 +96,8 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * @return      $isValid        Whether the next entry is valid
         */
        public function valid () {
         * @return      $isValid        Whether the next entry is valid
         */
        public function valid () {
-               // First calculate minimum block length
-               $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
-
-               // 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;
+               // Call pointer instance
+               return $this->getPointerInstance()->valid();
        }
 
        /**
        }
 
        /**
index 8e3ae4d8e11267a82923ec8e2381da7f5d61a312..9219ecc3d132f4458e9e2fe79da64e37642e429b 100644 (file)
@@ -161,10 +161,10 @@ class BaseFileStack extends BaseStacker {
         */
        protected function initFileStack ($fileName, $type) {
                // Get a stack file instance
         */
        protected function initFileStack ($fileName, $type) {
                // Get a stack file instance
-               $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileName));
+               $fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileName, $this));
 
                // Get iterator instance
 
                // Get iterator instance
-               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance, $this));
+               $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($fileInstance));
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);
 
                // Is the instance implementing the right interface?
                assert($iteratorInstance instanceof SeekableWritableFileIterator);