Copyright updated
[core.git] / inc / classes / main / io / class_FrameworkFileOutputPointer.php
1 <?php
2 /**
3  * A class for writing files
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007 - 2009 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 FrameworkFileOutputPointer 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                 // Clean-up a little
43                 $this->removeNumberFormaters();
44                 $this->removeSystemArray();
45         }
46
47         /**
48          * Destructor for cleaning purposes, etc
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          * @param       $mode           The output mode ('w', 'a' are valid)
67          * @throws      FileIsEmptyException    If the provided file name is empty.
68          * @throws      FilePointerNotOpened    If fopen() returns not a file
69          *                                                                      resource
70          * @return      void
71          */
72         public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
73                 // Some pre-sanity checks...
74                 if (is_null($fileName)) {
75                         // No filename given
76                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77                 }
78
79                 // Try to open a handler
80                 $filePointer = @fopen($fileName, $mode);
81                 if (($filePointer === null) || ($filePointer === false)) {
82                         // Something bad happend
83                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
84                 }
85
86                 // Create new instance
87                 $pointerInstance = new FrameworkFileOutputPointer();
88
89                 // Set file pointer and file name
90                 $pointerInstance->setPointer($filePointer);
91                 $pointerInstance->setFileName($fileName);
92
93                 // Return the instance
94                 return $pointerInstance;
95         }
96
97         /**
98          * Write data to a file pointer
99          *
100          * @param       $dataStream             The data stream we shall write to the file
101          * @return      mixed                   The result of fwrite()
102          * @throws      NullPointerException    If the file pointer instance
103          *                                                                      is not set by setPointer()
104          * @throws      InvalidFileResourceException    If there is being set
105          *                                                                                      an invalid file resource
106          */
107         public function writeToFile ($dataStream) {
108                 if (is_null($this->getPointer())) {
109                         // Pointer not initialized
110                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
111                 } elseif (!is_resource($this->getPointer())) {
112                         // Pointer is not a valid resource!
113                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
114                 }
115
116                 // Read data from the file pointer and return it
117                 return fwrite($this->getPointer(), $dataStream);
118         }
119
120         /**
121          * Close a file source and set it's instance to null and the file name
122          * to empty
123          *
124          * @return      void
125          * @throws      NullPointerException    If the file pointer instance
126          *                                                                      is not set by setPointer()
127          * @throws      InvalidFileResourceException    If there is being set
128          */
129         public function closeFile () {
130                 if (is_null($this->getPointer())) {
131                         // Pointer not initialized
132                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
133                 } elseif (!is_resource($this->getPointer())) {
134                         // Pointer is not a valid resource!
135                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
136                 }
137
138                 // Close the file pointer and reset the instance variable
139                 @fclose($this->getPointer());
140                 $this->setPointer(null);
141                 $this->setFileName("");
142         }
143
144         /**
145          * Setter for the file pointer
146          *
147          * @param       $filePointer    File resource
148          * @return      void
149          */
150         public final function setPointer ($filePointer) {
151                 // Sanity-check if pointer is a valid file resource
152                 if (is_resource($filePointer) || is_null($filePointer)) {
153                         // Is a valid resource
154                         $this->filePointer = $filePointer;
155                 } else {
156                         // Throw exception
157                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
158                 }
159         }
160
161         /**
162          * Getter for the file pointer
163          *
164          * @return      $filePointer    The file pointer which shall be a valid
165          *                                                      file resource
166          */
167         public final function getPointer () {
168                 return $this->filePointer;
169         }
170
171         /**
172          * Setter for file name
173          *
174          * @param       $fileName       The new file name
175          * @return      void
176          */
177         public final function setFileName ($fileName) {
178                 $fileName = (string) $fileName;
179                 $this->fileName = $fileName;
180         }
181
182         /**
183          * Getter for file name
184          *
185          * @return      $fileName       The current file name
186          */
187         public final function getFileName () {
188                 return $this->fileName;
189         }
190 }
191
192 // [EOF]
193 ?>