a235a8069aec35171aaa6f234dcecb755cd9f360
[core.git] / framework / main / classes / file_directories / output / text / class_FrameworkTextFileOutputPointer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filesystem\Pointer\Text;
4
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;
10
11 /**
12  * A class for writing files
13  *
14  * @author              Roland Haeder <webmaster@shipsimu.org>
15  * @version             0.0.0
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
19  *
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.
24  *
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.
29  *
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/>.
32  */
33 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
34         /**
35          * Protected constructor
36          *
37          * @return      void
38          */
39         protected function __construct () {
40                 // Call parent constructor
41                 parent::__construct(__CLASS__);
42         }
43
44         /**
45          * Create a file pointer based on the given file. The file will also
46          * be verified here.
47          *
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
52          * @return      void
53          */
54         public static final function createFrameworkTextFileOutputPointer ($fileName, $mode) {
55                 // Some pre-sanity checks...
56                 if (is_null($fileName)) {
57                         // No filename given
58                         throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
59                 } // END - if
60
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);
66                 } // END - if
67
68                 // Create new instance
69                 $pointerInstance = new FrameworkTextFileOutputPointer();
70
71                 // Set file pointer and file name
72                 $pointerInstance->setPointer($filePointer);
73                 $pointerInstance->setFileName($fileName);
74
75                 // Return the instance
76                 return $pointerInstance;
77         }
78
79         /**
80          * Write data to a file pointer
81          *
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
88          */
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);
96                 }
97
98                 // Write data to the file pointer and return written bytes
99                 return fwrite($this->getPointer(), $dataStream);
100         }
101
102         /**
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).
106          *
107          * @return      void
108          * @throws      UnsupportedOperationException   If this method is called
109          */
110         public function analyzeFile () {
111                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
112         }
113
114         /**
115          * Writes at given position by seeking to it.
116          *
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
121          */
122         public function writeAtPosition ($seedPosition, $data) {
123                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
124         }
125
126         /**
127          * Advances to next "block" of bytes
128          *
129          * @return      void
130          * @throws      UnsupportedOperationException   If this method is called
131          */
132         public function next () {
133                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
134         }
135
136         /**
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.
139          *
140          * @return      $isValid        Whether the next entry is valid
141          * @throws      UnsupportedOperationException   If this method is called
142          */
143         public function valid () {
144                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
145         }
146
147         /**
148          * Gets current seek position ("key").
149          *
150          * @return      $key    Current key in iteration
151          * @throws      UnsupportedOperationException   If this method is called
152          */
153         public function key () {
154                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
155         }
156
157 }