3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
7 use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
8 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
13 use \InvalidArgumentException;
16 * An abstract file class
18 * @author Roland Haeder <webmaster@ship-simu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.ship-simu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
39 * Counter for total entries
41 private $totalEntries = 0;
44 * An instance of a file I/O pointer class (not handler)
46 private $pointerInstance = NULL;
49 * Protected constructor
51 * @param $className Name of the class
54 protected function __construct (string $className) {
55 // Call parent constructor
56 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: className=%s - CONSTRUCTED!', $className));
57 parent::__construct($className);
60 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
64 * Setter for FilePointer instance
66 * @param $pointerInstance An instance of an FilePointer class
69 protected final function setPointerInstance (FilePointer $pointerInstance = NULL) {
70 $this->pointerInstance = $pointerInstance;
74 * Getter for FilePointer instance
76 * @return $pointerInstance An instance of an FilePointer class
78 public final function getPointerInstance () {
79 return $this->pointerInstance;
83 * Unsets pointer instance which triggers a call of __destruct() if the
84 * instance is still there. This is surely not fatal on already "closed"
85 * file pointer instances.
87 * I don't want to mess around with above setter by giving it a default
88 * value NULL as setter should always explicitly only set (existing) object
89 * instances and NULL is NULL.
93 protected final function unsetPointerInstance () {
94 // Simply invoke setter with no parameter
95 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
96 $this->setPointerInstance();
99 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
103 * Destructor for cleaning purposes, etc
107 public final function __destruct() {
108 // Try to close a file
109 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: DESTRUCTED!');
112 // Call the parent destructor
113 parent::__destruct();
116 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
120 * "Getter" for abstracted file size
122 * @return $fileSize Size of abstracted file
124 public function getFileSize () {
125 // Call pointer instanze
126 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
127 return $this->getPointerInstance()->getFileSize();
131 * Getter for total entries
133 * @return $totalEntries Total entries in this file
135 public final function getCounter () {
137 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: Getting this->totalEntries=%d ... - CALLED!', $this->totalEntries));
138 return $this->totalEntries;
142 * Setter for total entries
144 * @param $totalEntries Total entries in this file
147 protected final function setCounter (int $counter) {
149 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: Setting this->totalEntries=%d ... - CALLED!', $counter));
150 $this->totalEntries = $counter;
158 protected final function incrementCounter () {
160 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
161 $this->totalEntries++;
164 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
168 * Getter for the file object
170 * @return $fileObject An instance of a SplFileObject
172 public final function getFileObject () {
173 // Call pointer instanze
174 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
175 return $this->getPointerInstance()->getFileObject();
179 * Getter for file's name
181 public final function getFilename () {
182 // Invole file object's method
183 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
184 return $this->getFileObject()->getFilename();
188 * Close a file source and set it's instance to null and the file name
193 public function closeFile () {
194 // Close down pointer instance as well by unsetting it
195 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
196 $this->unsetPointerInstance();
199 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
205 * @return $size Size (in bytes) of file
206 * @todo Handle seekStatus
208 public function size () {
209 // Call pointer instance
210 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
211 return $this->getPointerInstance()->size();
215 * Read data a file pointer
217 * @return mixed The result of fread()
218 * @throws NullPointerException If the file pointer instance
219 * is not set by setFileObject()
220 * @throws InvalidResourceException If there is being set
222 public function readFromFile () {
223 // Call pointer instance
224 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
225 return $this->getPointerInstance()->readFromFile();
229 * Write data to a file pointer
231 * @param $dataStream The data stream we shall write to the file
232 * @return mixed Number of writes bytes or false on error
233 * @throws NullPointerException If the file pointer instance
234 * is not set by setFileObject()
235 * @throws InvalidResourceException If there is being set
236 * an invalid file resource
238 public function writeToFile (string $dataStream) {
240 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: dataStream()=%d - CALLED!', strlen($dataStream)));
241 if (empty($dataStream)) {
243 throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
246 // Call pointer instance
247 return $this->getPointerInstance()->writeToFile($dataStream);
251 * Determines whether the EOF has been reached
253 * @return $isEndOfFileReached Whether the EOF has been reached
255 public final function isEndOfFileReached () {
256 // Call pointer instance
257 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
258 return $this->getPointerInstance()->isEndOfFileReached();