<?php
// Own namespace
-namespace CoreFramework\FileSystem;
+namespace Org\Mxchange\CoreFramework\FileSystem;
// Import framework stuff
-use CoreFramework\Generic\NullPointerException;
-use CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
+use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
+// Import SPL stuff
+use \SplFileObject;
/**
* A general FileIo class
*
* @author Roland Haeder <webmaster@ship-simu.org>
* @version 0.0.0
- * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.ship-simu.org
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
+abstract class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFile {
/**
- * The current file we are working in
+ * The file object
*/
- private $fileName = '';
-
- /**
- * The file pointer
- */
- private $filePointer = NULL;
+ private $fileObject = NULL;
/**
* Protected constructor
*/
public final function __destruct() {
// Is there a resource pointer? Then we have to close the file here!
- if (is_resource($this->getPointer())) {
+ if (is_object($this->getFileObject())) {
// Try to close a file
$this->closeFile();
} // END - if
* to empty.
*
* @return void
- * @throws NullPointerException If the file pointer instance is not set by setPointer()
- * @throws InvalidResourceException If there is being set
+ * @throws NullPointerException If the file pointer instance is not set by setFileObject()
+ * @throws LogicException If there is no object being set
*/
public function closeFile () {
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
- if (is_null($this->getPointer())) {
+ if (is_null($this->getFileObject())) {
// Pointer not initialized
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
- } elseif (!is_resource($this->getPointer())) {
+ } elseif (!is_object($this->getFileObject())) {
// Pointer is not a valid resource!
- throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
+ throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
}
// Debug message
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileObject()->getPathname()));
- // Close the file pointer and reset the instance variable
- @fclose($this->getPointer());
- $this->setPointer(NULL);
- $this->setFileName('');
+ // Close the file pointer by NULL-ing it
+ $this->resetFileObject();
// Debug message
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
}
/**
- * Setter for the file pointer
+ * Resets file object instance to NULL
*
- * @param $filePointer File resource
* @return void
*/
- protected final function setPointer ($filePointer) {
- $this->filePointer = $filePointer;
- }
-
- /**
- * Getter for the file pointer
- *
- * @return $filePointer The file pointer which shall be a valid file resource
- */
- public final function getPointer () {
- return $this->filePointer;
+ protected final function resetFileObject () {
+ // Set it to NULL
+ $this->fileObject = NULL;
}
/**
- * Setter for file name
+ * Setter for the file object
*
- * @param $fileName The new file name
+ * @param $fileObject An instance of a SplFileObject class
* @return void
*/
- protected final function setFileName ($fileName) {
- $fileName = (string) $fileName;
- $this->fileName = $fileName;
+ protected final function setFileObject (SplFileObject $fileObject) {
+ $this->fileObject = $fileObject;
}
/**
- * Getter for file name
+ * Getter for the file object
*
- * @return $fileName The current file name
+ * @return $fileObject An instance of a SplFileObject class
*/
- public final function getFileName () {
- return $this->fileName;
+ public final function getFileObject () {
+ return $this->fileObject;
}
/**
* @return $seekPosition Current seek position
*/
public final function determineSeekPosition () {
- return ftell($this->getPointer());
+ return $this->getFileObject()->ftell();
}
/**
* @return $isEndOfFileReached Whether the EOF has been reached
*/
public final function isEndOfFileReached () {
- return feof($this->getPointer());
+ return $this->getFileObject()->eof();
}
/**
*/
public function seek ($offset, $whence = SEEK_SET) {
// Seek to position
- $status = fseek($this->getPointer(), $offset, $whence);
+ $status = $this->getFileObject()->fseek($offset, $whence);
// Return status
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));