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 <<<<<<< HEAD:framework/main/classes/file_directories/class_BaseFileIo.php
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
23 >>>>>>> Some updates::inc/main/classes/file_directories/class_BaseFileIo.php
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.ship-simu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
44 private $fileObject = NULL;
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct ($className) {
53 // Call parent constructor
54 parent::__construct($className);
58 * Destructor for cleaning purposes, etc
62 public final function __destruct() {
63 // Is there a resource pointer? Then we have to close the file here!
64 if (is_object($this->getFileObject())) {
65 // Try to close a file
69 // Call the parent destructor
74 * Close a file source and set it's instance to null and the file name
78 * @throws NullPointerException If the file pointer instance is not set by setPointer()
79 * @throws LogicException If there is no object being set
81 public function closeFile () {
83 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $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())));
94 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
96 // Close the file pointer by NULL-ing it
97 $this->resetFileObject();
100 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
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 ($offset, $whence = SEEK_SET) {
159 $status = $this->getFileObject()->fseek($offset, $whence);
162 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
169 * @return $size Size (in bytes) of file
170 * @todo Handle seekStatus
172 public function size () {
173 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
175 // Get current seek position
176 $seekPosition = $this->determineSeekPosition();
179 $seekStatus = $this->seek(0, SEEK_END);
180 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
182 // Get position again (which is the end of the file)
183 $size = $this->determineSeekPosition();
185 // Reset seek position to old
186 $this->seek($seekPosition);
189 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));