3 * An abstract file class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 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 BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
26 * Counter for total entries
28 private $totalEntries = 0;
31 * The current file we are working in
33 private $fileName = '';
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 // Try to close a file
55 // Call the parent destructor
60 * "Getter" for abstracted file size
62 * @return $fileSize Size of abstracted file
64 public function getFileSize () {
65 // Call pointer instanze
66 return $this->getPointerInstance()->getFileSize();
70 * Getter for total entries
72 * @return $totalEntries Total entries in this file
74 public final function getCounter () {
76 return $this->totalEntries;
80 * Setter for total entries
82 * @param $totalEntries Total entries in this file
85 protected final function setCounter ($counter) {
87 $this->totalEntries = $counter;
95 protected final function incrementCounter () {
97 $this->totalEntries++;
101 * Getter for the file pointer
103 * @return $filePointer The file pointer which shall be a valid file resource
104 * @throws UnsupportedOperationException If this method is called
106 public final function getPointer () {
107 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
111 * Setter for file name
113 * @param $fileName The new file name
116 protected final function setFileName ($fileName) {
117 $fileName = (string) $fileName;
118 $this->fileName = $fileName;
122 * Getter for file name
124 * @return $fileName The current file name
126 public final function getFileName () {
127 return $this->fileName;
131 * Close a file source and set it's instance to null and the file name
136 public function closeFile () {
138 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
140 // Close down pointer instance as well by unsetting it
141 $this->unsetPointerInstance();
144 $this->setFileName('');
147 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
153 * @return $size Size (in bytes) of file
154 * @todo Handle seekStatus
156 public function size () {
157 // Call pointer instance
158 return $this->getPointerInstance()->size();
162 * Read data a file pointer
164 * @return mixed The result of fread()
165 * @throws NullPointerException If the file pointer instance
166 * is not set by setPointer()
167 * @throws InvalidResourceException If there is being set
169 public function readFromFile () {
170 // Call pointer instance
171 return $this->getPointerInstance()->readFromFile();
175 * Write data to a file pointer
177 * @param $dataStream The data stream we shall write to the file
178 * @return mixed Number of writes bytes or FALSE on error
179 * @throws NullPointerException If the file pointer instance
180 * is not set by setPointer()
181 * @throws InvalidResourceException If there is being set
182 * an invalid file resource
184 public function writeToFile ($dataStream) {
185 // Call pointer instance
186 return $this->getPointerInstance()->writeToFile($dataStream);
190 * Determines whether the EOF has been reached
192 * @return $isEndOfFileReached Whether the EOF has been reached
194 public final function isEndOfFileReached () {
195 // Call pointer instance
196 return $this->getPointerInstance()->isEndOfFileReached();