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