3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Input;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\BaseFileIo;
8 use Org\Mxchange\CoreFramework\Filesystem\FileIoException;
9 use Org\Mxchange\CoreFramework\Filesystem\FileNotFoundException;
10 use Org\Mxchange\CoreFramework\Filesystem\FileReadProtectedException;
11 use Org\Mxchange\CoreFramework\Filesystem\Pointer\InputPointer;
12 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
13 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
14 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
15 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
22 * A class for reading text files
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer {
45 * Protected constructor
49 private function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Create a file pointer based on the given file. The file will also
58 * @param $fileName The file name we shall pass to fopen()
59 * @throws FileIoException If the file is not reachable
60 * @throws FileReadProtectedException If the file cannot be read from
63 public static final function createFrameworkTextFileInputPointer (SplFileInfo $fileInstance) {
65 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
66 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
67 // File cannot be reached
68 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
69 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
70 // File does not exist!
71 throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
72 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
73 // File cannot be read from (but exists)
74 throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
77 // Try to open a handler
78 $fileObject = $fileInstance->openFile('r');
81 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('TEXT-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
82 if (!($fileObject instanceof SplFileObject)) {
83 // Something bad happend
84 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
87 // Create new instance
88 $pointerInstance = new FrameworkTextFileInputPointer();
90 // Set file pointer and file name
91 $pointerInstance->setFileObject($fileObject);
93 // Return the instance
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('TEXT-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
95 return $pointerInstance;
99 * Read data a file pointer
101 * @return $data Read data from file
103 public function readFromFile () {
104 // Read 1024 Byte data from the file pointer and return it
105 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('RAW-FILE-INPUT-POINTER: CALLED!');
106 return $this->read(1024);
110 * Reads a line, maximum 4096 Bytes from current file pointer
112 * @return $data Read data from file
114 public function readLine () {
115 // Read whole line from the file pointer and return it
116 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('RAW-FILE-INPUT-POINTER: CALLED!');
117 return $this->read();
121 * Reads given amount of bytes from file.
123 * @param $bytes Amount of bytes to read or whole line (only text files)
124 * @return $data Data read from file
125 * @throws OutOfBoundsException If the position is not seekable
126 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
127 * @throws LogicException If $fileObject is not an object
129 public function read (int $bytes = 0) {
130 // Some sanity checks
131 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-INPUT-POINTER: bytes=%d - CALLED!', $bytes));
133 // Cannot be below zero
134 throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
135 } elseif (is_null($this->getFileObject())) {
136 // Pointer not initialized
137 throw new NullPointerException($this, FrameworkInterface::EXCEPTION_IS_NULL_POINTER);
138 } elseif (!is_object($this->getFileObject())) {
139 // Pointer is not a valid resource!
140 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
145 // Try to read given characters
146 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-INPUT-POINTER: Invoking this->fileObject->fread(%d) ...', $bytes));
147 $data = $this->getFileObject()->fread($bytes);
149 // Try to read whole line
150 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('RAW-FILE-INPUT-POINTER: Invoking this->fileObject->fgets() ...');
151 $data = $this->getFileObject()->fgets();
155 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-INPUT-POINTER: data()=%d - EXIT!', strlen($data)));
160 * Analyzes entries in index file. This will count all found (and valid)
161 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
162 * only gaps are found, the file is considered as "virgin" (no entries).
165 * @throws UnsupportedOperationException If this method is called
167 public function analyzeFileStructure () {
168 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
172 * Advances to next "block" of bytes
175 * @throws UnsupportedOperationException If this method is called
177 public function next () {
178 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
182 * Checks wether the current entry is valid (not at the end of the file).
183 * This method will return true if an emptied (nulled) entry has been found.
185 * @return $isValid Whether the next entry is valid
186 * @throws UnsupportedOperationException If this method is called
188 public function valid () {
189 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
193 * Gets current seek position ("key").
195 * @return $key Current key in iteration
196 * @throws UnsupportedOperationException If this method is called
198 public function key () {
199 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);