Continued:
[core.git] / framework / main / classes / file_directories / input / text / class_FrameworkTextFileInputPointer.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer\Input;
4
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;
16
17 // Import SPL stuff
18 use \SplFileInfo;
19 use \SplFileObject;
20
21 /**
22  * A class for reading text files
23  *
24  * @author              Roland Haeder <webmaster@shipsimu.org>
25  * @version             0.0.0
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
29  *
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.
34  *
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.
39  *
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/>.
42  */
43 class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer {
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         private function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52         }
53
54         /**
55          * Create a file pointer based on the given file. The file will also
56          * be verified here.
57          *
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
61          * @return      void
62          */
63         public static final function createFrameworkTextFileInputPointer (SplFileInfo $fileInstance) {
64                 // Check parameter
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);
75                 }
76
77                 // Try to open a handler
78                 $fileObject = $fileInstance->openFile('r');
79
80                 // Is it valid?
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);
85                 }
86
87                 // Create new instance
88                 $pointerInstance = new FrameworkTextFileInputPointer();
89
90                 // Set file pointer and file name
91                 $pointerInstance->setFileObject($fileObject);
92
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;
96         }
97
98         /**
99          * Read data a file pointer
100          *
101          * @return      $data   Read data from file
102          */
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);
107         }
108
109         /**
110          * Reads a line, maximum 4096 Bytes from current file pointer
111          *
112          * @return      $data   Read data from file
113          */
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();
118         }
119
120         /**
121          * Reads given amount of bytes from file.
122          *
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
128          */
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));
132                 if ($bytes < 0) {
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);
141                 }
142
143                 // Is $bytes set?
144                 if ($bytes > 0) {
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);
148                 } else {
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();
152                 }
153
154                 // Then return it
155                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('RAW-FILE-INPUT-POINTER: data()=%d - EXIT!', strlen($data)));
156                 return $data;
157         }
158
159         /**
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).
163          *
164          * @return      void
165          * @throws      UnsupportedOperationException   If this method is called
166          */
167         public function analyzeFileStructure () {
168                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
169         }
170
171         /**
172          * Advances to next "block" of bytes
173          *
174          * @return      void
175          * @throws      UnsupportedOperationException   If this method is called
176          */
177         public function next () {
178                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
179         }
180
181         /**
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.
184          *
185          * @return      $isValid        Whether the next entry is valid
186          * @throws      UnsupportedOperationException   If this method is called
187          */
188         public function valid () {
189                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
190         }
191
192         /**
193          * Gets current seek position ("key").
194          *
195          * @return      $key    Current key in iteration
196          * @throws      UnsupportedOperationException   If this method is called
197          */
198         public function key () {
199                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
200         }
201
202 }