]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/output/raw/class_FrameworkRawFileOutputPointer.php
Rewritten:
[core.git] / framework / main / classes / file_directories / output / raw / class_FrameworkRawFileOutputPointer.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filesystem\Pointer\Output;
4
5 // Import framework stuff
6 use CoreFramework\FileSystem\BaseFileIo;
7 use CoreFramework\Filesystem\Pointer\OutputPointer;
8 use CoreFramework\Generic\NullPointerException;
9
10 // Import SPL stuff
11 use \SplFileInfo;
12
13 /**
14  * A class for writing files
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class FrameworkRawFileOutputPointer extends BaseFileIo implements OutputPointer {
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Create a file pointer based on the given file. The file will also
48          * be verified here.
49          *
50          * @param       $infoInstance   An instance of a SplFileInfo class
51          * @param       $mode           The output mode ('w', 'a' are valid)
52          * @throws      FileIsEmptyException    If the provided file name is empty.
53          * @throws      FileIoException                 If fopen() returns not a file resource
54          * @return      void
55          */
56         public static final function createFrameworkRawFileOutputPointer (SplFileInfo $infoInstance, $mode) {
57                 // Some pre-sanity checks...
58                 if (is_null($infoInstance)) {
59                         // No infoInstance given
60                         throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
61                 } // END - if
62
63                 // Try to open a handler
64                 $fileObject = $infoInstance->openFile($mode);
65                 if ((is_null($fileObject)) || ($fileObject === false)) {
66                         // Something bad happend
67                         throw new FileIoException ($infoInstance, self::EXCEPTION_FILE_POINTER_INVALID);
68                 } // END - if
69
70                 // Create new instance
71                 $pointerInstance = new FrameworkRawFileOutputPointer();
72
73                 // Set file pointer and file name
74                 $pointerInstance->setFileObject($fileObject);
75
76                 // Return the instance
77                 return $pointerInstance;
78         }
79
80         /**
81          * Write data to a file pointer
82          *
83          * @param       $dataStream             The data stream we shall write to the file
84          * @return      mixed                   Number of writes bytes or false on error
85          * @throws      NullPointerException    If the file pointer instance is not set by setPointer()
86          * @throws      LogicException  If there is no object being set
87          */
88         public function writeToFile ($dataStream) {
89                 if (is_null($this->getFileObject())) {
90                         // Pointer not initialized
91                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
92                 } elseif (!is_object($this->getFileObject())) {
93                         // Pointer is not a valid resource!
94                         throw new LogicException(sprintf('this->fileObject[]=%s is no object', gettype($this->getFileObject())));
95                 }
96
97                 // Write data to the file pointer and return written bytes
98                 return $this->getFileObject()->fwrite($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 }