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 = '';
33 private $backBuffer = '';
36 * Currently loaded block (will be returned by current())
38 private $currentBlock = '';
41 * Protected constructor
43 * @param $className Name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
50 // Init counters and gaps array
51 $this->initCountersGapsArray();
55 * Destructor for cleaning purposes, etc
59 public final function __destruct() {
60 // Try to close a file
63 // Call the parent destructor
68 * Getter for the file pointer
70 * @return $filePointer The file pointer which shall be a valid file resource
71 * @throws UnsupportedOperationException If this method is called
73 public final function getPointer () {
74 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
78 * Setter for file name
80 * @param $fileName The new file name
83 protected final function setFileName ($fileName) {
84 $fileName = (string) $fileName;
85 $this->fileName = $fileName;
89 * Getter for file name
91 * @return $fileName The current file name
93 public final function getFileName () {
94 return $this->fileName;
98 * Initializes the back-buffer by setting it to an empty string.
102 private function initBackBuffer () {
103 // Simply call the setter
104 $this->setBackBuffer('');
108 * Setter for backBuffer field
110 * @param $backBuffer Characters to "store" in back-buffer
113 private function setBackBuffer ($backBuffer) {
114 // Cast to string (so no arrays or objects)
115 $backBuffer = (string) $backBuffer;
118 $this->backBuffer = $backBuffer;
122 * Getter for backBuffer field
124 * @return $backBuffer Characters "stored" in back-buffer
126 private function getBackBuffer () {
127 return $this->backBuffer;
131 * Setter for currentBlock field
133 * @param $currentBlock Characters to set a currently loaded block
136 private function setCurrentBlock ($currentBlock) {
137 // Cast to string (so no arrays or objects)
138 $currentBlock = (string) $currentBlock;
141 $this->currentBlock = $currentBlock;
145 * Gets currently read data
147 * @return $current Currently read data
149 public function getCurrentBlock () {
151 return $this->currentBlock;
155 * Initializes this file class
157 * @param $fileName Name of this abstract file
160 protected function initFile ($fileName) {
161 // Get a file i/o pointer instance
162 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
164 // ... and set it here
165 $this->setPointerInstance($pointerInstance);
169 * Close a file source and set it's instance to null and the file name
175 public function closeFile () {
176 $this->partialStub('Unfinished method.');
179 $this->setFileName('');
183 * Determines seek position
185 * @return $seekPosition Current seek position
187 public function determineSeekPosition () {
188 // Call pointer instance
189 return $this->getPointerInstance()->determineSeekPosition();
193 * Seek to given offset (default) or other possibilities as fseek() gives.
195 * @param $offset Offset to seek to (or used as "base" for other seeks)
196 * @param $whence Added to offset (default: only use offset to seek to)
197 * @return $status Status of file seek: 0 = success, -1 = failed
199 public function seek ($offset, $whence = SEEK_SET) {
200 // Call pointer instance
201 return $this->getPointerInstance()->seek($offset, $whence);
207 * @return $size Size (in bytes) of file
208 * @todo Handle seekStatus
210 public function size () {
211 // Call pointer instance
212 return $this->getPointerInstance()->size();
216 * Read data a file pointer
218 * @return mixed The result of fread()
219 * @throws NullPointerException If the file pointer instance
220 * is not set by setPointer()
221 * @throws InvalidResourceException If there is being set
223 public function readFromFile () {
224 // Call pointer instance
225 return $this->getPointerInstance()->readFromFile();
229 * Reads given amount of bytes from file.
231 * @param $bytes Amount of bytes to read
232 * @return $data Data read from file
234 public function read ($bytes) {
235 // Call pointer instance
236 return $this->getPointerInstance()->read($bytes);
240 * Write data to a file pointer
242 * @param $dataStream The data stream we shall write to the file
243 * @return mixed Number of writes bytes or FALSE on error
244 * @throws NullPointerException If the file pointer instance
245 * is not set by setPointer()
246 * @throws InvalidResourceException If there is being set
247 * an invalid file resource
249 public function writeToFile ($dataStream) {
250 // Call pointer instance
251 return $this->getPointerInstance()->writeToFile($dataStream);
255 * Rewinds to the beginning of the file
257 * @return $status Status of this operation
259 public function rewind () {
260 // Call pointer instance
261 return $this->getPointerInstance()->rewind();
265 * Determines whether the EOF has been reached
267 * @return $isEndOfFileReached Whether the EOF has been reached
269 public final function isEndOfFileReached () {
270 // Call pointer instance
271 return $this->getPointerInstance()->isEndOfFileReached();
275 * Analyzes entries in index file. This will count all found (and valid)
276 * entries, mark invalid as damaged and count gaps ("fragmentation"). If
277 * only gaps are found, the file is considered as "virgin" (no entries).
281 public function analyzeFile () {
282 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] CALLED!', __METHOD__, __LINE__));
284 // Make sure the file is initialized
285 assert($this->isFileInitialized());
287 // Init counters and gaps array
288 $this->initCountersGapsArray();
290 // Output message (as this may take some time)
291 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] Analyzing file structure ... (this may take some time)', __METHOD__, __LINE__));
293 // First rewind to the begining
296 // Then try to load all entries
297 while ($this->valid()) {
302 $current = $this->getCurrentBlock();
305 self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] current=%s', __METHOD__, __LINE__, print_r($current, TRUE)));
308 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:%d:] EXIT!', __METHOD__, __LINE__));
312 * Advances to next "block" of bytes
315 * @todo This method will load large but empty files in a whole
317 public function next () {
318 // Is there nothing to read?
319 if (!$this->valid()) {
324 // Make sure the block instance is set
325 assert($this->getBlockInstance() instanceof CalculatableBlock);
327 // First calculate minimum block length
328 $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
330 // Short be more than zero!
333 // Wait until a entry/block separator has been found
334 $data = $this->getBackBuffer();
335 while ((!$this->isEndOfFileReached()) && (!$this->getBlockInstance()->isBlockSeparatorFound($data))) {
336 // Then read the block
337 $data .= $this->read($length);
338 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('data()=' . strlen($data));
342 if ($this->isEndOfFileReached()) {
343 // Set whole data as current block
344 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('Calling setCurrentBlock(' . strlen($data) . ') ...');
345 $this->setCurrentBlock($data);
347 // Then abort here silently
348 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('EOF reached.');
353 * Init back-buffer which is the data that has been found beyond the
356 $this->initBackBuffer();
359 $dataArray = explode(self::getBlockSeparator(), $data);
361 // This array must contain two elements
362 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('dataArray=' . print_r($dataArray, TRUE));
363 assert(count($dataArray) == 2);
365 // Left part is the actual block, right one the back-buffer data
366 $this->setCurrentBlock($dataArray[0]);
367 $this->setBackBuffer($dataArray[1]);
371 * Checks wether the current entry is valid (not at the end of the file).
372 * This method will return TRUE if an emptied (nulled) entry has been found.
374 * @return $isValid Whether the next entry is valid
376 public function valid () {
377 // Make sure the block instance is set
378 assert($this->getBlockInstance() instanceof CalculatableBlock);
380 // First calculate minimum block length
381 $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
383 // Short be more than zero!
386 // Get current seek position
387 $seekPosition = $this->key();
389 // Then try to read it
390 $data = $this->read($length);
392 // If some bytes could be read, all is fine
393 $isValid = ((is_string($data)) && (strlen($data) > 0));
396 $headerSize = $this->getBlockInstance()->getHeaderSize();
398 // Is the seek position at or beyond the header?
399 if ($seekPosition >= $headerSize) {
400 // Seek back to old position
401 $this->seek($seekPosition);
403 // Seek directly behind the header
404 $this->seek($headerSize);
412 * Gets current seek position ("key").
414 * @return $key Current key in iteration
416 public function key () {
417 // Call pointer instance
418 return $this->getPointerInstance()->determineSeekPosition();