-int main(int argc, char ** args){
- int help_needed;
- int name_set;
- int file_set;
- string name="Philipp";
- string filename;
-
- argreader ar("ldc2");
- ar.add_param("n","name=","Enter other name",&name_set,&name,"<name>");
- ar.add_param("h","help","Give help",&help_needed);
- ar.add_free_param("<file>","File to read data from",&file_set,&filename);
-
-
-
- dump_vector(ar.read_args(argc,args));
-
- if (help_needed==1) dump_vector(ar.get_help());
-
- printf("Hallo %s!\n",name.c_str());
- exit(0);
- tape_block * myblock=0;
- do{
- if (myblock) delete myblock;
- myblock=tape_block::gen_from_fd(0);
-
- vector<string> desc=myblock->get_description();
- for (vector<string>::iterator iter=desc.begin();
- iter!=desc.end();iter++)
- printf("%s\n",(*iter).c_str());
- } while (myblock->get_state()==tape_block::TBS_OK);
- return 0;
-}
+ // Process command line first time
+ if(dump_vector(ar.read_args(argc,args))){
+ dump_vector(ar.get_help());
+ exit(7);
+ }
+
+ if (do_help) {
+ dump_vector(ar.get_help());
+ exit(0);
+ }
+
+ // If user has a config file, use it.
+ if (config_file_set){
+ fprintf(stderr,"Using config file:\"%s\"\n",config_file.c_str());
+ if(dump_vector(ar.read_file(config_file))){
+ dump_vector(ar.get_help());
+ exit(7);
+ }
+
+ // Process command line a second time to override values from config file.
+ if(dump_vector(ar.read_args(argc,args))||do_help){
+ dump_vector(ar.get_help());
+ exit(7);
+ }
+ }
+
+ if (infile_set==1){
+ fprintf(stderr,"Opening file for input:%s\n",infile.c_str());
+ in_fd=open(infile.c_str(),O_RDONLY);
+ if (in_fd<0){
+ fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",infile.c_str());
+ exit (1);
+ }
+ }
+
+ if (outfile_set==1){
+ fprintf(stderr,"Opening file for output:%s\n",outfile.c_str());
+ out_fd=open(outfile.c_str(),O_WRONLY|O_TRUNC);
+ if (out_fd<0){
+ fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",outfile.c_str());
+ exit (1);
+ }
+ }
+
+ // The configuration implications according to /cfg007/
+ if (in_fd==0) pause_on_checksum_error=0;
+ if (pause_on_checksum_error==1) ignore_checksum_errors=1;
+ if (output_info||output_called||output_exported||output_unsatisfied)
+ list_contents=0;
+
+
+ // Now we go to read the data!
+
+ vector<tape_block*> tape;
+ vector<vector<tape_block *> > objects;
+ vector<tape_block *> current_object;
+
+ 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;
+
+ 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 (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 (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;
+ break;
+ case tape_block::TBS_CHECKSUM:
+ delete block;
+ if (ignore_checksum_errors){
+ fprintf(stderr,"Warning: Block checksum wrong!\n");
+ warnings++;
+ } else {
+ fprintf(stderr,"Error: Block checksum wrong!\n");
+ 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 (in_fd){
+ fprintf(stderr,"Error: Could not read from \"%s\"!\n",infile.c_str());
+ } else {
+ 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);
+ blocks_read++;
+
+ if (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(list_contents)
+ dump_vector(block->get_description());
+
+ if (block->get_type()==0x10){ // Unknown data block!
+ if (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
+ close(in_fd);
+
+ // Now we have our blocks, tell the user about that!
+ char buffer[200];
+ sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n",
+ read_pointer,blocks_read,errors,warnings);
+ if(output_info){
+ write (out_fd,buffer,strlen(buffer));
+ } else {
+ fprintf(stderr,buffer);
+ }
+
+ if (errors>0){
+ fprintf(stderr,"Errors make me sick. Goodbye!\n");
+ exit(errcode);
+ }
+
+
+ return 0;
+} // main()