3 * A class for writing files
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class FrameworkFileOutputPointer extends BaseFrameworkSystem {
26 * The current file we are working in
28 private $fileName = "";
33 private $filePointer = null;
36 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
43 $this->removeNumberFormaters();
44 $this->removeSystemArray();
48 * Destructor for cleaning purposes, etc
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
57 // Call the parent destructor
62 * Create a file pointer based on the given file. The file will also
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
72 public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
73 // Some pre-sanity checks...
74 if (is_null($fileName)) {
76 throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
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);
86 // Create new instance
87 $pointerInstance = new FrameworkFileOutputPointer();
89 // Set file pointer and file name
90 $pointerInstance->setPointer($filePointer);
91 $pointerInstance->setFileName($fileName);
93 // Return the instance
94 return $pointerInstance;
98 * Write data to a file pointer
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
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);
116 // Read data from the file pointer and return it
117 return fwrite($this->getPointer(), $dataStream);
121 * Close a file source and set it's instance to null and the file name
125 * @throws NullPointerException If the file pointer instance
126 * is not set by setPointer()
127 * @throws InvalidFileResourceException If there is being set
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);
138 // Close the file pointer and reset the instance variable
139 @fclose($this->getPointer());
140 $this->setPointer(null);
141 $this->setFileName("");
145 * Setter for the file pointer
147 * @param $filePointer File resource
150 public final function setPointer ($filePointer) {
151 // Sanity-check if the pointer is a valid file resource
152 if (is_resource($filePointer) || is_null($filePointer)) {
153 // Is a valid resource
154 $this->filePointer = $filePointer;
157 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
162 * Getter for the file pointer
164 * @return $filePointer The file pointer which shall be a valid
167 public final function getPointer () {
168 return $this->filePointer;
172 * Setter for file name
174 * @param $fileName The new file name
177 public final function setFileName ($fileName) {
178 $fileName = (string) $fileName;
179 $this->fileName = $fileName;
183 * Getter for file name
185 * @return $fileName The current file name
187 public final function getFileName () {
188 return $this->fileName;