]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/input/raw/class_FrameworkRawFileInputPointer.php
Continued:
[core.git] / framework / main / classes / file_directories / input / raw / class_FrameworkRawFileInputPointer.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 \InvalidArgumentException;
19 use \SplFileInfo;
20 use \SplFileObject;
21 use \UnexpectedValueException;
22
23 /**
24  * A class for reading files
25  *
26  * @author              Roland Haeder <webmaster@shipsimu.org>
27  * @version             0.0.0
28  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
29  * @license             GNU GPL 3.0 or any newer version
30  * @link                http://www.shipsimu.org
31  *
32  * This program is free software: you can redistribute it and/or modify
33  * it under the terms of the GNU General Public License as published by
34  * the Free Software Foundation, either version 3 of the License, or
35  * (at your option) any later version.
36  *
37  * This program is distributed in the hope that it will be useful,
38  * but WITHOUT ANY WARRANTY; without even the implied warranty of
39  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40  * GNU General Public License for more details.
41  *
42  * You should have received a copy of the GNU General Public License
43  * along with this program. If not, see <http://www.gnu.org/licenses/>.
44  */
45 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
46         /**
47          * Protected constructor
48          *
49          * @return      void
50          */
51         private function __construct () {
52                 // Call parent constructor
53                 parent::__construct(__CLASS__);
54         }
55
56         /**
57          * Create a file pointer based on the given file. The file will also
58          * be verified here.
59          *
60          * @param       $fileInstance   An instance of a SplFileInfo class
61          * @throws      FileIoException                         If the file is not reachable
62          * @throws      FileReadProtectedException      If the file is not found or cannot be read
63          * @throws      FileNotFoundException           If the file does not exist
64          * @return      void
65          */
66         public static final function createFrameworkRawFileInputPointer (SplFileInfo $fileInstance) {
67                 // Some pre-sanity checks...
68                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileInstance[%s]=%s - CALLED!', get_class($fileInstance), $fileInstance->__toString()));
69                 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
70                         // File cannot be accessed (due to open_basedir restriction)
71                         throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
72                 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (!$fileInstance->isFile())) {
73                         // File does not exist
74                         throw new FileNotFoundException($fileInstance, self::EXCEPTION_FILE_NOT_FOUND);
75                 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
76                         // File exists but cannot be read from
77                         throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
78                 }
79
80                 // Try to open a handler
81                 $fileObject = $fileInstance->openFile('rb');
82
83                 // Is it valid?
84                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject[]=%s', gettype($fileObject)));
85                 if (!($fileObject instanceof SplFileObject)) {
86                         // Something bad happend
87                         throw new FileIoException($fileInstance, self::EXCEPTION_FILE_POINTER_INVALID);
88                 }
89
90                 // Create new instance
91                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: fileObject.pathname=%s', $fileObject->getPathname()));
92                 $pointerInstance = new FrameworkRawFileInputPointer();
93
94                 // Set file pointer and file name
95                 $pointerInstance->setFileObject($fileObject);
96
97                 // Return the instance
98                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
99                 return $pointerInstance;
100         }
101
102         /**
103          * Read data a file pointer
104          *
105          * @return      mixed   The result of fread()
106          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
107          * @throws      LogicException  If there is no object being set
108          */
109         public function readFromFile () {
110                 // Trace message
111                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-INPUT-POINTER: CALLED!');
112                 if (is_null($this->getFileObject())) {
113                         // Pointer not initialized
114                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
115                 } elseif (!is_object($this->getFileObject())) {
116                         // Pointer is not a valid resource!
117                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
118                 }
119
120                 // Read data from the file pointer and return it
121                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('RAW-FILE-INPUT-POINTER: Invoking this->read(1024) - EXIT!');
122                 return $this->read(1024);
123         }
124
125         /**
126          * Reads a line, maximum 4096 Bytes from current file pointer
127          *
128          * @return      $data   Read data from file
129          * @throws      UnsupportedOperationException   If this method is called
130          */
131         public function readLine () {
132                 // Not supported in binary files ...
133                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
134         }
135
136         /**
137          * Reads given amount of bytes from file.
138          *
139          * @param       $bytes  Amount of bytes to read
140          * @return      $data   Data read from file
141          * @throws      InvalidArgumentException        If a parameter is not valid
142          * @throws      UnexpectedValueException        If fread() returns a non-string value
143          */
144         public function read (int $bytes = 0) {
145                 // Trace message
146                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: bytes=%d - CALLED!', $bytes));
147                 if ($bytes < 0) {
148                         // Throw IAE
149                         throw new InvalidArgumentException(sprintf('bytes=%d is an invalid value, only zero or postive numbers are accepted', $bytes));
150                 }
151
152                 // Try to read given characters
153                 $data = $this->getFileObject()->fread($bytes);
154
155                 // Is it valid?
156                 if (!is_string($data)) {
157                         // Is not a string
158                         throw new UnexpectedValueException(sprintf('Returned data[]=%s is not expected.', gettype($data)));
159                 }
160
161                 // Then return it
162                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: data()=%d - EXIT!', strlen($data)));
163                 return $data;
164         }
165
166         /**
167          * Analyzes entries in index file. This will count all found (and valid)
168          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
169          * only gaps are found, the file is considered as "virgin" (no entries).
170          *
171          * @return      void
172          * @throws      UnsupportedOperationException   If this method is called
173          */
174         public function analyzeFileStructure () {
175                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
176         }
177
178         /**
179          * Advances to next "block" of bytes
180          *
181          * @return      void
182          * @throws      UnsupportedOperationException   If this method is called
183          */
184         public function next () {
185                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
186         }
187
188         /**
189          * Checks wether the current entry is valid (not at the end of the file).
190          * This method will return true if an emptied (nulled) entry has been found.
191          *
192          * @return      $isValid        Whether the next entry is valid
193          * @throws      UnsupportedOperationException   If this method is called
194          */
195         public function valid () {
196                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
197         }
198
199         /**
200          * Gets current seek position ("key").
201          *
202          * @return      $key    Current key in iteration
203          * @throws      UnsupportedOperationException   If this method is called
204          */
205         public function key () {
206                 throw new UnsupportedOperationException([$this, __FUNCTION__], self::EXCEPTION_UNSPPORTED_OPERATION);
207         }
208
209 }