728160adecbfe97666a815d8b9d3b76844550e81
[core.git] / framework / main / classes / file_directories / input / raw / class_FrameworkRawFileInputPointer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filesystem\Pointer\Input;
4
5 // Import framework stuff
6 use CoreFramework\Bootstrap\FrameworkBootstrap;
7 use CoreFramework\FileSystem\BaseFileIo;
8 use CoreFramework\Filesystem\FileNotFoundException;
9 use CoreFramework\FileSystem\FileReadProtectedException;
10 use CoreFramework\Filesystem\Pointer\InputPointer;
11 use CoreFramework\Generic\NullPointerException;
12 use CoreFramework\Generic\UnsupportedOperationException;
13 use CoreFramework\Object\BaseFrameworkSystem;
14
15 /**
16  * A class for reading files
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.shipsimu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Create a file pointer based on the given file. The file will also
50          * be verified here.
51          *
52          * @param       $fileName       The file name we shall pass to fopen()
53          * @throws      FileIsEmptyException            If the provided file name is empty.
54          * @throws      FileIoException                         If the file is not reachable
55          * @throws      FileReadProtectedException      If the file is not found or cannot be read
56          * @throws      FileNotFoundException           If the file does not exist
57          * @return      void
58          */
59         public static final function createFrameworkRawFileInputPointer ($fileName) {
60                 // Some pre-sanity checks...
61                 if ((is_null($fileName)) || (empty($fileName))) {
62                         // No filename given
63                         throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
64                 } elseif (!FrameworkBootstrap::isReachableFilePath($fileName)) {
65                         // File cannot be accessed (due to open_basedir restriction)
66                         throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
67                 } elseif ((!FrameworkBootstrap::isReadableFile($fileName)) && (file_exists($fileName))) {
68                         // File exists but cannot be read from
69                         throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
70                 } elseif ((!FrameworkBootstrap::isReadableFile($fileName)) && (!file_exists($fileName))) {
71                         // File does not exist
72                         throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_NOT_FOUND);
73                 }
74
75                 // Try to open a handler
76                 $filePointer = fopen($fileName, 'rb');
77                 if ((is_null($filePointer)) || ($filePointer === false)) {
78                         // Something bad happend
79                         throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
80                 } // END - if
81
82                 // Create new instance
83                 $pointerInstance = new FrameworkRawFileInputPointer();
84
85                 // Set file pointer and file name
86                 $pointerInstance->setPointer($filePointer);
87                 $pointerInstance->setFileName($fileName);
88
89                 // Return the instance
90                 return $pointerInstance;
91         }
92
93         /**
94          * Read data a file pointer
95          *
96          * @return      mixed   The result of fread()
97          * @throws      NullPointerException    If the file pointer instance
98          *                                                                      is not set by setPointer()
99          * @throws      InvalidResourceException        If there is being set
100          */
101         public function readFromFile () {
102                 if (is_null($this->getPointer())) {
103                         // Pointer not initialized
104                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
105                 } elseif (!is_resource($this->getPointer())) {
106                         // Pointer is not a valid resource!
107                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
108                 }
109
110                 // Read data from the file pointer and return it
111                 return $this->read(1024);
112         }
113
114         /**
115          * Reads a line, maximum 4096 Bytes from current file pointer
116          *
117          * @return      $data   Read data from file
118          * @throws      UnsupportedOperationException   If this method is called
119          */
120         public function readLine () {
121                 // Not supported in binary files ...
122                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
123         }
124
125         /**
126          * Reads given amount of bytes from file.
127          *
128          * @param       $bytes  Amount of bytes to read
129          * @return      $data   Data read from file
130          */
131         public function read ($bytes = NULL) {
132                 // $bytes shall be integer
133                 assert(is_int($bytes));
134
135                 // Try to read given characters
136                 $data = fread($this->getPointer(), $bytes);
137
138                 // Then return it
139                 return $data;
140         }
141
142         /**
143          * Analyzes entries in index file. This will count all found (and valid)
144          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
145          * only gaps are found, the file is considered as "virgin" (no entries).
146          *
147          * @return      void
148          * @throws      UnsupportedOperationException   If this method is called
149          */
150         public function analyzeFile () {
151                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
152         }
153
154         /**
155          * Advances to next "block" of bytes
156          *
157          * @return      void
158          * @throws      UnsupportedOperationException   If this method is called
159          */
160         public function next () {
161                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
162         }
163
164         /**
165          * Checks wether the current entry is valid (not at the end of the file).
166          * This method will return true if an emptied (nulled) entry has been found.
167          *
168          * @return      $isValid        Whether the next entry is valid
169          * @throws      UnsupportedOperationException   If this method is called
170          */
171         public function valid () {
172                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
173         }
174
175         /**
176          * Gets current seek position ("key").
177          *
178          * @return      $key    Current key in iteration
179          * @throws      UnsupportedOperationException   If this method is called
180          */
181         public function key () {
182                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
183         }
184
185 }