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