Continued with file-based stacks:
authorRoland Haeder <roland@mxchange.org>
Sat, 17 May 2014 20:41:22 +0000 (22:41 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 17 May 2014 20:41:22 +0000 (22:41 +0200)
- added flushFileHeader()
- still there will be a lot stub messages

Signed-off-by: Roland Häder <roland@mxchange.org>
contrib/file_stack/format.txt
inc/classes/main/stacker/file/class_BaseFileStack.php

index 400443863194fc12a7a93ab51e07baa34afd6cd4..637baf6f811203c1cf9449e20a223674c8e1297b 100644 (file)
@@ -5,11 +5,11 @@ This is the format for a file-based stack which works as any Stackable class.
 
 General made up:
 
-Purpose: | "magic"   | separator | count    | position      | separator |
----------+-----------+-----------+----------+---------------+-----------+
-Bytes:   | 9         | 1         | 20 (hex) | 2 - n^2 (hex) | 1         |
----------+-----------+-----------+----------+---------------+-----------+
-Example: | STACKv1.0 | 00        | 00...ff  | 05            | ff        |
+Purpose: | "magic"   | separator | count    | position | separator |
+---------+-----------+-----------+----------+----------+-----------+
+Bytes:   | 9         | 1         | 20 (hex) | 20 (hex) | 1         |
+---------+-----------+-----------+----------+----------+-----------+
+Example: | STACKv1.0 | 00        | 00...ff  | 00...ff  | ff        |
 
 Continued:
 
index f0539e10fc0f0130a222cae198b9ca370f48f295..8491479d9a41ae7efc474d374b68a688218a1434 100644 (file)
@@ -42,6 +42,16 @@ class BaseFileStack extends BaseStacker {
         */
        const SEPARATOR_HASH_NAME = 0x05;
 
+       /**
+        * Length of count
+        */
+       const COUNT_LENGTH = 20;
+
+       /**
+        * Length of position
+        */
+       const COUNT_POSITION = 20;
+
        /**
         * Protected constructor
         *
@@ -103,6 +113,50 @@ class BaseFileStack extends BaseStacker {
                return $isInitialized;
        }
 
+       /**
+        * Creates the file-stack's header
+        *
+        * @return      void
+        */
+       private function createFileHeader () {
+               // The file's header should not be initialized here
+               assert(!$this->isFileHeaderInitialized());
+
+               // Init  counter
+               $this->getIteratorInstance()->initCounter();
+
+               // Flush file header
+               $this->flushFileHeader();
+       }
+
+       /**
+        * Flushes the file header
+        *
+        * @return      void
+        */
+       private function flushFileHeader () {
+               // Put all informations together
+               $header = sprintf('%s%s%s%s%s',
+                       // Magic
+                       self::STACK_MAGIC,
+
+                       // Separator magic<->count
+                       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),
+
+                       // Position (will be zero)
+                       str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
+
+                       // Separator position<->entries
+                       chr(self::SEPARATOR_SEEK_POS_ENTRIES)
+               );
+
+               // Write it to disk
+               $this->getIteratorInstance()->writeAtPosition(0, $header);
+       }
+
        /**
         * Initializes this file-based stack.
         *
@@ -129,6 +183,9 @@ class BaseFileStack extends BaseStacker {
                } else {
                        // No, then create it (which may pre-allocate the stack)
                        $this->createFileHeader();
+
+                       // And pre-allocate a bit
+                       $this->preAllocateFile();
                }
        }