3 * A general FileIo class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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 BaseFileIo extends BaseFrameworkSystem {
26 * The current file we are working in
28 private $fileName = '';
33 private $filePointer = NULL;
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Destructor for cleaning purposes, etc
51 public final function __destruct() {
52 // Is there a resource pointer? Then we have to close the file here!
53 if (is_resource($this->getPointer())) {
54 // Try to close a file
58 // Call the parent destructor
63 * Close a file source and set it's instance to null and the file name
67 * @throws NullPointerException If the file pointer instance is not set by setPointer()
68 * @throws InvalidResourceException If there is being set
70 private function closeFile () {
72 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
74 if (is_null($this->getPointer())) {
75 // Pointer not initialized
76 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
77 } elseif (!is_resource($this->getPointer())) {
78 // Pointer is not a valid resource!
79 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
83 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
85 // Close the file pointer and reset the instance variable
86 @fclose($this->getPointer());
87 $this->setPointer(NULL);
88 $this->setFileName('');
91 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
95 * Setter for the file pointer
97 * @param $filePointer File resource
100 protected final function setPointer ($filePointer) {
101 $this->filePointer = $filePointer;
105 * Getter for the file pointer
107 * @return $filePointer The file pointer which shall be a valid file resource
109 public final function getPointer () {
110 return $this->filePointer;
114 * Setter for file name
116 * @param $fileName The new file name
119 protected final function setFileName ($fileName) {
120 $fileName = (string) $fileName;
121 $this->fileName = $fileName;
125 * Getter for file name
127 * @return $fileName The current file name
129 public final function getFileName () {
130 return $this->fileName;
134 * Determines seek position
136 * @return $seekPosition Current seek position
138 public final function determineSeekPosition () {
139 return ftell($this->getPointer());
143 * Determines whether the EOF has been reached
145 * @return $isEndOfFileReached Whether the EOF has been reached
147 public final function isEndOfFileReached () {
148 return feof($this->getPointer());
152 * Seek to given offset (default) or other possibilities as fseek() gives.
154 * @param $offset Offset to seek to (or used as "base" for other seeks)
155 * @param $whence Added to offset (default: only use offset to seek to)
156 * @return $status Status of file seek: 0 = success, -1 = failed
158 public function seek ($offset, $whence = SEEK_SET) {
160 $status = fseek($this->getPointer(), $offset, $whence);
163 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
170 * @return $size Size (in bytes) of file
171 * @todo Handle seekStatus
173 public function size () {
174 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
176 // Get current seek position
177 $seekPosition = $this->determineSeekPosition();
180 $seekStatus = $this->seek(0, SEEK_END);
181 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
183 // Get position again (which is the end of the file)
184 $size = $this->determineSeekPosition();
186 // Reset seek position to old
187 $this->seek($seekPosition);
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));