// 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;
// Import SPL stuff
use \SplFileInfo;
+use \UnexpectedValueException;
/**
* A general file-based stack class
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseFileStack extends BaseStacker {
+ // Exception codes
+ const EXCEPTION_BAD_MAGIC = 0xe100;
+
/**
* Magic for this stack
*/
*
* @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)
/* 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);
// 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__));
}
* @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,
// 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__));
}
--- /dev/null
+<?php
+// Own namespace
+namespace Org\Mxchange\CoreFramework\FileStack;
+
+// Import framework stuff
+use Org\Mxchange\CoreFramework\Generic\FrameworkException;
+
+/**
+ * Invalid/bad magic found
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @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 <http://www.gnu.org/licenses/>.
+ */
+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);
+ }
+
+}