Typos fixed and special command resolver are now possible
[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          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43
44                 // Clean-up a little
45                 $this->removeNumberFormaters();
46                 $this->removeSystemArray();
47         }
48
49         /**
50          * Destructor for cleaning purposes, etc
51          *
52          * @return      void
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 to fopen()
70          * @throws      FileIsEmptyException    If the provided file name is empty.
71          * @throws      FilePointerNotOpenedException   If fopen() returns not a
72          *                                                                                      file resource
73          * @return      void
74          */
75         public final static function createFrameworkFileInputPointer ($fileName) {
76                 // Some pre-sanity checks...
77                 if ((is_null($fileName)) || (empty($fileName))) {
78                         // No filename given
79                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
80                 } elseif (!file_exists($fileName)) {
81                         // File does not exist!
82                         throw new FileNotFoundException($fileName, self::EXCEPTION_FILE_NOT_FOUND);
83                 } elseif (!is_readable($fileName)) {
84                         // File does not exist!
85                         throw new FileReadProtectedException($fileName, self::EXCEPTION_FILE_CANNOT_BE_READ);
86                 }
87
88                 // Try to open a handler
89                 $filePointer = @fopen($fileName, 'rb');
90                 if ((is_null($filePointer)) || ($filePointer === false)) {
91                         // Something bad happend
92                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
93                 } // END - if
94
95                 // Create new instance
96                 $pointerInstance = new FrameworkFileInputPointer();
97
98                 // Set file pointer and file name
99                 $pointerInstance->setPointer($filePointer);
100                 $pointerInstance->setFileName($fileName);
101
102                 // Return the instance
103                 return $pointerInstance;
104         }
105
106         /**
107          * Read data a file pointer
108          *
109          * @return      mixed   The result of fread()
110          * @throws      NullPointerException    If the file pointer instance
111          *                                                                      is not set by setPointer()
112          * @throws      InvalidFileResourceException    If there is being set
113          *                                                                      an invalid file resource
114          */
115         public function readFromFile () {
116                 if (is_null($this->getPointer())) {
117                         // Pointer not initialized
118                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
119                 } elseif (!is_resource($this->getPointer())) {
120                         // Pointer is not a valid resource!
121                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
122                 }
123
124                 // Read data from the file pointer and return it
125                 return fread($this->getPointer(), 1024);
126         }
127
128         /**
129          * Read lines from a file pointer
130          *
131          * @return      mixed   The result of fread()
132          * @throws      NullPointerException    If the file pointer instance
133          *                                                                      is not set by setPointer()
134          * @throws      InvalidFileResourceException    If there is being set
135          *                                                                      an invalid file resource
136          */
137         public function readLinesFromFile () {
138                 if (is_null($this->getPointer())) {
139                         // Pointer not initialized
140                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
141                 } elseif (!is_resource($this->getPointer())) {
142                         // Pointer is not a valid resource!
143                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
144                 }
145
146                 // Read data from the file pointer and return it
147                 return fgets($this->getPointer(), 1024);
148         }
149
150         /**
151          * Close a file source and set it's instance to null and the file name
152          * to empty
153          *
154          * @return      void
155          * @throws      NullPointerException    If the file pointer instance
156          *                                                                      is not set by setPointer()
157          * @throws      InvalidFileResourceException    If there is being set
158          */
159         public function closeFile () {
160                 if (is_null($this->getPointer())) {
161                         // Pointer not initialized
162                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
163                 } elseif (!is_resource($this->getPointer())) {
164                         // Pointer is not a valid resource!
165                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
166                 }
167
168                 // Close the file pointer and reset the instance variable
169                 @fclose($this->getPointer());
170                 $this->setPointer(null);
171                 $this->setFileName("");
172         }
173
174         /**
175          * Setter for the file pointer
176          *
177          * @param       $filePointer    File resource
178          * @return      void
179          */
180         public final function setPointer ($filePointer) {
181                 // Sanity-check if pointer is a valid file resource
182                 if (is_resource($filePointer) || is_null($filePointer)) {
183                         // Is a valid resource
184                         $this->filePointer = $filePointer;
185                 } else {
186                         // Throw exception
187                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
188                 }
189         }
190
191         /**
192          * Getter for the file pointer
193          *
194          * @return      $filePointer    The file pointer which shall be a valid
195          *                                                      file resource
196          */
197         public final function getPointer () {
198                 return $this->filePointer;
199         }
200
201         /**
202          * Setter for file name
203          *
204          * @param       $fileName       The new file name
205          * @return      void
206          */
207         public final function setFileName ($fileName) {
208                 $fileName = (string) $fileName;
209                 $this->fileName = $fileName;
210         }
211
212         /**
213          * Getter for file name
214          *
215          * @return      $fileName       The current file name
216          */
217         public final function getFileName () {
218                 return $this->fileName;
219         }
220 }
221
222 // [EOF]
223 ?>