Rewrite continued:
[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
10 /**
11  * A class for writing files
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.shipsimu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class FrameworkTextFileOutputPointer extends BaseFileIo implements OutputPointer {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Create a file pointer based on the given file. The file will also
45          * be verified here.
46          *
47          * @param       $fileName       The file name we shall pass to fopen()
48          * @param       $mode           The output mode ('w', 'a' are valid)
49          * @throws      FileIsEmptyException    If the provided file name is empty.
50          * @throws      FileIoException                 If fopen() returns not a file resource
51          * @return      void
52          */
53         public static final function createFrameworkTextFileOutputPointer ($fileName, $mode) {
54                 // Some pre-sanity checks...
55                 if (is_null($fileName)) {
56                         // No filename given
57                         throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
58                 } // END - if
59
60                 // Try to open a handler
61                 $filePointer = @fopen($fileName, $mode);
62                 if ((is_null($filePointer)) || ($filePointer === false)) {
63                         // Something bad happend
64                         throw new FileIoException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
65                 } // END - if
66
67                 // Create new instance
68                 $pointerInstance = new FrameworkTextFileOutputPointer();
69
70                 // Set file pointer and file name
71                 $pointerInstance->setPointer($filePointer);
72                 $pointerInstance->setFileName($fileName);
73
74                 // Return the instance
75                 return $pointerInstance;
76         }
77
78         /**
79          * Write data to a file pointer
80          *
81          * @param       $dataStream             The data stream we shall write to the file
82          * @return      mixed                   Number of writes bytes or false on error
83          * @throws      NullPointerException    If the file pointer instance
84          *                                                                      is not set by setPointer()
85          * @throws      InvalidResourceException        If there is being set
86          *                                                                                      an invalid file resource
87          */
88         public function writeToFile ($dataStream) {
89                 if (is_null($this->getPointer())) {
90                         // Pointer not initialized
91                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
92                 } elseif (!is_resource($this->getPointer())) {
93                         // Pointer is not a valid resource!
94                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
95                 }
96
97                 // Write data to the file pointer and return written bytes
98                 return fwrite($this->getPointer(), $dataStream);
99         }
100
101         /**
102          * Analyzes entries in index file. This will count all found (and valid)
103          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
104          * only gaps are found, the file is considered as "virgin" (no entries).
105          *
106          * @return      void
107          * @throws      UnsupportedOperationException   If this method is called
108          */
109         public function analyzeFile () {
110                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
111         }
112
113         /**
114          * Writes at given position by seeking to it.
115          *
116          * @param       $seekPosition   Seek position in file
117          * @param       $data                   Data to be written
118          * @return      mixed                   Number of writes bytes or false on error
119          * @throws      UnsupportedOperationException   If this method is called
120          */
121         public function writeAtPosition ($seedPosition, $data) {
122                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
123         }
124
125         /**
126          * Advances to next "block" of bytes
127          *
128          * @return      void
129          * @throws      UnsupportedOperationException   If this method is called
130          */
131         public function next () {
132                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
133         }
134
135         /**
136          * Checks wether the current entry is valid (not at the end of the file).
137          * This method will return true if an emptied (nulled) entry has been found.
138          *
139          * @return      $isValid        Whether the next entry is valid
140          * @throws      UnsupportedOperationException   If this method is called
141          */
142         public function valid () {
143                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
144         }
145
146         /**
147          * Gets current seek position ("key").
148          *
149          * @return      $key    Current key in iteration
150          * @throws      UnsupportedOperationException   If this method is called
151          */
152         public function key () {
153                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
154         }
155
156 }