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 - 2022 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__)->debugOutput(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__)->debugOutput(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__)->debugOutput(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 return $this->read(1024);
109 * Reads a line, maximum 4096 Bytes from current file pointer
111 * @return $data Read data from file
113 public function readLine () {
114 // Read whole line from the file pointer and return it
115 return $this->read();
119 * Reads given amount of bytes from file.
121 * @param $bytes Amount of bytes to read or whole line (only text files)
122 * @return $data Data read from file
123 * @throws OutOfBoundsException If the position is not seekable
124 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
125 * @throws LogicException If $fileObject is not an object
127 public function read (int $bytes = 0) {
128 // Some sanity checks
130 // Cannot be below zero
131 throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
132 } elseif (is_null($this->getFileObject())) {
133 // Pointer not initialized
134 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
135 } elseif (!is_object($this->getFileObject())) {
136 // Pointer is not a valid resource!
137 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
142 // Try to read given characters
143 $data = $this->getFileObject()->fread($bytes);
145 // Try to read whole line
146 $data = $this->getFileObject()->fgets();
154 * Analyzes entries in index file. This will count all found (and valid)
155 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
156 * only gaps are found, the file is considered as "virgin" (no entries).
159 * @throws UnsupportedOperationException If this method is called
161 public function analyzeFileStructure () {
162 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
166 * Advances to next "block" of bytes
169 * @throws UnsupportedOperationException If this method is called
171 public function next () {
172 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
176 * Checks wether the current entry is valid (not at the end of the file).
177 * This method will return true if an emptied (nulled) entry has been found.
179 * @return $isValid Whether the next entry is valid
180 * @throws UnsupportedOperationException If this method is called
182 public function valid () {
183 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
187 * Gets current seek position ("key").
189 * @return $key Current key in iteration
190 * @throws UnsupportedOperationException If this method is called
192 public function key () {
193 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);