Rewritten:
[core.git] / framework / main / classes / file_directories / class_BaseFileIo.php
index 4ba2e96..1205ebe 100644 (file)
@@ -8,6 +8,9 @@ use CoreFramework\Filesystem\FilePointer;
 use CoreFramework\Generic\NullPointerException;
 use CoreFramework\Object\BaseFrameworkSystem;
 
+// Import SPL stuff
+use \SplFileObject;
+
 /**
  * A general FileIo class
  *
@@ -32,14 +35,9 @@ use CoreFramework\Object\BaseFrameworkSystem;
  */
 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
@@ -59,7 +57,7 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         */
        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
@@ -74,69 +72,57 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         *
         * @return      void
         * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
-        * @throws      InvalidResourceException        If there is being set
+        * @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;
        }
 
        /**
@@ -145,7 +131,7 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * @return      $seekPosition   Current seek position
         */
        public final function determineSeekPosition () {
-               return ftell($this->getPointer());
+               return $this->getFileObject()->ftell();
        }
 
        /**
@@ -154,7 +140,7 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         * @return      $isEndOfFileReached             Whether the EOF has been reached
         */
        public final function isEndOfFileReached () {
-               return feof($this->getPointer());
+               return $this->getFileObject()->feof();
        }
 
        /**
@@ -166,7 +152,7 @@ class BaseFileIo extends BaseFrameworkSystem implements FilePointer, CloseableFi
         */
        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));