0e070be17fc46d2e8e5c530706ee6a8a215acc54
[shipsimu.git] / inc / classes / main / io / class_FrameworkFileInputPointer.php
1 <?php
2 /**
3  * A class for reading files
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class FrameworkFileInputPointer extends BaseFrameworkSystem {
25         /**
26          * The current file we are working in
27          */
28         private $fileName = "";
29
30         /**
31          * The file pointer
32          */
33         private $filePointer = null;
34
35         /**
36          * Protected constructor
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41
42                 // Set part description
43                 $this->setObjectDescription("Handler for reading from files");
44
45                 // Create unique ID
46                 $this->generateUniqueId();
47
48                 // Clean-up a little
49                 $this->removeNumberFormaters();
50         }
51
52         /**
53          * Destructor for cleaning purposes, etc
54          */
55         public final function __destruct() {
56                 // Is there a resource pointer? Then we have to close the file here!
57                 if (is_resource($this->getPointer())) {
58                         // Try to close a file
59                         $this->closeFile();
60                 }
61
62                 // Call the parent destructor
63                 parent::__destruct();
64         }
65
66         /**
67          * Create a file pointer based on the given file. The file will also
68          * be verified here.
69          *
70          * @param               $fileName                               The file name we shall pass
71          *                                                              to fopen()
72          * @throws      FileIsEmptyException    If the provided file name is empty.
73          * @throws      FilePointerNotOpenedException           If fopen() returns not a
74          *                                                                              file resource
75          * @return      void
76          */
77         public final static function createFrameworkFileInputPointer ($fileName) {
78                 // Some pre-sanity checks...
79                 if ((is_null($fileName)) || (empty($fileName))) {
80                         // No filename given
81                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
82                 } elseif (!file_exists($fileName)) {
83                         // File does not exist!
84                         throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_NOT_FOUND);
85                 } elseif (!is_readable($fileName)) {
86                         // File does not exist!
87                         throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
88                 }
89
90                 // Try to open a handler
91                 $filePointer = @fopen($fileName, 'rb');
92                 if ((is_null($filePointer)) || ($filePointer === false)) {
93                         // Something bad happend
94                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
95                 } // END - if
96
97                 // Create new instance
98                 $pointerInstance = new FrameworkFileInputPointer();
99
100                 // Set file pointer and file name
101                 $pointerInstance->setPointer($filePointer);
102                 $pointerInstance->setFileName($fileName);
103
104                 // Return the instance
105                 return $pointerInstance;
106         }
107
108         /**
109          * Read data a file pointer
110          *
111          * @return      mixed   The result of fread()
112          * @throws      NullPointerException    If the file pointer instance
113          *                                                              is not set by setPointer()
114          * @throws      InvalidFileResourceException    If there is being set
115          *                                                                      an invalid file resource
116          */
117         public function readFromFile () {
118                 if (is_null($this->getPointer())) {
119                         // Pointer not initialized
120                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
121                 } elseif (!is_resource($this->getPointer())) {
122                         // Pointer is not a valid resource!
123                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
124                 }
125
126                 // Read data from the file pointer and return it
127                 return fread($this->getPointer(), 1024);
128         }
129
130         /**
131          * Read lines from a file pointer
132          *
133          * @return      mixed   The result of fread()
134          * @throws      NullPointerException    If the file pointer instance
135          *                                                              is not set by setPointer()
136          * @throws      InvalidFileResourceException    If there is being set
137          *                                                                      an invalid file resource
138          */
139         public function readLinesFromFile () {
140                 if (is_null($this->getPointer())) {
141                         // Pointer not initialized
142                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
143                 } elseif (!is_resource($this->getPointer())) {
144                         // Pointer is not a valid resource!
145                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
146                 }
147
148                 // Read data from the file pointer and return it
149                 return fgets($this->getPointer(), 1024);
150         }
151
152         /**
153          * Close a file source and set it's instance to null and the file name
154          * to empty
155          *
156          * @return      void
157          * @throws      NullPointerException    If the file pointer instance
158          *                                                              is not set by setPointer()
159          * @throws      InvalidFileResourceException    If there is being set
160          */
161         public function closeFile () {
162                 if (is_null($this->getPointer())) {
163                         // Pointer not initialized
164                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
165                 } elseif (!is_resource($this->getPointer())) {
166                         // Pointer is not a valid resource!
167                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
168                 }
169
170                 // Close the file pointer and reset the instance variable
171                 @fclose($this->getPointer());
172                 $this->setPointer(null);
173                 $this->setFileName("");
174         }
175
176         /**
177          * Setter for the file pointer
178          *
179          * @param               $filePointer    File resource
180          * @return      void
181          */
182         public final function setPointer ($filePointer) {
183                 // Sanity-check if the pointer is a valid file resource
184                 if (is_resource($filePointer) || is_null($filePointer)) {
185                         // Is a valid resource
186                         $this->filePointer = $filePointer;
187                 } else {
188                         // Throw exception
189                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
190                 }
191         }
192
193         /**
194          * Getter for the file pointer
195          *
196          * @return      $filePointer    The file pointer which shall be a valid
197          *                                              file resource
198          */
199         public final function getPointer () {
200                 return $this->filePointer;
201         }
202
203         /**
204          * Setter for file name
205          *
206          * @param               $fileName               The new file name
207          * @return      void
208          */
209         public final function setFileName ($fileName) {
210                 $fileName = (string) $fileName;
211                 $this->fileName = $fileName;
212         }
213
214         /**
215          * Getter for file name
216          *
217          * @return      $fileName               The current file name
218          */
219         public final function getFileName () {
220                 return $this->fileName;
221         }
222 }
223
224 // [EOF]
225 ?>