Moved analyzeFile() to BaseFile where a much better place is (and duplicate
[core.git] / inc / classes / main / file_directories / class_BaseFile.php
1 <?php
2 /**
3  * A general file class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseFile extends BaseFrameworkSystem {
25         /**
26          * The current file we are working in
27          */
28         private $fileName = '';
29
30         /**
31          * Protected constructor
32          *
33          * @param       $className      Name of the class
34          * @return      void
35          */
36         protected function __construct ($className) {
37                 // Call parent constructor
38                 parent::__construct($className);
39
40                 // Init counters and gaps array
41                 $this->initCountersGapsArray();
42         }
43
44         /**
45          * Destructor for cleaning purposes, etc
46          *
47          * @return      void
48          */
49         public final function __destruct() {
50                 // Try to close a file
51                 $this->closeFile();
52
53                 // Call the parent destructor
54                 parent::__destruct();
55         }
56
57         /**
58          * Getter for the file pointer
59          *
60          * @return      $filePointer    The file pointer which shall be a valid file resource
61          * @throws      UnsupportedOperationException   If this method is called
62          */
63         public final function getPointer () {
64                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
65         }
66
67         /**
68          * Setter for file name
69          *
70          * @param       $fileName       The new file name
71          * @return      void
72          */
73         protected final function setFileName ($fileName) {
74                 $fileName = (string) $fileName;
75                 $this->fileName = $fileName;
76         }
77
78         /**
79          * Getter for file name
80          *
81          * @return      $fileName       The current file name
82          */
83         public final function getFileName () {
84                 return $this->fileName;
85         }
86
87         /**
88          * Initializes this file class
89          *
90          * @param       $fileName       Name of this abstract file
91          * @return      void
92          */
93         protected function initFile ($fileName) {
94                 // Get a file i/o pointer instance
95                 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
96
97                 // ... and set it here
98                 $this->setPointerInstance($pointerInstance);
99         }
100
101         /**
102          * Close a file source and set it's instance to null and the file name
103          * to empty
104          *
105          * @return      void
106          * @todo        ~10% done?
107          */
108         public function closeFile () {
109                 $this->partialStub('Unfinished method.');
110
111                 // Remove file name
112                 $this->setFileName('');
113         }
114
115         /**
116          * Determines seek position
117          *
118          * @return      $seekPosition   Current seek position
119          */
120         public function determineSeekPosition () {
121                 return $this->getPointerInstance()->determineSeekPosition();
122         }
123
124         /**
125          * Seek to given offset (default) or other possibilities as fseek() gives.
126          *
127          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
128          * @param       $whence         Added to offset (default: only use offset to seek to)
129          * @return      $status         Status of file seek: 0 = success, -1 = failed
130          */
131         public function seek ($offset, $whence = SEEK_SET) {
132                 return $this->getPointerInstance()->seek($offset, $whence);
133         }
134
135         /**
136          * Size of this file
137          *
138          * @return      $size   Size (in bytes) of file
139          * @todo        Handle seekStatus
140          */
141         public function size () {
142                 return $this->getPointerInstance()->size();
143         }
144
145         /**
146          * Read data a file pointer
147          *
148          * @return      mixed   The result of fread()
149          * @throws      NullPointerException    If the file pointer instance
150          *                                                                      is not set by setPointer()
151          * @throws      InvalidResourceException        If there is being set
152          */
153         public function readFromFile () {
154                 return $this->getPointerInstance()->readFromFile();
155         }
156
157         /**
158          * Reads given amount of bytes from file.
159          *
160          * @param       $bytes  Amount of bytes to read
161          * @return      $data   Data read from file
162          */
163         public function read ($bytes) {
164                 return $this->getPointerInstance()->read($bytes);
165         }
166
167         /**
168          * Write data to a file pointer
169          *
170          * @param       $dataStream             The data stream we shall write to the file
171          * @return      mixed                   Number of writes bytes or FALSE on error
172          * @throws      NullPointerException    If the file pointer instance
173          *                                                                      is not set by setPointer()
174          * @throws      InvalidResourceException        If there is being set
175          *                                                                                      an invalid file resource
176          */
177         public function writeToFile ($dataStream) {
178                 return $this->getPointerInstance()->writeToFile($dataStream);
179         }
180
181         /**
182          * Rewinds to the beginning of the file
183          *
184          * @return      $status         Status of this operation
185          */
186         public function rewind () {
187                 return $this->getPointerInstance()->rewind();
188         }
189
190         /**
191          * Determines whether the EOF has been reached
192          *
193          * @return      $isEndOfFileReached             Whether the EOF has been reached
194          */
195         public final function isEndOfFileReached () {
196                 return $this->getPointerInstance()->isEndOfFileReached();
197         }
198
199         /**
200          * Analyzes entries in index file. This will count all found (and valid)
201          * entries, mark invalid as damaged and count gaps ("fragmentation"). If
202          * only gaps are found, the file is considered as "virgin" (no entries).
203          *
204          * @return      void
205          */
206         public function analyzeFile () {
207                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
208
209                 // Make sure the file is initialized
210                 assert($this->isFileInitialized());
211
212                 // Init counters and gaps array
213                 $this->initCountersGapsArray();
214
215                 // Output message (as this may take some time)
216                 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
217
218                 // First rewind to the begining
219                 $this->rewind();
220
221                 // Then try to load all entries
222                 while ($this->valid()) {
223                         // Go to next entry
224                         $this->next();
225
226                         // Get current entry
227                         $current = $this->current();
228
229                         // Simply output it
230                         self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE)));
231                 } // END - while
232
233                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
234         }
235 }
236
237 // [EOF]
238 ?>