3 namespace CoreFramework\FileSystem;
5 // Import framework stuff
6 use CoreFramework\Filesystem\CloseableFile;
7 use CoreFramework\Filesystem\FilePointer;
8 use CoreFramework\Generic\NullPointerException;
9 use CoreFramework\Object\BaseFrameworkSystem;
12 * A general FileIo class
14 * @author Roland Haeder <webmaster@ship-simu.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.ship-simu.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 BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
35 * The current file we are working in
37 private $fileName = '';
42 private $filePointer = NULL;
45 * Protected constructor
47 * @param $className Name of the class
50 protected function __construct ($className) {
51 // Call parent constructor
52 parent::__construct($className);
56 * Destructor for cleaning purposes, etc
60 public final function __destruct() {
61 // Is there a resource pointer? Then we have to close the file here!
62 if (is_resource($this->getPointer())) {
63 // Try to close a file
67 // Call the parent destructor
72 * Close a file source and set it's instance to null and the file name
76 * @throws NullPointerException If the file pointer instance is not set by setPointer()
77 * @throws InvalidResourceException If there is being set
79 public function closeFile () {
81 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
83 if (is_null($this->getPointer())) {
84 // Pointer not initialized
85 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
86 } elseif (!is_resource($this->getPointer())) {
87 // Pointer is not a valid resource!
88 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
92 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
94 // Close the file pointer and reset the instance variable
95 @fclose($this->getPointer());
96 $this->setPointer(NULL);
97 $this->setFileName('');
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
104 * Setter for the file pointer
106 * @param $filePointer File resource
109 protected final function setPointer ($filePointer) {
110 $this->filePointer = $filePointer;
114 * Getter for the file pointer
116 * @return $filePointer The file pointer which shall be a valid file resource
118 public final function getPointer () {
119 return $this->filePointer;
123 * Setter for file name
125 * @param $fileName The new file name
128 protected final function setFileName ($fileName) {
129 $fileName = (string) $fileName;
130 $this->fileName = $fileName;
134 * Getter for file name
136 * @return $fileName The current file name
138 public final function getFileName () {
139 return $this->fileName;
143 * Determines seek position
145 * @return $seekPosition Current seek position
147 public final function determineSeekPosition () {
148 return ftell($this->getPointer());
152 * Determines whether the EOF has been reached
154 * @return $isEndOfFileReached Whether the EOF has been reached
156 public final function isEndOfFileReached () {
157 return feof($this->getPointer());
161 * Seek to given offset (default) or other possibilities as fseek() gives.
163 * @param $offset Offset to seek to (or used as "base" for other seeks)
164 * @param $whence Added to offset (default: only use offset to seek to)
165 * @return $status Status of file seek: 0 = success, -1 = failed
167 public function seek ($offset, $whence = SEEK_SET) {
169 $status = fseek($this->getPointer(), $offset, $whence);
172 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
179 * @return $size Size (in bytes) of file
180 * @todo Handle seekStatus
182 public function size () {
183 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
185 // Get current seek position
186 $seekPosition = $this->determineSeekPosition();
189 $seekStatus = $this->seek(0, SEEK_END);
190 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
192 // Get position again (which is the end of the file)
193 $size = $this->determineSeekPosition();
195 // Reset seek position to old
196 $this->seek($seekPosition);
199 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));