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