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\Object\BaseFrameworkSystem;
14 * A class for reading text files
16 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
35 class FrameworkTextFileInputPointer extends BaseFileIo implements InputPointer {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Create a file pointer based on the given file. The file will also
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 cannot be read from
56 public static final function createFrameworkTextFileInputPointer ($fileName) {
57 // Some pre-sanity checks...
58 if ((is_null($fileName)) || (empty($fileName))) {
60 throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
61 } elseif (!FrameworkBootstrap::isReachableFilePath($fileName)) {
62 // File cannot be reached
63 throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
64 } elseif ((!FrameworkBootstrap::isReadableFile($fileName)) && (!file_exists($fileName))) {
65 // File does not exist!
66 throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
67 } elseif ((!FrameworkBootstrap::isReadableFile($fileName)) && (file_exists($fileName))) {
68 // File cannot be read from (but exists)
69 throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
72 // Try to open a handler
73 $filePointer = fopen($fileName, 'r');
74 if ((is_null($filePointer)) || ($filePointer === false)) {
75 // Something bad happend
76 throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
79 // Create new instance
80 $pointerInstance = new FrameworkTextFileInputPointer();
82 // Set file pointer and file name
83 $pointerInstance->setPointer($filePointer);
84 $pointerInstance->setFileName($fileName);
86 // Return the instance
87 return $pointerInstance;
91 * Read data a file pointer
93 * @return $data Read data from file
95 public function readFromFile () {
96 // Read 1024 Byte data from the file pointer and return it
97 return $this->read(1024);
101 * Reads a line, maximum 4096 Bytes from current file pointer
103 * @return $data Read data from file
105 public function readLine () {
106 // Read whole line from the file pointer and return it
107 return $this->read();
111 * Reads given amount of bytes from file.
113 * @param $bytes Amount of bytes to read or whole line (only text files)
114 * @return $data Data read from file
115 * @throws NullPointerException If the file pointer instance
116 * is not set by setPointer()
117 * @throws InvalidResourceException If there is being set
119 public function read ($bytes = NULL) {
120 // Some sanity checks
121 if (is_null($this->getPointer())) {
122 // Pointer not initialized
123 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
124 } elseif (!is_resource($this->getPointer())) {
125 // Pointer is not a valid resource!
126 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
130 if (is_int($bytes)) {
131 // Try to read given characters
132 $data = fgets($this->getPointer(), $bytes);
134 // Try to read whole line
135 $data = fgets($this->getPointer());
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);