* @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.shipsimu.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 . */ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputPointer { /** * Protected constructor * * @return void */ private function __construct () { // Call parent constructor parent::__construct(__CLASS__); } /** * Create a file pointer based on the given file. The file will also * be verified here. * * @param $fileInstance An instance of a SplFileInfo class * @return void * @throws FileReadProtectedException If PHP cannot read an existing file * @throws FileWriteProtectedException If PHP cannot write an existing file * @throws PathWriteProtectedException If PHP cannot write to an existing path * @throws FileIoException If fopen() returns not a file resource */ public static final function createFrameworkFileInputOutputPointer (SplFileInfo $fileInstance) { // Some pre-sanity checks... /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance)); if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) { // File exists but cannot be read throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE); } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (file_exists($fileInstance))) { // File exists but cannot be read throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ); } elseif (!is_writable($fileInstance->getPath())) { // Path is not writable throw new PathWriteProtectedException($fileInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN); } elseif (($fileInstance->isFile()) && (!$fileInstance->isWritable())) { // File exists but cannot be written throw new FileWriteProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN); } // Try to open a handler $fileObject = $fileInstance->openFile('c+b'); // Is it valid? /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: fileObject[]=%s', gettype($fileObject))); if (!($fileObject instanceof SplFileObject)) { // Something bad happend throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID); } // Create new instance $pointerInstance = new FrameworkFileInputOutputPointer(); // Set file object and file name $pointerInstance->setFileObject($fileObject); // Return the instance /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString())); return $pointerInstance; } /** * Read 1024 bytes data from a file pointer * * @return mixed The result of fread() */ public function readFromFile () { // Read data from the file pointer and return it /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!'); $data = $this->read(1024); // Return data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%d - EXIT!', gettype($data), $data)); return $data; } /** * Write data to a file pointer * * @param $dataStream The data stream we shall write to the file * @return mixed Number of writes bytes or false on error */ public function writeToFile (string $dataStream) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: dataStream(%d)=%s - CALLED!', strlen($dataStream), $dataStream)); if (empty($dataStream)) { // Empty dataStream throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } // Get length $length = strlen($dataStream); // Write data to the file pointer and return written bytes /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: Invoking this->fileObject->fwrite(%s,%d) ...', $dataStream, $length)); $status = $this->getFileObject()->fwrite($dataStream, $length); // Return status /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status)); return $status; } /** * 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 OutOfBoundsException If the position is not seekable * @throws InvalidArgumentException If a parameter is not valid */ public function writeAtPosition (int $seekPosition, string $dataStream) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,dataStream(%d)=%s - CALLED!', $seekPosition, strlen($dataStream), $dataStream)); if ($seekPosition < 0) { // Invalid seek position throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition)); } elseif (empty($dataStream)) { // Empty dataStream throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT); } elseif (($this->getFileSize() > 0 || $seekPosition > 0) && $this->seek($seekPosition) === -1) { // Could not seek throw new InvalidArgumentException(sprintf('Could not seek to seekPosition=%d', $seekPosition)); } // Then write the data at that position /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: Invoking this->writeToFile(%s) ...', $dataStream)); $status = $this->writeToFile($dataStream); // Return status /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status)); return $status; } /** * Rewinds to the beginning of the file * * @return void */ public function rewind () { /// Rewind the pointer /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!'); $this->getFileObject()->rewind(); // Trace message /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: EXIT!'); } /** * Seeks to given position * * @param $seekPosition Seek position in file * @param $whence "Seek mode" (see http://de.php.net/fseek) * @return $status Status of this operation * @throws OutOfBoundsException If the position is not seekable */ public function seek (int $seekPosition, int $whence = SEEK_SET) { // Validate parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: seekPosition=%d,whence=%d - CALLED!', $seekPosition, $whence)); if ($seekPosition < 0) { // Invalid seek position throw new OutOfBoundsException(sprintf('seekPosition=%d is not valid.', $seekPosition)); } elseif ($whence < 0) { // Invalid seek position throw new OutOfBoundsException(sprintf('whence=%d is not valid.', $whence)); } // Move the file pointer $status = $this->getFileObject()->fseek($seekPosition, $whence); // Return status /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: status[%s]=%d - EXIT!', gettype($status), $status)); return $status; } /** * Reads a line, maximum 4096 Bytes from current file pointer * * @return $data Read data from file */ public function readLine () { // Read whole line /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!'); $data = $this->read(); // Return data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data)); return $data; } /** * Reads given amount of bytes from file. * * @param $bytes Amount of bytes to read * @return $data Data read from file * @throws OutOfBoundsException If the position is not seekable */ public function read (int $bytes = 0) { // Validatre parameter /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: bytes=%d - CALLED!', $bytes)); if ($bytes < 0) { // Bytes cannot be lesser than zero throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes)); } // Is $bytes bigger than zero? if ($bytes > 0) { // Try to read given characters $data = $this->getFileObject()->fread($bytes); } else { // Try to read whole line $data = $this->getFileObject()->fgets(); } // Then return it /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: data[%s]=%s - EXIT!', gettype($data), $data)); return $data; } /** * Analyzes entries in index file. This will count all found (and valid) * entries, mark invalid as damaged and count gaps ("fragmentation"). If * only gaps are found, the file is considered as "virgin" (no entries). * * @return void * @throws UnsupportedOperationException If this method is called */ public function analyzeFileStructure () { throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); } /** * Advances to next "block" of bytes * * @return void * @throws UnsupportedOperationException If this method is called */ public function next () { throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); } /** * 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 * @throws UnsupportedOperationException If this method is called */ public function valid () { throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); } /** * Gets current seek position ("key"). * * @return $key Current key in iteration * @throws UnsupportedOperationException If this method is called */ public function key () { throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION); } /** * "Getter" for file size * * @return $fileSize Size of currently loaded file * @throws UnexpectedValueException If $fileData does not contain "size" */ public function getFileSize () { // Get file's data /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-INPUT-OUTPUT-POINTER: CALLED!'); $fileData = $this->getFileObject()->fstat(); // Make sure the required array key is there if (!isset($fileData['size'])) { // Not valid array throw new UnexpectedValueException(sprintf('fileData=%s has no element "size"', print_r($fileData, TRUE)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE); } // Return size /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-INPUT-OUTPUT-POINTER: fileData[size]=%d - EXIT!', $fileData['size'])); return $fileData['size']; } }