]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Fri, 8 Jan 2021 07:27:10 +0000 (08:27 +0100)
committerRoland Häder <roland@mxchange.org>
Fri, 8 Jan 2021 07:27:10 +0000 (08:27 +0100)
- added missing interface constants
- also BaseFileIndex' header array has now associative array elements

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/main/classes/index/file/class_BaseFileIndex.php
framework/main/classes/stacker/file/class_BaseFileStack.php
framework/main/interfaces/filesystem/binary/class_BinaryFile.php

index ae2fa3a914ba889d17fc3b6cc8ed15db08428496..2b536cc4caa0784cfe29c5b1faf2b2afdf7f752c 100644 (file)
@@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
 use Org\Mxchange\CoreFramework\Filesystem\File\BinaryFile;
 use Org\Mxchange\CoreFramework\Index\BaseIndex;
 use Org\Mxchange\CoreFramework\Index\Indexable;
+use Org\Mxchange\CoreFramework\Utils\Arrays\ArrayUtils;
 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 
 // Import SPL stuff
@@ -109,22 +110,28 @@ abstract class BaseFileIndex extends BaseIndex implements FileIndexer {
                 */
                $header = explode(chr(BinaryFile::SEPARATOR_HEADER_DATA), $data);
 
-               // Check if the array has only 3 elements
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: header()=%d', count($header)));
+               // Map numeric entries to associative (alpha-numeric) elements
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: HEADER_INDEX_ELEMENT_COUNT=%d,header()=%d', BinaryFile::HEADER_INDEX_ELEMENT_COUNT, count($header)));
                //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-INDEX: header(%d)=%s', count($header), print_r($header, true)));
-               if (count($header) != 2) {
+               $header = ArrayUtils::mapNumericKeysToAssociative($header, [
+                       BinaryFile::HEADER_NAME_MAGIC,
+                       BinaryFile::HEADER_NAME_TOTAL_ENTRIES,
+               ]);
+
+               // Check if the array has only 2 elements
+               if (count($header) != BinaryFile::HEADER_INDEX_ELEMENT_COUNT) {
                        // Bad header
-                       throw new UnexpectedValueException(sprintf('header()=%d is not expected value 2', count($header)));
-               } elseif ($header[0] !== Indexable::INDEX_MAGIC) {
+                       throw new UnexpectedValueException(sprintf('header()=%d is not expected value %d', count($header), BinaryFile::HEADER_INDEX_ELEMENT_COUNT));
+               } elseif ($header[BinaryFile::HEADER_NAME_MAGIC] !== Indexable::INDEX_MAGIC) {
                        // Magic must be in first element
-                       throw new UnexpectedValueException(sprintf('header[0]=%s is not the expected magic (%s)', $header[0], Indexable::INDEX_MAGIC));
-               } elseif (strlen($header[1]) != BinaryFile::LENGTH_COUNT) {
+                       throw new UnexpectedValueException(sprintf('header[%s]=%s is not the expected magic (%s)', BinaryFile::HEADER_NAME_MAGIC, $header[BinaryFile::HEADER_NAME_MAGIC], Indexable::INDEX_MAGIC));
+               } elseif (strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]) != BinaryFile::LENGTH_COUNT) {
                        // Length of total entries not matching
-                       throw new UnexpectedValueException(sprintf('header[1](%d)=%s does not have expected length %d', strlen($header[1]), $header[1], BinaryFile::LENGTH_COUNT));
+                       throw new UnexpectedValueException(sprintf('header[%s](%d)=%s does not have expected length %d', BinaryFile::HEADER_NAME_TOTAL_ENTRIES, strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]), $header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES], BinaryFile::LENGTH_COUNT));
                }
 
                // Decode count
-               $header[1] = hex2bin($header[1]);
+               $header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES] = hex2bin($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]);
 
                // Set it here
                $this->getIteratorInstance()->getBinaryFileInstance()->setHeader($header);
index 59aac997e63e6925c4233c9e0dd48d33d9f47325..0a2ca5ce2f4aab2176823f43ead80463cfaa27c8 100644 (file)
@@ -11,8 +11,8 @@ use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
 use Org\Mxchange\CoreFramework\Stack\BaseStacker;
 use Org\Mxchange\CoreFramework\Traits\Index\IndexableTrait;
 use Org\Mxchange\CoreFramework\Traits\Iterator\IteratorTrait;
-use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 use Org\Mxchange\CoreFramework\Utils\Arrays\ArrayUtils;
+use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 
 // Import SPL stuff
 use \InvalidArgumentException;
@@ -135,21 +135,18 @@ abstract class BaseFileStack extends BaseStacker {
                ]);
 
                // Check if the array has only 3 elements
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_ELEMENT_COUNT=%d,header(%d)=%s', BinaryFile::HEADER_ELEMENT_COUNT, count($header), print_r($header, true)));
-               if (count($header) != BinaryFile::HEADER_ELEMENT_COUNT) {
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: HEADER_STACK_ELEMENT_COUNT=%d,header(%d)=%s', BinaryFile::HEADER_STACK_ELEMENT_COUNT, count($header), print_r($header, true)));
+               if (count($header) != BinaryFile::HEADER_STACK_ELEMENT_COUNT) {
                        // Header array count is not expected
                        throw new UnexpectedValueException(sprintf('data=%s has %d elements, but expected is %d',
                                $data,
                                count($header),
-                               BinaryFile::HEADER_ELEMENT_COUNT
+                               BinaryFile::HEADER_STACK_ELEMENT_COUNT
                        ));
                } elseif ($header[BinaryFile::HEADER_NAME_MAGIC] != StackableFile::STACK_MAGIC) {
                        // Bad magic
                        throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC);
-               }
-
-               // Check length of count and seek position
-               if (strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]) != BinaryFile::LENGTH_COUNT) {
+               } elseif (strlen($header[BinaryFile::HEADER_NAME_TOTAL_ENTRIES]) != BinaryFile::LENGTH_COUNT) {
                        // Count length not valid
                        throw new UnexpectedValueException(sprintf('header[%s](%d)=%s is not expected %d length',
                                BinaryFile::HEADER_NAME_TOTAL_ENTRIES,
index cdaa235042a9812625833de78d06b23e6d1d1c01..6aa0e97948b2eb7f205db88e14716188c669d2bc 100644 (file)
@@ -90,6 +90,15 @@ interface BinaryFile extends Filesystem {
         */
        const GAPS_INDEX_END = 'end';
 
+       // Header names
+       const HEADER_NAME_MAGIC = 'magic';
+       const HEADER_NAME_TOTAL_ENTRIES = 'total';
+       const HEADER_NAME_SEEK_POSITION = 'seek';
+
+       // Header element counts
+       const HEADER_INDEX_ELEMENT_COUNT = 2;
+       const HEADER_STACK_ELEMENT_COUNT = 3;
+
        /**
         * Reads the file header
         *