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