]> git.mxchange.org Git - core.git/commitdiff
Possible implementation for next().
authorRoland Haeder <roland@mxchange.org>
Fri, 23 May 2014 23:02:25 +0000 (01:02 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 23 May 2014 23:02:25 +0000 (01:02 +0200)
Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/main/decorator/cache/.htaccess [new file with mode: 0644]
inc/classes/main/iterator/io/class_FileIoIterator.php

diff --git a/inc/classes/main/decorator/cache/.htaccess b/inc/classes/main/decorator/cache/.htaccess
new file mode 100644 (file)
index 0000000..3a42882
--- /dev/null
@@ -0,0 +1 @@
+Deny from all
index 12cf82ea461e8f32fc5e3f32e4b11b6627dbf57a..736280a3db49ab13ac4dd14c089d71fd3e1e4393 100644 (file)
  * 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
         *
@@ -53,19 +63,61 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
                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 () {
-               // Default is null
-               $current = null;
-
-               $this->partialStub('Please implement this method.');
-
                // Return it
-               return $current;
+               return $this->currentBlock;
        }
 
        /**
@@ -84,7 +136,37 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * @return      void
         */
        public function next () {
-               $this->partialStub('Please implement this method.');
+               // 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->getBlockInstance()->isBlockSeparatorFound($data)) {
+                       // Then read the block
+                       $data .= $this->read($length);
+               } // END - if
+
+               /*
+                * Init back-buffer which is the data that has been found beyond the
+                * separator.
+                */
+               $this->initBackBuffer();
+
+               // Separate data
+               $dataArray = explode($this->getBlockInstance()->getBlockSeparator(), $data);
+
+               // Left part is the actual block, right one the back-buffer data
+               $this->setCurrentBlock($dataArray[0]);
+               $this->setBackBuffer($dataArray[1]);
        }
 
        /**