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 text 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 FrameworkTextFileInputPointer 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 cannot be read from
54 public static final function createFrameworkTextFileInputPointer ($fileName) {
55 // Some pre-sanity checks...
56 if ((is_null($fileName)) || (empty($fileName))) {
58 throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
59 } elseif (!BaseFrameworkSystem::isReachableFilePath($fileName)) {
60 // File cannot be reached
61 throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
62 } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (!file_exists($fileName))) {
63 // File does not exist!
64 throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
65 } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (file_exists($fileName))) {
66 // File cannot be read from (but exists)
67 throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
70 // Try to open a handler
71 $filePointer = fopen($fileName, 'r');
72 if ((is_null($filePointer)) || ($filePointer === FALSE)) {
73 // Something bad happend
74 throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
77 // Create new instance
78 $pointerInstance = new FrameworkTextFileInputPointer();
80 // Set file pointer and file name
81 $pointerInstance->setPointer($filePointer);
82 $pointerInstance->setFileName($fileName);
84 // Return the instance
85 return $pointerInstance;
89 * Read data a file pointer
91 * @return $data Read data from file
93 public function readFromFile () {
94 // Read 1024 Byte data from the file pointer and return it
95 return $this->read(1024);
99 * Reads a line, maximum 4096 Bytes from current file pointer
101 * @return $data Read data from file
103 public function readLine () {
104 // Read whole line from the file pointer and return it
105 return $this->read();
109 * Reads given amount of bytes from file.
111 * @param $bytes Amount of bytes to read or whole line (only text files)
112 * @return $data Data read from file
113 * @throws NullPointerException If the file pointer instance
114 * is not set by setPointer()
115 * @throws InvalidResourceException If there is being set
117 public function read ($bytes = NULL) {
118 // Some sanity checks
119 if (is_null($this->getPointer())) {
120 // Pointer not initialized
121 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
122 } elseif (!is_resource($this->getPointer())) {
123 // Pointer is not a valid resource!
124 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
128 if (is_int($bytes)) {
129 // Try to read given characters
130 $data = fgets($this->getPointer(), $bytes);
132 // Try to read whole line
133 $data = fgets($this->getPointer());
141 * Analyzes entries in index file. This will count all found (and valid)
142 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
143 * only gaps are found, the file is considered as "virgin" (no entries).
146 * @throws UnsupportedOperationException If this method is called
148 public function analyzeFile () {
149 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
153 * Advances to next "block" of bytes
156 * @throws UnsupportedOperationException If this method is called
158 public function next () {
159 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
163 * Checks wether the current entry is valid (not at the end of the file).
164 * This method will return TRUE if an emptied (nulled) entry has been found.
166 * @return $isValid Whether the next entry is valid
167 * @throws UnsupportedOperationException If this method is called
169 public function valid () {
170 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
174 * Gets current seek position ("key").
176 * @return $key Current key in iteration
177 * @throws UnsupportedOperationException If this method is called
179 public function key () {
180 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);