3 * A class for reading files
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class FrameworkFileInputPointer extends BaseFrameworkSystem {
26 * The current file we are working in
28 private $fileName = '';
33 private $filePointer = NULL;
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Destructor for cleaning purposes, etc
50 public final function __destruct() {
51 // Is there a resource pointer? Then we have to close the file here!
52 if (is_resource($this->getPointer())) {
53 // Try to close a file
57 // Call the parent destructor
62 * Create a file pointer based on the given file. The file will also
65 * @param $fileName The file name we shall pass to fopen()
66 * @throws FileIsEmptyException If the provided file name is empty.
67 * @throws FileIoException If fopen() returns not a file resource
70 public static final function createFrameworkFileInputPointer ($fileName) {
71 // Some pre-sanity checks...
72 if ((is_null($fileName)) || (empty($fileName))) {
74 throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
75 } elseif (!file_exists($fileName)) {
76 // File does not exist!
77 throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_FOUND);
78 } elseif (!is_readable($fileName)) {
79 // File does not exist!
80 throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
83 // Try to open a handler
84 $filePointer = fopen($fileName, 'rb');
85 if ((is_null($filePointer)) || ($filePointer === false)) {
86 // Something bad happend
87 throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
90 // Create new instance
91 $pointerInstance = new FrameworkFileInputPointer();
93 // Set file pointer and file name
94 $pointerInstance->setPointer($filePointer);
95 $pointerInstance->setFileName($fileName);
97 // Return the instance
98 return $pointerInstance;
102 * Read data a file pointer
104 * @return mixed The result of fread()
105 * @throws NullPointerException If the file pointer instance
106 * is not set by setPointer()
107 * @throws InvalidResourceException If there is being set
108 * an invalid file resource
110 public function readFromFile () {
111 if (is_null($this->getPointer())) {
112 // Pointer not initialized
113 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
114 } elseif (!is_resource($this->getPointer())) {
115 // Pointer is not a valid resource!
116 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
119 // Read data from the file pointer and return it
120 return fread($this->getPointer(), 1024);
124 * Close a file source and set it's instance to null and the file name
128 * @throws NullPointerException If the file pointer instance
129 * is not set by setPointer()
130 * @throws InvalidResourceException If there is being set
132 public function closeFile () {
133 if (is_null($this->getPointer())) {
134 // Pointer not initialized
135 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
136 } elseif (!is_resource($this->getPointer())) {
137 // Pointer is not a valid resource!
138 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
141 // Close the file pointer and reset the instance variable
142 @fclose($this->getPointer());
143 $this->setPointer(NULL);
144 $this->setFileName('');
148 * Setter for the file pointer
150 * @param $filePointer File resource
153 public final function setPointer ($filePointer) {
154 // Sanity-check if pointer is a valid file resource
155 if (is_resource($filePointer) || is_null($filePointer)) {
156 // Is a valid resource
157 $this->filePointer = $filePointer;
160 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
165 * Getter for the file pointer
167 * @return $filePointer The file pointer which shall be a valid
170 public final function getPointer () {
171 return $this->filePointer;
175 * Setter for file name
177 * @param $fileName The new file name
180 public final function setFileName ($fileName) {
181 $fileName = (string) $fileName;
182 $this->fileName = $fileName;
186 * Getter for file name
188 * @return $fileName The current file name
190 public final function getFileName () {
191 return $this->fileName;