From: Roland Häder Date: Tue, 24 Nov 2020 06:15:51 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=c83be8d1aeaccd8a81a5595a82712f7848250f52;hp=498e6b065ce47804bff4e1073592a2cc8e28f8ef Continued: - rewrote more assert() lines to thrown exceptions Signed-off-by: Roland Häder --- diff --git a/framework/main/classes/stacker/file/class_BaseFileStack.php b/framework/main/classes/stacker/file/class_BaseFileStack.php index 388f33ee..6c1f3d1c 100644 --- a/framework/main/classes/stacker/file/class_BaseFileStack.php +++ b/framework/main/classes/stacker/file/class_BaseFileStack.php @@ -5,6 +5,7 @@ namespace Org\Mxchange\CoreFramework\Stacker\Filesystem; // Import framework stuff use Org\Mxchange\CoreFramework\Factory\Filesystem\Stack\FileStackIndexFactory; use Org\Mxchange\CoreFramework\Factory\ObjectFactory; +use Org\Mxchange\CoreFramework\FileStack\InvalidMagicException; use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile; use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException; use Org\Mxchange\CoreFramework\Index\Indexable; @@ -14,6 +15,7 @@ use Org\Mxchange\CoreFramework\Utils\String\StringUtils; // Import SPL stuff use \SplFileInfo; +use \UnexpectedValueException; /** * A general file-based stack class @@ -38,6 +40,9 @@ use \SplFileInfo; * along with this program. If not, see . */ abstract class BaseFileStack extends BaseStacker { + // Exception codes + const EXCEPTION_BAD_MAGIC = 0xe100; + /** * Magic for this stack */ @@ -98,11 +103,12 @@ abstract class BaseFileStack extends BaseStacker { * * @return void * @todo To hard assertions here, better rewrite them to exceptions + * @throws UnexpectedValueException If header is not proper length + * @throws InvalidMagicException If a bad magic was found */ public function readFileHeader () { - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__)); - // First rewind to beginning as the header sits at the beginning ... + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__)); $this->getIteratorInstance()->rewind(); // Then read it (see constructor for calculation) @@ -110,13 +116,24 @@ abstract class BaseFileStack extends BaseStacker { /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize())); // Have all requested bytes been read? - assert(strlen($data) == $this->getIteratorInstance()->getHeaderSize()); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__)); + if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) { + // Bad data length + throw new UnexpectedValueException(sprintf('data(%d)=%s does not match iteratorInstance->headerSize=%d', + strlen($data), + $data, + $this->getIteratorInstance()->getHeaderSize() + )); + } // Last character must be the separator /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: data(-1)=%s', dechex(ord(substr($data, -1, 1))))); - assert(substr($data, -1, 1) == chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__)); + if (substr($data, -1, 1) !== chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) { + // Not valid separator + throw new UnexpectedValueException(sprintf('data=%s does not have separator=%s at the end.', + $data, + BaseBinaryFile::SEPARATOR_HEADER_ENTRIES + )); + } // Okay, then remove it $data = substr($data, 0, -1); @@ -138,23 +155,39 @@ 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(%d)=%s', count($header), print_r($header, true))); - assert(count($header) == 3); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__)); - - // Check magic - assert($header[0] == self::STACK_MAGIC); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__)); + if (count($header) != 3) { + // Header array count is not expected + throw new UnexpectedValueException(sprintf('data=%s has %d elements, expected 3', + $data, + count($header) + )); + } elseif ($header[0] != self::STACK_MAGIC) { + // Bad magic + throw new InvalidMagicException($data, self::EXCEPTION_BAD_MAGIC); + } // Check length of count and seek position - assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__)); - assert(strlen($header[2]) == BaseBinaryFile::LENGTH_POSITION); - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__)); + if (strlen($header[1]) != BaseBinaryFile::LENGTH_COUNT) { + // Count length not valid + throw new UnexpectedValueException(sprintf('header[1](%d)=%s is not expected %d length', + strlen($header[1]), + $header[1], + BaseBinaryFile::LENGTH_COUNT + )); + } elseif (strlen($header[1]) != BaseBinaryFile::LENGTH_POSITION) { + // Position length not valid + throw new UnexpectedValueException(sprintf('header[2](%d)=%s is not expected %d length', + strlen($header[1]), + $header[1], + BaseBinaryFile::LENGTH_POSITION + )); + } // Decode count and seek position $header[1] = hex2bin($header[1]); $header[2] = hex2bin($header[2]); + // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__)); } @@ -164,9 +197,8 @@ abstract class BaseFileStack extends BaseStacker { * @return void */ public function flushFileHeader () { - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__)); - // Put all informations together + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__)); $header = sprintf('%s%s%s%s%s%s', // Magic self::STACK_MAGIC, @@ -190,6 +222,7 @@ abstract class BaseFileStack extends BaseStacker { // Write it to disk (header is always at seek position 0) $this->getIteratorInstance()->writeData(0, $header, false); + // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__)); } diff --git a/framework/main/exceptions/class_ b/framework/main/exceptions/class_ index c6ce6228..8b8d392b 100644 --- a/framework/main/exceptions/class_ +++ b/framework/main/exceptions/class_ @@ -29,7 +29,7 @@ class ???Exception extends FrameworkException { * @param $code Error code * @return void */ - public function __construct ($message, $code) { + public function __construct (string $message, int $code) { // Call parent exception constructor parent::__construct($message, $code); } diff --git a/framework/main/exceptions/stacker/class_InvalidMagicException.php b/framework/main/exceptions/stacker/class_InvalidMagicException.php new file mode 100644 index 00000000..ade8fe2f --- /dev/null +++ b/framework/main/exceptions/stacker/class_InvalidMagicException.php @@ -0,0 +1,50 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class InvalidMagicException extends FrameworkException { + /** + * Constructor with data string + * + * @param $data Raw data + * @param $code Error code + * @return void + */ + public function __construct (string $data, int $code) { + // Construct message + $message = sprintf('data(%d)=%s has a bad magic, expected: %s', + strlen($data), + $data, + BaseFileStack::STACK_MAGIC + ); + + // Call parent exception constructor + parent::__construct($message, $code); + } + +}