3 namespace Org\Mxchange\CoreFramework\Filesystem\Pointer;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
8 use Org\Mxchange\CoreFramework\FileSystem\FileReadProtectedException;
9 use Org\Mxchange\CoreFramework\FileSystem\FileWriteProtectedException;
10 use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
11 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
12 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
13 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
19 * A class for reading files
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 <<<<<<< HEAD:framework/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
27 >>>>>>> Some updates::inc/main/classes/file_directories/io/class_FrameworkFileInputOutputPointer.php
28 * @license GNU GPL 3.0 or any newer version
29 * @link http://www.shipsimu.org
31 * This program is free software: you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation, either version 3 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program. If not, see <http://www.gnu.org/licenses/>.
44 class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputPointer {
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
56 * Create a file pointer based on the given file. The file will also
59 * @param $fileInstance An instance of a SplFileInfo class
61 * @throws FileReadProtectedException If PHP cannot read an existing file
62 * @throws FileWriteProtectedException If PHP cannot write an existing file
63 * @throws PathWriteProtectedException If PHP cannot write to an existing path
64 * @throws FileIoException If fopen() returns not a file resource
66 public static final function createFrameworkFileInputOutputPointer (SplFileInfo $fileInstance) {
67 // Some pre-sanity checks...
68 if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
69 // File exists but cannot be read
70 throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
71 } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (file_exists($fileInstance))) {
72 // File exists but cannot be read
73 throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
74 } elseif (!is_writable($fileInstance->getPath())) {
75 // Path is not writable
76 throw new PathWriteProtectedException($fileInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
77 } elseif (($fileInstance->isFile()) && (!$fileInstance->isWritable())) {
78 // File exists but cannot be written
79 throw new FileWriteProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
82 // Try to open a handler
83 $fileObject = $fileInstance->openFile('c+b');
86 if ((is_null($fileObject)) || ($fileObject === false)) {
87 // Something bad happend
88 throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID);
91 // Create new instance
92 $pointerInstance = new FrameworkFileInputOutputPointer();
94 // Set file object and file name
95 $pointerInstance->setFileObject($fileObject);
97 // Return the instance
98 return $pointerInstance;
102 * Validates file pointer and throws exceptions. This method does not return
103 * anything (not reliable) as this method checks the file pointer and on
104 * case of an error it throws an exception. If this method does not throw
105 * any exceptions, the file pointer seems to be fine.
108 * @throws NullPointerException If the file pointer instance
109 * is not set by setPointer()
110 * @todo Add more checks
112 private function validateFilePointer () {
113 if (is_null($this->getFileObject())) {
114 // Pointer not initialized
115 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
122 * Read 1024 bytes data from a file pointer
124 * @return mixed The result of fread()
126 public function readFromFile () {
127 // Validate the pointer
128 $this->validateFilePointer();
130 // Read data from the file pointer and return it
131 return $this->read(1024);
135 * Write data to a file pointer
137 * @param $dataStream The data stream we shall write to the file
138 * @return mixed Number of writes bytes or false on error
140 public function writeToFile ($dataStream) {
141 // Validate the pointer
142 $this->validateFilePointer();
144 // Write data to the file pointer and return written bytes
145 return $this->getFileObject()->fwrite($dataStream, strlen($dataStream));
149 * Writes at given position by seeking to it.
151 * @param $seekPosition Seek position in file
152 * @param $data Data to be written
153 * @return mixed Number of writes bytes or false on error
155 public function writeAtPosition ($seekPosition, $data) {
157 $this->seek($seekPosition);
159 // Then write the data at that position
160 return $this->writeToFile($data);
164 * Rewinds to the beginning of the file
166 * @return $status Status of this operation
168 public function rewind () {
169 // Validate the pointer
170 $this->validateFilePointer();
172 // Rewind the pointer
173 return $this->getFileObject()->rewind();
177 * Seeks to given position
179 * @param $seekPosition Seek position in file
180 * @param $whence "Seek mode" (see http://de.php.net/fseek)
181 * @return $status Status of this operation
183 public function seek ($seekPosition, $whence = SEEK_SET) {
184 // Validate the pointer
185 $this->validateFilePointer();
187 // Move the file pointer
188 return $this->getFileObject()->fseek($seekPosition, $whence);
192 * Reads a line, maximum 4096 Bytes from current file pointer
194 * @return $data Read data from file
196 public function readLine () {
198 return $this->read();
202 * Reads given amount of bytes from file.
204 * @param $bytes Amount of bytes to read
205 * @return $data Data read from file
207 public function read ($bytes = NULL) {
208 // Validate the pointer
209 $this->validateFilePointer();
212 if (is_int($bytes)) {
213 // Try to read given characters
214 $data = $this->getFileObject()->fread($bytes);
216 // Try to read whole line
217 $data = $this->getFileObject()->fgets();
225 * Analyzes entries in index file. This will count all found (and valid)
226 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
227 * only gaps are found, the file is considered as "virgin" (no entries).
230 * @throws UnsupportedOperationException If this method is called
232 public function analyzeFile () {
233 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
237 * Advances to next "block" of bytes
240 * @throws UnsupportedOperationException If this method is called
242 public function next () {
243 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
247 * Checks wether the current entry is valid (not at the end of the file).
248 * This method will return true if an emptied (nulled) entry has been found.
250 * @return $isValid Whether the next entry is valid
251 * @throws UnsupportedOperationException If this method is called
253 public function valid () {
254 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
258 * Gets current seek position ("key").
260 * @return $key Current key in iteration
261 * @throws UnsupportedOperationException If this method is called
263 public function key () {
264 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
268 * "Getter" for file size
270 * @return $fileSize Size of currently loaded file
272 public function getFileSize () {
273 // Check if the pointer is still valid
274 $this->validateFilePointer();
277 $fileData = $this->getFileObject()->fstat();
279 // Make sure the required array key is there
280 assert(isset($fileData['size']));
283 return $fileData['size'];