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