class_FrameworkFileInputPointer.php

Go to the documentation of this file.
00001 <?php
00024 class FrameworkFileInputPointer extends BaseFrameworkSystem {
00028         private $fileName = "";
00029 
00033         private $filePointer = null;
00034 
00040         protected function __construct () {
00041                 // Call parent constructor
00042                 parent::__construct(__CLASS__);
00043 
00044                 // Clean-up a little
00045                 $this->removeNumberFormaters();
00046                 $this->removeSystemArray();
00047         }
00048 
00054         public final function __destruct() {
00055                 // Is there a resource pointer? Then we have to close the file here!
00056                 if (is_resource($this->getPointer())) {
00057                         // Try to close a file
00058                         $this->closeFile();
00059                 }
00060 
00061                 // Call the parent destructor
00062                 parent::__destruct();
00063         }
00064 
00075         public final static function createFrameworkFileInputPointer ($fileName) {
00076                 // Some pre-sanity checks...
00077                 if ((is_null($fileName)) || (empty($fileName))) {
00078                         // No filename given
00079                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00080                 } elseif (!file_exists($fileName)) {
00081                         // File does not exist!
00082                         throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_NOT_FOUND);
00083                 } elseif (!is_readable($fileName)) {
00084                         // File does not exist!
00085                         throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
00086                 }
00087 
00088                 // Try to open a handler
00089                 $filePointer = @fopen($fileName, 'rb');
00090                 if ((is_null($filePointer)) || ($filePointer === false)) {
00091                         // Something bad happend
00092                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
00093                 } // END - if
00094 
00095                 // Create new instance
00096                 $pointerInstance = new FrameworkFileInputPointer();
00097 
00098                 // Set file pointer and file name
00099                 $pointerInstance->setPointer($filePointer);
00100                 $pointerInstance->setFileName($fileName);
00101 
00102                 // Return the instance
00103                 return $pointerInstance;
00104         }
00105 
00115         public function readFromFile () {
00116                 if (is_null($this->getPointer())) {
00117                         // Pointer not initialized
00118                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00119                 } elseif (!is_resource($this->getPointer())) {
00120                         // Pointer is not a valid resource!
00121                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00122                 }
00123 
00124                 // Read data from the file pointer and return it
00125                 return fread($this->getPointer(), 1024);
00126         }
00127 
00137         public function readLinesFromFile () {
00138                 if (is_null($this->getPointer())) {
00139                         // Pointer not initialized
00140                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00141                 } elseif (!is_resource($this->getPointer())) {
00142                         // Pointer is not a valid resource!
00143                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00144                 }
00145 
00146                 // Read data from the file pointer and return it
00147                 return fgets($this->getPointer(), 1024);
00148         }
00149 
00159         public function closeFile () {
00160                 if (is_null($this->getPointer())) {
00161                         // Pointer not initialized
00162                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00163                 } elseif (!is_resource($this->getPointer())) {
00164                         // Pointer is not a valid resource!
00165                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00166                 }
00167 
00168                 // Close the file pointer and reset the instance variable
00169                 @fclose($this->getPointer());
00170                 $this->setPointer(null);
00171                 $this->setFileName("");
00172         }
00173 
00180         public final function setPointer ($filePointer) {
00181                 // Sanity-check if pointer is a valid file resource
00182                 if (is_resource($filePointer) || is_null($filePointer)) {
00183                         // Is a valid resource
00184                         $this->filePointer = $filePointer;
00185                 } else {
00186                         // Throw exception
00187                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
00188                 }
00189         }
00190 
00197         public final function getPointer () {
00198                 return $this->filePointer;
00199         }
00200 
00207         public final function setFileName ($fileName) {
00208                 $fileName = (string) $fileName;
00209                 $this->fileName = $fileName;
00210         }
00211 
00217         public final function getFileName () {
00218                 return $this->fileName;
00219         }
00220 }
00221 
00222 // [EOF]
00223 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6