Debug infos with var()=%d are for length and can be decimal.
[core.git] / inc / classes / main / file_directories / binary / class_BaseBinaryFile.php
index 230b4cbb1165ad3d0f806e176dbcde9fe84b95f6..e4c3aeb5d5a74fb75fd8a237c091448f4f0bc37e 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-class BaseBinaryFile extends BaseFile {
+class BaseBinaryFile extends BaseAbstractFile {
        /**
         * Separator for header data
         */
@@ -33,19 +33,24 @@ class BaseBinaryFile extends BaseFile {
        const SEPARATOR_HEADER_ENTRIES = 0x02;
 
        /**
-        * Separator hash->name
+        * Separator group->hash
         */
-       const SEPARATOR_HASH_NAME = 0x03;
+       const SEPARATOR_GROUP_HASH = 0x03;
+
+       /**
+        * Separator hash->value
+        */
+       const SEPARATOR_HASH_VALUE = 0x04;
 
        /**
         * Separator entry->entry
         */
-       const SEPARATOR_ENTRIES = 0x04;
+       const SEPARATOR_ENTRIES = 0x05;
 
        /**
         * Separator type->position
         */
-       const SEPARATOR_TYPE_POSITION = 0x05;
+       const SEPARATOR_TYPE_POSITION = 0x06;
 
        /**
         * Length of count
@@ -58,9 +63,9 @@ class BaseBinaryFile extends BaseFile {
        const LENGTH_POSITION = 20;
 
        /**
-        * Length of name
+        * Length of group
         */
-       const LENGTH_NAME = 10;
+       const LENGTH_GROUP = 10;
 
        /**
         * Maximum length of entry type
@@ -225,7 +230,7 @@ class BaseBinaryFile extends BaseFile {
         *
         * @return      $seekPosition   Current seek position (stored here in object)
         */
-       protected final function getSeekPosition () {
+       public final function getSeekPosition () {
                // Get it
                return $this->seekPosition;
        }
@@ -280,7 +285,7 @@ class BaseBinaryFile extends BaseFile {
                // ... to write it back into the file
                $this->flushFileHeader();
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
 
        /**
@@ -294,7 +299,7 @@ class BaseBinaryFile extends BaseFile {
                // Seek to currently ("old") saved position
                $this->seek($this->getSeekPosition());
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
 
        /**
@@ -311,16 +316,6 @@ class BaseBinaryFile extends BaseFile {
                return $isFound;
        }
 
-       /**
-        * 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);
-       }
-
        /**
         * Initializes the back-buffer by setting it to an empty string.
         *
@@ -400,12 +395,15 @@ class BaseBinaryFile extends BaseFile {
         * @param       $flushHeader    Whether to flush the header (default: flush)
         * @return      void
         */
-       protected function writeData ($seekPosition, $data, $flushHeader = TRUE) {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%s - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
+       public function writeData ($seekPosition, $data, $flushHeader = TRUE) {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data()=%d - CALLED!', __METHOD__, __LINE__, $seekPosition, strlen($data)));
 
                // Write data at given position
                $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
 
+               // Increment counter
+               $this->incrementCounter();
+
                // Update seek position
                $this->updateSeekPosition();
 
@@ -516,7 +514,7 @@ class BaseBinaryFile extends BaseFile {
                // Rewind seek position (to beginning of file) and update/flush file header
                $this->rewindUpdateSeekPosition();
 
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!!', __METHOD__, __LINE__));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
        }
 
        /**
@@ -585,7 +583,10 @@ class BaseBinaryFile extends BaseFile {
         * @param       $bytes  Amount of bytes to read
         * @return      $data   Data read from file
         */
-       public function read ($bytes) {
+       public function read ($bytes = NULL) {
+               // $bytes shall be integer
+               assert(is_int($bytes));
+
                // Call pointer instance
                return $this->getPointerInstance()->read($bytes);
        }
@@ -593,7 +594,7 @@ class BaseBinaryFile extends BaseFile {
        /**
         * Rewinds to the beginning of the file
         *
-        * @return      $status         Status of this operation
+        * @return      $status         Status of this operation
         */
        public function rewind () {
                // Call pointer instance
@@ -640,7 +641,7 @@ class BaseBinaryFile extends BaseFile {
                        } // END - if
 
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%s', __METHOD__, __LINE__, strlen($current)));
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current()=%d', __METHOD__, __LINE__, strlen($current)));
                } // END - while
 
                // If the last read block is empty, check gaps
@@ -681,7 +682,7 @@ class BaseBinaryFile extends BaseFile {
                } // END - if
 
                // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%s', __FUNCTION__, __LINE__, $this->key()));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] key()=%d', __FUNCTION__, __LINE__, $this->key()));
 
                // Make sure the block instance is set
                assert($this->getBlockInstance() instanceof CalculatableBlock);
@@ -706,7 +707,7 @@ class BaseBinaryFile extends BaseFile {
                        $block = $this->read($length);
 
                        // Debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%s,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] block()=%d,length=%s', __FUNCTION__, __LINE__, strlen($block), $length));
 
                        // Is it all empty?
                        if (strlen(trim($block)) == 0) {
@@ -721,7 +722,7 @@ class BaseBinaryFile extends BaseFile {
                        $data .= $block;
 
                        // A debug message
-                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%s', __FUNCTION__, __LINE__, strlen($data)));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d] data()=%d', __FUNCTION__, __LINE__, strlen($data)));
                } // END - while
 
                // EOF reached?
@@ -829,6 +830,24 @@ class BaseBinaryFile extends BaseFile {
                // Call block instance
                $this->getBlockInstance()->flushFileHeader();
        }
+
+       /**
+        * Searches for next suitable gap the given length of data can fit in
+        * including padding bytes.
+        *
+        * @param       $length                 Length of raw data
+        * @return      $seekPosition   Found next gap's seek position
+        */
+       public function searchNextGap ($length) {
+               // If the file is only gaps, no need to seek
+               if ($this->isFileOnlyGaps()) {
+                       // The first empty block is the first one right after the header
+                       return ($this->getHeaderSize() + 1);
+               } // END - if
+
+               // @TODO Unfinished
+               $this->partialStub('length=' . $length);
+       }
 }
 
 // [EOF]