From c837cb3fc838a6a303ed38b51ca7f4e9fc278fbd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 8 Jan 2021 08:27:10 +0100 Subject: [PATCH] Continued: - added missing interface constants - also BaseFileIndex' header array has now associative array elements MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../index/file/class_BaseFileIndex.php | 25 ++++++++++++------- .../stacker/file/class_BaseFileStack.php | 13 ++++------ .../filesystem/binary/class_BinaryFile.php | 9 +++++++ 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/framework/main/classes/index/file/class_BaseFileIndex.php b/framework/main/classes/index/file/class_BaseFileIndex.php index ae2fa3a9..2b536cc4 100644 --- a/framework/main/classes/index/file/class_BaseFileIndex.php +++ b/framework/main/classes/index/file/class_BaseFileIndex.php @@ -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); diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index 59aac997..0a2ca5ce 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -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, diff --git a/framework/main/interfaces/filesystem/binary/class_BinaryFile.php b/framework/main/interfaces/filesystem/binary/class_BinaryFile.php index cdaa2350..6aa0e979 100644 --- a/framework/main/interfaces/filesystem/binary/class_BinaryFile.php +++ b/framework/main/interfaces/filesystem/binary/class_BinaryFile.php @@ -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 * -- 2.39.5