Introduced CalculatableBlock + basic implementation for valid().
[core.git] / inc / classes / main / iterator / io / class_FileIoIterator.php
1 <?php
2 /**
3  * A file i/o iterator
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class FileIoIterator extends BaseIterator implements SeekableWritableFileIterator {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Creates an instance of this class
37          *
38          * @param       $pointerInstance        An instance of a InputOutputPointer class
39          * @param       $blockInstance          An instance of a CalculatableBlock class
40          * @return      $iteratorInstance       An instance of a Iterator class
41          */
42         public final static function createFileIoIterator (InputOutputPointer $pointerInstance, CalculatableBlock $blockInstance) {
43                 // Get new instance
44                 $iteratorInstance = new FileIoIterator();
45
46                 // Set the instance here
47                 $iteratorInstance->setPointerInstance($pointerInstance);
48
49                 // Set the block instance here
50                 $iteratorInstance->setBlockInstance($blockInstance);
51
52                 // Return the prepared instance
53                 return $iteratorInstance;
54         }
55
56         /**
57          * Gets currently read data
58          *
59          * @return      $current        Currently read data
60          */
61         public function current () {
62                 // Default is null
63                 $current = null;
64
65                 $this->partialStub('Please implement this method.');
66
67                 // Return it
68                 return $current;
69         }
70
71         /**
72          * Gets current seek position ("key").
73          *
74          * @return      $key    Current key in iteration
75          */
76         public function key () {
77                 // Return it
78                 return $this->getPointerInstance()->determineSeekPosition();
79         }
80
81         /**
82          * Advances to next "block" of bytes
83          *
84          * @return      void
85          */
86         public function next () {
87                 $this->partialStub('Please implement this method.');
88         }
89
90         /**
91          * Rewinds to the beginning of the file
92          *
93          * @return      $status         Status of this operation
94          */
95         public function rewind () {
96                 // Call pointer instance
97                 return $this->getPointerInstance()->rewind();
98         }
99
100         /**
101          * Checks wether the current entry is valid (not at the end of the file).
102          * This method will return TRUE if an emptied (nulled) entry has been found.
103          *
104          * @return      $isValid        Whether the next entry is valid
105          */
106         public function valid () {
107                 // First calculate minimum block length
108                 $length = $this->getBlockInstance()->caluclateMinimumBlockLength();
109
110                 // Short be more than zero!
111                 assert($length > 0);
112
113                 // Get current seek position
114                 $seekPosition = $this->key();
115
116                 // Then try to read it
117                 $data = $this->read($length);
118
119                 // If some bytes could be read, all is fine
120                 $isValid = ((is_string($data)) && (strlen($data) > 0));
121
122                 // Seek back to old position
123                 $this->seek($seekPosition);
124
125                 // Return result
126                 return $isValid;
127         }
128
129         /**
130          * Seeks to given position
131          *
132          * @param       $seekPosition   Seek position in file
133          * @return      $status                 Status of this operation
134          */
135         public function seek ($seekPosition) {
136                 // Call pointer instance
137                 return $this->getPointerInstance()->seek($seekPosition);
138         }
139
140         /**
141          * Writes at given position by seeking to it.
142          *
143          * @param       $seekPosition   Seek position in file
144          * @param       $data                   Data to be written
145          * @return      void
146          */
147         public function writeAtPosition ($seekPosition, $data) {
148                 // First seek to it
149                 $this->seek($seekPosition);
150
151                 // Then write the data at that position
152                 $this->getPointerInstance()->writeToFile($data);
153         }
154
155         /**
156          * Size of file stack
157          *
158          * @return      $size   Size (in bytes) of file
159          */
160         public function size () {
161                 // Call the pointer object
162                 $size = $this->getPointerInstance()->size();
163
164                 // Return result
165                 return $size;
166         }
167
168         /**
169          * Reads given amount of bytes from file.
170          *
171          * @param       $bytes  Amount of bytes to read
172          * @return      $data   Data read from file
173          */
174         public function read ($bytes) {
175                 // Call pointer instance
176                 return $this->getPointerInstance()->read($bytes);
177         }
178 }
179
180 // [EOF]
181 ?>