3 namespace CoreFramework\FileSystem;
5 // Import framework stuff
6 use CoreFramework\Generic\NullPointerException;
7 use CoreFramework\Object\BaseFrameworkSystem;
10 * A general FileIo class
12 * @author Roland Haeder <webmaster@ship-simu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.ship-simu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
33 * The current file we are working in
35 private $fileName = '';
40 private $filePointer = NULL;
43 * Protected constructor
45 * @param $className Name of the class
48 protected function __construct ($className) {
49 // Call parent constructor
50 parent::__construct($className);
54 * Destructor for cleaning purposes, etc
58 public final function __destruct() {
59 // Is there a resource pointer? Then we have to close the file here!
60 if (is_resource($this->getPointer())) {
61 // Try to close a file
65 // Call the parent destructor
70 * Close a file source and set it's instance to null and the file name
74 * @throws NullPointerException If the file pointer instance is not set by setPointer()
75 * @throws InvalidResourceException If there is being set
77 public function closeFile () {
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
81 if (is_null($this->getPointer())) {
82 // Pointer not initialized
83 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
84 } elseif (!is_resource($this->getPointer())) {
85 // Pointer is not a valid resource!
86 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
90 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
92 // Close the file pointer and reset the instance variable
93 @fclose($this->getPointer());
94 $this->setPointer(NULL);
95 $this->setFileName('');
98 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
102 * Setter for the file pointer
104 * @param $filePointer File resource
107 protected final function setPointer ($filePointer) {
108 $this->filePointer = $filePointer;
112 * Getter for the file pointer
114 * @return $filePointer The file pointer which shall be a valid file resource
116 public final function getPointer () {
117 return $this->filePointer;
121 * Setter for file name
123 * @param $fileName The new file name
126 protected final function setFileName ($fileName) {
127 $fileName = (string) $fileName;
128 $this->fileName = $fileName;
132 * Getter for file name
134 * @return $fileName The current file name
136 public final function getFileName () {
137 return $this->fileName;
141 * Determines seek position
143 * @return $seekPosition Current seek position
145 public final function determineSeekPosition () {
146 return ftell($this->getPointer());
150 * Determines whether the EOF has been reached
152 * @return $isEndOfFileReached Whether the EOF has been reached
154 public final function isEndOfFileReached () {
155 return feof($this->getPointer());
159 * Seek to given offset (default) or other possibilities as fseek() gives.
161 * @param $offset Offset to seek to (or used as "base" for other seeks)
162 * @param $whence Added to offset (default: only use offset to seek to)
163 * @return $status Status of file seek: 0 = success, -1 = failed
165 public function seek ($offset, $whence = SEEK_SET) {
167 $status = fseek($this->getPointer(), $offset, $whence);
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
177 * @return $size Size (in bytes) of file
178 * @todo Handle seekStatus
180 public function size () {
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
183 // Get current seek position
184 $seekPosition = $this->determineSeekPosition();
187 $seekStatus = $this->seek(0, SEEK_END);
188 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
190 // Get position again (which is the end of the file)
191 $size = $this->determineSeekPosition();
193 // Reset seek position to old
194 $this->seek($seekPosition);
197 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));