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\NullPointerException;
13 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
14 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
21 * A class for reading text files
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer {
44 * Protected constructor
48 private function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
54 * Create a file pointer based on the given file. The file will also
57 * @param $fileName The file name we shall pass to fopen()
58 * @throws FileIoException If the file is not reachable
59 * @throws FileReadProtectedException If the file cannot be read from
62 public static final function createFrameworkTextFileInputPointer (SplFileInfo $fileInstance) {
64 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
65 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
66 // File cannot be reached
67 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
68 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
69 // File does not exist!
70 throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
71 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
72 // File cannot be read from (but exists)
73 throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
76 // Try to open a handler
77 $fileObject = $fileInstance->openFile('r');
80 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
81 if (!($fileObject instanceof SplFileObject)) {
82 // Something bad happend
83 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
86 // Create new instance
87 $pointerInstance = new FrameworkTextFileInputPointer();
89 // Set file pointer and file name
90 $pointerInstance->setFileObject($fileObject);
92 // Return the instance
93 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('TEXT-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
94 return $pointerInstance;
98 * Read data a file pointer
100 * @return $data Read data from file
102 public function readFromFile () {
103 // Read 1024 Byte data from the file pointer and return it
104 return $this->read(1024);
108 * Reads a line, maximum 4096 Bytes from current file pointer
110 * @return $data Read data from file
112 public function readLine () {
113 // Read whole line from the file pointer and return it
114 return $this->read();
118 * Reads given amount of bytes from file.
120 * @param $bytes Amount of bytes to read or whole line (only text files)
121 * @return $data Data read from file
122 * @throws OutOfBoundsException If the position is not seekable
123 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
124 * @throws LogicException If $fileObject is not an object
126 public function read (int $bytes = 0) {
127 // Some sanity checks
129 // Cannot be below zero
130 throw new OutOfBoundsException(sprintf('bytes=%d is not valid', $bytes));
131 } elseif (is_null($this->getFileObject())) {
132 // Pointer not initialized
133 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
134 } elseif (!is_object($this->getFileObject())) {
135 // Pointer is not a valid resource!
136 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
141 // Try to read given characters
142 $data = $this->getFileObject()->fread($bytes);
144 // Try to read whole line
145 $data = $this->getFileObject()->fgets();
153 * Analyzes entries in index file. This will count all found (and valid)
154 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
155 * only gaps are found, the file is considered as "virgin" (no entries).
158 * @throws UnsupportedOperationException If this method is called
160 public function analyzeFileStructure () {
161 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
165 * Advances to next "block" of bytes
168 * @throws UnsupportedOperationException If this method is called
170 public function next () {
171 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
175 * Checks wether the current entry is valid (not at the end of the file).
176 * This method will return true if an emptied (nulled) entry has been found.
178 * @return $isValid Whether the next entry is valid
179 * @throws UnsupportedOperationException If this method is called
181 public function valid () {
182 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
186 * Gets current seek position ("key").
188 * @return $key Current key in iteration
189 * @throws UnsupportedOperationException If this method is called
191 public function key () {
192 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);