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;
15 * A general FileIo class
17 * @author Roland Haeder <webmaster@ship-simu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.ship-simu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
40 private $fileObject = NULL;
43 * Protected constructor
45 * @param $className Name of the class
48 protected function __construct ($className) {
49 // Call parent constructor
50 parent::__construct($className);
54 * Destructor for cleaning purposes, etc
58 public final function __destruct() {
59 // Is there a resource pointer? Then we have to close the file here!
60 if (is_object($this->getFileObject())) {
61 // Try to close a file
65 // Call the parent destructor
70 * Close a file source and set it's instance to null and the file name
74 * @throws NullPointerException If the file pointer instance is not set by setPointer()
75 * @throws LogicException If there is no object being set
77 public function closeFile () {
79 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
81 if (is_null($this->getFileObject())) {
82 // Pointer not initialized
83 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
84 } elseif (!is_object($this->getFileObject())) {
85 // Pointer is not a valid resource!
86 throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
90 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
92 // Close the file pointer by NULL-ing it
93 $this->resetFileObject();
96 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
100 * Resets file object instance to NULL
104 protected final function resetFileObject () {
106 $this->fileObject = NULL;
110 * Setter for the file object
112 * @param $fileObject An instance of a SplFileObject class
115 protected final function setFileObject (SplFileObject $fileObject) {
116 $this->fileObject = $fileObject;
120 * Getter for the file object
122 * @return $fileObject An instance of a SplFileObject class
124 public final function getFileObject () {
125 return $this->fileObject;
129 * Determines seek position
131 * @return $seekPosition Current seek position
133 public final function determineSeekPosition () {
134 return $this->getFileObject()->ftell();
138 * Determines whether the EOF has been reached
140 * @return $isEndOfFileReached Whether the EOF has been reached
142 public final function isEndOfFileReached () {
143 return $this->getFileObject()->eof();
147 * Seek to given offset (default) or other possibilities as fseek() gives.
149 * @param $offset Offset to seek to (or used as "base" for other seeks)
150 * @param $whence Added to offset (default: only use offset to seek to)
151 * @return $status Status of file seek: 0 = success, -1 = failed
153 public function seek ($offset, $whence = SEEK_SET) {
155 $status = $this->getFileObject()->fseek($offset, $whence);
158 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
165 * @return $size Size (in bytes) of file
166 * @todo Handle seekStatus
168 public function size () {
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
171 // Get current seek position
172 $seekPosition = $this->determineSeekPosition();
175 $seekStatus = $this->seek(0, SEEK_END);
176 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
178 // Get position again (which is the end of the file)
179 $size = $this->determineSeekPosition();
181 // Reset seek position to old
182 $this->seek($seekPosition);
185 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));