3 * A general file-based stack class
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 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 BaseFileStack extends BaseStacker {
26 * Magic for this stack
28 const STACK_MAGIC = 'STACKv0.1';
31 * Separator magic->count
33 const SEPARATOR_MAGIC_COUNT = 0x00;
36 * Separator position->entries
38 const SEPARATOR_SEEK_POS_ENTRIES = 0xff;
41 * Separator hash->name
43 const SEPARATOR_HASH_NAME = 0x05;
48 const COUNT_LENGTH = 20;
53 const COUNT_POSITION = 20;
56 * Counter for total entries
58 private $totalEntries = 0;
61 * Current seek position
63 private $seekPosition = 0;
66 * Protected constructor
68 * @param $className Name of the class
71 protected function __construct ($className) {
72 // Call parent constructor
73 parent::__construct($className);
77 * Getter for total entries
79 * @return $totalEntries Total entries in this stack
81 private function getCounter () {
83 return $this->totalEntries;
91 private function incrementCounter () {
93 $this->totalEntries++;
97 * Getter for seek position
99 * @return $seekPosition Current seek position (stored here in object)
101 private function getSeekPosition () {
103 return $this->seekPosition;
107 * Setter for seek position
109 * @param $seekPosition Current seek position (stored here in object)
112 private function setSeekPosition ($seekPosition) {
114 $this->seekPosition = $seekPosition;
118 * Updates seekPosition attribute from file to avoid to much access on file.
122 private function updateSeekPosition () {
123 // Get key (= seek position)
124 $seekPosition = $this->getIteratorInstance()->key();
127 $this->setSeekPosition($seekPosition);
131 * Checks whether the file header is initialized
133 * @return $isInitialized Whether the file header is initialized
135 private function isFileHeaderInitialized () {
136 // Default is not initialized
137 $isInitialized = FALSE;
139 // Is the file initialized?
140 if ($this->isFileInitialized()) {
141 // Some bytes has been written, so rewind to start of it.
142 $this->getIteratorInstance()->rewind();
145 $this->readFileHeader();
149 return $isInitialized;
153 * Checks whether the file-based stack has been initialized
155 * @return $isInitialized Whether the file's size is zero
157 private function isFileInitialized () {
158 // Get it from iterator which holds the pointer instance. If FALSE is returned
159 $fileSize = $this->getIteratorInstance()->size();
162 * The returned file size should not be FALSE or NULL as this means
163 * that the pointer class does not work correctly.
165 assert(is_int($fileSize));
167 // Is more than 0 returned?
168 $isInitialized = ($fileSize > 0);
171 return $isInitialized;
175 * Creates the file-stack's header
179 private function createFileHeader () {
180 // The file's header should not be initialized here
181 assert(!$this->isFileHeaderInitialized());
184 $this->flushFileHeader();
188 * Flushes the file header
192 private function flushFileHeader () {
193 // Put all informations together
194 $header = sprintf('%s%s%s%s%s',
198 // Separator magic<->count
199 chr(self::SEPARATOR_MAGIC_COUNT),
201 // Total entries (will be zero) and pad it to 20 chars
202 str_pad($this->dec2hex($this->getCounter()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
204 // Position (will be zero)
205 str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
207 // Separator position<->entries
208 chr(self::SEPARATOR_SEEK_POS_ENTRIES)
211 // Write it to disk (header is always at seek position 0)
212 $this->getIteratorInstance()->writeAtPosition(0, $header);
214 // Update seek position
215 $this->updateSeekPosition();
219 * Initializes this file-based stack.
221 * @param $fileName File name of this stack
224 protected function initFileStack ($fileName) {
225 // Get a file i/o pointer instance
226 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
228 // Get iterator instance
229 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
231 // Is the instance implementing the right interface?
232 assert($iteratorInstance instanceof SeekableWritableFileIterator);
235 $this->setIteratorInstance($iteratorInstance);
237 // Is the file's header initialized?
238 if ($this->isFileHeaderInitialized()) {
240 $this->loadFileHeader();
242 // No, then create it (which may pre-allocate the stack)
243 $this->createFileHeader();
245 // And pre-allocate a bit
246 $this->preAllocateFile();
251 * Initializes given stacker
253 * @param $stackerName Name of the stack
254 * @param $forceReInit Force re-initialization
256 * @throws AlreadyInitializedStackerException If the stack is already initialized
258 public function initStack ($stackerName, $forceReInit = FALSE) {
259 // Is the stack already initialized?
260 if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) {
261 // Then throw the exception
262 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
265 // Initialize the given stack
266 $this->partialStub('stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit));
270 * Checks whether the given stack is initialized (set in array $stackers)
272 * @param $stackerName Name of the stack
273 * @return $isInitialized Whether the stack is initialized
275 public function isStackInitialized ($stackerName) {
277 $this->partialStub('stackerName=' . $stackerName);
278 $isInitialized = TRUE;
281 return $isInitialized;
285 * Getter for size of given stack (array count)
287 * @param $stackerName Name of the stack
288 * @return $count Size of stack (array count)
289 * @throws NoStackerException If given stack is missing
291 public function getStackCount ($stackerName) {
292 // Is the stack not yet initialized?
293 if (!$this->isStackInitialized($stackerName)) {
294 // Throw an exception
295 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
298 // Now, count the array of entries
299 $this->partialStub('stackerName=' . $stackerName);
307 * Adds a value to given stack
309 * @param $stackerName Name of the stack
310 * @param $value Value to add to this stacker
312 * @throws FullStackerException Thrown if the stack is full
314 protected function addValue ($stackerName, $value) {
315 // Is the stack not yet initialized or full?
316 if (!$this->isStackInitialized($stackerName)) {
318 $this->initStack($stackerName);
319 } elseif ($this->isStackFull($stackerName)) {
321 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
324 // Now add the value to the stack
325 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
329 * Get last value from named stacker
331 * @param $stackerName Name of the stack
332 * @return $value Value of last added value
333 * @throws NoStackerException If the named stacker was not found
334 * @throws EmptyStackerException If the named stacker is empty
336 protected function getLastValue ($stackerName) {
337 // Is the stack not yet initialized or full?
338 if (!$this->isStackInitialized($stackerName)) {
339 // Throw an exception
340 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
341 } elseif ($this->isStackEmpty($stackerName)) {
342 // Throw an exception
343 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
346 // Now get the last value
347 $this->partialStub('stackerName=' . $stackerName);
355 * Get first value from named stacker
357 * @param $stackerName Name of the stack
358 * @return $value Value of last added value
359 * @throws NoStackerException If the named stacker was not found
360 * @throws EmptyStackerException If the named stacker is empty
362 protected function getFirstValue ($stackerName) {
363 // Is the stack not yet initialized or full?
364 if (!$this->isStackInitialized($stackerName)) {
365 // Throw an exception
366 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
367 } elseif ($this->isStackEmpty($stackerName)) {
368 // Throw an exception
369 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
372 // Now get the first value
373 $this->partialStub('stackerName=' . $stackerName);
381 * "Pops" last entry from stack
383 * @param $stackerName Name of the stack
384 * @return $value Value "poped" from array
385 * @throws NoStackerException If the named stacker was not found
386 * @throws EmptyStackerException If the named stacker is empty
388 protected function popLast ($stackerName) {
389 // Is the stack not yet initialized or full?
390 if (!$this->isStackInitialized($stackerName)) {
391 // Throw an exception
392 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
393 } elseif ($this->isStackEmpty($stackerName)) {
394 // Throw an exception
395 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
398 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
399 $this->partialStub('stackerName=' . $stackerName);
404 * "Pops" first entry from stack
406 * @param $stackerName Name of the stack
407 * @return $value Value "shifted" from array
408 * @throws NoStackerException If the named stacker was not found
409 * @throws EmptyStackerException If the named stacker is empty
411 protected function popFirst ($stackerName) {
412 // Is the stack not yet initialized or full?
413 if (!$this->isStackInitialized($stackerName)) {
414 // Throw an exception
415 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
416 } elseif ($this->isStackEmpty($stackerName)) {
417 // Throw an exception
418 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
421 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
422 $this->partialStub('stackerName=' . $stackerName);