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