3 namespace CoreFramework\Filesystem\Pointer\Input;
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;
15 * A class for reading files
17 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
36 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Create a file pointer based on the given file. The file will also
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
58 public static final function createFrameworkRawFileInputPointer ($fileName) {
59 // Some pre-sanity checks...
60 if ((is_null($fileName)) || (empty($fileName))) {
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);
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);
81 // Create new instance
82 $pointerInstance = new FrameworkRawFileInputPointer();
84 // Set file pointer and file name
85 $pointerInstance->setPointer($filePointer);
86 $pointerInstance->setFileName($fileName);
88 // Return the instance
89 return $pointerInstance;
93 * Read data a file pointer
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
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);
109 // Read data from the file pointer and return it
110 return $this->read(1024);
114 * Reads a line, maximum 4096 Bytes from current file pointer
116 * @return $data Read data from file
117 * @throws UnsupportedOperationException If this method is called
119 public function readLine () {
120 // Not supported in binary files ...
121 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
125 * Reads given amount of bytes from file.
127 * @param $bytes Amount of bytes to read
128 * @return $data Data read from file
130 public function read ($bytes = NULL) {
131 // $bytes shall be integer
132 assert(is_int($bytes));
134 // Try to read given characters
135 $data = fread($this->getPointer(), $bytes);
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).
147 * @throws UnsupportedOperationException If this method is called
149 public function analyzeFile () {
150 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
154 * Advances to next "block" of bytes
157 * @throws UnsupportedOperationException If this method is called
159 public function next () {
160 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
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.
167 * @return $isValid Whether the next entry is valid
168 * @throws UnsupportedOperationException If this method is called
170 public function valid () {
171 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
175 * Gets current seek position ("key").
177 * @return $key Current key in iteration
178 * @throws UnsupportedOperationException If this method is called
180 public function key () {
181 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);