]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/file_directories/class_BaseFileIo.php
Merge branch 'master' of git.mxchange.org:/var/cache/git/repos/core
[core.git] / inc / classes / main / file_directories / class_BaseFileIo.php
index a18a8529df11a831ffd6b332cb0fe3e2e7cff126..f449f11dfb99deed448c0fb7f0cecf670a0bef23 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -61,11 +61,10 @@ class BaseFileIo extends BaseFrameworkSystem {
 
        /**
         * Close a file source and set it's instance to null and the file name
-        * to empty
+        * 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 setPointer()
         * @throws      InvalidResourceException        If there is being set
         */
        public function closeFile () {
@@ -89,15 +88,14 @@ class BaseFileIo extends BaseFrameworkSystem {
         * @param       $filePointer    File resource
         * @return      void
         */
-       public final function setPointer ($filePointer) {
+       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
+        * @return      $filePointer    The file pointer which shall be a valid file resource
         */
        public final function getPointer () {
                return $this->filePointer;
@@ -109,7 +107,7 @@ class BaseFileIo extends BaseFrameworkSystem {
         * @param       $fileName       The new file name
         * @return      void
         */
-       public final function setFileName ($fileName) {
+       protected final function setFileName ($fileName) {
                $fileName = (string) $fileName;
                $this->fileName = $fileName;
        }
@@ -122,6 +120,67 @@ class BaseFileIo extends BaseFrameworkSystem {
        public final function getFileName () {
                return $this->fileName;
        }
+
+       /**
+        * Determines seek position
+        *
+        * @return      $seekPosition   Current seek position
+        */
+       public final function determineSeekPosition () {
+               return ftell($this->getPointer());
+       }
+
+       /**
+        * Determines whether the EOF has been reached
+        *
+        * @return      $isEndOfFileReached             Whether the EOF has been reached
+        */
+       public final function isEndOfFileReached () {
+               return feof($this->getPointer());
+       }
+
+       /**
+        * Seek to given offset (default) or other possibilities as fseek() gives.
+        *
+        * @param       $offset         Offset to seek to (or used as "base" for other seeks)
+        * @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) {
+               // Seek to position
+               $status = fseek($this->getPointer(), $offset, $whence);
+
+               // Return status
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] status=%d', __METHOD__, __LINE__, $status));
+               return $status;
+       }
+
+       /**
+        * Size of this file
+        *
+        * @return      $size   Size (in bytes) of file
+        * @todo        Handle seekStatus
+        */
+       public function size () {
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
+
+               // Get current seek position
+               $seekPosition = $this->determineSeekPosition();
+
+               // Seek to end
+               $seekStatus = $this->seek(0, SEEK_END);
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] seekStatus=%d', __METHOD__, __LINE__, $seekStatus));
+
+               // Get position again  (which is the end of the file)
+               $size = $this->determineSeekPosition();
+
+               // Reset seek position to old
+               $this->seek($seekPosition);
+
+               // Return size
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] size=%s - EXIT!', __METHOD__, __LINE__, $size));
+               return $size;
+       }
 }
 
 // [EOF]