3 * A class for writing files
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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__);
44 * Destructor for cleaning purposes, etc
46 public final function __destruct() {
47 // Is there a resource pointer? Then we have to close the file here!
48 if (is_resource($this->getPointer())) {
49 // Try to close a file
53 // Call the parent destructor
58 * Create a file pointer based on the given file. The file will also
61 * @param $fileName The file name we shall pass to fopen()
62 * @param $mode The output mode ('w', 'a' are valid)
63 * @throws FileIsEmptyException If the provided file name is empty.
64 * @throws FilePointerNotOpened If fopen() returns not a file
68 public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
69 // Some pre-sanity checks...
70 if (is_null($fileName)) {
72 throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
75 // Try to open a handler
76 $filePointer = @fopen($fileName, $mode);
77 if (($filePointer === null) || ($filePointer === false)) {
78 // Something bad happend
79 throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
82 // Create new instance
83 $pointerInstance = new FrameworkFileOutputPointer();
85 // Set file pointer and file name
86 $pointerInstance->setPointer($filePointer);
87 $pointerInstance->setFileName($fileName);
89 // Return the instance
90 return $pointerInstance;
94 * Write data to a file pointer
96 * @param $dataStream The data stream we shall write to the file
97 * @return mixed The result of fwrite()
98 * @throws NullPointerException If the file pointer instance
99 * is not set by setPointer()
100 * @throws InvalidResourceException If there is being set
101 * an invalid file resource
103 public function writeToFile ($dataStream) {
104 if (is_null($this->getPointer())) {
105 // Pointer not initialized
106 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
107 } elseif (!is_resource($this->getPointer())) {
108 // Pointer is not a valid resource!
109 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
112 // Read data from the file pointer and return it
113 return fwrite($this->getPointer(), $dataStream);
117 * Close a file source and set it's instance to null and the file name
121 * @throws NullPointerException If the file pointer instance
122 * is not set by setPointer()
123 * @throws InvalidResourceException If there is being set
125 public function closeFile () {
126 if (is_null($this->getPointer())) {
127 // Pointer not initialized
128 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
129 } elseif (!is_resource($this->getPointer())) {
130 // Pointer is not a valid resource!
131 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
134 // Close the file pointer and reset the instance variable
135 @fclose($this->getPointer());
136 $this->setPointer(null);
137 $this->setFileName('');
141 * Setter for the file pointer
143 * @param $filePointer File resource
146 public final function setPointer ($filePointer) {
147 // Sanity-check if pointer is a valid file resource
148 if (is_resource($filePointer) || is_null($filePointer)) {
149 // Is a valid resource
150 $this->filePointer = $filePointer;
153 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
158 * Getter for the file pointer
160 * @return $filePointer The file pointer which shall be a valid
163 public final function getPointer () {
164 return $this->filePointer;
168 * Setter for file name
170 * @param $fileName The new file name
173 public final function setFileName ($fileName) {
174 $fileName = (string) $fileName;
175 $this->fileName = $fileName;
179 * Getter for file name
181 * @return $fileName The current file name
183 public final function getFileName () {
184 return $this->fileName;