]> git.mxchange.org Git - core.git/commitdiff
Continued with file-based hash:
authorRoland Haeder <roland@mxchange.org>
Sat, 17 May 2014 21:11:24 +0000 (23:11 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 17 May 2014 21:11:24 +0000 (23:11 +0200)
- added a lot stuff, such as getter/setter, seek(), rewind()
- moved class fields totalEntries and seekPosition to BaseFileStack

Signed-off-by: Roland Häder <roland@mxchange.org>
inc/classes/interfaces/io/pointer/io/class_InputOutputPointer.php
inc/classes/interfaces/iterator/class_SeekableWritableFileIterator.php
inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php
inc/classes/main/iterator/io/class_FileIoIterator.php
inc/classes/main/stacker/file/class_BaseFileStack.php

index 6c2de09c78a1a8e49c6156b3d563e8ceca0036b8..529dbe6f1ad36a22677511d7fbbdc2b7ba6fecc1 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 interface InputOutputPointer extends InputPointer, OutputPointer {
+       /**
+        * Rewinds to the beginning of the file
+        *
+        * @return      void
+        */
+       function rewind ();
+
+       /**
+        * Seeks to given position
+        *
+        * @param       $seekPosition   Seek position in file
+        * @param       $whence                 "Seek mode" (see http://de.php.net/fseek)
+        * @return      void
+        */
+       function seek ($seekPosition, $whence = SEEK_SET);
 }
 
 // [EOF]
index 33147d986a5fa064393493b4367e2993f85a79d0..ab64d36626f43f95964003e3d871383bcd48007e 100644 (file)
@@ -37,6 +37,15 @@ interface SeekableWritableFileIterator extends SeekableIterator {
         * @return      $size   Size (in bytes) of file
         */
        function size ();
+
+       /**
+        * Writes at given position by seeking to it.
+        *
+        * @param       $seekPosition   Seek position in file
+        * @param       $data                   Data to be written
+        * @return      void
+        */
+       function writeAtPosition ($seedPosition, $data);
 }
 
 // [EOF]
index 078c206be29400b5aee821748067a73f2827630d..cebb1fad9229640ca38b27f845fe6a8f9cbb6a03 100644 (file)
@@ -115,7 +115,29 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP
                }
 
                // Write data to the file pointer and return written bytes
-               return fwrite($this->getPointer(), $dataStream);
+               return fwrite($this->getPointer(), $dataStream, strlen($dataStream));
+       }
+
+       /**
+        * Rewinds to the beginning of the file
+        *
+        * @return      void
+        */
+       public function rewind () {
+               // Rewind the pointer
+               assert(rewind($this->getPointer() === 1);
+       }
+
+       /**
+        * Seeks to given position
+        *
+        * @param       $seekPosition   Seek position in file
+        * @param       $whence                 "Seek mode" (see http://de.php.net/fseek)
+        * @return      void
+        */
+       public function seek ($seekPosition, $whence = SEEK_SET) {
+               // Move the file pointer
+               assert(fseek($this->getPointerInstance(), $seekPosition, $whence) === 0);
        }
 }
 
