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\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
13 use \OutOfBoundsException;
17 * A general FileIo class
19 * @author Roland Haeder <webmaster@ship-simu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.ship-simu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
42 private $fileObject = NULL;
45 * Protected constructor
47 * @param $className Name of the class
50 protected function __construct (string $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 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: this->fileObject[]=%s - DESTRUCTOR!', gettype($this->getFileObject())));
63 if (is_object($this->getFileObject())) {
64 // Try to close a file
65 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: Invoking this->closeFile() ...');
69 // Call the parent destructor
73 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
77 * Close a file source and set it's instance to null and the file name
81 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
82 * @throws LogicException If there is no object being set
84 public function closeFile () {
86 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
87 if (is_null($this->getFileObject())) {
88 // Pointer not initialized
89 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
90 } elseif (!is_object($this->getFileObject())) {
91 // Pointer is not a valid resource!
92 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())), FrameworkInterface::EXCEPTION_LOGIC_EXCEPTION);
95 // Close the file pointer by NULL-ing it
96 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
97 $this->resetFileObject();
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
104 * Resets file object instance to NULL
108 protected final function resetFileObject () {
110 $this->fileObject = NULL;
114 * Setter for the file object
116 * @param $fileObject An instance of a SplFileObject class
119 protected final function setFileObject (SplFileObject $fileObject) {
120 $this->fileObject = $fileObject;
124 * Getter for the file object
126 * @return $fileObject An instance of a SplFileObject class
128 public final function getFileObject () {
129 return $this->fileObject;
133 * Determines seek position
135 * @return $seekPosition Current seek position
137 public final function determineSeekPosition () {
138 return $this->getFileObject()->ftell();
142 * Determines whether the EOF has been reached
144 * @return $isEndOfFileReached Whether the EOF has been reached
146 public final function isEndOfFileReached () {
147 return $this->getFileObject()->eof();
151 * Seek to given offset (default) or other possibilities as fseek() gives.
153 * @param $offset Offset to seek to (or used as "base" for other seeks)
154 * @param $whence Added to offset (default: only use offset to seek to)
155 * @return $status Status of file seek: 0 = success, -1 = failed
157 public function seek (int $offset, int $whence = SEEK_SET) {
158 // Validate parameter
159 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
162 throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset));
166 $status = $this->getFileObject()->fseek($offset, $whence);
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status));
176 * @return $size Size (in bytes) of file
177 * @todo Handle seekStatus
179 public function size () {
180 // Get current seek position
181 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
182 $seekPosition = $this->determineSeekPosition();
185 $seekStatus = $this->seek(0, SEEK_END);
187 // Get position again (which is the end of the file)
188 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus[%s]=%d', gettype($seekStatus), $seekStatus));
189 $size = $this->determineSeekPosition();
191 // Reset seek position to old
192 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size[%s]=%d', gettype($size), $size));
193 $this->seek($seekPosition);
196 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%d - EXIT!', $size));