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