index 03c0c25b2f81747e00e17c6c4f422af2a8e58cd5..0dceb7ab31bb6b8725a8662e0170770d764b4727 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 class FileIoIterator extends BaseIterator implements SeekableWritableFileIterator {
-       /**
-        * Current absolute seek position (returned by key())
-        */
-       private $seekPosition = FALSE;
-
-       /**
-        * Total entries (read from file)
-        */
-       private $totalEntriesFile = FALSE;
-
        /**
         * Protected constructor
         *
@@ -80,13 +70,8 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * @return      $key    Current key in iteration
         */
        public function key () {
-               // Default is null
-               $key = null;
-
-               $this->partialStub('Please implement this method.');
-
                // Return it
-               return $key;
+               return $this->getPointerInstance()->getSeekPosition();
        }
 
        /**
@@ -104,7 +89,8 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * @return      void
         */
        public function rewind () {
-               $this->partialStub('Please implement this method.');
+               // Call pointer instance
+               $this->getPointerInstance()->rewind();
        }
 
        /**
@@ -113,8 +99,24 @@ class FileIoIterator extends BaseIterator implements SeekableWritableFileIterato
         * @param       $seekPosition   Seek position in file
         * @return      void
         */
-       public function seek ($seedPosition) {
-               $this->partialStub('Please implement this method. seekPosition=' . $seekPosition);
+       public function seek ($seekPosition) {
+               // Call pointer instance
+               $this->getPointerInstance()->seek($seekPosition);
+       }
+
+       /**
+        * Writes at given position by seeking to it.
+        *
+        * @param       $seekPosition   Seek position in file
+        * @param       $data                   Data to be written
+        * @return      void
+        */
+       public function writeAtPosition ($seedPosition, $data) {
+               // First seek to it
+               $this->seek($seekPosition);
+
+               // Then write the data at that position
+               $this->getPointerInstance()->writeToFile($data);
        }
 
        /**
index 8491479d9a41ae7efc474d374b68a688218a1434..70bd7006f5c1c345e273f59790c256470af3a0f8 100644 (file)
@@ -52,6 +52,16 @@ class BaseFileStack extends BaseStacker {
         */
        const COUNT_POSITION = 20;
 
+       /**
+        * Counter for total entries
+        */
+       private $totalEntries = 0;
+
+       /**
+        * Current seek position
+        */
+       private $seekPosition = 0;
+
        /**
         * Protected constructor
         *
@@ -63,6 +73,60 @@ class BaseFileStack extends BaseStacker {
                parent::__construct($className);
        }
 
+       /**
+        * Getter for total entries
+        *
+        * @return      $totalEntries   Total entries in this stack
+        */
+       private function getCounter () {
+               // Get it
+               return $this->totalEntries;
+       }
+
+       /**
+        * Increment counter
+        *
+        * @return      void
+        */
+       private function incrementCounter () {
+               // Get it
+               $this->totalEntries++;
+       }
+
+       /**
+        * Getter for seek position
+        *
+        * @return      $seekPosition   Current seek position (stored here in object)
+        */
+       private function getSeekPosition () {
+               // Get it
+               return $this->seekPosition;
+       }
+
+       /**
+        * Setter for seek position
+        *
+        * @param       $seekPosition   Current seek position (stored here in object)
+        * @return      void
+        */
+       private function setSeekPosition ($seekPosition) {
+               // And set it
+               $this->seekPosition = $seekPosition;
+       }
+
+       /**
+        * Updates seekPosition attribute from file to avoid to much access on file.
+        *
+        * @return      void
+        */
+       private function updateSeekPosition () {
+               // Get key (= seek position)
+               $seekPosition = $this->getIteratorInstance()->key();
+
+               // And set it here
+               $this->setSeekPosition($seekPosition);
+       }
+
        /**
         * Checks whether the file header is initialized
         *
@@ -122,9 +186,6 @@ class BaseFileStack extends BaseStacker {
                // The file's header should not be initialized here
                assert(!$this->isFileHeaderInitialized());
 
-               // Init  counter
-               $this->getIteratorInstance()->initCounter();
-
                // Flush file header
                $this->flushFileHeader();
        }
@@ -144,7 +205,7 @@ class BaseFileStack extends BaseStacker {
                        chr(self::SEPARATOR_MAGIC_COUNT),
 
                        // Total entries (will be zero) and pad it to 20 chars
-                       str_pad($this->dec2hex($this->getIteratorInstance()->getCount()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
+                       str_pad($this->dec2hex($this->getCounter()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
 
                        // Position (will be zero)
                        str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
@@ -153,8 +214,11 @@ class BaseFileStack extends BaseStacker {
                        chr(self::SEPARATOR_SEEK_POS_ENTRIES)
                );
 
-               // Write it to disk
+               // Write it to disk (header is always at seek position 0)
                $this->getIteratorInstance()->writeAtPosition(0, $header);
+
+               // Update seek position
+               $this->updateSeekPosition();
        }
 
        /**