]> 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 \SplFileInfo;
18 use \SplFileObject;
19
20 /**
21  * A class for reading files
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
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.
33  *
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.
38  *
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/>.
41  */
42 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         private function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51         }
52
53         /**
54          * Create a file pointer based on the given file. The file will also
55          * be verified here.
56          *
57          * @param       $fileInstance   An instance of a SplFileInfo class
58          * @throws      FileIoException                         If the file is not reachable
59          * @throws      FileReadProtectedException      If the file is not found or cannot be read
60          * @throws      FileNotFoundException           If the file does not exist
61          * @return      void
62          */
63         public static final function createFrameworkRawFileInputPointer (SplFileInfo $fileInstance) {
64                 // Some pre-sanity checks...
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 accessed (due to open_basedir restriction)
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_NOT_FOUND);
72                 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && ($fileInstance->isFile())) {
73                         // File exists but cannot be read from
74                         throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
75                 }
76
77                 // Try to open a handler
78                 $fileObject = $fileInstance->openFile('rb');
79
80                 // Is it valid?
81                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-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 FrameworkRawFileInputPointer();
89
90                 // Set file pointer and file name
91                 $pointerInstance->setFileObject($fileObject);
92
93                 // Return the instance
94                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('RAW-FILE-INPUT-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
95                 return $pointerInstance;
96         }
97
98         /**
99          * Read data a file pointer
100          *
101          * @return      mixed   The result of fread()
102          * @throws      NullPointerException    If the file pointer instance is not set by setFileObject()
103          * @throws      LogicException  If there is no object being set
104          */
105         public function readFromFile () {
106                 if (is_null($this->getFileObject())) {
107                         // Pointer not initialized
108                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
109                 } elseif (!is_object($this->getFileObject())) {
110                         // Pointer is not a valid resource!
111                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
112                 }
113
114                 // Read data from the file pointer and return it
115                 return $this->read(1024);
116         }
117
118         /**
119          * Reads a line, maximum 4096 Bytes from current file pointer
120          *
121          * @return      $data   Read data from file
122          * @throws      UnsupportedOperationException   If this method is called
123          */
124         public function readLine () {
125                 // Not supported in binary files ...
126                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
127         }
128
129         /**
130          * Reads given amount of bytes from file.
131          *
132          * @param       $bytes  Amount of bytes to read
133          * @return      $data   Data read from file
134          */
135         public function read (int $bytes = 0) {
136                 // Try to read given characters
137                 $data = $this->getFileObject()->fread($bytes);
138
139                 // Then return it
140                 return $data;
141         }
142
143         /**
144          * Analyzes entries in index file. This will count all found (and valid)
145          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
146          * only gaps are found, the file is considered as "virgin" (no entries).
147          *
148          * @return      void
149          * @throws      UnsupportedOperationException   If this method is called
150          */
151         public function analyzeFileStructure () {
152                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
153         }
154
155         /**
156          * Advances to next "block" of bytes
157          *
158          * @return      void
159          * @throws      UnsupportedOperationException   If this method is called
160          */
161         public function next () {
162                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
163         }
164
165         /**
166          * Checks wether the current entry is valid (not at the end of the file).
167          * This method will return true if an emptied (nulled) entry has been found.
168          *
169          * @return      $isValid        Whether the next entry is valid
170          * @throws      UnsupportedOperationException   If this method is called
171          */
172         public function valid () {
173                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
174         }
175
176         /**
177          * Gets current seek position ("key").
178          *
179          * @return      $key    Current key in iteration
180          * @throws      UnsupportedOperationException   If this method is called
181          */
182         public function key () {
183                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
184         }
185
186 }