3 namespace CoreFramework\Filesystem\Pointer\Input;
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;
16 * A class for reading files
18 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
37 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Create a file pointer based on the given file. The file will also
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
59 public static final function createFrameworkRawFileInputPointer ($fileName) {
60 // Some pre-sanity checks...
61 if ((is_null($fileName)) || (empty($fileName))) {
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);
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);
82 // Create new instance
83 $pointerInstance = new FrameworkRawFileInputPointer();
85 // Set file pointer and file name
86 $pointerInstance->setPointer($filePointer);
87 $pointerInstance->setFileName($fileName);
89 // Return the instance
90 return $pointerInstance;
94 * Read data a file pointer
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
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);
110 // Read data from the file pointer and return it
111 return $this->read(1024);
115 * Reads a line, maximum 4096 Bytes from current file pointer
117 * @return $data Read data from file
118 * @throws UnsupportedOperationException If this method is called
120 public function readLine () {
121 // Not supported in binary files ...
122 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
126 * Reads given amount of bytes from file.
128 * @param $bytes Amount of bytes to read
129 * @return $data Data read from file
131 public function read ($bytes = NULL) {
132 // $bytes shall be integer
133 assert(is_int($bytes));
135 // Try to read given characters
136 $data = fread($this->getPointer(), $bytes);
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).
148 * @throws UnsupportedOperationException If this method is called
150 public function analyzeFile () {
151 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
155 * Advances to next "block" of bytes
158 * @throws UnsupportedOperationException If this method is called
160 public function next () {
161 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
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.
168 * @return $isValid Whether the next entry is valid
169 * @throws UnsupportedOperationException If this method is called
171 public function valid () {
172 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
176 * Gets current seek position ("key").
178 * @return $key Current key in iteration
179 * @throws UnsupportedOperationException If this method is called
181 public function key () {
182 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);