exit 255
fi
-grep __call "${DEBUG_FILE}" | cut -d "[" -f 4 | cut -d "]" -f 1
+grep -a __call "${DEBUG_FILE}" | cut -d "[" -f 4 | cut -d "]" -f 1
* @param $index Index to seek for
* @return void
*/
- public function seek ($index) {
+ public function seek (int $index) {
// Rewind to beginning
$this->rewind();
use \BadMethodCallException;
use \InvalidArgumentException;
use \SplFileInfo;
+use \UnexpectedValueException;
/**
* A general binary file class
* @return $isGapsOnly Whether the abstracted file only contains gaps
*/
private function isFileOnlyGaps () {
- // First/last gap found?
- /* Only for debugging
- if (isset($this->gaps[0])) {
- // Output first and last gap
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] this->gaps[0]=%s,this->gaps[%s]=%s', print_r($this->gaps[0], true), (count($this->gaps) - 1), print_r($this->gaps[count($this->gaps) - 1], true)));
- }
- */
-
- // Now count every gap
+ // Count every gap
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$gapsSize = 0;
foreach ($this->gaps as $gap) {
// Calculate size of found gap: end-start including both
$gapsSize += ($gap[self::GAPS_INDEX_END] - $gap[self::GAPS_INDEX_START]);
}
- // Debug output
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] gapsSize=%s,this->headerSize=%s', $gapsSize, $this->getHeaderSize()));
-
// Total gap size + header size must be same as file size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: gapsSize=%s,this->headerSize=%s', $gapsSize, $this->getHeaderSize()));
$isGapsOnly = (($this->getHeaderSize() + $gapsSize) == $this->getFileSize());
// Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isGapsOnly=%d - EXIT!', intval($isGapsOnly)));
return $isGapsOnly;
}
* @return void
*/
public function initCountersGapsArray () {
- // Init counter and seek position
+ // Init counter and seek position to header size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$this->setCounter(0);
- $this->setSeekPosition(0);
+ $this->setSeekPosition($this->getHeaderSize());
// Init arrays
$this->gaps = [];
$this->damagedEntries = [];
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
}
/**
* @return $seekPosition Current seek position (stored here in object)
*/
public final function getSeekPosition () {
- // Get it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Getting this->seekPosition=%d - CALLED!', $this->seekPosition));
return $this->seekPosition;
}
* @return void
*/
protected final function setSeekPosition (int $seekPosition) {
- // And set it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Setting seekPosition=%d - CALLED!', $seekPosition));
$this->seekPosition = $seekPosition;
}
* @return void
*/
public function updateSeekPosition () {
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!'));
-
// Get key (= seek position)
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$seekPosition = $this->key();
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] Setting seekPosition=%s', $seekPosition));
// And set it here
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition));
$this->setSeekPosition($seekPosition);
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!'));
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
* Seeks to beginning of file, updates seek position in this object and
* flushes the header.
*
+ * @param $flushHeader Wether the file's header should be flushed (default: false)
* @return void
*/
- protected function rewindUpdateSeekPosition () {
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!'));
-
- // flushFileHeader must be callable
- assert(is_callable(array($this, 'flushFileHeader')));
-
+ protected function rewindUpdateSeekPosition (bool $flushHeader = false) {
// Seek to beginning of file
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: flushHeader=%d - CALLED!', intval($flushHeader)));
$this->rewind();
// And update seek position ...
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->updateSeekPosition() ...');
$this->updateSeekPosition();
- // ... to write it back into the file
- $this->flushFileHeader();
+ // Flush headers?
+ if ($flushHeader) {
+ // ... to write it back into the file
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->flushFileHeader() ...');
+ $this->flushFileHeader();
+ }
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!'));
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
* @return void
*/
protected function seekToOldPosition () {
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!'));
-
// Seek to currently ("old") saved position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$this->seek($this->getSeekPosition());
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] EXIT!'));
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
*
* @param $str String to look in
* @return $isFound Whether the block separator has been found
+ * @throws InvalidArgumentException If a parameter is not valid
*/
- public static function isBlockSeparatorFound ($str) {
+ public static function isBlockSeparatorFound (string $str) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: str=%s - CALLED!', $str));
+ if (empty($str)) {
+ // Throw IAE
+ throw new InvalidArgumentException('Parameter "str" is empty');
+ }
+
// Determine it
$isFound = (strpos($str, chr(self::SEPARATOR_ENTRIES)) !== false);
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isFound=%d - EXIT!', intval($isFound)));
return $isFound;
}
*/
private function initBackBuffer () {
// Simply call the setter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$this->setBackBuffer('');
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
/**
* Setter for currentBlock field
*
- * @param $currentBlock Characters to set a currently loaded block
+ * @param $currentBlock Characters to set a currently loaded block
* @return void
*/
private function setCurrentBlock (string $currentBlock) {
}
// Write data at given position
- $this->getPointerInstance()->writeAtPosition($seekPosition, $data);
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->writeAtPosition(%d,%s) ...', $seekPosition, $data));
+ $this->writeAtPosition($seekPosition, $data);
// Increment counter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->incrementCounter() ...');
$this->incrementCounter();
// Update seek position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->updateSeekPosition() ...');
$this->updateSeekPosition();
// Flush the header?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: flushHeader=%d', intval($flushHeader)));
if ($flushHeader === true) {
// Flush header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->flushFileHeader() ...');
$this->flushFileHeader();
// Seek to old position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->seekToOldPosition() ...');
$this->seekToOldPosition();
}
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
+ /**
+ * Writes at given position by seeking to it.
+ *
+ * @param $seekPosition Seek position in file
+ * @param $dataStream Data to be written
+ * @return mixed Number of writes bytes or false on error
+ * @throws InvalidArgumentException If a parameter is not valid
+ */
+ public function writeAtPosition (int $seekPosition, string $dataStream) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
+ if ($seekPosition < 0) {
+ // Invalid seek position
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition));
+ } elseif (empty($dataStream)) {
+ // Empty dataStream
+ throw new InvalidArgumentException('Parameter "dataStream" is empty');
+ }
+
+ // Call pointer's method
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->pointerInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
+ $status = $this->getPointerInstance()->writeAtPosition($seekPosition, $dataStream);
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: status[%s]=%d - EXIT!', gettype($status), $status));
+ return $status;
+ }
+
/**
* Marks the currently loaded block as empty (with length of the block)
*
*/
protected function markCurrentBlockAsEmpty (int $length) {
// Validate parameter
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d - CALLED!', $length));
if ($length < 1) {
// Length cannot below one
throw new InvalidArgumentException(sprintf('length=%d is not valid', $length));
));
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
// Is the file initialized?
if ($this->isFileInitialized()) {
// Some bytes has been written, so rewind to start of it.
- $rewindStatus = $this->rewind();
-
- // Is the rewind() call successfull?
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: rewindStatus=%d', $rewindStatus));
- if ($rewindStatus != 1) {
- // Something bad happened
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Could not rewind().');
- }
+ $this->rewind();
// Read file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->readFileHeader() ...');
$this->readFileHeader();
+ // Get header count
+ $headerCount = count($this->getHeader());
+
// The above method does already check the header
- $isInitialized = true;
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: headerCount=%d', $headerCount));
+ $isInitialized = ($headerCount > 0);
}
// Return result
*/
public function isFileInitialized () {
// Get it from iterator which holds the pointer instance. If false is returned
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$fileSize = $this->size();
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize=%s', $fileSize));
/*
* The returned file size should not be false or NULL as this means
* that the pointer class does not work correctly.
*/
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: fileSize[%s]=%d', gettype($fileSize), $fileSize));
assert(is_int($fileSize));
// Is more than 0 returned?
$isInitialized = ($fileSize > 0);
// Return result
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isInitialized=%d - EXIT!', intval($isInitialized)));
return $isInitialized;
}
* Creates the assigned file
*
* @return void
+ * @throws BadMethodCallException If this file's header is already initialized
*/
public function createFileHeader () {
// The file's header should not be initialized here
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
- assert(!$this->isFileHeaderInitialized());
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+ if ($this->isFileHeaderInitialized()) {
+ // Bad method call
+ //* DEBUG-DIE: */ die(sprintf('[%s:%d]: this=%s', __METHOD__, __LINE__, print_r($this, TRUE)));
+ throw new BadMethodCallException('File header is already initialized but method called');
+ }
// Simple flush file header which will create it.
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->flushFileHeader() ...');
$this->flushFileHeader();
// Rewind seek position (to beginning of file) and update/flush file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Calling this->rewindUpdateSeekPosition() ...');
$this->rewindUpdateSeekPosition();
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: Pre-allocating file ...');
// Calculate minimum length for one entry
- $minLengthEntry = $this->getBlockInstance()->calculateMinimumBlockLength();
+ $minimumBlockLength = $this->getBlockInstance()->calculateMinimumBlockLength();
// Calulcate seek position
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: minLengthEntry=%s', $minLengthEntry));
- $seekPosition = $minLengthEntry * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: minimumBlockLength=%s', $minimumBlockLength));
+ $seekPosition = $minimumBlockLength * FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($type . '_pre_allocate_count');
// Now simply write a NUL there. This will pre-allocate the file.
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition));
- $this->writeData($seekPosition, chr(0));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->writeAtPosition(%d,NUL) ...', $seekPosition));
+ $this->writeAtPosition($seekPosition, chr(0));
- // Rewind seek position (to beginning of file) and update/flush file header
- $this->rewindUpdateSeekPosition();
+ // Rewind seek position
+ $this->rewind();
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
* @param $whence Added to offset (default: only use offset to seek to)
* @return $status Status of file seek: 0 = success, -1 = failed
*/
- public function seek (int $offset, $whence = SEEK_SET) {
+ public function seek (int $offset, int $whence = SEEK_SET) {
// Call pointer instance
return $this->getPointerInstance()->seek($offset, $whence);
}
*/
public function analyzeFile () {
// Make sure the file is initialized
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
if (!$this->isFileInitialized()) {
// Bad method call
throw new BadMethodCallException('Method called but file is not initialized.');
$this->doRunDefragmentation();
}
}
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
*/
public function next () {
// Is there nothing to read?
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
if (!$this->valid()) {
// Nothing to read
return;
}
// First calculate minimum block length
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: key()=%d', $this->key()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: key()=%d', $this->key()));
$length = $this->getBlockInstance()->calculateMinimumBlockLength();
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%s', $length));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
// Read possibly back-buffered bytes from previous call of next().
$data = $this->getBackBuffer();
* "block" may not fit, so this loop will continue until the EOB or EOF
* has been reached whatever comes first.
*/
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
- while ((!$this->isEndOfFileReached()) && (!self::isBlockSeparatorFound($data))) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
+ while ((!$this->isEndOfFileReached()) && (empty($data) || !self::isBlockSeparatorFound($data))) {
// Then read the next possible block
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling this->read(%d) ...', $length));
$block = $this->read($length);
// Is it all empty?
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: block()=%d,length=%s', strlen($block), $length));
- if (strlen(trim($block)) == 0) {
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: block()=%d,length=%s', strlen($block), $length));
+ if (strlen(trim($block, chr(0))) == 0) {
// Mark this block as empty
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling markCurrentBlockAsEmpty(%d) ...', strlen($block)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling markCurrentBlockAsEmpty(%d) ...', strlen($block)));
$this->markCurrentBlockAsEmpty(strlen($block));
// Skip to next block
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CONTINUE!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CONTINUE!');
continue;
}
$data .= $block;
// A debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data()=%d', strlen($data)));
}
// EOF reached?
if ($this->isEndOfFileReached()) {
// Set whole data as current read block
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling setCurrentBlock(%d) ...', strlen($data)));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: Calling setCurrentBlock(%d) ...', strlen($data)));
$this->setCurrentBlock($data);
// Then abort here silently
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EOF reached - EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EOF reached - EXIT!');
return;
}
$dataArray = explode(chr(self::SEPARATOR_ENTRIES), $data);
// This array must contain two elements
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: dataArray=' . print_r($dataArray, true));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray()=%d', count($dataArray)));
+ //* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: dataArray=%s', print_r($dataArray, true)));
if (count($dataArray) != 2) {
// Bad count
throw new UnexpectedValueException(sprintf('dataArray()=%d is not expected, want 2', count($dataArray)));
$this->setBackBuffer($dataArray[1]);
// Trace message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
* This method will return true if an emptied (nulled) entry has been found.
*
* @return $isValid Whether the next entry is valid
+ * @throws UnexpectedValueException If some value is not expected
*/
public function valid () {
// First calculate minimum block length
$length = $this->getBlockInstance()->calculateMinimumBlockLength();
// Short be more than zero!
- assert($length > 0);
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: length=%d', $length));
+ if ($length < 1) {
+ // Throw UVE
+ throw new UnexpectedValueException(sprintf('length=%d is not expected', $length));
+ }
// Get current seek position
$seekPosition = $this->key();
// Then try to read it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: seekPosition=%d', $seekPosition));
$data = $this->read($length);
// If some bytes could be read, all is fine
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: data[%s]()=%d', gettype($data), strlen($data)));
$isValid = ((is_string($data)) && (strlen($data) > 0));
// Get header size
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isValid=%d', intval($isValid)));
$headerSize = $this->getHeaderSize();
// Is the seek position at or beyond the header?
if ($seekPosition >= $headerSize) {
// Seek back to old position
- $this->seek($seekPosition);
+ $isValid = ($isValid && $this->seek($seekPosition) === 0);
} else {
// Seek directly behind the header
- $this->seek($headerSize);
+ $isValid = ($isValid && $this->seek($headerSize) === 0);
}
// Return result
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: isValid=%d - EXIT!', intval($isValid)));
return $isValid;
}
*/
public function key () {
// Call pointer instance
- return $this->getPointerInstance()->determineSeekPosition();
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
+ $key = $this->getPointerInstance()->determineSeekPosition();
+
+ // Return key
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-BINARY-FILE: key[%s]=%d - EXIT!', gettype($key), $key));
+ return $key;
}
/**
*/
public function readFileHeader () {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$this->getBlockInstance()->readFileHeader();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
*/
public function flushFileHeader () {
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: CALLED!');
$this->getBlockInstance()->flushFileHeader();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-BINARY-FILE: EXIT!');
}
/**
*/
public final function getCounter () {
// Get it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-ABSTRACT-FILE: Getting this->totalEntries=%d ... - CALLED!', $this->totalEntries));
return $this->totalEntries;
}
*/
protected final function setCounter (int $counter) {
// Set it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-ABSTRACT-FILE: Setting this->totalEntries=%d ... - CALLED!', $counter));
$this->totalEntries = $counter;
}
*/
protected final function incrementCounter () {
// Get it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: CALLED!');
$this->totalEntries++;
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!');
}
/**
use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
// Import SPL stuff
+use \InvalidArgumentException;
use \SplFileObject;
/**
*/
public function closeFile () {
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
if (is_null($this->getFileObject())) {
// Pointer not initialized
}
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
// Close the file pointer by NULL-ing it
$this->resetFileObject();
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: EXIT!'));
}
/**
* @return $status Status of file seek: 0 = success, -1 = failed
*/
public function seek (int $offset, int $whence = SEEK_SET) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
+ if ($offset < 0) {
+ // Throw IAE
+ throw new InvalidArgumentException(sprintf('offset=%d is not valid', $offset));
+ }
+
// Seek to position
$status = $this->getFileObject()->fseek($offset, $whence);
// Return status
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status));
return $status;
}
*/
public function size () {
// Get current seek position
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
$seekPosition = $this->determineSeekPosition();
// Seek to end
$seekStatus = $this->seek(0, SEEK_END);
// Get position again (which is the end of the file)
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus=%d', $seekStatus));
$size = $this->determineSeekPosition();
// Reset seek position to old
$this->seek($seekPosition);
// Return size
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%s - EXIT!', $size));
return $size;
}
}
// First seek to it, if file size is larger than zero
- if ($this->getFileSize() > 0 && !$this->seek($seekPosition)) {
+ if (($this->getFileSize() > 0 || $seekPosition > 0) && $this->seek($seekPosition) === -1) {
// Could not seek
throw new InvalidArgumentException(sprintf('Could not seek to seekPosition=%d', $seekPosition));
}
/**
* Rewinds to the beginning of the file
*
- * @return $status Status of this operation
+ * @return void
*/
public function rewind () {
// Rewind the pointer
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: CALLED!');
- return $this->getFileObject()->rewind();
+ $this->getFileObject()->rewind();
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-INPUT-OUTPUT-POINTER: EXIT!');
}
/**
*/
public function read (int $bytes = 0) {
// Validatre parameter
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: bytes=%d - CALLED!', $bytes));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: bytes=%d - CALLED!', $bytes));
if ($bytes < 0) {
// Bytes cannot be lesser than zero
throw new InvalidArgumentException(sprintf('bytes=%d is not valid', $bytes));
}
// Then return it
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data));
return $data;
}
$data,
$this->getIteratorInstance()->getHeaderSize()
));
+ } elseif (empty(trim($data, chr(0)))) {
+ // Empty file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: File header is empty - EXIT!');
+ return;
} elseif (substr($data, -1, 1) != chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES)) {
// Bad last character
throw new UnexpectedValueException(sprintf('data=%s does not end with "%s"',
$data = substr($data, 0, -1);
// And update seek position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->updateSeekPosition() ...');
$this->getIteratorInstance()->updateSeekPosition();
/*
);
// 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('BASE-INDEX: Calling this->iteratorInstance->writeAtPosition(0, header=%s) ...', $header));
+ $this->getIteratorInstance()->writeAtPosition(0, $header);
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
$this->setIteratorInstance($iteratorInstance);
// Calculate header size
- $this->getIteratorInstance()->setHeaderSize(
+ $headerSize = (
strlen(Indexable::INDEX_MAGIC) +
strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
BaseBinaryFile::LENGTH_COUNT +
strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
);
+ // Set it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: Setting headerSize=%d ...', $headerSize));
+ $this->getIteratorInstance()->setHeaderSize($headerSize);
+
// Init counters and gaps array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->initCountersGapsArray() ...');
$this->getIteratorInstance()->initCountersGapsArray();
+ // Default is not created
+ $created = false;
+
// Is the file's header initialized?
if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
- // No, then create it (which may pre-allocate the index)
+ // First pre-allocate a bit
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->preAllocateFile(index) ...');
+ $this->getIteratorInstance()->preAllocateFile('index');
+
+ // Then write file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->createFileHeader() ...');
$this->getIteratorInstance()->createFileHeader();
- // And pre-allocate a bit
- $this->getIteratorInstance()->preAllocateFile('index');
+ // Mark as freshly created
+ $created = true;
}
// Load the file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->readFileHeader() ...');
$this->readFileHeader();
- // Count all entries in file
- $this->getIteratorInstance()->analyzeFile();
+ // Freshly created?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-INDEX: created=%d', intval($created)));
+ if (!$created) {
+ // Count all entries in file
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: Calling this->iteratorInstance->analyzeFile() ...');
+ $this->getIteratorInstance()->analyzeFile();
+ }
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-INDEX: EXIT!');
}
}
+ /**
+ * Writes at given position by seeking to it.
+ *
+ * @param $seekPosition Seek position in file
+ * @param $dataStream Data to be written
+ * @return mixed Number of writes bytes or false on error
+ * @throws InvalidArgumentException If a parameter is not valid
+ */
+ public function writeAtPosition (int $seekPosition, string $dataStream) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
+ if ($seekPosition < 0) {
+ // Invalid seek position
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition));
+ } elseif (empty($dataStream)) {
+ // Empty dataStream
+ throw new InvalidArgumentException('Parameter "dataStream" is empty');
+ }
+
+ // Call iterated object's method
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: Calling this->iteratorInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
+ $status = $this->getIteratorInstance()->writeAtPosition($seekPosition, $dataStream);
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-STACK-INDEX: status[%s]=%d - EXIT!', gettype($status), $status));
+ return $status;
+ }
+
}
public function rewind () {
// Call block instance
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: CALLED!');
- $status = $this->getBlockInstance()->rewind();
+ $this->getBlockInstance()->rewind();
- // Return status
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: status=%d - EXIT!', intval($status)));
- return $status;
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
/**
* Seeks to given position
*
* @param $seekPosition Seek position in file
+ * @param $whence Added to offset (default: only use offset to seek to)
* @return $status Status of this operation
* @throws InvalidArgumentException If a parameter is not valid
*/
- public function seek (int $seekPosition) {
+ public function seek (int $seekPosition, int $whence = SEEK_SET) {
// Validate parameter
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d - CALLED!', $seekPosition));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
if ($seekPosition < 0) {
// Throw IAE
throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition));
}
// Call block instance
- $status = $this->getBlockInstance()->seek($seekPosition);
+ $status = $this->getBlockInstance()->seek($seekPosition, $whence);
// Return status
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: status=%d - EXIT!', intval($status)));
}
// Call block instance
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: Calling this->blockInstance->writeData(%d,data()=%d,%d) ...', $seekPosition, strlen($data), intval($flushHeader)));
$this->getBlockInstance()->writeData($seekPosition, $data, $flushHeader);
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('FILE-ITERATOR: EXIT!');
}
+ /**
+ * Writes at given position by seeking to it.
+ *
+ * @param $seekPosition Seek position in file
+ * @param $dataStream Data to be written
+ * @return mixed Number of writes bytes or false on error
+ * @throws InvalidArgumentException If a parameter is not valid
+ */
+ public function writeAtPosition (int $seekPosition, string $dataStream) {
+ // Validate parameter
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
+ if ($seekPosition < 0) {
+ // Invalid seek position
+ throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid.', $seekPosition));
+ } elseif (empty($dataStream)) {
+ // Empty dataStream
+ throw new InvalidArgumentException('Parameter "dataStream" is empty');
+ }
+
+ // Call iterated object's method
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: Calling this->blockInstance->writeAtPosition(%d, %s) ...', $seekPosition, $dataStream));
+ $status = $this->getBlockInstance()->writeAtPosition($seekPosition, $dataStream);
+
+ // Return status
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: status[%s]=%d - EXIT!', gettype($status), $status));
+ return $status;
+ }
+
/**
* Getter for seek position
*
$seekPosition = $this->getBlockInstance()->getSeekPosition();
// Return position
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d - EXIT!', $seekPosition));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition[%s]=%d - EXIT!', gettype($seekPosition), $seekPosition));
return $seekPosition;
}
$seekPosition = $this->getBlockInstance()->searchNextGap($length);
// Return position
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition=%d - EXIT!', $seekPosition));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FILE-ITERATOR: seekPosition[%s]=%d - EXIT!', gettype($seekPosition), $seekPosition));
return $seekPosition;
}
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function output ($outStream = false, $stripTags = false) {
+ public final function output (string $outStream = '', bool $stripTags = false) {
print trim($outStream) . PHP_EOL;
}
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function outputStream ($output, $stripTags = false) {
+ public final function outputStream (string $output, bool $stripTags = false) {
// Strip HTML tags out?
if ($stripTags === true) {
// Prepare the output without HTML tags
$output = trim(html_entity_decode(strip_tags(stripslashes($output))));
- } // END - if
+ }
// Are debug times enabled?
if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_output_timings') == 'Y') {
// Output it first
$output = $this->getPrintableExecutionTime() . $output;
- } // END - if
+ }
// And print it out...
printf('%s%s', str_replace('->', '->', $output), PHP_EOL);
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function output ($outStream = false, $stripTags = false) {
+ public final function output (string $outStream = '', bool $stripTags = false) {
// Empty output will be silently ignored
- if ($outStream !== false) {
+ if (!empty($outStream)) {
$this->outputStream($outStream, $stripTags);
- } // END - if
+ }
}
/**
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function outputStream ($output, $stripTags = false) {
+ public final function outputStream (string $output, bool $stripTags = false) {
// Split multiple lines into and array to put them out line-by-line
$errorLines = explode(chr(10), $output);
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function output ($outStream = false, $stripTags = false) {
+ public final function output (string $outStream = '', bool $stripTags = false) {
// Empty output will be silently ignored
- if ($outStream !== false) {
+ if (!empty($outStream)) {
$this->outputStream($outStream);
- } // END - if
+ }
}
/**
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function outputStream ($output, $stripTags = false) {
+ public final function outputStream (string $output, bool $stripTags = false) {
// Strip out any <br />
$output = str_replace('<br />', '', $output);
printf('<!-- %s -->' . PHP_EOL, stripslashes($output));
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function output ($outStream = false, $stripTags = false) {
+ public final function output (string $outStream = '', bool $stripTags = false) {
// Empty output will be silently ignored
- if ($outStream !== false) {
+ if (!empty($outStream)) {
$this->outputStream($outStream, $stripTags);
- } // END - if
+ }
}
/**
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- public final function output ($outStream = false, $stripTags = false) {
+ public final function output (string $outStream = '', bool $stripTags = false) {
print(stripslashes($outStream));
}
// Then read it (see constructor for calculation)
$data = $this->getIteratorInstance()->read($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?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Read %d bytes (%d wanted).', strlen($data), $this->getIteratorInstance()->getHeaderSize()));
if (strlen($data) != $this->getIteratorInstance()->getHeaderSize()) {
// Bad data length
throw new UnexpectedValueException(sprintf('data(%d)=%s does not match iteratorInstance->headerSize=%d',
$data,
$this->getIteratorInstance()->getHeaderSize()
));
+ } elseif (empty(trim($data, chr(0)))) {
+ // Empty header, file is freshly pre-allocated
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Empty file header detected - EXIT!');
+ return;
}
// Last character must be the separator
$data = substr($data, 0, -1);
// And update seek position
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->updateSeekPosition() ...');
$this->getIteratorInstance()->updateSeekPosition();
/*
$this->getIteratorInstance()->setHeader($header);
// Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: EXIT!', __METHOD__, __LINE__));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
}
/**
// Separator magic<->count
chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
- // Total entries (will be zero) and pad it to 20 chars
+ // Padded total entries
str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getCounter()), BaseBinaryFile::LENGTH_COUNT, '0', STR_PAD_LEFT),
// Separator count<->seek position
chr(BaseBinaryFile::SEPARATOR_HEADER_DATA),
- // Position (will be zero)
+ // Padded seek position
str_pad(StringUtils::dec2hex($this->getIteratorInstance()->getSeekPosition(), 2), BaseBinaryFile::LENGTH_POSITION, '0', STR_PAD_LEFT),
// Separator position<->entries
);
// 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('BASE-FILE-STACK: Calling this->iteratorInstance->writeAtPosition(0, header=%s) ...', $header));
+ $this->getIteratorInstance()->writeAtPosition(0, $header);
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: EXIT!');
$this->setIteratorInstance($iteratorInstance);
// Calculate header size
- $this->getIteratorInstance()->setHeaderSize(
+ $headerSize = (
strlen(StackableFile::STACK_MAGIC) +
strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_DATA)) +
BaseBinaryFile::LENGTH_COUNT +
strlen(chr(BaseBinaryFile::SEPARATOR_HEADER_ENTRIES))
);
+ // Setting it
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: Setting headerSize=%d ...', $headerSize));
+ $this->getIteratorInstance()->setHeaderSize($headerSize);
+
// Init counters and gaps array
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->initCountersGapsArray() ...');
$this->getIteratorInstance()->initCountersGapsArray();
+ // Default is not created/already exists
+ $created = false;
+
// Is the file's header initialized?
if (!$this->getIteratorInstance()->isFileHeaderInitialized()) {
- // No, then create it (which may pre-allocate the stack)
+ // First pre-allocate a bit
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->preAllocateFile(file_stack) ...');
+ $this->getIteratorInstance()->preAllocateFile('file_stack');
+
+ // Then create file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: this->iteratorInstance->createFileHeader() ...');
$this->getIteratorInstance()->createFileHeader();
- // And pre-allocate a bit
- $this->getIteratorInstance()->preAllocateFile('file_stack');
+ // Set flag
+ $created = true;
}
// Load the file header
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->readFileHeader() ...');
$this->readFileHeader();
- // Count all entries in file
- $this->getIteratorInstance()->analyzeFile();
+ // Not created/already exists?
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: created=%d', intval($created)));
+ if (!$created) {
+ // Count all entries in file
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-STACK: Calling this->iteratorInstance->analyzeFile() ...');
+ $this->getIteratorInstance()->analyzeFile();
+ }
/*
* Get stack index instance. This can be used for faster
* "defragmentation" and startup.
*/
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: fileInfoInstance[%s]=%s,type=%s', get_class($fileInfoInstance), $fileInfoInstance, $type));
$indexInstance = FileStackIndexFactory::createFileStackIndexInstance($fileInfoInstance, $type);
// And set it here
* @throws UnsupportedOperationException This method is not (and maybe never will be) supported
*/
public function writeData (int $seekPosition, string $data, bool $flushHeader = true) {
+ // Not supported
/* 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);
}
+ /**
+ * Writes at given position by seeking to it.
+ *
+ * @param $seekPosition Seek position in file
+ * @param $dataStream Data to be written
+ * @return mixed Number of writes bytes or false on error
+ * @throws UnsupportedOperationException This method is not (and maybe never will be) supported
+ */
+ public function writeAtPosition (int $seekPosition, string $dataStream) {
+ // Not supported
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-STACK: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream));
+ throw new UnsupportedOperationException(array($this, __FUNCTION__, $this->getIteratorInstance()->getPointerInstance()), self::EXCEPTION_UNSPPORTED_OPERATION);
+ }
+
/**
* Writes given value to the file and returns a hash and gap position for it
*
* Seeks to given position
*
* @param $seekPosition Seek position in file
+ * @param $whence Added to offset (default: only use offset to seek to)
* @return $status Status of this operation
* @throws InvalidArgumentException If a parameter is invalid
*/
- public function seek (int $seekPosition) {
+ public function seek (int $seekPosition, int $whence = SEEK_SET) {
// Validate parameter
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d - CALLED!', $seekPosition));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('FIFO-FILE-STACK: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence));
if ($seekPosition < 0) {
// Invalid seek position
throw new InvalidArgumentException(sprintf('seekPosition=%d is not valid', $seekPosition));
*/
function writeData (int $seekPosition, string $data, bool $flushHeader = true);
+ /**
+ * Writes at given position by seeking to it.
+ *
+ * @param $seekPosition Seek position in file
+ * @param $dataStream Data to be written
+ * @return mixed Number of writes bytes or false on error
+ * @throws InvalidArgumentException If a parameter is not valid
+ */
+ function writeAtPosition (int $seekPosition, string $dataStream);
+
/**
* Searches for next suitable gap the given length of data can fit in
* including padding bytes.
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- function outputStream ($output, $stripTags = false);
+ function outputStream (string $output, bool $stripTags = false);
}
* @param $stripTags Whether HTML tags shall be stripped out
* @return void
*/
- function output ($outStream = false, $stripTags = false);
+ function output (string $outStream = '', bool $stripTags = false);
}
*/
function writeData (int $seekPosition, string $data, bool $flushHeader = true);
+ /**
+ * Writes at given position by seeking to it.
+ *
+ * @param $seekPosition Seek position in file
+ * @param $data Data to be written
+ * @return mixed Number of writes bytes or false on error
+ */
+ function writeAtPosition (int $seedPosition, string $data);
+
/**
* Getter for seek position
*