]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/file_directories/class_BaseFileIo.php
Continued:
[core.git] / framework / main / classes / file_directories / class_BaseFileIo.php
index 0bdf57a2b81fd3086fd8992c91e2e781b54b4751..ff2edc86a59cdb33f1aa5c9dbde55e73dfa33b5d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 // Own namespace
-namespace Org\Mxchange\CoreFramework\FileSystem;
+namespace Org\Mxchange\CoreFramework\Filesystem;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
@@ -9,6 +9,7 @@ use Org\Mxchange\CoreFramework\Generic\NullPointerException;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 
 // Import SPL stuff
+use \OutOfBoundsException;
 use \SplFileObject;
 
 /**
@@ -16,7 +17,7 @@ use \SplFileObject;
  *
  * @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 - 2021 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -33,7 +34,7 @@ use \SplFileObject;
  * 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 file object
         */
@@ -45,7 +46,7 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * @param       $className      Name of the class
         * @return      void
         */
-       protected function __construct ($className) {
+       protected function __construct (string $className) {
                // Call parent constructor
                parent::__construct($className);
        }
@@ -57,13 +58,18 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         */
        public final function __destruct() {
                // Is there a resource pointer? Then we have to close the file here!
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: this->fileObject[]=%s - DESTRUCTOR!', gettype($this->getFileObject())));
                if (is_object($this->getFileObject())) {
                        // Try to close a file
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: Calling this->closeFile() ...');
                        $this->closeFile();
-               } // END - if
+               }
 
                // Call the parent destructor
                parent::__destruct();
+
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
        }
 
        /**
@@ -71,13 +77,12 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * to empty.
         *
         * @return      void
-        * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
+        * @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->getFileObject()->getPathname()));
-
+               // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: fileName=%s - CALLED!', $this->getFileObject()->getPathname()));
                if (is_null($this->getFileObject())) {
                        // Pointer not initialized
                        throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
@@ -86,14 +91,12 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
                        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->getFileObject()->getPathname()));
-
                // Close the file pointer by NULL-ing it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: Closing file %s ...', $this->getFileObject()->getPathname()));
                $this->resetFileObject();
 
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
+               // Trace message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: EXIT!');
        }
 
        /**
@@ -140,7 +143,7 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * @return      $isEndOfFileReached             Whether the EOF has been reached
         */
        public final function isEndOfFileReached () {
-               return $this->getFileObject()->feof();
+               return $this->getFileObject()->eof();
        }
 
        /**
@@ -150,12 +153,19 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * @param       $whence         Added to offset (default: only use offset to seek to)
         * @return      $status         Status of file seek: 0 = success, -1 = failed
         */
-       public function seek ($offset, $whence = SEEK_SET) {
+       public function seek (int $offset, int $whence = SEEK_SET) {
+               // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: offset=%d,whence=%d - CALLED!', $offset, $whence));
+               if ($offset < 0) {
+                       // Throw exception
+                       throw new OutOfBoundsException(sprintf('offset=%d is not valid', $offset));
+               }
+
                // Seek to position
                $status = $this->getFileObject()->fseek($offset, $whence);
 
                // Return status
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: status=%d - EXIT!', $status));
                return $status;
        }
 
@@ -166,23 +176,23 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * @todo        Handle seekStatus
         */
        public function size () {
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
-
                // Get current seek position
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FILE-IO: CALLED!');
                $seekPosition = $this->determineSeekPosition();
 
                // Seek to end
                $seekStatus = $this->seek(0, SEEK_END);
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
 
-               // Get position again  (which is the end of the file)
+               // Get position again (which is the end of the file)
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: seekStatus[%s]=%d', gettype($seekStatus), $seekStatus));
                $size = $this->determineSeekPosition();
 
                // Reset seek position to old
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size[%s]=%d', gettype($size), $size));
                $this->seek($seekPosition);
 
                // Return size
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-FILE-IO: size=%d - EXIT!', $size));
                return $size;
        }