3 namespace Org\Mxchange\CoreFramework\Filesystem;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
7 use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
12 use \OutOfBoundsException;
16 * A general FileIo class
18 * @author Roland Haeder <webmaster@ship-simu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 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 BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
41 private $fileObject = NULL;
44 * Protected constructor
46 * @param $className Name of the class
49 protected function __construct (string $className) {
50 // Call parent constructor
51 parent::__construct($className);
55 * Destructor for cleaning purposes, etc
59 public final function __destruct() {
60 // Is there a resource pointer? Then we have to close the file here!
61 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: this->fileObject[]=%s - DESTRUCTOR!', gettype($this->getFileObject())));
62 if (is_object($this->getFileObject())) {
63 // Try to close a file
64 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: Calling this->closeFile() ...');
68 // Call the parent destructor
72 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
76 * Close a file source and set it's instance to null and the file name
80 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
81 * @throws LogicException If there is no object being set
83 public function closeFile () {
85 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
86 if (is_null($this->getFileObject())) {
87 // Pointer not initialized
88 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
89 } elseif (!is_object($this->getFileObject())) {
90 // Pointer is not a valid resource!
91 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
94 // Close the file pointer by NULL-ing it
95 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
96 $this->resetFileObject();
99 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
103 * Resets file object instance to NULL
107 protected final function resetFileObject () {
109 $this->fileObject = NULL;
113 * Setter for the file object
115 * @param $fileObject An instance of a SplFileObject class
118 protected final function setFileObject (SplFileObject $fileObject) {
119 $this->fileObject = $fileObject;
123 * Getter for the file object
125 * @return $fileObject An instance of a SplFileObject class
127 public final function getFileObject () {
128 return $this->fileObject;
132 * Determines seek position
134 * @return $seekPosition Current seek position
136 public final function determineSeekPosition () {
137 return $this->getFileObject()->ftell();
141 * Determines whether the EOF has been reached
143 * @return $isEndOfFileReached Whether the EOF has been reached
145 public final function isEndOfFileReached () {
146 return $this->getFileObject()->eof();
150 * Seek to given offset (default) or other possibilities as fseek() gives.
152 * @param $offset Offset to seek to (or used as "base" for other seeks)
153 * @param $whence Added to offset (default: only use offset to seek to)
154 * @return $status Status of file seek: 0 = success, -1 = failed
156 public function seek (int $offset, int $whence = SEEK_SET) {
157 // Validate parameter
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
161 throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset));
165 $status = $this->getFileObject()->fseek($offset, $whence);
168 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status));
175 * @return $size Size (in bytes) of file
176 * @todo Handle seekStatus
178 public function size () {
179 // Get current seek position
180 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
181 $seekPosition = $this->determineSeekPosition();
184 $seekStatus = $this->seek(0, SEEK_END);
186 // Get position again (which is the end of the file)
187 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus=%d', $seekStatus));
188 $size = $this->determineSeekPosition();
190 // Reset seek position to old
191 $this->seek($seekPosition);
194 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%s - EXIT!', $size));