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 * Protected constructor
58 * @param $className Name of the class
61 protected function __construct ($className) {
62 // Call parent constructor
63 parent::__construct($className);
67 * Checks whether the file header is initialized
69 * @return $isInitialized Whether the file header is initialized
71 private function isFileHeaderInitialized () {
72 // Default is not initialized
73 $isInitialized = FALSE;
75 // Is the file initialized?
76 if ($this->isFileInitialized()) {
77 // Some bytes has been written, so rewind to start of it.
78 $this->getIteratorInstance()->rewind();
81 $this->readFileHeader();
85 return $isInitialized;
89 * Checks whether the file-based stack has been initialized
91 * @return $isInitialized Whether the file's size is zero
93 private function isFileInitialized () {
94 // Default is not initialized
95 $isInitialized = FALSE;
97 // Get it from iterator which holds the pointer instance. If FALSE is returned
98 $fileSize = $this->getIteratorInstance()->size();
101 * The returned file size should not be FALSE or NULL as this means
102 * that the pointer class does not work correctly.
104 assert(is_int($fileSize));
106 // Is more than 0 returned?
108 // So is the header written?
109 $isInitialized = $this->getIteratorInstance()->isHeaderInitialized();
113 return $isInitialized;
117 * Creates the file-stack's header
121 private function createFileHeader () {
122 // The file's header should not be initialized here
123 assert(!$this->isFileHeaderInitialized());
126 $this->getIteratorInstance()->initCounter();
129 $this->flushFileHeader();
133 * Flushes the file header
137 private function flushFileHeader () {
138 // Put all informations together
139 $header = sprintf('%s%s%s%s%s',
143 // Separator magic<->count
144 chr(self::SEPARATOR_MAGIC_COUNT),
146 // Total entries (will be zero) and pad it to 20 chars
147 str_pad($this->dec2hex($this->getIteratorInstance()->getCount()), self::COUNT_LENGTH, '0', STR_PAD_LEFT),
149 // Position (will be zero)
150 str_pad($this->dec2hex(0, 2), self::COUNT_POSITION, '0', STR_PAD_LEFT),
152 // Separator position<->entries
153 chr(self::SEPARATOR_SEEK_POS_ENTRIES)
157 $this->getIteratorInstance()->writeAtPosition(0, $header);
161 * Initializes this file-based stack.
163 * @param $fileName File name of this stack
166 protected function initFileStack ($fileName) {
167 // Get a file i/o pointer instance
168 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
170 // Get iterator instance
171 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
173 // Is the instance implementing the right interface?
174 assert($iteratorInstance instanceof SeekableWritableFileIterator);
177 $this->setIteratorInstance($iteratorInstance);
179 // Is the file's header initialized?
180 if ($this->isFileHeaderInitialized()) {
182 $this->loadFileHeader();
184 // No, then create it (which may pre-allocate the stack)
185 $this->createFileHeader();
187 // And pre-allocate a bit
188 $this->preAllocateFile();
193 * Initializes given stacker
195 * @param $stackerName Name of the stack
196 * @param $forceReInit Force re-initialization
198 * @throws AlreadyInitializedStackerException If the stack is already initialized
200 public function initStack ($stackerName, $forceReInit = FALSE) {
201 // Is the stack already initialized?
202 if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) {
203 // Then throw the exception
204 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
207 // Initialize the given stack
208 $this->partialStub('stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit));
212 * Checks whether the given stack is initialized (set in array $stackers)
214 * @param $stackerName Name of the stack
215 * @return $isInitialized Whether the stack is initialized
217 public function isStackInitialized ($stackerName) {
219 $this->partialStub('stackerName=' . $stackerName);
220 $isInitialized = TRUE;
223 return $isInitialized;
227 * Getter for size of given stack (array count)
229 * @param $stackerName Name of the stack
230 * @return $count Size of stack (array count)
231 * @throws NoStackerException If given stack is missing
233 public function getStackCount ($stackerName) {
234 // Is the stack not yet initialized?
235 if (!$this->isStackInitialized($stackerName)) {
236 // Throw an exception
237 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
240 // Now, count the array of entries
241 $this->partialStub('stackerName=' . $stackerName);
249 * Adds a value to given stack
251 * @param $stackerName Name of the stack
252 * @param $value Value to add to this stacker
254 * @throws FullStackerException Thrown if the stack is full
256 protected function addValue ($stackerName, $value) {
257 // Is the stack not yet initialized or full?
258 if (!$this->isStackInitialized($stackerName)) {
260 $this->initStack($stackerName);
261 } elseif ($this->isStackFull($stackerName)) {
263 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
266 // Now add the value to the stack
267 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
271 * Get last value from named stacker
273 * @param $stackerName Name of the stack
274 * @return $value Value of last added value
275 * @throws NoStackerException If the named stacker was not found
276 * @throws EmptyStackerException If the named stacker is empty
278 protected function getLastValue ($stackerName) {
279 // Is the stack not yet initialized or full?
280 if (!$this->isStackInitialized($stackerName)) {
281 // Throw an exception
282 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
283 } elseif ($this->isStackEmpty($stackerName)) {
284 // Throw an exception
285 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
288 // Now get the last value
289 $this->partialStub('stackerName=' . $stackerName);
297 * Get first value from named stacker
299 * @param $stackerName Name of the stack
300 * @return $value Value of last added value
301 * @throws NoStackerException If the named stacker was not found
302 * @throws EmptyStackerException If the named stacker is empty
304 protected function getFirstValue ($stackerName) {
305 // Is the stack not yet initialized or full?
306 if (!$this->isStackInitialized($stackerName)) {
307 // Throw an exception
308 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
309 } elseif ($this->isStackEmpty($stackerName)) {
310 // Throw an exception
311 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
314 // Now get the first value
315 $this->partialStub('stackerName=' . $stackerName);
323 * "Pops" last entry from stack
325 * @param $stackerName Name of the stack
326 * @return $value Value "poped" from array
327 * @throws NoStackerException If the named stacker was not found
328 * @throws EmptyStackerException If the named stacker is empty
330 protected function popLast ($stackerName) {
331 // Is the stack not yet initialized or full?
332 if (!$this->isStackInitialized($stackerName)) {
333 // Throw an exception
334 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
335 } elseif ($this->isStackEmpty($stackerName)) {
336 // Throw an exception
337 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
340 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
341 $this->partialStub('stackerName=' . $stackerName);
346 * "Pops" first entry from stack
348 * @param $stackerName Name of the stack
349 * @return $value Value "shifted" from array
350 * @throws NoStackerException If the named stacker was not found
351 * @throws EmptyStackerException If the named stacker is empty
353 protected function popFirst ($stackerName) {
354 // Is the stack not yet initialized or full?
355 if (!$this->isStackInitialized($stackerName)) {
356 // Throw an exception
357 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
358 } elseif ($this->isStackEmpty($stackerName)) {
359 // Throw an exception
360 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
363 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
364 $this->partialStub('stackerName=' . $stackerName);