<?php
// Own namespace
-namespace CoreFramework\Filesystem\Pointer;
+namespace Org\Mxchange\CoreFramework\Filesystem\Pointer;
// Import framework stuff
-use CoreFramework\Bootstrap\FrameworkBootstrap;
-use CoreFramework\FileSystem\BaseFileIo;
-use CoreFramework\FileSystem\FileReadProtectedException;
-use CoreFramework\FileSystem\FileWriteProtectedException;
-use CoreFramework\Filesystem\PathWriteProtectedException;
-use CoreFramework\Generic\NullPointerException;
-use CoreFramework\Object\BaseFrameworkSystem;
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\FileSystem\BaseFileIo;
+use Org\Mxchange\CoreFramework\FileSystem\FileReadProtectedException;
+use Org\Mxchange\CoreFramework\FileSystem\FileWriteProtectedException;
+use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
+use Org\Mxchange\CoreFramework\Generic\NullPointerException;
+use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
+use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
+
+// Import SPL stuff
+use \SplFileInfo;
/**
* A class for reading files
*
* @author Roland Haeder <webmaster@shipsimu.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 - 2019 Core Developer Team
* @license GNU GPL 3.0 or any newer version
* @link http://www.shipsimu.org
*
* Create a file pointer based on the given file. The file will also
* be verified here.
*
- * @param $fileName The file name we shall pass to fopen()
+ * @param $fileInstance An instance of a SplFileInfo class
* @return void
- * @throws FileIsEmptyException If the given file name is NULL or empty
* @throws FileReadProtectedException If PHP cannot read an existing file
* @throws FileWriteProtectedException If PHP cannot write an existing file
* @throws PathWriteProtectedException If PHP cannot write to an existing path
* @throws FileIoException If fopen() returns not a file resource
*/
- public static final function createFrameworkFileInputOutputPointer ($fileName) {
+ public static final function createFrameworkFileInputOutputPointer (SplFileInfo $fileInstance) {
// Some pre-sanity checks...
- if ((is_null($fileName)) || (empty($fileName))) {
- // No filename given
- throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
- } elseif (!FrameworkBootstrap::isReachableFilePath($fileName)) {
+ if (!FrameworkBootstrap::isReachableFilePath($fileInstance)) {
// File exists but cannot be read
- throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE);
- } elseif ((!FrameworkBootstrap::isReadableFile($fileName)) && (file_exists($fileName))) {
+ throw new FileIoException($fileInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
+ } elseif ((!FrameworkBootstrap::isReadableFile($fileInstance)) && (file_exists($fileInstance))) {
// File exists but cannot be read
- throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
- } elseif ((file_exists($fileName)) && (!is_writable($fileName))) {
- // File exists but cannot be written
- throw new FileWriteProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
- } elseif (!is_writable(dirname($fileName))) {
+ throw new FileReadProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_READ);
+ } elseif (!is_writable($fileInstance->getPath())) {
// Path is not writable
- throw new PathWriteProtectedException($fileName, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
+ throw new PathWriteProtectedException($fileInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
+ } elseif (($fileInstance->isFile()) && (!$fileInstance->isWritable())) {
+ // File exists but cannot be written
+ throw new FileWriteProtectedException($fileInstance, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN);
}
// Try to open a handler
- $filePointer = fopen($fileName, 'c+b');
- if ((is_null($filePointer)) || ($filePointer === false)) {
+ $fileObject = $fileInstance->openFile('c+b');
+
+ // Is it valid?
+ if ((is_null($fileObject)) || ($fileObject === false)) {
// Something bad happend
- throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
+ throw new FileIoException($fileInstance->getPathname(), self::EXCEPTION_FILE_POINTER_INVALID);
} // END - if
// Create new instance
$pointerInstance = new FrameworkFileInputOutputPointer();
- // Set file pointer and file name
- $pointerInstance->setPointer($filePointer);
- $pointerInstance->setFileName($fileName);
+ // Set file object and file name
+ $pointerInstance->setFileObject($fileObject);
// Return the instance
return $pointerInstance;
*
* @return void
* @throws NullPointerException If the file pointer instance
- * is not set by setPointer()
- * @throws InvalidResourceException If there is being set
+ * is not set by setFileObject()
+ * @todo Add more checks
*/
private function validateFilePointer () {
- 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())) {
- // Pointer is not a valid resource!
- throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
- }
+ } // END - if
// All fine here
}
$this->validateFilePointer();
// Write data to the file pointer and return written bytes
- return fwrite($this->getPointer(), $dataStream, strlen($dataStream));
+ return $this->getFileObject()->fwrite($dataStream, strlen($dataStream));
}
/**
$this->validateFilePointer();
// Rewind the pointer
- return rewind($this->getPointer());
+ return $this->getFileObject()->rewind();
}
/**
$this->validateFilePointer();
// Move the file pointer
- return fseek($this->getPointer(), $seekPosition, $whence);
+ return $this->getFileObject()->fseek($seekPosition, $whence);
}
/**
// Is $bytes set?
if (is_int($bytes)) {
// Try to read given characters
- $data = fread($this->getPointer(), $bytes);
+ $data = $this->getFileObject()->fread($bytes);
} else {
// Try to read whole line
- $data = fread($this->getPointer());
+ $data = $this->getFileObject()->fgets();
}
// Then return it
$this->validateFilePointer();
// Get file's data
- $fileData = fstat($this->getPointer());
+ $fileData = $this->getFileObject()->fstat();
// Make sure the required array key is there
assert(isset($fileData['size']));