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:
*/
const SEPARATOR_HASH_NAME = 0x05;
+ /**
+ * Length of count
+ */
+ const COUNT_LENGTH = 20;
+
+ /**
+ * Length of position
+ */
+ const COUNT_POSITION = 20;
+
/**
* Protected constructor
*
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.
*
} else {
// No, then create it (which may pre-allocate the stack)
$this->createFileHeader();
+
+ // And pre-allocate a bit
+ $this->preAllocateFile();
}
}