]> git.mxchange.org Git - shipsimu.git/blobdiff - ship-simu/inc/classes/main/io/class_FrameworkFileInputPointer.php
Initial import of current development status
[shipsimu.git] / ship-simu / inc / classes / main / io / class_FrameworkFileInputPointer.php
diff --git a/ship-simu/inc/classes/main/io/class_FrameworkFileInputPointer.php b/ship-simu/inc/classes/main/io/class_FrameworkFileInputPointer.php
new file mode 100644 (file)
index 0000000..62e0d33
--- /dev/null
@@ -0,0 +1,200 @@
+<?php
+/**
+ * A class for reading files
+ */
+class FrameworkFileInputPointer extends BaseFrameworkSystem {
+       /**
+        * The current file we are working in
+        */
+       private $fileName = "";
+
+       /**
+        * The file pointer
+        */
+       private $filePointer = null;
+
+       /**
+        * Private constructor
+        */
+       private final function __construct () {
+               // Call parent constructor
+               parent::constructor(__CLASS__);
+
+               // Set part description
+               $this->setPartDescr("Dateiausgabe-Handler");
+
+               // Create unique ID
+               $this->createUniqueID();
+
+               // Clean-up a little
+               $this->removeNumberFormaters();
+       }
+
+       /**
+        * 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.
+        *
+        * @param               $fileName                               The file name we shall pass
+        *                                                              to fopen()
+        * @throws      FileIsEmptyException    If the provided file name is empty.
+        * @throws      FilePointerNotOpenedException           If fopen() returns not a
+        *                                                                              file resource
+        * @return      void
+        */
+       public final static function createFrameworkFileInputPointer ($fileName) {
+               // Some pre-sanity checks...
+               if (is_null($fileName)) {
+                       // No filename given
+                       throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               }
+
+               // Try to open a handler
+               $filePointer = @fopen($fileName, 'rb');
+               if (($filePointer === null) || ($filePointer === false)) {
+                       // Something bad happend
+                       throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
+               }
+
+               // 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      InvalidFileResourceException    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 InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
+               }
+
+               // Read data from the file pointer and return it
+               return fread($this->getPointer(), 1024);
+       }
+
+       /**
+        * Read lines from a file pointer
+        *
+        * @return      mixed   The result of fread()
+        * @throws      NullPointerException    If the file pointer instance
+        *                                                              is not set by setPointer()
+        * @throws      InvalidFileResourceException    If there is being set
+        *                                                                      an invalid file resource
+        */
+       public function readLinesFromFile () {
+               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 InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
+               }
+
+               // Read data from the file pointer and return it
+               return fgets($this->getPointer(), 1024);
+       }
+
+       /**
+        * 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      InvalidFileResourceException    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 InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
+               }
+
+               // 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 the 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 InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
+               }
+       }
+
+       /**
+        * 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]
+?>