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 \InvalidArgumentException;
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
67 // Call the parent destructor
71 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
75 * Close a file source and set it's instance to null and the file name
79 * @throws NullPointerException If the file pointer instance is not set by setFileObject()
80 * @throws LogicException If there is no object being set
82 public function closeFile () {
84 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
85 if (is_null($this->getFileObject())) {
86 // Pointer not initialized
87 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
88 } elseif (!is_object($this->getFileObject())) {
89 // Pointer is not a valid resource!
90 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
93 // Close the file pointer by NULL-ing it
94 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
95 $this->resetFileObject();
98 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
102 * Resets file object instance to NULL
106 protected final function resetFileObject () {
108 $this->fileObject = NULL;
112 * Setter for the file object
114 * @param $fileObject An instance of a SplFileObject class
117 protected final function setFileObject (SplFileObject $fileObject) {
118 $this->fileObject = $fileObject;
122 * Getter for the file object
124 * @return $fileObject An instance of a SplFileObject class
126 public final function getFileObject () {
127 return $this->fileObject;
131 * Determines seek position
133 * @return $seekPosition Current seek position
135 public final function determineSeekPosition () {
136 return $this->getFileObject()->ftell();
140 * Determines whether the EOF has been reached
142 * @return $isEndOfFileReached Whether the EOF has been reached
144 public final function isEndOfFileReached () {
145 return $this->getFileObject()->eof();
149 * Seek to given offset (default) or other possibilities as fseek() gives.
151 * @param $offset Offset to seek to (or used as "base" for other seeks)
152 * @param $whence Added to offset (default: only use offset to seek to)
153 * @return $status Status of file seek: 0 = success, -1 = failed
155 public function seek (int $offset, int $whence = SEEK_SET) {
156 // Validate parameter
157 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
160 throw new InvalidArgumentException(sprintf('offset=%d is not valid', $offset));
164 $status = $this->getFileObject()->fseek($offset, $whence);
167 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status));
174 * @return $size Size (in bytes) of file
175 * @todo Handle seekStatus
177 public function size () {
178 // Get current seek position
179 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
180 $seekPosition = $this->determineSeekPosition();
183 $seekStatus = $this->seek(0, SEEK_END);
185 // Get position again (which is the end of the file)
186 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus=%d', $seekStatus));
187 $size = $this->determineSeekPosition();
189 // Reset seek position to old
190 $this->seek($seekPosition);
193 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%s - EXIT!', $size));