Debug infos with var()=%d are for length and can be decimal.
[core.git] / inc / classes / main / index / file_stack / class_FileStackIndex.php
index faa79b060c52cc39e67710b19e43f99cd1063ea7..f5a4585de677f9bdf9ec87d960a1eb9b4b67ac49 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 FileStackIndex extends BaseIndex implements IndexableStack {
+class FileStackIndex extends BaseIndex implements IndexableStack, Registerable {
        /**
         * Protected constructor
         *
@@ -42,9 +42,64 @@ class FileStackIndex extends BaseIndex implements IndexableStack {
                // Get a new instance
                $indexInstance = new FileStackIndex();
 
+               // Initialize index
+               $indexInstance->initIndex($fileName);
+
                // Return the prepared instance
                return $indexInstance;
        }
+
+       /**
+        * Adds given hash to an index file
+        *
+        * @param       $groupId        Name of stack to add hash for
+        * @param       $data           Hash and gap position to be added to the index
+        * @return      void
+        */
+       public function addHashToIndex ($groupId, array $data) {
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gap=%d,length=%d - CALLED!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION], $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH]));
+
+               // Raw data been written to the file
+               $rawData = sprintf('%s%s%s%s%s%s%s',
+                       $groupId,
+                       self::SEPARATOR_GROUP_HASH,
+                       hex2bin($data[BaseFileStack::ARRAY_INDEX_HASH]),
+                       self::SEPARATOR_HASH_GAP_POSITION,
+                       $data[BaseFileStack::ARRAY_INDEX_GAP_POSITION],
+                       self::SEPARATOR_GAP_LENGTH,
+                       $data[BaseFileStack::ARRAY_INDEX_DATA_LENGTH]
+               );
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData)));
+
+               // Search for next free gap
+               $gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
+
+               // Gap position cannot be smaller than header length + 1
+               assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], $gapPosition));
+
+               // Then write the data at that gap
+               $this->getIteratorInstance()->writeData($gapPosition, $rawData);
+
+               // Debug message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d - EXIT!', __METHOD__, __LINE__, $groupId, $data[BaseFileStack::ARRAY_INDEX_HASH], strlen($rawData)));
+       }
+
+       /**
+        * 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) {
+               $this->partialStub('length=' . $length);
+       }
 }
 
 // [EOF]