From 6fad95e67a217fe10f5cd024ac13bf4f07416c0b Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 14 May 2014 21:17:21 +0200 Subject: [PATCH] Rewrote both file i/o classes to have a common BaseFileIo class. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- inc/classes/main/class_Base | 2 +- ...eInputPointer.php => class_BaseFileIo.php} | 90 +++-------------- inc/classes/main/io/directory/.htaccess | 1 + .../class_FrameworkDirectoryPointer.php | 0 inc/classes/main/io/input/.htaccess | 1 + .../input/class_FrameworkFileInputPointer.php | 94 ++++++++++++++++++ inc/classes/main/io/io_handler/.htaccess | 1 + .../{ => io_handler}/class_FileIoStream.php | 0 inc/classes/main/io/output/.htaccess | 1 + .../class_FrameworkFileOutputPointer.php | 99 +------------------ 10 files changed, 113 insertions(+), 176 deletions(-) rename inc/classes/main/io/{class_FrameworkFileInputPointer.php => class_BaseFileIo.php} (50%) create mode 100644 inc/classes/main/io/directory/.htaccess rename inc/classes/main/io/{ => directory}/class_FrameworkDirectoryPointer.php (100%) create mode 100644 inc/classes/main/io/input/.htaccess create mode 100644 inc/classes/main/io/input/class_FrameworkFileInputPointer.php create mode 100644 inc/classes/main/io/io_handler/.htaccess rename inc/classes/main/io/{ => io_handler}/class_FileIoStream.php (100%) create mode 100644 inc/classes/main/io/output/.htaccess rename inc/classes/main/io/{ => output}/class_FrameworkFileOutputPointer.php (57%) diff --git a/inc/classes/main/class_Base b/inc/classes/main/class_Base index cdef34a6..992be1bf 100644 --- a/inc/classes/main/class_Base +++ b/inc/classes/main/class_Base @@ -1,6 +1,6 @@ * @version 0.0.0 diff --git a/inc/classes/main/io/class_FrameworkFileInputPointer.php b/inc/classes/main/io/class_BaseFileIo.php similarity index 50% rename from inc/classes/main/io/class_FrameworkFileInputPointer.php rename to inc/classes/main/io/class_BaseFileIo.php index f46e48ae..a18a8529 100644 --- a/inc/classes/main/io/class_FrameworkFileInputPointer.php +++ b/inc/classes/main/io/class_BaseFileIo.php @@ -1,12 +1,12 @@ + * @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 - 2012 Core Developer Team * @license GNU GPL 3.0 or any newer version - * @link http://www.shipsimu.org + * @link http://www.ship-simu.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,9 +19,9 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with this program. If not, see . + * along with this program. If not, see . */ -class FrameworkFileInputPointer extends BaseFrameworkSystem { +class BaseFileIo extends BaseFrameworkSystem { /** * The current file we are working in */ @@ -35,11 +35,12 @@ class FrameworkFileInputPointer extends BaseFrameworkSystem { /** * Protected constructor * + * @param $className Name of the class * @return void */ - protected function __construct () { + protected function __construct ($className) { // Call parent constructor - parent::__construct(__CLASS__); + parent::__construct($className); } /** @@ -52,74 +53,12 @@ class FrameworkFileInputPointer extends BaseFrameworkSystem { if (is_resource($this->getPointer())) { // Try to close a file $this->closeFile(); - } + } // END - if // Call the parent destructor parent::__destruct(); } - /** - * 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() - * @throws FileIsEmptyException If the provided file name is empty. - * @throws FileIoException If fopen() returns not a file resource - * @return void - */ - public static final function createFrameworkFileInputPointer ($fileName) { - // Some pre-sanity checks... - if ((is_null($fileName)) || (empty($fileName))) { - // No filename given - throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); - } elseif (!file_exists($fileName)) { - // File does not exist! - throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_FOUND); - } elseif (!is_readable($fileName)) { - // File does not exist! - throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ); - } - - // Try to open a handler - $filePointer = fopen($fileName, 'rb'); - if ((is_null($filePointer)) || ($filePointer === FALSE)) { - // Something bad happend - throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID); - } // END - if - - // Create new instance - $pointerInstance = new FrameworkFileInputPointer(); - - // Set file pointer and file name - $pointerInstance->setPointer($filePointer); - $pointerInstance->setFileName($fileName); - - // Return the instance - return $pointerInstance; - } - - /** - * Read data a file pointer - * - * @return mixed The result of fread() - * @throws NullPointerException If the file pointer instance - * is not set by setPointer() - * @throws InvalidResourceException If there is being set - * an invalid file resource - */ - public function readFromFile () { - if (is_null($this->getPointer())) { - // 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); - } - - // Read data from the file pointer and return it - return fread($this->getPointer(), 1024); - } - /** * Close a file source and set it's instance to null and the file name * to empty @@ -151,14 +90,7 @@ class FrameworkFileInputPointer extends BaseFrameworkSystem { * @return void */ public final function setPointer ($filePointer) { - // Sanity-check if pointer is a valid file resource - if (is_resource($filePointer) || is_null($filePointer)) { - // Is a valid resource - $this->filePointer = $filePointer; - } else { - // Throw exception - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } + $this->filePointer = $filePointer; } /** diff --git a/inc/classes/main/io/directory/.htaccess b/inc/classes/main/io/directory/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/io/directory/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/io/class_FrameworkDirectoryPointer.php b/inc/classes/main/io/directory/class_FrameworkDirectoryPointer.php similarity index 100% rename from inc/classes/main/io/class_FrameworkDirectoryPointer.php rename to inc/classes/main/io/directory/class_FrameworkDirectoryPointer.php diff --git a/inc/classes/main/io/input/.htaccess b/inc/classes/main/io/input/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/io/input/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/io/input/class_FrameworkFileInputPointer.php b/inc/classes/main/io/input/class_FrameworkFileInputPointer.php new file mode 100644 index 00000000..b6b9cb3a --- /dev/null +++ b/inc/classes/main/io/input/class_FrameworkFileInputPointer.php @@ -0,0 +1,94 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.shipsimu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class FrameworkFileInputPointer extends BaseFileIo { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * 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() + * @throws FileIsEmptyException If the provided file name is empty. + * @throws FileIoException If fopen() returns not a file resource + * @return void + */ + public static final function createFrameworkFileInputPointer ($fileName) { + // Some pre-sanity checks... + if ((is_null($fileName)) || (empty($fileName))) { + // No filename given + throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING); + } elseif (!file_exists($fileName)) { + // File does not exist! + throw new FileIoException($fileName, self::EXCEPTION_FILE_NOT_FOUND); + } elseif (!is_readable($fileName)) { + // File does not exist! + throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ); + } + + // Try to open a handler + $fileInstance = fopen($fileName, 'rb'); + if ((is_null($fileInstance)) || ($fileInstance === FALSE)) { + // Something bad happend + throw new FileIoException($fileName, self::EXCEPTION_FILE_POINTER_INVALID); + } // END - if + + // Create new instance + $pointerInstance = new FrameworkFileInputPointer(); + + // Set file pointer and file name + $pointerInstance->setInstance($fileInstance); + $pointerInstance->setFileName($fileName); + + // Return the instance + return $pointerInstance; + } + + /** + * Read data a file pointer + * + * @return mixed The result of fread() + * @throws NullPointerException If the file pointer instance + * is not set by setPointer() + */ + public function readFromFile () { + if (!$this->getInstance() instanceof SplFileObject) { + // Pointer not initialized + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } + + // Read data from the file pointer and return it + return fread($this->getInstance(), 1024); + } +} + +// [EOF] +?> diff --git a/inc/classes/main/io/io_handler/.htaccess b/inc/classes/main/io/io_handler/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/io/io_handler/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/io/class_FileIoStream.php b/inc/classes/main/io/io_handler/class_FileIoStream.php similarity index 100% rename from inc/classes/main/io/class_FileIoStream.php rename to inc/classes/main/io/io_handler/class_FileIoStream.php diff --git a/inc/classes/main/io/output/.htaccess b/inc/classes/main/io/output/.htaccess new file mode 100644 index 00000000..3a428827 --- /dev/null +++ b/inc/classes/main/io/output/.htaccess @@ -0,0 +1 @@ +Deny from all diff --git a/inc/classes/main/io/class_FrameworkFileOutputPointer.php b/inc/classes/main/io/output/class_FrameworkFileOutputPointer.php similarity index 57% rename from inc/classes/main/io/class_FrameworkFileOutputPointer.php rename to inc/classes/main/io/output/class_FrameworkFileOutputPointer.php index 8d80e715..1af2697b 100644 --- a/inc/classes/main/io/class_FrameworkFileOutputPointer.php +++ b/inc/classes/main/io/output/class_FrameworkFileOutputPointer.php @@ -21,39 +21,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class FrameworkFileOutputPointer extends BaseFrameworkSystem { - /** - * The current file we are working in - */ - private $fileName = ''; - - /** - * The file pointer - */ - private $filePointer = NULL; - +class FrameworkFileOutputPointer extends BaseFileIo { /** * Protected constructor + * + * @return void */ protected function __construct () { // Call parent constructor parent::__construct(__CLASS__); } - /** - * Destructor for cleaning purposes, etc - */ - public final function __destruct() { - // Is there a resource pointer? Then we have to close the file here! - if (is_resource($this->getPointer())) { - // Try to close a file - $this->closeFile(); - } - - // Call the parent destructor - parent::__destruct(); - } - /** * Create a file pointer based on the given file. The file will also * be verified here. @@ -111,77 +89,6 @@ class FrameworkFileOutputPointer extends BaseFrameworkSystem { // Write data to the file pointer and return written bytes return fwrite($this->getPointer(), $dataStream); } - - /** - * Close a file source and set it's instance to null and the file name - * to empty - * - * @return void - * @throws NullPointerException If the file pointer instance - * is not set by setPointer() - * @throws InvalidResourceException If there is being set - */ - public function closeFile () { - if (is_null($this->getPointer())) { - // 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); - } - - // Close the file pointer and reset the instance variable - @fclose($this->getPointer()); - $this->setPointer(NULL); - $this->setFileName(''); - } - - /** - * Setter for the file pointer - * - * @param $filePointer File resource - * @return void - */ - public final function setPointer ($filePointer) { - // Sanity-check if pointer is a valid file resource - if (is_resource($filePointer) || is_null($filePointer)) { - // Is a valid resource - $this->filePointer = $filePointer; - } else { - // Throw exception - throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE); - } - } - - /** - * Getter for the file pointer - * - * @return $filePointer The file pointer which shall be a valid - * file resource - */ - public final function getPointer () { - return $this->filePointer; - } - - /** - * Setter for file name - * - * @param $fileName The new file name - * @return void - */ - public final function setFileName ($fileName) { - $fileName = (string) $fileName; - $this->fileName = $fileName; - } - - /** - * Getter for file name - * - * @return $fileName The current file name - */ - public final function getFileName () { - return $this->fileName; - } } // [EOF] -- 2.39.5