62e0d3324589ffe0ec0c28bf3c116edc0b480c3e
[shipsimu.git] / ship-simu / inc / classes / main / io / class_FrameworkFileInputPointer.php
1 <?php
2 /**
3  * A class for reading files
4  */
5 class FrameworkFileInputPointer extends BaseFrameworkSystem {
6         /**
7          * The current file we are working in
8          */
9         private $fileName = "";
10
11         /**
12          * The file pointer
13          */
14         private $filePointer = null;
15
16         /**
17          * Private constructor
18          */
19         private final function __construct () {
20                 // Call parent constructor
21                 parent::constructor(__CLASS__);
22
23                 // Set part description
24                 $this->setPartDescr("Dateiausgabe-Handler");
25
26                 // Create unique ID
27                 $this->createUniqueID();
28
29                 // Clean-up a little
30                 $this->removeNumberFormaters();
31         }
32
33         /**
34          * Destructor for cleaning purposes, etc
35          */
36         public final function __destruct() {
37                 // Is there a resource pointer? Then we have to close the file here!
38                 if (is_resource($this->getPointer())) {
39                         // Try to close a file
40                         $this->closeFile();
41                 }
42
43                 // Call the parent destructor
44                 parent::__destruct();
45         }
46
47         /**
48          * Create a file pointer based on the given file. The file will also
49          * be verified here.
50          *
51          * @param               $fileName                               The file name we shall pass
52          *                                                              to fopen()
53          * @throws      FileIsEmptyException    If the provided file name is empty.
54          * @throws      FilePointerNotOpenedException           If fopen() returns not a
55          *                                                                              file resource
56          * @return      void
57          */
58         public final static function createFrameworkFileInputPointer ($fileName) {
59                 // Some pre-sanity checks...
60                 if (is_null($fileName)) {
61                         // No filename given
62                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
63                 }
64
65                 // Try to open a handler
66                 $filePointer = @fopen($fileName, 'rb');
67                 if (($filePointer === null) || ($filePointer === false)) {
68                         // Something bad happend
69                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
70                 }
71
72                 // Create new instance
73                 $pointerInstance = new FrameworkFileInputPointer();
74
75                 // Set file pointer and file name
76                 $pointerInstance->setPointer($filePointer);
77                 $pointerInstance->setFileName($fileName);
78
79                 // Return the instance
80                 return $pointerInstance;
81         }
82
83         /**
84          * Read data a file pointer
85          *
86          * @return      mixed   The result of fread()
87          * @throws      NullPointerException    If the file pointer instance
88          *                                                              is not set by setPointer()
89          * @throws      InvalidFileResourceException    If there is being set
90          *                                                                      an invalid file resource
91          */
92         public function readFromFile () {
93                 if (is_null($this->getPointer())) {
94                         // Pointer not initialized
95                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
96                 } elseif (!is_resource($this->getPointer())) {
97                         // Pointer is not a valid resource!
98                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
99                 }
100
101                 // Read data from the file pointer and return it
102                 return fread($this->getPointer(), 1024);
103         }
104
105         /**
106          * Read lines from a file pointer
107          *
108          * @return      mixed   The result of fread()
109          * @throws      NullPointerException    If the file pointer instance
110          *                                                              is not set by setPointer()
111          * @throws      InvalidFileResourceException    If there is being set
112          *                                                                      an invalid file resource
113          */
114         public function readLinesFromFile () {
115                 if (is_null($this->getPointer())) {
116                         // Pointer not initialized
117                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
118                 } elseif (!is_resource($this->getPointer())) {
119                         // Pointer is not a valid resource!
120                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
121                 }
122
123                 // Read data from the file pointer and return it
124                 return fgets($this->getPointer(), 1024);
125         }
126
127         /**
128          * Close a file source and set it's instance to null and the file name
129          * to empty
130          *
131          * @return      void
132          * @throws      NullPointerException    If the file pointer instance
133          *                                                              is not set by setPointer()
134          * @throws      InvalidFileResourceException    If there is being set
135          */
136         public function closeFile () {
137                 if (is_null($this->getPointer())) {
138                         // Pointer not initialized
139                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
140                 } elseif (!is_resource($this->getPointer())) {
141                         // Pointer is not a valid resource!
142                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
143                 }
144
145                 // Close the file pointer and reset the instance variable
146                 @fclose($this->getPointer());
147                 $this->setPointer(null);
148                 $this->setFileName("");
149         }
150
151         /**
152          * Setter for the file pointer
153          *
154          * @param               $filePointer    File resource
155          * @return      void
156          */
157         public final function setPointer ($filePointer) {
158                 // Sanity-check if the pointer is a valid file resource
159                 if (is_resource($filePointer) || is_null($filePointer)) {
160                         // Is a valid resource
161                         $this->filePointer = $filePointer;
162                 } else {
163                         // Throw exception
164                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
165                 }
166         }
167
168         /**
169          * Getter for the file pointer
170          *
171          * @return      $filePointer    The file pointer which shall be a valid
172          *                                              file resource
173          */
174         public final function getPointer () {
175                 return $this->filePointer;
176         }
177
178         /**
179          * Setter for file name
180          *
181          * @param               $fileName               The new file name
182          * @return      void
183          */
184         public final function setFileName ($fileName) {
185                 $fileName = (string) $fileName;
186                 $this->fileName = $fileName;
187         }
188
189         /**
190          * Getter for file name
191          *
192          * @return      $fileName               The current file name
193          */
194         public final function getFileName () {
195                 return $this->fileName;
196         }
197 }
198
199 // [EOF]
200 ?>