3 namespace CoreFramework\FileSystem;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A general FileIo class
11 * @author Roland Haeder <webmaster@ship-simu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.ship-simu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
32 * The current file we are working in
34 private $fileName = '';
39 private $filePointer = NULL;
42 * Protected constructor
44 * @param $className Name of the class
47 protected function __construct ($className) {
48 // Call parent constructor
49 parent::__construct($className);
53 * Destructor for cleaning purposes, etc
57 public final function __destruct() {
58 // Is there a resource pointer? Then we have to close the file here!
59 if (is_resource($this->getPointer())) {
60 // Try to close a file
64 // Call the parent destructor
69 * Close a file source and set it's instance to null and the file name
73 * @throws NullPointerException If the file pointer instance is not set by setPointer()
74 * @throws InvalidResourceException If there is being set
76 public function closeFile () {
78 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
80 if (is_null($this->getPointer())) {
81 // Pointer not initialized
82 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
83 } elseif (!is_resource($this->getPointer())) {
84 // Pointer is not a valid resource!
85 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
89 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
91 // Close the file pointer and reset the instance variable
92 @fclose($this->getPointer());
93 $this->setPointer(NULL);
94 $this->setFileName('');
97 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
101 * Setter for the file pointer
103 * @param $filePointer File resource
106 protected final function setPointer ($filePointer) {
107 $this->filePointer = $filePointer;
111 * Getter for the file pointer
113 * @return $filePointer The file pointer which shall be a valid file resource
115 public final function getPointer () {
116 return $this->filePointer;
120 * Setter for file name
122 * @param $fileName The new file name
125 protected final function setFileName ($fileName) {
126 $fileName = (string) $fileName;
127 $this->fileName = $fileName;
131 * Getter for file name
133 * @return $fileName The current file name
135 public final function getFileName () {
136 return $this->fileName;
140 * Determines seek position
142 * @return $seekPosition Current seek position
144 public final function determineSeekPosition () {
145 return ftell($this->getPointer());
149 * Determines whether the EOF has been reached
151 * @return $isEndOfFileReached Whether the EOF has been reached
153 public final function isEndOfFileReached () {
154 return feof($this->getPointer());
158 * Seek to given offset (default) or other possibilities as fseek() gives.
160 * @param $offset Offset to seek to (or used as "base" for other seeks)
161 * @param $whence Added to offset (default: only use offset to seek to)
162 * @return $status Status of file seek: 0 = success, -1 = failed
164 public function seek ($offset, $whence = SEEK_SET) {
166 $status = fseek($this->getPointer(), $offset, $whence);
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
176 * @return $size Size (in bytes) of file
177 * @todo Handle seekStatus
179 public function size () {
180 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
182 // Get current seek position
183 $seekPosition = $this->determineSeekPosition();
186 $seekStatus = $this->seek(0, SEEK_END);
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
189 // Get position again (which is the end of the file)
190 $size = $this->determineSeekPosition();
192 // Reset seek position to old
193 $this->seek($seekPosition);
196 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));