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;
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct ($className) {
52 // Call parent constructor
53 parent::__construct($className);
57 * Checks whether the file header is initialized
59 * @return $isInitialized Whether the file header is initialized
61 private function isFileHeaderInitialized () {
62 // Default is not initialized
63 $isInitialized = FALSE;
65 // Is the file initialized?
66 if ($this->isFileInitialized()) {
67 // Some bytes has been written, so rewind to start of it.
68 $this->getIteratorInstance()->rewind();
71 $this->readFileHeader();
75 return $isInitialized;
79 * Checks whether the file-based stack has been initialized
81 * @return $isInitialized Whether the file's size is zero
83 private function isFileInitialized () {
84 // Default is not initialized
85 $isInitialized = FALSE;
87 // Get it from iterator which holds the pointer instance. If FALSE is returned
88 $fileSize = $this->getIteratorInstance()->size();
91 * The returned file size should not be FALSE or NULL as this means
92 * that the pointer class does not work correctly.
94 assert(is_int($fileSize));
96 // Is more than 0 returned?
98 // So is the header written?
99 $isInitialized = $this->getIteratorInstance()->isHeaderInitialized();
103 return $isInitialized;
107 * Initializes this file-based stack.
109 * @param $fileName File name of this stack
112 protected function initFileStack ($fileName) {
113 // Get a file i/o pointer instance
114 $pointerInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_output_class', array($fileName));
116 // Get iterator instance
117 $iteratorInstance = ObjectFactory::createObjectByConfiguredName('file_io_iterator_class', array($pointerInstance));
119 // Is the instance implementing the right interface?
120 assert($iteratorInstance instanceof SeekableWritableFileIterator);
123 $this->setIteratorInstance($iteratorInstance);
125 // Is the file's header initialized?
126 if ($this->isFileHeaderInitialized()) {
128 $this->loadFileHeader();
130 // No, then create it (which may pre-allocate the stack)
131 $this->createFileHeader();
136 * Initializes given stacker
138 * @param $stackerName Name of the stack
139 * @param $forceReInit Force re-initialization
141 * @throws AlreadyInitializedStackerException If the stack is already initialized
143 public function initStack ($stackerName, $forceReInit = FALSE) {
144 // Is the stack already initialized?
145 if (($forceReInit === FALSE) && ($this->isStackInitialized($stackerName))) {
146 // Then throw the exception
147 throw new AlreadyInitializedStackerException(array($this, $stackerName, $forceReInit), self::EXCEPTION_STACKER_ALREADY_INITIALIZED);
150 // Initialize the given stack
151 $this->partialStub('stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit));
155 * Checks whether the given stack is initialized (set in array $stackers)
157 * @param $stackerName Name of the stack
158 * @return $isInitialized Whether the stack is initialized
160 public function isStackInitialized ($stackerName) {
162 $this->partialStub('stackerName=' . $stackerName);
163 $isInitialized = TRUE;
166 return $isInitialized;
170 * Getter for size of given stack (array count)
172 * @param $stackerName Name of the stack
173 * @return $count Size of stack (array count)
174 * @throws NoStackerException If given stack is missing
176 public function getStackCount ($stackerName) {
177 // Is the stack not yet initialized?
178 if (!$this->isStackInitialized($stackerName)) {
179 // Throw an exception
180 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
183 // Now, count the array of entries
184 $this->partialStub('stackerName=' . $stackerName);
192 * Adds a value to given stack
194 * @param $stackerName Name of the stack
195 * @param $value Value to add to this stacker
197 * @throws FullStackerException Thrown if the stack is full
199 protected function addValue ($stackerName, $value) {
200 // Is the stack not yet initialized or full?
201 if (!$this->isStackInitialized($stackerName)) {
203 $this->initStack($stackerName);
204 } elseif ($this->isStackFull($stackerName)) {
206 throw new FullStackerException(array($this, $stackerName, $value), self::EXCEPTION_STACKER_IS_FULL);
209 // Now add the value to the stack
210 $this->partialStub('stackerName=' . $stackerName . ',value[]=' . gettype($value));
214 * Get last value from named stacker
216 * @param $stackerName Name of the stack
217 * @return $value Value of last added value
218 * @throws NoStackerException If the named stacker was not found
219 * @throws EmptyStackerException If the named stacker is empty
221 protected function getLastValue ($stackerName) {
222 // Is the stack not yet initialized or full?
223 if (!$this->isStackInitialized($stackerName)) {
224 // Throw an exception
225 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
226 } elseif ($this->isStackEmpty($stackerName)) {
227 // Throw an exception
228 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
231 // Now get the last value
232 $this->partialStub('stackerName=' . $stackerName);
240 * Get first value from named stacker
242 * @param $stackerName Name of the stack
243 * @return $value Value of last added value
244 * @throws NoStackerException If the named stacker was not found
245 * @throws EmptyStackerException If the named stacker is empty
247 protected function getFirstValue ($stackerName) {
248 // Is the stack not yet initialized or full?
249 if (!$this->isStackInitialized($stackerName)) {
250 // Throw an exception
251 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
252 } elseif ($this->isStackEmpty($stackerName)) {
253 // Throw an exception
254 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
257 // Now get the first value
258 $this->partialStub('stackerName=' . $stackerName);
266 * "Pops" last entry from stack
268 * @param $stackerName Name of the stack
269 * @return $value Value "poped" from array
270 * @throws NoStackerException If the named stacker was not found
271 * @throws EmptyStackerException If the named stacker is empty
273 protected function popLast ($stackerName) {
274 // Is the stack not yet initialized or full?
275 if (!$this->isStackInitialized($stackerName)) {
276 // Throw an exception
277 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
278 } elseif ($this->isStackEmpty($stackerName)) {
279 // Throw an exception
280 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
283 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
284 $this->partialStub('stackerName=' . $stackerName);
289 * "Pops" first entry from stack
291 * @param $stackerName Name of the stack
292 * @return $value Value "shifted" from array
293 * @throws NoStackerException If the named stacker was not found
294 * @throws EmptyStackerException If the named stacker is empty
296 protected function popFirst ($stackerName) {
297 // Is the stack not yet initialized or full?
298 if (!$this->isStackInitialized($stackerName)) {
299 // Throw an exception
300 throw new NoStackerException(array($this, $stackerName), self::EXCEPTION_NO_STACKER_FOUND);
301 } elseif ($this->isStackEmpty($stackerName)) {
302 // Throw an exception
303 throw new EmptyStackerException(array($this, $stackerName), self::EXCEPTION_STACKER_IS_EMPTY);
306 // Now, remove the last entry, we don't care about the return value here, see elseif() block above
307 $this->partialStub('stackerName=' . $stackerName);