Added more methods (required).
[core.git] / inc / classes / main / file_directories / class_BaseFile.php
index 8881b6a64b60cbc55e3c97c401a9cbae1fd6a1b5..303371c6791e25579d226e7a0f32693f8f577a1f 100644 (file)
@@ -27,15 +27,28 @@ class BaseFile extends BaseFrameworkSystem {
         */
        private $fileName = '';
 
+       /**
+        * Back-buffer
+        */
+       private $backBuffer = '';
+
+       /**
+        * Currently loaded block (will be returned by current())
+        */
+       private $currentBlock = '';
+
        /**
         * Protected constructor
         *
         * @param       $className      Name of the class
-        * @return      void
+y       * @return      void
         */
        protected function __construct ($className) {
                // Call parent constructor
                parent::__construct($className);
+
+               // Init counters and gaps array
+               $this->initCountersGapsArray();
        }
 
        /**
@@ -51,6 +64,16 @@ class BaseFile extends BaseFrameworkSystem {
                parent::__destruct();
        }
 
+       /**
+        * Getter for the file pointer
+        *
+        * @return      $filePointer    The file pointer which shall be a valid file resource
+        * @throws      UnsupportedOperationException   If this method is called
+        */
+       public final function getPointer () {
+               throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
+       }
+
        /**
         * Setter for file name
         *
@@ -71,6 +94,63 @@ class BaseFile extends BaseFrameworkSystem {
                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;
+       }
+
+       /**
+        * Gets currently read data
+        *
+        * @return      $current        Currently read data
+        */
+       public function getCurrentBlock () {
+               // Return it
+               return $this->currentBlock;
+       }
+
        /**
         * Initializes this file class
         *
@@ -105,6 +185,7 @@ class BaseFile extends BaseFrameworkSystem {
         * @return      $seekPosition   Current seek position
         */
        public function determineSeekPosition () {
+               // Call pointer instance
                return $this->getPointerInstance()->determineSeekPosition();
        }
 
@@ -116,6 +197,7 @@ class BaseFile extends BaseFrameworkSystem {
         * @return      $status         Status of file seek: 0 = success, -1 = failed
         */
        public function seek ($offset, $whence = SEEK_SET) {
+               // Call pointer instance
                return $this->getPointerInstance()->seek($offset, $whence);
        }
 
@@ -126,6 +208,7 @@ class BaseFile extends BaseFrameworkSystem {
         * @todo        Handle seekStatus
         */
        public function size () {
+               // Call pointer instance
                return $this->getPointerInstance()->size();
        }
 
@@ -138,6 +221,7 @@ class BaseFile extends BaseFrameworkSystem {
         * @throws      InvalidResourceException        If there is being set
         */
        public function readFromFile () {
+               // Call pointer instance
                return $this->getPointerInstance()->readFromFile();
        }
 
@@ -148,6 +232,7 @@ class BaseFile extends BaseFrameworkSystem {
         * @return      $data   Data read from file
         */
        public function read ($bytes) {
+               // Call pointer instance
                return $this->getPointerInstance()->read($bytes);
        }
 
@@ -162,6 +247,7 @@ class BaseFile extends BaseFrameworkSystem {
         *                                                                                      an invalid file resource
         */
        public function writeToFile ($dataStream) {
+               // Call pointer instance
                return $this->getPointerInstance()->writeToFile($dataStream);
        }
 
@@ -171,8 +257,166 @@ class BaseFile extends BaseFrameworkSystem {
         * @return      $status         Status of this operation
         */
        public function rewind () {
+               // Call pointer instance
                return $this->getPointerInstance()->rewind();
        }
+
+       /**
+        * Determines whether the EOF has been reached
+        *
+        * @return      $isEndOfFileReached             Whether the EOF has been reached
+        */
+       public final function isEndOfFileReached () {
+               // Call pointer instance
+               return $this->getPointerInstance()->isEndOfFileReached();
+       }
+
+       /**
+        * Analyzes entries in index file. This will count all found (and valid)
+        * entries, mark invalid as damaged and count gaps ("fragmentation"). If
+        * only gaps are found, the file is considered as "virgin" (no entries).
+        *
+        * @return      void
+        */
+       public function analyzeFile () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+
+               // Make sure the file is initialized
+               assert($this->isFileInitialized());
+
+               // Init counters and gaps array
+               $this->initCountersGapsArray();
+
+               // Output message (as this may take some time)
+               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
+
+               // First rewind to the begining
+               $this->rewind();
+
+               // Then try to load all entries
+               while ($this->valid()) {
+                       // Go to next entry
+                       $this->next();
+
+                       // Get current entry
+                       $current = $this->getCurrentBlock();
+
+                       // Simply output it
+                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE)));
+               } // END - while
+
+               //* 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;
+       }
+
+       /**
+        * Gets current seek position ("key").
+        *
+        * @return      $key    Current key in iteration
+        */
+       public function key () {
+               // Call pointer instance
+               return $this->getPointerInstance()->determineSeekPosition();
+       }
 }
 
 // [EOF]