* @version 0.0.0 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile { /** * The file object */ private $fileObject = NULL; /** * Protected constructor * * @param $className Name of the class * @return void */ protected function __construct (string $className) { // Call parent constructor /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FILE-IO: className=%s - CONSTRUCTED!', $className)); parent::__construct($className); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FILE-IO: EXIT!'); } /** * Destructor for cleaning purposes, etc * * @return void */ public final function __destruct() { // Is there a resource pointer? Then we have to close the file here! /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FILE-IO: this->fileObject[]=%s - DESTRUCTOR!', gettype($this->getFileObject()))); if (is_object($this->getFileObject())) { // Try to close a file /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FILE-IO: Invoking this->closeFile() ...'); $this->closeFile(); } // Call the parent destructor parent::__destruct(); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FILE-IO: EXIT!'); } /** * Close a file source and set it's instance to null and the file name * to empty. * * @return void * @throws NullPointerException If the file pointer instance is not set by setFileObject() * @throws LogicException If there is no object being set */ public function closeFile () { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname())); if (is_null($this->getFileObject())) { // Pointer not initialized throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER); } elseif (!is_object($this->getFileObject())) { // Pointer is not a valid resource! throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION); } // Close the file pointer by NULL-ing it /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname())); $this->resetFileObject(); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FILE-IO: EXIT!'); } /** * Resets file object instance to NULL * * @return void */ protected final function resetFileObject () { // Set it to NULL /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FILE-IO: Setting this->fileObject=NULL - CALLED!'); $this->fileObject = NULL; // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-FILE-IO: EXIT!'); } /** * Setter for the file object * * @param $fileObject An instance of a SplFileObject class * @return void */ protected final function setFileObject (SplFileObject $fileObject) { $this->fileObject = $fileObject; } /** * Getter for the file object * * @return $fileObject An instance of a SplFileObject class */ public final function getFileObject () { return $this->fileObject; } /** * Determines seek position * * @return $seekPosition Current seek position */ public final function determineSeekPosition () { return $this->getFileObject()->ftell(); } /** * Determines whether the EOF has been reached * * @return $isEndOfFileReached Whether the EOF has been reached */ public final function isEndOfFileReached () { return $this->getFileObject()->eof(); } /** * Seek to given offset (default) or other possibilities as fseek() gives. * * @param $offset Offset to seek to (or used as "base" for other seeks) * @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, int $whence = SEEK_SET) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence)); if ($offset < 0) { // Throw exception throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset)); } // Seek to position $status = $this->getFileObject()->fseek($offset, $whence); // Return status /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status)); return $status; } /** * Size of this file * * @return $size Size (in bytes) of file * @todo Handle seekStatus */ public function size () { // Get current seek position /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('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__)->debugMessage(sprintf('BASE-FILE-IO: seekStatus[%s]=%d', gettype($seekStatus), $seekStatus)); $size = $this->determineSeekPosition(); // Reset seek position to old /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-FILE-IO: size[%s]=%d', gettype($size), $size)); $this->seek($seekPosition); // Return size /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-FILE-IO: size=%d - EXIT!', $size)); return $size; } }