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