5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class BaseFile extends BaseFrameworkSystem {
26 * The current file we are working in
28 private $fileName = '';
31 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
40 // Init counters and gaps array
41 $this->initCountersGapsArray();
45 * Destructor for cleaning purposes, etc
49 public final function __destruct() {
50 // Try to close a file
53 // Call the parent destructor
58 * Getter for the file pointer
60 * @return $filePointer The file pointer which shall be a valid file resource
61 * @throws UnsupportedOperationException If this method is called
63 public final function getPointer () {
64 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
68 * Setter for file name
70 * @param $fileName The new file name
73 protected final function setFileName ($fileName) {
74 $fileName = (string) $fileName;
75 $this->fileName = $fileName;
79 * Getter for file name
81 * @return $fileName The current file name
83 public final function getFileName () {
84 return $this->fileName;
88 * Initializes this file class
90 * @param $fileName Name of this abstract file
93 protected function initFile ($fileName) {
94 // Get a file i/o pointer instance
95 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
97 // ... and set it here
98 $this->setPointerInstance($pointerInstance);
102 * Close a file source and set it's instance to null and the file name
108 public function closeFile () {
109 $this->partialStub('Unfinished method.');
112 $this->setFileName('');
116 * Determines seek position
118 * @return $seekPosition Current seek position
120 public function determineSeekPosition () {
121 return $this->getPointerInstance()->determineSeekPosition();
125 * Seek to given offset (default) or other possibilities as fseek() gives.
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
131 public function seek ($offset, $whence = SEEK_SET) {
132 return $this->getPointerInstance()->seek($offset, $whence);
138 * @return $size Size (in bytes) of file
139 * @todo Handle seekStatus
141 public function size () {
142 return $this->getPointerInstance()->size();
146 * Read data a file pointer
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
153 public function readFromFile () {
154 return $this->getPointerInstance()->readFromFile();
158 * Reads given amount of bytes from file.
160 * @param $bytes Amount of bytes to read
161 * @return $data Data read from file
163 public function read ($bytes) {
164 return $this->getPointerInstance()->read($bytes);
168 * Write data to a file pointer
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
177 public function writeToFile ($dataStream) {
178 return $this->getPointerInstance()->writeToFile($dataStream);
182 * Rewinds to the beginning of the file
184 * @return $status Status of this operation
186 public function rewind () {
187 return $this->getPointerInstance()->rewind();
191 * Determines whether the EOF has been reached
193 * @return $isEndOfFileReached Whether the EOF has been reached
195 public final function isEndOfFileReached () {
196 return $this->getPointerInstance()->isEndOfFileReached();
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).
206 public function analyzeFile () {
207 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
209 // Make sure the file is initialized
210 assert($this->isFileInitialized());
212 // Init counters and gaps array
213 $this->initCountersGapsArray();
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__));
218 // First rewind to the begining
221 // Then try to load all entries
222 while ($this->valid()) {
227 $current = $this->current();
230 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE)));
233 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));