use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
use Org\Mxchange\CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
-use Org\Mxchange\CoreFramework\Filesystem\Block;
use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
-use Org\Mxchange\CoreFramework\Filesystem\FrameworkDirectory;
use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
use Org\Mxchange\CoreFramework\Generic\NullPointerException;
use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
use Org\Mxchange\CoreFramework\Handler\Handleable;
use Org\Mxchange\CoreFramework\Helper\Helper;
-use Org\Mxchange\CoreFramework\Index\Indexable;
use Org\Mxchange\CoreFramework\Lists\Listable;
use Org\Mxchange\CoreFramework\Loader\ClassLoader;
use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
*/
private $pointerInstance = NULL;
- /**
- * An instance of an Indexable class
- */
- private $indexInstance = NULL;
-
- /**
- * An instance of a Block class
- */
- private $blockInstance = NULL;
-
- /**
- * A FrameworkDirectory instance
- */
- private $directoryInstance = NULL;
-
- /**
- * The concrete output instance
- */
- private $outputInstance = NULL;
-
/**
* State instance
*/
const EXCEPTION_INVALID_SOCKET = 0x03e;
const EXCEPTION_SELF_INSTANCE = 0x03f;
- /**
- * Hexadecimal->Decimal translation array
- */
- private static $hexdec = array(
- '0' => 0,
- '1' => 1,
- '2' => 2,
- '3' => 3,
- '4' => 4,
- '5' => 5,
- '6' => 6,
- '7' => 7,
- '8' => 8,
- '9' => 9,
- 'a' => 10,
- 'b' => 11,
- 'c' => 12,
- 'd' => 13,
- 'e' => 14,
- 'f' => 15
- );
-
- /**
- * Decimal->hexadecimal translation array
- */
- private static $dechex = array(
- 0 => '0',
- 1 => '1',
- 2 => '2',
- 3 => '3',
- 4 => '4',
- 5 => '5',
- 6 => '6',
- 7 => '7',
- 8 => '8',
- 9 => '9',
- 10 => 'a',
- 11 => 'b',
- 12 => 'c',
- 13 => 'd',
- 14 => 'e',
- 15 => 'f'
- );
-
/**
* Startup time in miliseconds
*/
$this->pointerInstance = NULL;
}
- /**
- * Setter for Indexable instance
- *
- * @param $indexInstance An instance of an Indexable class
- * @return void
- */
- protected final function setIndexInstance (Indexable $indexInstance) {
- $this->indexInstance = $indexInstance;
- }
-
- /**
- * Getter for Indexable instance
- *
- * @return $indexInstance An instance of an Indexable class
- */
- public final function getIndexInstance () {
- return $this->indexInstance;
- }
-
- /**
- * Setter for Block instance
- *
- * @param $blockInstance An instance of an Block class
- * @return void
- */
- protected final function setBlockInstance (Block $blockInstance) {
- $this->blockInstance = $blockInstance;
- }
-
- /**
- * Getter for Block instance
- *
- * @return $blockInstance An instance of an Block class
- */
- public final function getBlockInstance () {
- return $this->blockInstance;
- }
-
- /**
- * Setter for FrameworkDirectory instance
- *
- * @param $directoryInstance A FrameworkDirectory instance
- * @return void
- */
- protected final function setDirectoryInstance (FrameworkDirectory $directoryInstance) {
- $this->directoryInstance = $directoryInstance;
- }
-
- /**
- * Getter for FrameworkDirectory instance
- *
- * @return $directoryInstance A FrameworkDirectory instance
- */
- protected final function getDirectoryInstance () {
- return $this->directoryInstance;
- }
-
/**
* Setter for state instance
*
return $this->stateInstance;
}
- /**
- * Setter for output instance
- *
- * @param $outputInstance The debug output instance
- * @return void
- */
- public final function setOutputInstance (OutputStreamer $outputInstance) {
- $this->outputInstance = $outputInstance;
- }
-
- /**
- * Getter for output instance
- *
- * @return $outputInstance The debug output instance
- */
- public final function getOutputInstance () {
- return $this->outputInstance;
- }
-
/**
* Setter for call-back instance
*
// Return result
return $hasSlept;
}
- /**
- * Converts a hexadecimal string, even with negative sign as first string to
- * a decimal number using BC functions.
- *
- * This work is based on comment #86673 on php.net documentation page at:
- * <http://de.php.net/manual/en/function.dechex.php#86673>
- *
- * @param $hex Hexadecimal string
- * @return $dec Decimal number
- */
- protected function hex2dec ($hex) {
- // Convert to all lower-case
- $hex = strtolower($hex);
-
- // Detect sign (negative/positive numbers)
- $sign = '';
- if (substr($hex, 0, 1) == '-') {
- $sign = '-';
- $hex = substr($hex, 1);
- } // END - if
-
- // Decode the hexadecimal string into a decimal number
- $dec = 0;
- for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) {
- $factor = self::$hexdec[substr($hex, $i, 1)];
- $dec = bcadd($dec, bcmul($factor, $e));
- } // END - for
-
- // Return the decimal number
- return $sign . $dec;
- }
-
- /**
- * Converts even very large decimal numbers, also signed, to a hexadecimal
- * string.
- *
- * This work is based on comment #97756 on php.net documentation page at:
- * <http://de.php.net/manual/en/function.hexdec.php#97756>
- *
- * @param $dec Decimal number, even with negative sign
- * @param $maxLength Optional maximum length of the string
- * @return $hex Hexadecimal string
- */
- protected function dec2hex ($dec, $maxLength = 0) {
- // maxLength can be zero or devideable by 2
- assert(($maxLength == 0) || (($maxLength % 2) == 0));
-
- // Detect sign (negative/positive numbers)
- $sign = '';
- if ($dec < 0) {
- $sign = '-';
- $dec = abs($dec);
- } // END - if
-
- // Encode the decimal number into a hexadecimal string
- $hex = '';
- do {
- $hex = self::$dechex[($dec % (2 ^ 4))] . $hex;
- $dec /= (2 ^ 4);
- } while ($dec >= 1);
-
- /*
- * Leading zeros are required for hex-decimal "numbers". In some
- * situations more leading zeros are wanted, so check for both
- * conditions.
- */
- if ($maxLength > 0) {
- // Prepend more zeros
- $hex = str_pad($hex, $maxLength, '0', STR_PAD_LEFT);
- } elseif ((strlen($hex) % 2) != 0) {
- // Only make string's length dividable by 2
- $hex = '0' . $hex;
- }
-
- // Return the hexadecimal string
- return $sign . $hex;
- }
-
- /**
- * Converts a ASCII string (0 to 255) into a decimal number.
- *
- * @param $asc The ASCII string to be converted
- * @return $dec Decimal number
- */
- protected function asc2dec ($asc) {
- // Convert it into a hexadecimal number
- $hex = bin2hex($asc);
-
- // And back into a decimal number
- $dec = $this->hex2dec($hex);
-
- // Return it
- return $dec;
- }
-
- /**
- * Converts a decimal number into an ASCII string.
- *
- * @param $dec Decimal number
- * @return $asc An ASCII string
- */
- protected function dec2asc ($dec) {
- // First convert the number into a hexadecimal string
- $hex = $this->dec2hex($dec);
-
- // Then convert it into the ASCII string
- $asc = $this->hex2asc($hex);
-
- // Return it
- return $asc;
- }
-
- /**
- * Converts a hexadecimal number into an ASCII string. Negative numbers
- * are not allowed.
- *
- * @param $hex Hexadecimal string
- * @return $asc An ASCII string
- */
- protected function hex2asc ($hex) {
- // Check for length, it must be devideable by 2
- //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('hex='.$hex);
- assert((strlen($hex) % 2) == 0);
-
- // Walk the string
- $asc = '';
- for ($idx = 0; $idx < strlen($hex); $idx+=2) {
- // Get the decimal number of the chunk
- $part = hexdec(substr($hex, $idx, 2));
-
- // Add it to the final string
- $asc .= chr($part);
- } // END - for
-
- // Return the final string
- return $asc;
- }
/**
* Checks whether the given encoded data was encoded with Base64
*/
private $currentBlock = '';
+ /**
+ * An instance of a Block class
+ */
+ private $blockInstance = NULL;
+
/**
* Protected constructor
*
$this->initCountersGapsArray();
}
+ /**
+ * Setter for Block instance
+ *
+ * @param $blockInstance An instance of an Block class
+ * @return void
+ */
+ protected final function setBlockInstance (Block $blockInstance) {
+ $this->blockInstance = $blockInstance;
+ }
+
+ /**
+ * Getter for Block instance
+ *
+ * @return $blockInstance An instance of an Block class
+ */
+ public final function getBlockInstance () {
+ return $this->blockInstance;
+ }
+
/**
* Checks whether the abstracted file only contains gaps by counting all
* gaps' bytes together and compare it to total length.
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class FileIterator extends BaseIterator implements SeekableWritableFileIterator {
+ /**
+ * An instance of a Block class
+ */
+ private $blockInstance = NULL;
+
/**
* Protected constructor
*
return $iteratorInstance;
}
+ /**
+ * Setter for Block instance
+ *
+ * @param $blockInstance An instance of an Block class
+ * @return void
+ */
+ protected final function setBlockInstance (Block $blockInstance) {
+ $this->blockInstance = $blockInstance;
+ }
+
+ /**
+ * Getter for Block instance
+ *
+ * @return $blockInstance An instance of an Block class
+ */
+ public final function getBlockInstance () {
+ return $this->blockInstance;
+ }
+
/**
* Gets currently read data
*
* @return void
* @throws AlreadyInitializedStackerException If the stack is already initialized
*/
- public function initStack ($stackerName, $forceReInit = false) {
+ public function initStack (string $stackerName, bool $forceReInit = false) {
// Is the stack already initialized?
if (($forceReInit === false) && ($this->isStackInitialized($stackerName))) {
// Then throw the exception
*
* @return void
*/
- public function initStacks (array $stacks, $forceReInit = false) {
+ public function initStacks (array $stacks, bool $forceReInit = false) {
// "Walk" through all (more will be added as needed
foreach ($stacks as $stackerName) {
// Init this stack
* @param $stackerName Name of the stack
* @return $isInitialized Whether the stack is initialized
*/
- public function isStackInitialized ($stackerName) {
+ public function isStackInitialized (string $stackerName) {
// Is is there?
$isInitialized = ($this->isValidGenericArrayKey('stacks', $stackerName, 'entries'));
* @return $isFull Whether the stack is full
* @throws NoStackerException If given stack is missing
*/
- protected function isStackFull ($stackerName) {
+ protected function isStackFull (string $stackerName) {
// Is the stack not yet initialized?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
* @return $isEmpty Whether the stack is empty
* @throws NoStackerException If given stack is missing
*/
- public function isStackEmpty ($stackerName) {
+ public function isStackEmpty (string $stackerName) {
// Is the stack not yet initialized?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
* @return $count Size of stack (array count)
* @throws NoStackerException If given stack is missing
*/
- public function getStackCount ($stackerName) {
+ public function getStackCount (string $stackerName) {
// Is the stack not yet initialized?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
* @return void
* @throws FullStackerException Thrown if the stack is full
*/
- protected function addValue ($stackerName, $value) {
+ protected function addValue (string $stackerName, $value) {
// Is the stack not yet initialized or full?
if (!$this->isStackInitialized($stackerName)) {
// Then do it here
* @throws NoStackerException If the named stacker was not found
* @throws EmptyStackerException If the named stacker is empty
*/
- protected function getLastValue ($stackerName) {
+ protected function getLastValue (string $stackerName) {
// Is the stack not yet initialized or full?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
* @throws NoStackerException If the named stacker was not found
* @throws EmptyStackerException If the named stacker is empty
*/
- protected function getFirstValue ($stackerName) {
+ protected function getFirstValue (string $stackerName) {
// Is the stack not yet initialized or full?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
* @throws NoStackerException If the named stacker was not found
* @throws EmptyStackerException If the named stacker is empty
*/
- protected function popLast ($stackerName) {
+ protected function popLast (string $stackerName) {
// Is the stack not yet initialized or full?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
* @throws NoStackerException If the named stacker was not found
* @throws EmptyStackerException If the named stacker is empty
*/
- protected function popFirst ($stackerName) {
+ protected function popFirst (string $stackerName) {
// Is the stack not yet initialized or full?
if (!$this->isStackInitialized($stackerName)) {
// Throw an exception
use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
use Org\Mxchange\CoreFramework\Filesystem\File\BaseBinaryFile;
use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Index\Indexable;
use Org\Mxchange\CoreFramework\Iterator\Filesystem\SeekableWritableFileIterator;
use Org\Mxchange\CoreFramework\Stacker\BaseStacker;
*/
const ARRAY_INDEX_DATA_LENGTH = 'length';
+ /**
+ * An instance of an Indexable class
+ */
+ private $indexInstance = NULL;
+
/**
* Protected constructor
*
parent::__construct($className);
}
+ /**
+ * Setter for Indexable instance
+ *
+ * @param $indexInstance An instance of an Indexable class
+ * @return void
+ */
+ protected final function setIndexInstance (Indexable $indexInstance) {
+ $this->indexInstance = $indexInstance;
+ }
+
+ /**
+ * Getter for Indexable instance
+ *
+ * @return $indexInstance An instance of an Indexable class
+ */
+ public final function getIndexInstance () {
+ return $this->indexInstance;
+ }
+
/**
* Reads the file header
*
* @todo To hard assertions here, better rewrite them to exceptions
*/
public function readFileHeader () {
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+ /* 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 ...
$this->getIteratorInstance()->rewind();
// Then read it (see constructor for calculation)
$data = $this->getIteratorInstance()->read($this->getIteratorInstance()->getHeaderSize());
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Read %d bytes (%d wanted).', __METHOD__, __LINE__, strlen($data), $this->getIteratorInstance()->getHeaderSize()));
+ /* 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('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
// Last character must be the separator
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] data(-1)=%s', __METHOD__, __LINE__, dechex(ord(substr($data, -1, 1)))));
+ /* 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('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
// Okay, then remove it
$data = substr($data, 0, -1);
$this->getIteratorInstance()->setHeader($header);
// Check if the array has only 3 elements
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] header(%d)=%s', __METHOD__, __LINE__, count($header), print_r($header, true)));
+ /* 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('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
+ /* 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('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
// Check length of count and seek position
assert(strlen($header[1]) == BaseBinaryFile::LENGTH_COUNT);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
+ /* 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('[%s:%d:] Passed assert().', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Passed assert().', __METHOD__, __LINE__));
// Decode count and seek position
$header[1] = hex2bin($header[1]);
$header[2] = hex2bin($header[2]);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+ /* 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('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: CALLED!', __METHOD__, __LINE__));
// Put all informations together
$header = sprintf('%s%s%s%s%s%s',
// Write it to disk (header is always at seek position 0)
$this->getIteratorInstance()->writeData(0, $header, false);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
}
/**
* @return void
* @todo Currently the stack file is not cached, please implement a memory-handling class and if enough RAM is found, cache the whole stack file.
*/
- protected function initFileStack (SplFileInfo $fileInfoInstance, $type) {
+ protected function initFileStack (SplFileInfo $fileInfoInstance, string $type) {
// Get a stack file instance
$fileInstance = ObjectFactory::createObjectByConfiguredName('stack_file_class', array($fileInfoInstance, $this));
* @return void
* @throws FullStackerException If the stack is full
*/
- protected function addValue ($stackerName, $value) {
+ protected function addValue (string $stackerName, $value) {
// Do some tests
if ($this->isStackFull($stackerName)) {
// Stacker is full
throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
} // END - if
- // Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName . ',value[' . gettype($value) . ']=' . print_r($value, true));
-
// No objects/resources are allowed as their serialization takes to long
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] stackerName=' . $stackerName . ',value[' . gettype($value) . ']=' . print_r($value, true));
assert(!is_object($value));
assert(!is_resource($value));
* @return $value Value of last added value
* @throws EmptyStackerException If the stack is empty
*/
- protected function getLastValue ($stackerName) {
+ protected function getLastValue (string $stackerName) {
// Is the stack not yet initialized or full?
if ($this->isStackEmpty($stackerName)) {
// Throw an exception
* @return $value Value of last added value
* @throws EmptyStackerException If the stack is empty
*/
- protected function getFirstValue ($stackerName) {
+ protected function getFirstValue (string $stackerName) {
// Is the stack not yet initialized or full?
if ($this->isStackEmpty($stackerName)) {
// Throw an exception
* @return $value Value "poped" from array
* @throws EmptyStackerException If the stack is empty
*/
- protected function popLast ($stackerName) {
+ protected function popLast (string $stackerName) {
// Is the stack not yet initialized or full?
if ($this->isStackEmpty($stackerName)) {
// Throw an exception
* @return $value Value "shifted" from array
* @throws EmptyStackerException If the named stacker is empty
*/
- protected function popFirst ($stackerName) {
+ protected function popFirst (string $stackerName) {
// Is the stack not yet initialized or full?
if ($this->isStackEmpty($stackerName)) {
// Throw an exception
* @param $stackerName Name of the stack
* @return $isFull Whether the stack is full
*/
- protected function isStackFull ($stackerName) {
+ protected function isStackFull (string $stackerName) {
// File-based stacks will only run full if the disk space is low.
// @TODO Please implement this, returning false
$isFull = false;
* @return $isEmpty Whether the stack is empty
* @throws NoStackerException If given stack is missing
*/
- public function isStackEmpty ($stackerName) {
+ public function isStackEmpty (string $stackerName) {
// So, is the stack empty?
$isEmpty = (($this->getStackCount($stackerName)) == 0);
* @return void
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
- public function initStack ($stackerName, $forceReInit = false) {
+ public function initStack (string $stackerName, bool $forceReInit = false) {
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @return void
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
- public function initStacks (array $stacks, $forceReInit = false) {
+ public function initStacks (array $stacks, bool $forceReInit = false) {
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @return $isInitialized Whether the stack is initialized
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
- public function isStackInitialized ($stackerName) {
+ public function isStackInitialized (string $stackerName) {
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @param $stackerName Name of the stack
* @return $count Size of stack (array count)
*/
- public function getStackCount ($stackerName) {
+ public function getStackCount (string $stackerName) {
// Now, simply return the found count value, this must be up-to-date then!
return $this->getIteratorInstance()->getCounter();
}
* @return void
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
- public final function setHeaderSize ($headerSize) {
+ public final function setHeaderSize (int $headerSize) {
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @return void
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
- public function writeData ($seekPosition, $data, $flushHeader = true) {
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekPosition=%s,data[]=%s,flushHeader=%d', __METHOD__, __LINE__, $seekPosition, gettype($data), intval($flushHeader)));
+ public function writeData ($seekPosition, $data, bool $flushHeader = true) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%s,data[]=%s,flushHeader=%d', $seekPosition, gettype($data), intval($flushHeader)));
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
public function writeValueToFile ($groupId, $value) {
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,value[%s]=%s', __METHOD__, __LINE__, $groupId, gettype($value), print_r($value, true)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,value[%s]=%s', $groupId, gettype($value), print_r($value, true)));
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @return $seekPosition Found next gap's seek position
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
- public function searchNextGap ($length) {
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] length=%s', __METHOD__, __LINE__, $length));
+ public function searchNextGap (int $length) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: length=%s', $length));
throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
}
* @param $encoded Encoded value to be written to the file
* @return $data Gap position and length of the raw data
*/
- public function writeDataToFreeGap ($groupId, $hash, $encoded) {
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,encoded()=%d - CALLED!', __METHOD__, __LINE__, $groupId, $hash, strlen($encoded)));
-
+ public function writeDataToFreeGap ($groupId, string $hash, $encoded) {
// Raw data been written to the file
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,encoded()=%d - CALLED!', $groupId, $hash, strlen($encoded)));
$rawData = sprintf('%s%s%s%s%s',
$groupId,
BaseBinaryFile::SEPARATOR_GROUP_HASH,
$encoded
);
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d', __METHOD__, __LINE__, $groupId, $hash, strlen($rawData)));
-
// Search for next free gap
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d', $groupId, $hash, strlen($rawData)));
$gapPosition = $this->getIteratorInstance()->searchNextGap(strlen($rawData));
// Gap position cannot be smaller than header length + 1
assert($gapPosition > $this->getIteratorInstance()->getHeaderSize());
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,gapPosition=%s', __METHOD__, __LINE__, $groupId, $hash, $gapPosition));
-
// Then write the data at that gap
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,gapPosition=%s', $groupId, $hash, $gapPosition));
$this->getIteratorInstance()->writeData($gapPosition, $rawData);
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] groupId=%s,hash=%s,rawData()=%d - EXIT!', __METHOD__, __LINE__, $groupId, $hash, strlen($rawData)));
-
// Return gap position, hash and length of raw data
- return array(
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: groupId=%s,hash=%s,rawData()=%d - EXIT!', $groupId, $hash, strlen($rawData)));
+ return [
self::ARRAY_INDEX_GAP_POSITION => $gapPosition,
self::ARRAY_INDEX_HASH => $hash,
- self::ARRAY_INDEX_DATA_LENGTH => strlen($rawData)
- );
+ self::ARRAY_INDEX_DATA_LENGTH => strlen($rawData),
+ ];
}
}
* Array with bitmasks and such for pack/unpack methods to support both
* 32-bit and 64-bit systems
*/
- private static $packingData = array(
- 32 => array(
+ private static $packingData = [
+ 32 => [
'step' => 3,
'left' => 0xffff0000,
'right' => 0x0000ffff,
'factor' => 16,
'format' => 'II',
- ),
- 64 => array(
+ ],
+ 64 => [
'step' => 7,
'left' => 0xffffffff00000000,
'right' => 0x00000000ffffffff,
'factor' => 32,
'format' => 'NN'
- )
- );
+ ]
+ ];
+
+ /**
+ * Hexadecimal->Decimal translation array
+ */
+ private static $hexdec = [
+ '0' => 0,
+ '1' => 1,
+ '2' => 2,
+ '3' => 3,
+ '4' => 4,
+ '5' => 5,
+ '6' => 6,
+ '7' => 7,
+ '8' => 8,
+ '9' => 9,
+ 'a' => 10,
+ 'b' => 11,
+ 'c' => 12,
+ 'd' => 13,
+ 'e' => 14,
+ 'f' => 15
+ ];
+
+ /**
+ * Decimal->hexadecimal translation array
+ */
+ private static $dechex = [
+ 0 => '0',
+ 1 => '1',
+ 2 => '2',
+ 3 => '3',
+ 4 => '4',
+ 5 => '5',
+ 6 => '6',
+ 7 => '7',
+ 8 => '8',
+ 9 => '9',
+ 10 => 'a',
+ 11 => 'b',
+ 12 => 'c',
+ 13 => 'd',
+ 14 => 'e',
+ 15 => 'f'
+ ];
/**
* Simple 64-bit check, thanks to "Salman A" from stackoverflow.com:
return $price;
}
+ /**
+ * Converts a hexadecimal string, even with negative sign as first string to
+ * a decimal number using BC functions.
+ *
+ * This work is based on comment #86673 on php.net documentation page at:
+ * <http://de.php.net/manual/en/function.dechex.php#86673>
+ *
+ * @param $hex Hexadecimal string
+ * @return $dec Decimal number
+ */
+ public static function hex2dec (string $hex) {
+ // Convert to all lower-case
+ $hex = strtolower($hex);
+
+ // Detect sign (negative/positive numbers)
+ $sign = '';
+ if (substr($hex, 0, 1) == '-') {
+ $sign = '-';
+ $hex = substr($hex, 1);
+ }
+
+ // Decode the hexadecimal string into a decimal number
+ $dec = 0;
+ for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) {
+ $factor = self::$hexdec[substr($hex, $i, 1)];
+ $dec = bcadd($dec, bcmul($factor, $e));
+ }
+
+ // Return the decimal number
+ return $sign . $dec;
+ }
+
+ /**
+ * Converts even very large decimal numbers, also signed, to a hexadecimal
+ * string.
+ *
+ * This work is based on comment #97756 on php.net documentation page at:
+ * <http://de.php.net/manual/en/function.hexdec.php#97756>
+ *
+ * @param $dec Decimal number, even with negative sign
+ * @param $maxLength Optional maximum length of the string
+ * @return $hex Hexadecimal string
+ */
+ public static function dec2hex (string $dec, int $maxLength = 0) {
+ // maxLength can be zero or devideable by 2
+ assert(($maxLength == 0) || (($maxLength % 2) == 0));
+
+ // Detect sign (negative/positive numbers)
+ $sign = '';
+ if ($dec < 0) {
+ $sign = '-';
+ $dec = abs($dec);
+ }
+
+ // Encode the decimal number into a hexadecimal string
+ $hex = '';
+ do {
+ $hex = self::$dechex[($dec % (2 ^ 4))] . $hex;
+ $dec /= (2 ^ 4);
+ } while ($dec >= 1);
+
+ /*
+ * Leading zeros are required for hex-decimal "numbers". In some
+ * situations more leading zeros are wanted, so check for both
+ * conditions.
+ */
+ if ($maxLength > 0) {
+ // Prepend more zeros
+ $hex = str_pad($hex, $maxLength, '0', STR_PAD_LEFT);
+ } elseif ((strlen($hex) % 2) != 0) {
+ // Only make string's length dividable by 2
+ $hex = '0' . $hex;
+ }
+
+ // Return the hexadecimal string
+ return $sign . $hex;
+ }
+
+ /**
+ * Converts a ASCII string (0 to 255) into a decimal number.
+ *
+ * @param $asc The ASCII string to be converted
+ * @return $dec Decimal number
+ */
+ public static function asc2dec (string $asc) {
+ // Convert it into a hexadecimal number
+ $hex = bin2hex($asc);
+
+ // And back into a decimal number
+ $dec = self::hex2dec($hex);
+
+ // Return it
+ return $dec;
+ }
+
+ /**
+ * Converts a decimal number into an ASCII string.
+ *
+ * @param $dec Decimal number
+ * @return $asc An ASCII string
+ */
+ public static function dec2asc (string $dec) {
+ // First convert the number into a hexadecimal string
+ $hex = self::dec2hex($dec);
+
+ // Then convert it into the ASCII string
+ $asc = self::hex2asc($hex);
+
+ // Return it
+ return $asc;
+ }
+
+ /**
+ * Converts a hexadecimal number into an ASCII string. Negative numbers
+ * are not allowed.
+ *
+ * @param $hex Hexadecimal string
+ * @return $asc An ASCII string
+ */
+ public static function hex2asc ($hex) {
+ // Check for length, it must be devideable by 2
+ //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('hex='.$hex);
+ assert((strlen($hex) % 2) == 0);
+
+ // Walk the string
+ $asc = '';
+ for ($idx = 0; $idx < strlen($hex); $idx+=2) {
+ // Get the decimal number of the chunk
+ $part = hexdec(substr($hex, $idx, 2));
+
+ // Add it to the final string
+ $asc .= chr($part);
+ }
+
+ // Return the final string
+ return $asc;
+ }
+
/**
* Pack a string into a "binary format". Please execuse me that this is
* widely undocumented. :-(
* @param $headerSize Size of file header
* @return void
*/
- function setHeaderSize ($headerSize);
+ function setHeaderSize (int $headerSize);
/**
* Getter for header array
* @param $encoded Encoded value to be written to the file
* @return $data Gap position and length of the raw data
*/
- function writeDataToFreeGap ($groupId, $hash, $encoded);
+ function writeDataToFreeGap ($groupId, string $hash, $encoded);
/**
* Writes data at given position
* @param $flushHeader Whether to flush the header (default: flush)
* @return void
*/
- function writeData ($seekPosition, $data, $flushHeader = true);
+ function writeData ($seekPosition, $data, bool $flushHeader = true);
/**
* Searches for next suitable gap the given length of data can fit in
* @param $length Length of raw data
* @return $seekPosition Found next gap's seek position
*/
- function searchNextGap ($length);
+ function searchNextGap (int $length);
}
* @param $headerSize Size of file header
* @return void
*/
- function setHeaderSize ($headerSize);
+ function setHeaderSize (int $headerSize);
/**
* Getter for header array
* @param $flushHeader Whether to flush the header (default: flush)
* @return void
*/
- function writeData ($seekPosition, $data, $flushHeader = true);
+ function writeData ($seekPosition, $data, bool $flushHeader = true);
/**
* Getter for seek position
* @param $encoded Encoded value to be written to the file
* @return $data Gap position and length of the raw data
*/
- function writeDataToFreeGap ($groupId, $hash, $encoded);
+ function writeDataToFreeGap ($groupId, string $hash, $encoded);
/**
* Searches for next suitable gap the given length of data can fit in
* @param $length Length of raw data
* @return $seekPosition Found next gap's seek position
*/
- function searchNextGap ($length);
+ function searchNextGap (int $length);
}
* @return void
* @throws StackerFullException If the stacker is full
*/
- function pushNamed ($stackerName, $value);
+ function pushNamed (string $stackerName, $value);
/**
* 'Pops' a value from a named stacker and returns it's value
* @throws NoStackerException If the named stacker was not found
* @throws EmptyStackerException If the named stacker is empty
*/
- function popNamed ($stackerName);
+ function popNamed (string $stackerName);
/**
* Get value from named stacker but don't "pop" it
* @throws NoStackerException If the named stacker was not found
* @throws EmptyStackerException If the named stacker is empty
*/
- function getNamed ($stackerName);
+ function getNamed (string $stackerName);
/**
* Checks whether the given stack is initialized (set in array $stackers)
* @param $stackerName Name of the stack
* @return $isInitialized Whether the stack is initialized
*/
- function isStackInitialized ($stackerName);
+ function isStackInitialized (string $stackerName);
/**
* Checks whether the given stack is empty
* @return $isEmpty Whether the stack is empty
* @throws NoStackerException If given stack is missing
*/
- function isStackEmpty ($stackerName);
+ function isStackEmpty (string $stackerName);
}
// Import framework stuff
use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
/**
* An abstract middleware class for all other middlware classes
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
abstract class BaseMiddleware extends BaseFrameworkSystem {
+ /**
+ * The concrete output instance
+ */
+ private $outputInstance = NULL;
+
/**
* Protected constructor
*
parent::__construct($className);
}
+ /**
+ * Setter for output instance
+ *
+ * @param $outputInstance The debug output instance
+ * @return void
+ */
+ protected final function setOutputInstance (OutputStreamer $outputInstance) {
+ $this->outputInstance = $outputInstance;
+ }
+
+ /**
+ * Getter for output instance
+ *
+ * @return $outputInstance The debug output instance
+ */
+ protected final function getOutputInstance () {
+ return $this->outputInstance;
+ }
+
}
* configured for
* @return $debugInstance An instance of this middleware class
*/
- public static final function createDebugMiddleware ($outputClass, $className) {
+ public static final function createDebugMiddleware (string $outputClass, string $className) {
//* DEBUG-DIE: */ die(__METHOD__.': outputClass=' . $outputClass . ',className=' . $className);
// Create an instance if this middleware
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function output ($outStream, $stripTags = false) {
+ public final function output (string $outStream, bool $stripTags = false) {
// Is the output stream set
if (empty($outStream)) {
// @TODO Initialization phase