3 namespace CoreFramework\Filesystem\Pointer\Text;
5 // Import framework stuff
6 use CoreFramework\FileSystem\BaseFileIo;
7 use CoreFramework\Filesystem\Pointer\OutputPointer;
8 use CoreFramework\Generic\NullPointerException;
9 use CoreFramework\Generic\UnsupportedOperationException;
12 * A class for writing files
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Create a file pointer based on the given file. The file will also
48 * @param $fileName The file name we shall pass to fopen()
49 * @param $mode The output mode ('w', 'a' are valid)
50 * @throws FileIsEmptyException If the provided file name is empty.
51 * @throws FileIoException If fopen() returns not a file resource
54 public static final function createFrameworkTextFileOutputPointer ($fileName, $mode) {
55 // Some pre-sanity checks...
56 if (is_null($fileName)) {
58 throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
61 // Try to open a handler
62 $filePointer = @fopen($fileName, $mode);
63 if ((is_null($filePointer)) || ($filePointer === false)) {
64 // Something bad happend
65 throw new FileIoException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
68 // Create new instance
69 $pointerInstance = new FrameworkTextFileOutputPointer();
71 // Set file pointer and file name
72 $pointerInstance->setPointer($filePointer);
73 $pointerInstance->setFileName($fileName);
75 // Return the instance
76 return $pointerInstance;
80 * Write data to a file pointer
82 * @param $dataStream The data stream we shall write to the file
83 * @return mixed Number of writes bytes or false on error
84 * @throws NullPointerException If the file pointer instance
85 * is not set by setPointer()
86 * @throws InvalidResourceException If there is being set
87 * an invalid file resource
89 public function writeToFile ($dataStream) {
90 if (is_null($this->getPointer())) {
91 // Pointer not initialized
92 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
93 } elseif (!is_resource($this->getPointer())) {
94 // Pointer is not a valid resource!
95 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
98 // Write data to the file pointer and return written bytes
99 return fwrite($this->getPointer(), $dataStream);
103 * Analyzes entries in index file. This will count all found (and valid)
104 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
105 * only gaps are found, the file is considered as "virgin" (no entries).
108 * @throws UnsupportedOperationException If this method is called
110 public function analyzeFile () {
111 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
115 * Writes at given position by seeking to it.
117 * @param $seekPosition Seek position in file
118 * @param $data Data to be written
119 * @return mixed Number of writes bytes or false on error
120 * @throws UnsupportedOperationException If this method is called
122 public function writeAtPosition ($seedPosition, $data) {
123 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
127 * Advances to next "block" of bytes
130 * @throws UnsupportedOperationException If this method is called
132 public function next () {
133 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
137 * Checks wether the current entry is valid (not at the end of the file).
138 * This method will return true if an emptied (nulled) entry has been found.
140 * @return $isValid Whether the next entry is valid
141 * @throws UnsupportedOperationException If this method is called
143 public function valid () {
144 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
148 * Gets current seek position ("key").
150 * @return $key Current key in iteration
151 * @throws UnsupportedOperationException If this method is called
153 public function key () {
154 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);