APP_NAME=ldc2
APP_OBJECTS= main.o silent_code.o configuration_manager.o tool.o config.o
CCFLAGS+=-Wall
-BLOCK_OBJECTS=data_block_0_10.o data_block_0_14.o data_block_0_1.o data_block_0_24.o\
+
+
+
+BUILD_STAMP="\"`uname -a`\""
+VERSION="\"0.7\""
+BUILD_DATE="\"`date` \""
+
+CCFLAGS+=-D BUILD_STAMP=$(BUILD_STAMP) -D VERSION=$(VERSION) -D BUILD_DATE=$(BUILD_DATE)
+
+BLOCK_OBJECTS=tape_block.o eot_block.o data_block.o broken_block.o\
+ data_block_0_10.o data_block_0_14.o data_block_0_1.o data_block_0_24.o\
data_block_0_2.o data_block_0_30.o data_block_0_3.o data_block_0_4.o\
data_block_0_50.o data_block_0_54.o data_block_0_60.o data_block_0_44.o\
- data_block_0_64.o tape_block.o eot_block.o discard_block.o\
- data_block.o data_block_0.o data_block_0_0.o\
+ data_block_0_64.o \
+ data_block_0.o data_block_0_0.o\
data_block_0_label_extractor.o data_block_1.o data_block_2.o\
data_block_3.o data_block_4.o data_block_5.o \
- data_block_6.o data_block_7.o \
-
+ data_block_6.o data_block_7.o
+
-APP_OBJECTS+=$(BLOCK_OBJECTS)
+OBJECTS=$(BLOCK_OBJECTS) $(APP_OBJECTS)
SRC_DIR=src
default: $(APP_NAME)
-APP_SOURCES=$(foreach obj, $(APP_OBJECTS:.o=.cpp),$(SRC_DIR)/$(obj))
+clean:
+ @rm -rf $(APP_NAME) *.o doxy/ $(SRC_DIR)/*~ ul_timestamp *.dep
+ @$(MAKE) -C doc clean
-##All objects depend on all headers. simple but works
-#*.o: $(SRC_DIR)/*.hh
+APP_SOURCES=$(foreach obj, $(OBJECTS:.o=.cpp),$(SRC_DIR)/$(obj))
%.dep : $(SRC_DIR)/%.cpp
@ echo Building dependencies for $<.
%.o : $(SRC_DIR)/%.cpp %.dep
g++ -c $(CCFLAGS) -o $@ $<
-$(APP_NAME) : $(APP_OBJECTS)
- g++ -o$@ $^
+$(APP_NAME) : $(OBJECTS)
+ g++ -o$@ $^
-saustall: $(APP_OBJECTS:.o=.dep)
+saustall: $(OBJECTS:.o=.dep)
-clean:
- @rm -rf $(APP_NAME) *.o doxy/ $(SRC_DIR)/*~ ul_timestamp *.dep
- @$(MAKE) -C doc clean
-
-doxy/timestamp: $(APP_OBJECTS)
+doxy/timestamp: $(OBJECTS)
@ if [ ! -d doxy ]; then mkdir doxy; fi
@doxygen Doxyfile
touch $@
doxy: doxy/timestamp
+
upload: ul_timestamp
ul_timestamp: doxy/timestamp
all: $(APP_NAME) doxy doc
-.PHONY: all clean doxy upload docdata_block_0_0.o
+.PHONY: all clean doxy upload docdata_block_0_0.o default
.PRECIOUS: *.dep
-#$(APP_OBJECTS:.o=.d)
+#$(OBJECTS:.o=.d)
+
+%.hh %.cpp:
+ @ echo "Missing file \"$@\"... No problem :-)"
+
include $(wildcard *.dep)
+#include $(OBJECTS:.o=.dep)
\ No newline at end of file
--- /dev/null
+
+#include "broken_block.hh"
+
+using namespace std;
+
+/*!
+ *\brief Specialisation constructor.
+ */
+broken_block::broken_block(tape_block& org)
+ :tape_block(org)
+{
+ block_type=TBT_BROKEN;
+}
+
+/*!
+ *\brief Describe the block.
+ *\return A vector of text lines describing this block.
+ */
+vector<string> broken_block::get_description(){
+ vector<string> result;
+ string r_string="********* (B-B) Broken block";
+ result.insert(result.end(),r_string);
+ return result;
+}
+
+/*!
+ *\brief Determine if the block marks the end of an object
+ *\retval true The block marks the end of an object.
+ *\retval false The block does not mark the end of an object.
+ */
+bool broken_block::get_obj_end(){
+ return false;
+}
+
--- /dev/null
+#ifndef BROKEN_BLOCK_HH
+#define BROKEN_BLOCK_HH
+
+#include <vector>
+#include <string>
+
+#include "tape_block.hh"
+
+using namespace std;
+
+/*!
+ * Class representating a broken tape block
+ */
+class broken_block
+ : public tape_block
+{
+ friend class tape_block;
+
+private:
+ broken_block(tape_block&);
+
+public:
+ int get_word_size();
+ virtual vector<string> get_description();
+ virtual bool get_obj_end();
+};
+
+
+#endif
int cfg_output_unsatisfied=0;
int cfg_splitobjects=0;
int cfg_splitobjects_numbered=0;
-int cfg_ignoreblock_errors=0;
-int cfg_ignorechecksum_errors=0;
+int cfg_ignore_block_errors=0;
+int cfg_ignore_checksum_errors=0;
int cfg_pause_on_checksum_error=0;
int cfg_ignore_unknown_block_errors=0;
int cfg_ignore_object_integrity_errors=0;
int cfg_list_contents=1;
int cfg_verbose=0;
+int cfg_version=0;
int in_fd=0; //! File descriptor for data input
int out_fd=1; //! File descriptor for text output
ar.add_option_switch("b","ignore_block_errors",
"Ignore block integrity errors. This will output broken blocks,too",
- &cfg_ignoreblock_errors,true,true);
+ &cfg_ignore_block_errors,true,true);
ar.add_option_switch("k","ignore_checksum_errors",
"Ignore block checksum errors. Errors will be converted to warnings.",
- &cfg_ignorechecksum_errors,true,true);
+ &cfg_ignore_checksum_errors,true,true);
ar.add_option_switch("p","pause_on_checksum_error",
"Wait for user input on checksum error.",
ar.add_option_switch("v","verbose",
"Be a bit more verbose.",
&cfg_verbose,true,true);
+
+ ar.add_option_switch("V","version",
+ "Output version information and exit.",
+ &cfg_version,false,true);
ar.add_option_value("i","in_file",
"specify input file",
// If user has a config file, use it.
if (cfg_config_file_set){
- fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str());
+ if (cfg_verbose)
+ fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str());
if(dump_vector_err(ar.read_file(cfg_config_file))){
dump_vector_err(ar.get_help());
exit(7);
}
if (cfg_infile_set==1){
- fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str());
+ if (cfg_verbose)
+ fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str());
in_fd=open(cfg_infile.c_str(),O_RDONLY);
if (in_fd<0){
- fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",cfg_infile.c_str());
+ fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",
+ cfg_infile.c_str());
exit (1);
}
}
if (cfg_outfile_set==1){
- fprintf(stderr,"Opening file for output: \"%s\"\n",cfg_outfile.c_str());
- out_fd=open(cfg_outfile.c_str(),O_WRONLY|O_TRUNC);
+ if (cfg_verbose)
+ fprintf(stderr,"Opening file for output: \"%s\"\n",cfg_outfile.c_str());
+ out_fd=open(cfg_outfile.c_str(),O_CREAT|O_WRONLY|O_TRUNC,0666);
if (out_fd<0){
- fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",cfg_outfile.c_str());
+ fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",
+ cfg_outfile.c_str());
exit (1);
}
}
// The configuration implications according to /cfg007/
if (in_fd==0) cfg_pause_on_checksum_error=0;
- if (cfg_pause_on_checksum_error==1) cfg_ignorechecksum_errors=1;
+ if (cfg_pause_on_checksum_error==1) cfg_ignore_checksum_errors=1;
if (cfg_output_info||cfg_output_called||cfg_output_exported||cfg_output_unsatisfied)
cfg_list_contents=0;
extern int cfg_output_unsatisfied;
extern int cfg_splitobjects;
extern int cfg_splitobjects_numbered;
-extern int cfg_ignoreblock_errors;
-extern int cfg_ignorechecksum_errors;
+extern int cfg_ignore_block_errors;
+extern int cfg_ignore_checksum_errors;
extern int cfg_pause_on_checksum_error;
extern int cfg_ignore_unknown_block_errors;
extern int cfg_ignore_object_integrity_errors;
extern int cfg_list_contents;
extern int cfg_verbose;
+extern int cfg_version;
extern int in_fd; //! File descriptor for data input
extern int out_fd; //! File descriptor for text output
*\return the block type extracted from the block's data.
*/
int data_block::get_type(){
- if ((init_state==TBS_OK)&&word_data)
- return (word_data[0]&0xf000)>>12;
- else
- return block_type;
+ return (word_data[0]&0xf000)>>12;
}
/*!
-#ifndef DATA_BLOCK_H
-#define DATA_BLOCK_H
+#ifndef DATA_BLOCK_HH
+#define DATA_BLOCK_HH
#include<vector>
#include<string>
+++ /dev/null
-#include <string>
-#include <vector>
-
-#include "tape_block.hh"
-#include "discard_block.hh"
-
-
-using namespace std;
-
-/***************************************************************/
-/*!
- *\brief Copy constructor.
- */
-discard_block::discard_block(discard_block & org){
- operator=(org);
-}
-
-/***************************************************************/
-/*!
- *\brief Get a cleartext description of the block.
- *\return ldc style descriptive line for the block
- */
-vector<string> discard_block::get_description(){
- string r_string;
- switch(init_state){
- case TBS_OK:
- r_string="***** (X-XX) Discard block, init_status OK,\
- this is an illegal condition!";
- break;
- case TBS_EOF_LEGAL:
- r_string=" (EOF) Legal EOF";
- break;
- case TBS_EOF_ILLEGAL:
- r_string=" (EOF) Illegal EOF while in block!";
- break;
- case TBS_CHECKSUM:
- r_string=" (CHK) Checksum error in Block!";
- break;
- case TBS_IOERR:
- r_string=" (IOE) IO-Error during initialisation!";
- break;
- case TBS_DEFAULT:
- r_string="***** (X-XX) Discard block, init_status not set,\
- this is an illegal condition!";
- break;
- default:
- r_string="***** (X-XX) Discard block, init_status unknown,\
- this is an illegal condition!";
- break;
- }
- vector<string> result;
- result.insert(result.end(),r_string);
- return result;
-}
-
-/***************************************************************/
-/*!
- *\brief Specialisation constructor
- */
-discard_block::discard_block(tape_block & org)
- : tape_block(org)
-{
-}
-
-/***************************************************************/
-/*!
- *\brief Default constructor.
- */
-discard_block::discard_block(){}
+++ /dev/null
-#ifndef DISCARD_BLOCK_H
-#define DISCARD_BLOCK_H
-
-#include <string>
-#include <vector>
-
-#include "tape_block.hh"
-
-using namespace std;
-
-/*!
- *\brief Class representating an invalid block.
- *
- *Reasons for a block to be invalid are:\n
- * - EOF on fd used to initialise,
- * - Checksum error in a block
- * - IO error while reading from fd
- * The reason for the block to be a discard_block can be
- * resolved by using get_state().
- */
-class discard_block
- :public tape_block
-{
- friend tape_block* tape_block::gen_from_fd(int,void(*)(void*),
- void(*)(void*),void*);
-public: // methods
- discard_block(discard_block &);
- virtual vector<string> get_description();
-
-discard_block(tape_block &);
-private: // methods
-
- discard_block();
-
-}; // class discard_block
-
-
-#endif
#define HW_CONSTANTS_H
/* Block start delimiter */
-#define BLOCK_START_DELIMITER 0x81
+#define BLOCK_START_DELIMITER 0x81 //0201 (SOM:Start of Message)
#define MINIMUM_DATA_BLOCK_WORDSIZE 1
/* Block end delimiters */
#define EOB_LENGTH 2
-#define BLOCK_END_DELIMITER_1 0223
-//#define BLOCK_END_DELIMITER_2 0223 // ==0x93
+#define BLOCK_END_DELIMITER_1 0223 // XOFF 0x93
#define BLOCK_END_DELIMITER_2 0xFF
/* ldc2 preliminary main program */
+
#include <vector>
#include <string>
using namespace std;
+#ifndef BUILD_STAMP
+#define BUILD_STAMP ""
+#endif
+
+#ifndef VERSION
+#define VERSION "0.0"
+#endif
+
+#define ID_STRING "\n ldc2 - The X16 object analyser\n\
+ (C) 2007 Philipp Hachtmann\n\n\
+ Version %s, built %s\n %s\n\n"
+
+
+
/*!
*\brief The main routine.
*/
int main(int argc, char ** args){
+
// Do all the configuration stuff
do_config(argc,args);
- // Now we go to read the data!
-
+ if (cfg_version){
+ fprintf(stderr, ID_STRING, VERSION, BUILD_DATE, BUILD_STAMP);
+ exit(0);
+ }
vector<tape_block*> tape;
vector<vector<tape_block *> > objects;
tape_block * block=0;
bool read_ahead=true;
+
int read_pointer=0;
int block_start=0;
int block_end=0;
int blocks_read=0;
+
bool in_object=false;
+
int errors=0;
int warnings=0;
int errcode=0;
+ bool spaced=false;
+
while(read_ahead){
- block=tape_block::gen_from_fd(in_fd);
-
- block_start=read_pointer+block->get_discarded_bytes();
- block_end=block_start+block->get_raw_size()-1;
- read_pointer=block_end+1;
- //printf("Discarded:%2x, Raw Size:%2x\n",block->get_discarded_bytes(),block->get_raw_size());
- switch(block->get_state()){ // switchy
- case tape_block::TBS_EOF_LEGAL:
- delete block;
- if (!in_object){
- fprintf(stderr,"File successfully read.\n");
- } else {
- if (cfg_ignore_object_integrity_errors){
- fprintf(stderr,"Warning: Object integrity check failed!\n");
- warnings++;
- } else {
- fprintf(stderr,"Error: Object integrity check failed!\n");
- errors++;
- errcode=6;
- }
- }
- read_ahead=false;
- break;
- case tape_block::TBS_EOF_ILLEGAL:
- delete block;
- if (cfg_ignoreblock_errors){
- fprintf(stderr,"Warning: Block integrity check failed!\n");
- warnings++;
- } else {
- fprintf(stderr,"Error: Block integrity check failed!\n");
- errors++;
- errcode=3;
- }
- read_ahead=false;
- break;
- case tape_block::TBS_CHECKSUM:
- delete block;
- char buffer[200];
- snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n",
- blocks_read-1,
- block_start,
- block_end,
- block->get_raw_size());
- if (cfg_ignorechecksum_errors){
+ try{
+ block=tape_block::gen_from_fd(in_fd);
+ blocks_read++;
+ tape.insert(tape.end(),block);
+ block_start=read_pointer+block->get_discarded_bytes();
+ block_end=block_start+block->get_raw_size()-1;
+ read_pointer=block_end+1;
+
+ if (cfg_verbose) {
char buffer[200];
- snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n",
+ if (!spaced) fprintf(stderr,"\n");
+ spaced=false;
+
+ snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n",
blocks_read-1,
block_start,
block_end,
block->get_raw_size());
- write (out_fd,buffer,strlen(buffer));
- fprintf(stderr,"Warning: Block checksum wrong!\n");
- fprintf(stderr,buffer);
- warnings++;
- } else {
- fprintf(stderr,"Error: Block checksum wrong!\n");
fprintf(stderr,buffer);
- errors++;
- read_ahead=false;
- errcode=4;
}
- break;
- case tape_block::TBS_DEFAULT:
- delete block;
- fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n");
- errors++;
- errcode=100;
- read_ahead=false;
- break;
- case tape_block::TBS_IOERR:
- delete block;
+ if(cfg_list_contents)
+ dump_vector(block->get_description());
+ }
+
+ catch(tape_block::eof_legal_exception &e){
+ read_pointer+=e.get_consumed();
+ break; // Immediately leave read loop.
+ }
+
+ catch(tape_block::io_error_exception){
if (in_fd){
- fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str());
+ fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str());
} else {
- fprintf(stderr,"Error: Could not read from stdin!\n");
+ fprintf(stderr,"Error: Could not read from stdin!\n");
}
errors++;
- read_ahead=false;
errcode=2;
- case tape_block::TBS_OK:
- tape.insert(tape.end(),block);
+ break; // Immediately leave read loop.
+ }
+
+ catch(tape_block::eof_illegal_exception &e){
+ block=e.get_block();
blocks_read++;
+ tape.insert(tape.end(),block);
+ block_start=read_pointer+block->get_discarded_bytes();
+ block_end=block_start+block->get_raw_size()-1;
+ read_pointer=block_end+1;
- if (cfg_verbose){
- char buffer[200];
- snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n",
- blocks_read-1,
- block_start,
- block_end,
- block->get_raw_size());
- write (out_fd,buffer,strlen(buffer));
+ if (!spaced) fprintf(stderr,"\n");
+ spaced=false;
+ char buffer[200];
+ snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n",
+ blocks_read-1,
+ block_start,
+ block_end,
+ block->get_raw_size());
+ fprintf(stderr,buffer);
+
+ if (cfg_ignore_block_errors){
+ fprintf(stderr,"Warning: Block integrity check failed!\n");
+ warnings++;
+ } else {
+ fprintf(stderr,"Error: Block integrity check failed!\n");
+ errors++;
+ errcode=3;
+ read_ahead=false;
}
-
if(cfg_list_contents)
dump_vector(block->get_description());
+ fprintf(stderr,"\n");
+ spaced=true;
+ }
+
+ catch(tape_block::checksum_error_exception &e){
+ block=e.get_block();
+ blocks_read++;
+ tape.insert(tape.end(),block);
+ block_start=read_pointer+block->get_discarded_bytes();
+ block_end=block_start+block->get_raw_size()-1;
+ read_pointer=block_end+1;
- if (block->get_type()==0x10){ // Unknown data block!
- if (cfg_ignore_unknown_block_errors){
- fprintf(stderr,"Warning: Unknown block type!\n");
- warnings++;
- }else {
- fprintf(stderr,"Error: Unknown block type!\n");
- errors++;
- errcode=5;
- read_ahead=false;
- }
- }
- current_object.insert(current_object.end(),block);
+ if (!spaced) fprintf(stderr,"\n");
+ spaced=false;
+ char buffer[200];
+ snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n",
+ blocks_read-1,
+ block_start,
+ block_end,
+ block->get_raw_size());
+ fprintf(stderr,buffer);
- if ((block->get_type()==data_block::TBT_EOT)
- ||((block->get_type()==0)&&(block->get_subtype()==3))
- ||((block->get_type()==4)||(block->get_type()==3))
- ||((block->get_type()==0)&&(block->get_subtype()==014))){
- in_object=false;
- objects.insert(objects.end(),current_object);
- current_object.clear();
+ if (cfg_ignore_checksum_errors){
+ fprintf(stderr,"Warning: Block checksum wrong!\n");
+ warnings++;
} else {
- in_object=true;
+ fprintf(stderr,"Error: Block checksum wrong!\n");
+ errors++;
+ read_ahead=false;
+ errcode=4;
}
-
- break;
- } // switch
- } // generate loop
- close(in_fd);
+ if(cfg_list_contents)
+ dump_vector(block->get_description());
+ fprintf(stderr,"\n");
+ spaced=true;
+ }
+
+
+ } // while (read_ahead)
// Now we have our blocks, tell the user about that!
+ if (!spaced) fprintf(stderr,"\n");
+ spaced=false;
char buffer[200];
sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n",
- read_pointer,blocks_read,errors,warnings);
+ read_pointer,blocks_read,errors,warnings);
if(cfg_output_info){
write (out_fd,buffer,strlen(buffer));
-// } else {
-// fprintf(stderr,buffer);
+ } else {
+ fprintf(stderr,buffer);
}
-
+
if (errors>0){
- fprintf(stderr,"Errors make me sick. Goodbye!\n");
+ fprintf(stderr,"Failed. (%i)\n",errcode);
exit(errcode);
}
+
+ if (cfg_verbose){
+ fprintf(stderr,"Data read successfully.\n");
+ }
+ close(in_fd);
+
+// switch(block->get_state()){ // switchy
+// case tape_block::TBS_EOF_LEGAL:
+// delete block;
+// if (!in_object){
+// fprintf(stderr,"File successfully read.\n");
+// } else {
+// if (cfg_ignore_object_integrity_errors){
+// fprintf(stderr,"Warning: Object integrity check failed!\n");
+// warnings++;
+// } else {
+// fprintf(stderr,"Error: Object integrity check failed!\n");
+// errors++;
+// errcode=6;
+// }
+// }
+// read_ahead=false;
+// break;
+// case tape_block::TBS_EOF_ILLEGAL:
+// break;
+// case tape_block::TBS_CHECKSUM:
+// break;
+// case tape_block::TBS_DEFAULT:
+// delete block;
+// fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n");
+// errors++;
+// errcode=100;
+// read_ahead=false;
+// break;
+// case tape_block::TBS_IOERR:
+// case tape_block::TBS_OK:
+// tape.insert(tape.end(),block);
+// blocks_read++;
+
+// if (cfg_verbose){
+// char buffer[200];
+// snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n",
+// blocks_read-1,
+// block_start,
+// block_end,
+// block->get_raw_size());
+// write (out_fd,buffer,strlen(buffer));
+// }
+// if(cfg_list_contents)
+// dump_vector(block->get_description());
+
+// if (block->get_type()==0x10){ // Unknown data block!
+// if (cfg_ignore_unknown_block_errors){
+// fprintf(stderr,"Warning: Unknown block type!\n");
+// warnings++;
+// }else {
+// fprintf(stderr,"Error: Unknown block type!\n");
+// errors++;
+// errcode=5;
+// read_ahead=false;
+// }
+// }
+
+// current_object.insert(current_object.end(),block);
+
+// if ((block->get_type()==data_block::TBT_EOT)
+// ||((block->get_type()==0)&&(block->get_subtype()==3))
+// ||((block->get_type()==4)||(block->get_type()==3))
+// ||((block->get_type()==0)&&(block->get_subtype()==014))){
+// in_object=false;
+// objects.insert(objects.end(),current_object);
+// current_object.clear();
+// } else {
+// in_object=true;
+// }
+
+// break;
+// } // switch
+// } // generate loop
+
+
+// }
+
return 0;
} // main()
+
+
+#include <string>
+#include <vector>
+
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "tape_block.hh"
+
+#include "silent_code.hh"
+#include "hw_constants.hh"
+
+#include "eot_block.hh"
#include "data_block.hh"
+#include "broken_block.hh"
#include "data_block_0.hh"
#include "data_block_0_0.hh"
#include "data_block_0_1.hh"
#include "data_block_6.hh"
#include "data_block_7.hh"
-#include "eot_block.hh"
-#include "discard_block.hh"
-#include "silent_code.hh"
-#include "hw_constants.hh"
-
using namespace std;
/***************************************************************/
*/
void tape_block::operator=(tape_block &org){
block_type=org.block_type;
- init_state=org.init_state;
discarded_bytes=org.discarded_bytes;
raw_data=0;
raw_size=org.raw_size;
}
}
-/***************************************************************/
-/*!
- *\brief Query the block's state.
- *
- * If the state is not TBS_OK, the block must be discarded.
- *\return The block's initialisation state
- */
-tape_block::tb_state_t tape_block::get_state(){
- return init_state;
-}
-
/***************************************************************/
/*!
*\brief Determine the block's type
* This routine returns the block's type.\\
*\retval TBT_DATA if it's a data block
*\retval TBT_EOT if it's an end of tape mark
- *\retval TBR_DISCARD if the block is unusable
*
*\note This function is virtual. That means that if the tape block in
* question is of the subtype data_block which overwrites this method,
/***************************************************************/
/*!
- *\brief Static factory method with file descriptor support.
+ *\brief Static factory method for tape blocks.
*
* This method creates a tape_block by using the private
* constructor with file descriptor support.\n
- * It always returns a valid pointer, errors
- * or exceptional conditions during creation are marked in the
- * object's state.\n
+ *
* The factory method tries to determine the block's type and then
* looks for appropriate subclasses. If there's an appropriate
* subclass fitting the newly-created object, an
* methods not already present in tape_block.\n
* Objects of the following subclasses are automagically generated:\n
* - eot_block
- * - discard_block
* - data_block
* - data_block_0
* - data_block_0_0
* - data_block_6
* - data_block_7
*
- *\return Pointer to an object of type tape_block or a subclass.
- *\param fd A file descriptor where the read is taken from.
+ *\return Pointer to an instance of tape_block or a subclass.
+ *\param fd A file descriptor where data can be read from.
*\param input_start A pointer to a function
* called before reading data from fd.
*\param input_stop A pointer to a function
data_block_0 * n_data_block_0=0;
data_block_0 * n_data_block_0_x=0;
eot_block * n_eot_block=0;
- discard_block * n_discard_block=0;
-
+ broken_block * n_broken_block=0;
+
//Use the private constructor which reads in the block from a file descriptor
+ try{
n_tape_block=new tape_block(fd,input_start,input_stop,start_stop_arg);
+ }
- // Retype to data_block, eot_block, discard_block - if possible
+ catch(checksum_error_exception e){
+ n_broken_block=new broken_block(*(e.get_block()));
+ delete e.get_block();
+ throw checksum_error_exception(n_broken_block);
+ }
+
+ // Retype to data_block, eot_block - if possible
switch(n_tape_block->get_type()){
case tape_block::TBT_DATA:
n_data_block = new data_block(*n_tape_block);
n_eot_block=new eot_block(*n_tape_block);
delete n_tape_block;
return n_eot_block;
- case tape_block::TBT_DISCARD:
- n_discard_block = new discard_block(*n_tape_block);
- delete n_tape_block;
- return n_discard_block;
default: // Unknown block, a bad thing!
return n_tape_block;
}
/*!
*\brief Automatic constructor with file descriptor support
*
- * This constructor is used to initialise the block read in the block's
- * data via a file descriptor.\n
+ * This constructor is used to initialise the block read in the
+ * block's data via a file descriptor.\n
* This is done in the following way:
* - input_start() is called.
* - Data is read from fd. Stops on end of file or end of block
int eob_needed=EOB_LENGTH;
int block_complete=0;
- block_type= TBT_DISCARD;
- init_state= TBS_DEFAULT;
+ block_type= TBT_BROKEN;
discarded_bytes=0;
raw_data = 0;
raw_size = 0;
do {
retval=read(fd, &buffer, 1);
if (retval == 0){ // End of file
- block_type=TBT_DISCARD;
- init_state=TBS_EOF_LEGAL;
+ free(raw_data);
+ throw eof_legal_exception(discarded_bytes);
return;
}
if (retval == -1){ // Error reading from fd
- block_type=TBT_DISCARD;
- init_state=TBS_IOERR;
+ free(raw_data);
+ throw io_error_exception();
return;
}
discarded_bytes++;
break;
case 1:
if (buffer==EOT_SEQ_3){
- raw_data=(unsigned char*)malloc(3);
+ raw_data=(unsigned char*)realloc(raw_data,3);
raw_size=3;
discarded_bytes-=3;
raw_data[0]=EOT_SEQ_1;
raw_data[1]=EOT_SEQ_2;
raw_data[2]=EOT_SEQ_3;
block_type =TBT_EOT;
- init_state =TBS_OK;
return;
}
else eot_needed=EOT_LENGTH;
retval = read (fd, &buffer, 1);
if (retval == 0){ // End of file
- block_type=TBT_DISCARD;
- init_state=TBS_EOF_ILLEGAL;
- return;
+ broken_block * b_block=new broken_block(*this);
+ free(raw_data);
+ throw eof_illegal_exception(b_block);
}
if (retval == -1){ // Error reading from fd
- block_type=TBT_DISCARD;
- init_state=TBS_IOERR;
- return;
+ free(raw_data);
+ throw io_error_exception();
}
/* sort in the new byte */
break;
case 1:
if (buffer==BLOCK_END_DELIMITER_2){
- raw_size-=EOB_LENGTH; // Don't want the delimiter(s) in the data!
block_complete=1;
}
else eob_needed=EOB_LENGTH;
break;
}
}while (!block_complete);
+
if (input_stop) input_stop (start_stop_arg);
+
/* Now we have the data in. */
if (init_words()!=0){
- init_state=TBS_CHECKSUM;
- return;
+ broken_block * b_block=new broken_block(*this);
+ free(raw_data);
+ free(word_data);
+ throw checksum_error_exception(b_block);
}
-
+
if (test_checksum()!=0){
- init_state=TBS_CHECKSUM;
- return;
+ broken_block * b_block=new broken_block(*this);
+ free(raw_data);
+ free(word_data);
+ throw checksum_error_exception(b_block);
}
- raw_size+=EOB_LENGTH; // Now we want it...
-
block_type=TBT_DATA;
- init_state=TBS_OK;
}
*\retval 1 on error.
*/
int tape_block::init_words(){
- word_size=raw_size/3;
+ word_size=(raw_size-EOB_LENGTH)/3;
if (word_size<MINIMUM_DATA_BLOCK_WORDSIZE) return 1; // Block too short!
- word_data=(unsigned short *)malloc(raw_size*sizeof(unsigned short));
+ word_data=(unsigned short *)malloc(word_size*sizeof(unsigned short));
for (int pos=0; pos<word_size; pos++)
word_data[pos]=combine466(raw_data+3*pos);
return 0;
else return 1;
}
+/***************************************************************/
+/*!
+ *\brief Standard constructor of exception base class.
+ */
+tape_block::exception::exception(){
+}
+
+/***************************************************************/
+/*!
+ *\brief Standard desctructor.
+ */
+tape_block::exception::~exception(){
+}
+
+/***************************************************************/
+/*!
+ *\brief Get pointer to erroneous tape_block
+ *\return A pointer to a tape_block instance.
+ *\note The user has to take care to free the instance contained
+ * in objects of classes supporting this method.
+ */
+tape_block * tape_block::exception::get_block(){
+ return m_broken_block;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ */
+tape_block::checksum_error_exception::
+checksum_error_exception (tape_block * broken_block){
+ this->m_broken_block=broken_block;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ */
+tape_block::eof_illegal_exception::
+eof_illegal_exception (tape_block * broken_block){
+ this->m_broken_block=broken_block;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ *\param bytes_consumed Amount of bytes read while looking
+ * for a block.
+ */
+tape_block::eof_legal_exception::
+eof_legal_exception(int bytes_consumed){
+ this->m_broken_block=NULL;
+ this->bytes_consumed=bytes_consumed;
+}
+
+/***************************************************************/
+/*!
+ *\brief Get amount of consumed data before EOF came along.
+ *\return Amound of bytes read from fd while looking for
+ * new block or EOF.
+ */
+int tape_block::eof_legal_exception::get_consumed(){
+ return bytes_consumed;
+}
+
+/***************************************************************/
+/*!
+ *\brief Constructor
+ */
+tape_block::io_error_exception::
+io_error_exception(){
+ this->m_broken_block=NULL;
+}
+
+
+
+
+
+
-#ifndef TAPE_BLOCK_H
-#define TAPE_BLOCK_H
-
-#include <string>
-#include <vector>
+#ifndef TAPE_BLOCK_HH
+#define TAPE_BLOCK_HH
using namespace std;
* That may be some kind of data block or a end of tape mark.
*/
class tape_block{
-
+
public: // types
-
- /*!
- *\brief Initialisation state.
- */
- typedef enum {
- TBS_OK, //!< Block successfully initialised
- TBS_EOF_LEGAL, //!< Legal EOF while initialising
- TBS_EOF_ILLEGAL, //!< Illegal EOF while initialising
- TBS_CHECKSUM, //!< Checksum error
- TBS_IOERR, //!< I/O-Error while reading
- TBS_DEFAULT //!< Block not initialised.
- } tb_state_t;
-
+
/*!
* Tape block types.
*/
typedef enum {
TBT_DATA=0x10, //!< Data block
TBT_EOT, //!< End of tape block
- TBT_DISCARD //!< Invalid block, check block_type
+ TBT_BROKEN, //!< A broken block
} tb_type_t;
-
+
+ /*!
+ *\brief Local base class for exceptions.
+ */
+ class exception {
+ protected:
+ exception();
+ public:
+ virtual ~exception();
+ tape_block * get_block();
+ protected:
+ tape_block * m_broken_block;
+ };
+
+ /*!
+ *\brief Checksum error exception.
+ */
+ class checksum_error_exception
+ : public exception {
+ public:
+ checksum_error_exception(tape_block * block);
+ };
+
+ /*!
+ *\brief EOF while reading block exception.
+ */
+ class eof_illegal_exception
+ : public exception {
+ public:
+ eof_illegal_exception(tape_block * block);
+ };
+
+ /*!
+ *\brief EOF after reading block exception.
+ */
+ class eof_legal_exception
+ : public exception {
+ public:
+ eof_legal_exception(int bytes_consumed);
+ int get_consumed();
+ private:
+ int bytes_consumed;
+ tape_block * get_block();
+ };
+
+ /*!
+ *\brief IO error exception.
+ */
+ class io_error_exception
+ : exception {
+ public:
+ io_error_exception();
+ private:
+ tape_block * get_block();
+ };
+
public: // methods
virtual ~tape_block();
tape_block(tape_block &);
void operator=(tape_block &);
-
- tb_state_t get_state();
+
virtual int get_type();
virtual int get_subtype();
virtual vector<string> get_description();
void (*input_stop)(void *)=0,
void * start_stop_arg=0
);
-
+
protected: // methods
tape_block();
string get_typestring();
-
+
private: // methods
tape_block (int fd_p,
void(*input_start)(void *)=0,
int init_words(void);
int test_checksum();
-
+
protected: // members
int block_type; //!< Type of this block.
- tb_state_t init_state; //!< Initialisation state.
int discarded_bytes; //!< Amount of bytes discarded before beginning.
unsigned char * raw_data; //!< Raw block data in bytes.
int raw_size; //!< Size of the raw data.
int poolsize; //!< Amount of data malloc'ed
}; // class tape_block
-
#endif