X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Ffile_directories%2Fio%2Fclass_FrameworkFileInputOutputPointer.php;h=ae372f3532b56aeb6321b892855dca0c96edfd6b;hb=6f830b22ba3fd5a89c56177165a53e8fa94ff9c0;hp=b703f094e9962c27b81fcf951f1b9120fc29f96a;hpb=2ee664c52d9a8f3bd882212b19057a7c1096b7cf;p=core.git diff --git a/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php b/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php index b703f094..ae372f35 100644 --- a/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php +++ b/inc/classes/main/file_directories/io/class_FrameworkFileInputOutputPointer.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -41,6 +41,7 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP * @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) { @@ -48,12 +49,18 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP if ((is_null($fileName)) || (empty($fileName))) { // No filename given throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); - } elseif ((file_exists($fileName)) && (!is_readable($fileName))) { + } elseif (!BaseFrameworkSystem::isReachableFilePath($fileName)) { + // File exists but cannot be read + throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_REACHABLE); + } elseif ((!BaseFrameworkSystem::isReadableFile($fileName)) && (file_exists($fileName))) { // 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_READ); + throw new FileWriteProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_WRITTEN); + } elseif (!is_writable(dirname($fileName))) { + // Path is not writable + throw new PathWriteProtectedException($fileName, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN); } // Try to open a handler @@ -124,6 +131,21 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP return fwrite($this->getPointer(), $dataStream, strlen($dataStream)); } + /** + * Writes at given position by seeking to it. + * + * @param $seekPosition Seek position in file + * @param $data Data to be written + * @return mixed Number of writes bytes or FALSE on error + */ + public function writeAtPosition ($seekPosition, $data) { + // First seek to it + $this->seek($seekPosition); + + // Then write the data at that position + return $this->writeToFile($data); + } + /** * Rewinds to the beginning of the file * @@ -152,21 +174,34 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP return fseek($this->getPointer(), $seekPosition, $whence); } + /** + * Reads a line, maximum 4096 Bytes from current file pointer + * + * @return $data Read data from file + */ + public function readLine () { + // Read whole line + return $this->read(); + } + /** * Reads given amount of bytes from file. * * @param $bytes Amount of bytes to read * @return $data Data read from file */ - public function read ($bytes) { + public function read ($bytes = NULL) { // Validate the pointer $this->validateFilePointer(); - // Try to read given characters - $data = fread($this->getPointer(), $bytes); - - // Was this successfull? - assert(is_string($data)); + // Is $bytes set? + if (is_int($bytes)) { + // Try to read given characters + $data = fread($this->getPointer(), $bytes); + } else { + // Try to read whole line + $data = fread($this->getPointer()); + } // Then return it return $data; @@ -214,6 +249,25 @@ class FrameworkFileInputOutputPointer extends BaseFileIo implements InputOutputP public function key () { throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION); } + + /** + * "Getter" for file size + * + * @return $fileSize Size of currently loaded file + */ + public function getFileSize () { + // Check if the pointer is still valid + $this->validateFilePointer(); + + // Get file's data + $fileData = fstat($this->getPointer()); + + // Make sure the required array key is there + assert(isset($fileData['size'])); + + // Return size + return $fileData['size']; + } } // [EOF]