6 #include "tape_block.hh"
7 #include "data_block.hh"
8 #include "data_block_0.hh"
9 #include "data_block_0_0.hh"
10 #include "data_block_0_1.hh"
11 #include "data_block_0_2.hh"
12 #include "data_block_0_3.hh"
13 #include "data_block_0_4.hh"
14 #include "data_block_0_10.hh"
15 #include "data_block_0_14.hh"
16 #include "data_block_0_24.hh"
17 #include "data_block_0_44.hh"
18 #include "data_block_0_30.hh"
19 #include "data_block_0_54.hh"
20 #include "data_block_0_60.hh"
21 #include "data_block_0_50.hh"
23 #include "eot_block.hh"
24 #include "discard_block.hh"
25 #include "silent_code.hh"
26 #include "hw_constants.hh"
30 /***************************************************************/
34 tape_block::~tape_block(){
39 /***************************************************************/
41 *\brief Copy constructor.
43 tape_block::tape_block(tape_block
& org
){
47 /***************************************************************/
49 *\brief Assignment operator.
51 void tape_block::operator=(tape_block
&org
){
52 block_type
=org
.block_type
;
53 init_state
=org
.init_state
;
54 data_read
=org
.data_read
;
56 raw_size
=org
.raw_size
;
58 word_size
=org
.word_size
;
60 raw_data
=(unsigned char *)malloc(raw_size
);
61 memcpy(raw_data
,org
.raw_data
,raw_size
);
64 word_data
=(unsigned short *)malloc(word_size
*sizeof(unsigned short));
65 memcpy(word_data
,org
.word_data
,word_size
*sizeof(unsigned short));
69 /***************************************************************/
71 *\brief Query the block's state.
73 * If the state is not TBS_OK, the block must be discarded.
74 *\return The block's initialisation state
76 tape_block::tb_state_t
tape_block::get_state(){
80 /***************************************************************/
82 *\brief Determine the block's type
84 * This routine returns the block's type.\\
85 *\retval TBT_DATA if it's a data block
86 *\retval TBT_EOT if it's an end of tape mark
87 *\retval TBR_DISCARD if the block is unusable
89 *\note This function is virtual. That means that if the tape block in
90 * question is of the subtype data_block which overwrites this method,
91 * it will return the block type contained in the upper 4 bits of the
92 * first data word! Try to avoid testing for TBT_DATA. \n
93 * Blocks generated by gen_from_fd should never have TBT_DATA.
95 int tape_block::get_type(){
99 /***************************************************************/
101 *\brief Determine the block's subtype
103 * Some data block types have a subtype.
104 *\note This is a virtual method and to be used in derived classes.
107 int tape_block::get_subtype(){
111 /***************************************************************/
113 *\brief Get a cleartext description of the block.
114 *\return ldc style descriptive line for the block
116 vector
<string
> tape_block:: get_description(){
117 vector
<string
> result
;
118 result
.insert(result
.end(),
119 "***** "+get_typestring()+"Untyped tape block");
123 /***************************************************************/
125 *\brief Get size of the internal raw data buffer
126 *\return The size of the internal raw data buffer
128 int tape_block::get_raw_size(){
132 /***************************************************************/
134 *\brief Access internal raw data buffer
136 * The raw data buffer contains all block data except the \ \
137 * block start delimiter and block end delimiter.
138 *\return Pointer to internal raw data buffer of the block
140 unsigned char * tape_block::get_raw_data(){
144 /***************************************************************/
146 *\brief Static factory method with file descriptor support.
148 * This method creates a tape_block by using the private
149 * constructor with file descriptor support.\n
150 * It always returns a valid pointer, errors
151 * or exceptional conditions during creation are marked in the
153 * The factory method tries to determine the block's type and then
154 * looks for appropriate subclasses. If there's an appropriate
155 * subclass fitting the newly-created object, an
156 * object of the subclass will be created and a pointer to this
157 * object will be returned to the caller.\n
158 * This allows for easy use of the object using it's virtual
159 * interface methods or a cast of the pointer to a sub type pointer
160 * according to the type information, and then using the subtype's
161 * methods not already present in tape_block.\n
162 * Objects of the following subclasses are automagically generated:\n
187 *\return Pointer to an object of type tape_block or a subclass.
188 *\param fd A file descriptor where the read is taken from.
189 *\param input_start A pointer to a function
190 * called before reading data from fd.
191 *\param input_stop A pointer to a function
192 * called after reading data from fd.
194 *\param start_stop_arg A pointer passed to input_start
195 * and input_stop(). Can be anything.
197 tape_block
* tape_block::gen_from_fd(int fd
,
198 void(*input_start
)(void *),
199 void (*input_stop
)(void *),
200 void * start_stop_arg
)
202 tape_block
* res_block
;
203 res_block
=new tape_block(fd
,input_start
,input_stop
,start_stop_arg
);
204 data_block
* d_block
;
205 data_block_0
* d0_block
;
207 discard_block
* di_block
;
209 // Retype to data_block if possible
210 switch(res_block
->get_type()){
211 case tape_block::TBT_DATA
: // Make pointer a data block
212 d_block
=new data_block(*res_block
);
216 case tape_block::TBT_EOT
:
217 e_block
=new eot_block(*res_block
);
221 case tape_block::TBT_DISCARD
:
222 di_block
=new discard_block(*res_block
);
226 default: // All other cases
230 // Retype again, if it's a data_block now
232 switch(res_block
->get_type()){
234 d0_block
=new data_block_0(*d_block
);
237 // case 01: d_block=new data_block_1(*d_block); break;
238 // case 02: d_block=new data_block_2(*d_block); break;
239 // case 03: d_block=new data_block_3(*d_block); break;
240 // case 04: d_block=new data_block_4(*d_block); break;
241 // case 05: d_block=new data_block_5(*d_block); break;
242 // case 06: d_block=new data_block_6(*d_block); break;
243 // case 07: d_block=new data_block_7(*d_block); break;
250 if (res_block
->get_type()==0) switch(d0_block
->get_subtype()){
251 case 000: d0_block
=new data_block_0_0(*d0_block
); break;
252 case 001: d0_block
=new data_block_0_1(*d0_block
); break;
253 case 002: d0_block
=new data_block_0_2(*d0_block
); break;
254 case 003: d0_block
=new data_block_0_3(*d0_block
); break;
255 case 004: d0_block
=new data_block_0_4(*d0_block
); break;
256 case 010: d0_block
=new data_block_0_10(*d0_block
); break;
257 case 014: d0_block
=new data_block_0_14(*d0_block
); break;
258 case 024: d0_block
=new data_block_0_24(*d0_block
); break;
259 case 044: d0_block
=new data_block_0_44(*d0_block
); break;
260 case 030: d0_block
=new data_block_0_30(*d0_block
); break;
261 case 054: d0_block
=new data_block_0_54(*d0_block
); break;
262 case 060: d0_block
=new data_block_0_60(*d0_block
); break;
263 case 050: d0_block
=new data_block_0_50(*d0_block
); break;
273 /***************************************************************/
275 *\brief Default constructor.
277 tape_block::tape_block(){}
279 /***************************************************************/
281 *\brief get a short type description string.
282 *\return A small string to be used in description methods.
284 * The string is always 19 characters long.\n
285 * An example is "(99-99) ".
287 string
tape_block::get_typestring(){
289 sprintf(buffer
,"(%o-%o) ",get_type(),get_subtype());
291 return string(buffer
);
294 /***************************************************************/
296 *\brief Automatic constructor with file descriptor support
298 * This constructor is used to initialise the block read in the block's
299 * data via a file descriptor.\n
300 * This is done in the following way:
301 * - input_start() is called.
302 * - Data is read from fd. Stops on end of file or end of block
303 * or IO error. On EOF or IO error input_stop() is NOT called.
304 * - If there was no IO error the block's checksum is calculated
306 * - The block's type field is initialised.
307 * - input_stop() is called.
309 *\param fd A file descriptor where the read is taken from
310 *\param input_stop A pointer to a function called at the end of input
311 *\param input_start A pointer to a function called at the beginning
313 *\param start_stop_arg A pointer passed to input_start and input_stop().
315 tape_block::tape_block (int fd
,
316 void(*input_start
)(void *),
317 void (*input_stop
)(void *),
318 void * start_stop_arg
321 unsigned char buffer
;
323 int eot_needed
=EOT_LENGTH
;
324 int eob_needed
=EOB_LENGTH
;
325 int block_complete
=0;
327 block_type
= TBT_DISCARD
;
328 init_state
= TBS_DEFAULT
;
335 if (input_start
) input_start (start_stop_arg
);
337 /* Look for block start in input stream */
339 retval
=read(fd
, &buffer
, 1);
340 if (retval
== 0){ // End of file
341 block_type
=TBT_DISCARD
;
342 init_state
=TBS_EOF_LEGAL
;
345 if (retval
== -1){ // Error reading from fd
346 block_type
=TBT_DISCARD
;
347 init_state
=TBS_IOERR
;
352 /* Look for end ot tape sequence */
355 if (buffer
==EOT_SEQ_1
) eot_needed
--;
356 else eot_needed
=EOT_LENGTH
;
359 if (buffer
==EOT_SEQ_2
) eot_needed
--;
360 else eot_needed
=EOT_LENGTH
;
363 if (buffer
==EOT_SEQ_3
){
364 raw_data
=(unsigned char*)malloc(3);
366 raw_data
[0]=EOT_SEQ_1
;
367 raw_data
[1]=EOT_SEQ_2
;
368 raw_data
[2]=EOT_SEQ_3
;
373 else eot_needed
=EOT_LENGTH
;
376 } while (buffer
!= (unsigned char) BLOCK_START_DELIMITER
);
379 /* Now input the block data */
382 retval
= read (fd
, &buffer
, 1);
384 if (retval
== 0){ // End of file
385 block_type
=TBT_DISCARD
;
386 init_state
=TBS_EOF_ILLEGAL
;
389 if (retval
== -1){ // Error reading from fd
390 block_type
=TBT_DISCARD
;
391 init_state
=TBS_IOERR
;
394 data_read
++; // We have consumed a byte
396 /* sort in the new byte */
398 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
399 raw_data
[raw_size
-1]=buffer
;
401 /* Look for end ot block sequence */
404 if (buffer
==BLOCK_END_DELIMITER_1
) eob_needed
--;
405 else eob_needed
=EOB_LENGTH
;
408 if (buffer
==BLOCK_END_DELIMITER_2
){
409 raw_size
-=EOB_LENGTH
; // Don't want the delimiter(s) in the data!
410 raw_data
=(unsigned char*)realloc(raw_data
,raw_size
);
413 else eob_needed
=EOB_LENGTH
;
416 }while (!block_complete
);
417 if (input_stop
) input_stop (start_stop_arg
);
419 /* Now we have the data in. */
421 if (init_words()!=0){
422 init_state
=TBS_CHECKSUM
;
426 if (test_checksum()!=0){
427 init_state
=TBS_CHECKSUM
;
436 /***************************************************************/
438 *\brief Initialize word representation of data.
439 *\retval 0 on success.
442 int tape_block::init_words(){
443 word_size
=raw_size
/3;
444 if (word_size
<MINIMUM_DATA_BLOCK_WORDSIZE
) return 1; // Block too short!
445 word_data
=(unsigned short *)malloc(raw_size
*sizeof(unsigned short));
446 for (int pos
=0; pos
<word_size
; pos
++)
447 word_data
[pos
]=combine466(raw_data
+3*pos
);
451 /***************************************************************/
453 *\brief Calculate and check checksum.
454 *\retval 0 on success.
457 int tape_block::test_checksum(){
458 unsigned short sum
=0;
459 for (int pos
=0; pos
<word_size
-1;pos
++)
461 if (sum
==word_data
[word_size
-1]) return 0;