3 namespace CoreFramework\Filesystem\Pointer\Input;
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;
12 * A class for reading files
14 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
33 class FrameworkRawFileInputPointer extends BaseFileIo implements InputPointer {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Create a file pointer based on the given file. The file will also
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
55 public static final function createFrameworkRawFileInputPointer ($fileName) {
56 // Some pre-sanity checks...
57 if ((is_null($fileName)) || (empty($fileName))) {
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);
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);
78 // Create new instance
79 $pointerInstance = new FrameworkRawFileInputPointer();
81 // Set file pointer and file name
82 $pointerInstance->setPointer($filePointer);
83 $pointerInstance->setFileName($fileName);
85 // Return the instance
86 return $pointerInstance;
90 * Read data a file pointer
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
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);
106 // Read data from the file pointer and return it
107 return $this->read(1024);
111 * Reads a line, maximum 4096 Bytes from current file pointer
113 * @return $data Read data from file
114 * @throws UnsupportedOperationException If this method is called
116 public function readLine () {
117 // Not supported in binary files ...
118 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
122 * Reads given amount of bytes from file.
124 * @param $bytes Amount of bytes to read
125 * @return $data Data read from file
127 public function read ($bytes = NULL) {
128 // $bytes shall be integer
129 assert(is_int($bytes));
131 // Try to read given characters
132 $data = fread($this->getPointer(), $bytes);
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).
144 * @throws UnsupportedOperationException If this method is called
146 public function analyzeFile () {
147 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
151 * Advances to next "block" of bytes
154 * @throws UnsupportedOperationException If this method is called
156 public function next () {
157 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
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.
164 * @return $isValid Whether the next entry is valid
165 * @throws UnsupportedOperationException If this method is called
167 public function valid () {
168 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
172 * Gets current seek position ("key").
174 * @return $key Current key in iteration
175 * @throws UnsupportedOperationException If this method is called
177 public function key () {
178 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);