X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=inc%2Fclasses%2Fmain%2Ffile_directories%2Fclass_BaseFile.php;h=632cbdcec73bce2dfe7dffaf79711c2de2178988;hb=55a804571db8f1fade4c0cd3149ea847a3a7002d;hp=fb60162627ffd747a23ac92c928ffebe15e152a7;hpb=0af07e30d2e346b21cbb57b53c7550ce93baac06;p=core.git diff --git a/inc/classes/main/file_directories/class_BaseFile.php b/inc/classes/main/file_directories/class_BaseFile.php index fb601626..632cbdce 100644 --- a/inc/classes/main/file_directories/class_BaseFile.php +++ b/inc/classes/main/file_directories/class_BaseFile.php @@ -27,11 +27,21 @@ class BaseFile extends BaseFrameworkSystem { */ private $fileName = ''; + /** + * Back-buffer + */ + private $backBuffer = ''; + + /** + * Currently loaded block (will be returned by current()) + */ + private $currentBlock = ''; + /** * Protected constructor * * @param $className Name of the class - * @return void +y * @return void */ protected function __construct ($className) { // Call parent constructor @@ -84,6 +94,53 @@ class BaseFile extends BaseFrameworkSystem { return $this->fileName; } + /** + * Initializes the back-buffer by setting it to an empty string. + * + * @return void + */ + private function initBackBuffer () { + // Simply call the setter + $this->setBackBuffer(''); + } + + /** + * Setter for backBuffer field + * + * @param $backBuffer Characters to "store" in back-buffer + * @return void + */ + private function setBackBuffer ($backBuffer) { + // Cast to string (so no arrays or objects) + $backBuffer = (string) $backBuffer; + + // ... and set it + $this->backBuffer = $backBuffer; + } + + /** + * Getter for backBuffer field + * + * @return $backBuffer Characters "stored" in back-buffer + */ + private function getBackBuffer () { + return $this->backBuffer; + } + + /** + * Setter for currentBlock field + * + * @param $currentBlock Characters to set a currently loaded block + * @return void + */ + private function setCurrentBlock ($currentBlock) { + // Cast to string (so no arrays or objects) + $currentBlock = (string) $currentBlock; + + // ... and set it + $this->currentBlock = $currentBlock; + } + /** * Initializes this file class * @@ -193,7 +250,7 @@ class BaseFile extends BaseFrameworkSystem { * @return $isEndOfFileReached Whether the EOF has been reached */ public final function isEndOfFileReached () { - return $this->getPointerInstance()->isEndOfFileReached(); + return $this->isEndOfFileReached(); } /** @@ -232,6 +289,106 @@ class BaseFile extends BaseFrameworkSystem { //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__)); } + + /** + * Advances to next "block" of bytes + * + * @return void + * @todo This method will load large but empty files in a whole + */ + public function next () { + // Is there nothing to read? + if (!$this->valid()) { + // Nothing to read + return; + } // END - if + + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof CalculatableBlock); + + // First calculate minimum block length + $length = $this->getBlockInstance()->caluclateMinimumBlockLength(); + + // Short be more than zero! + assert($length > 0); + + // Wait until a entry/block separator has been found + $data = $this->getBackBuffer(); + while ((!$this->isEndOfFileReached()) && (!$this->getBlockInstance()->isBlockSeparatorFound($data))) { + // Then read the block + $data .= $this->read($length); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data)); + } // END - if + + // EOF reached? + if ($this->isEndOfFileReached()) { + // Set whole data as current block + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...'); + $this->setCurrentBlock($data); + + // Then abort here silently + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.'); + return; + } // END - if + + /* + * Init back-buffer which is the data that has been found beyond the + * separator. + */ + $this->initBackBuffer(); + + // Separate data + $dataArray = explode(self::getBlockSeparator(), $data); + + // This array must contain two elements + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE)); + assert(count($dataArray) == 2); + + // Left part is the actual block, right one the back-buffer data + $this->setCurrentBlock($dataArray[0]); + $this->setBackBuffer($dataArray[1]); + } + + /** + * Checks wether the current entry is valid (not at the end of the file). + * This method will return TRUE if an emptied (nulled) entry has been found. + * + * @return $isValid Whether the next entry is valid + */ + public function valid () { + // Make sure the block instance is set + assert($this->getBlockInstance() instanceof CalculatableBlock); + + // First calculate minimum block length + $length = $this->getBlockInstance()->caluclateMinimumBlockLength(); + + // Short be more than zero! + assert($length > 0); + + // Get current seek position + $seekPosition = $this->key(); + + // Then try to read it + $data = $this->read($length); + + // If some bytes could be read, all is fine + $isValid = ((is_string($data)) && (strlen($data) > 0)); + + // Get header size + $headerSize = $this->getBlockInstance()->getHeaderSize(); + + // Is the seek position at or beyond the header? + if ($seekPosition >= $headerSize) { + // Seek back to old position + $this->seek($seekPosition); + } else { + // Seek directly behind the header + $this->seek($headerSize); + } + + // Return result + return $isValid; + } } // [EOF]