--- /dev/null
+<?php
+/**
+ * A general FileIo class
+ *
+ * @author Roland Haeder <webmaster@ship-simu.org>
+ * @version 0.0.0
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
+ * @license GNU GPL 3.0 or any newer version
+ * @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
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+class BaseFileIo extends BaseFrameworkSystem {
+ /**
+ * The current file we are working in
+ */
+ private $fileName = '';
+
+ /**
+ * The file pointer
+ */
+ private $filePointer = NULL;
+
+ /**
+ * Protected constructor
+ *
+ * @param $className Name of the class
+ * @return void
+ */
+ protected function __construct ($className) {
+ // Call parent constructor
+ parent::__construct($className);
+ }
+
+ /**
+ * Destructor for cleaning purposes, etc
+ *
+ * @return void
+ */
+ 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();
+ } // END - if
+
+ // Call the parent destructor
+ parent::__destruct();
+ }
+
+ /**
+ * 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
+ */
+ private function closeFile () {
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
+
+ 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);
+ }
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: Closing file %s ...', __METHOD__, __LINE__, $this->getFileName()));
+
+ // Close the file pointer and reset the instance variable
+ @fclose($this->getPointer());
+ $this->setPointer(NULL);
+ $this->setFileName('');
+
+ // Debug message
+ //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
+ }
+
+ /**
+ * Setter for the file pointer
+ *
+ * @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;
+ }
+
+ /**
+ * Setter for file name
+ *
+ * @param $fileName The new file name
+ * @return void
+ */
+ protected 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;
+ }
+
+ /**
+ * 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]
+?>