| 1 | /* ldc2 preliminary main program */ |
| 2 | |
| 3 | #include <vector> |
| 4 | #include <string> |
| 5 | |
| 6 | #include "config.hh" |
| 7 | #include "tool.hh" |
| 8 | |
| 9 | #include "tape_block.hh" |
| 10 | #include "data_block_0.hh" |
| 11 | |
| 12 | using namespace std; |
| 13 | |
| 14 | /*! |
| 15 | *\brief The main routine. |
| 16 | */ |
| 17 | int main(int argc, char ** args){ |
| 18 | |
| 19 | // Do all the configuration stuff |
| 20 | do_config(argc,args); |
| 21 | |
| 22 | // Now we go to read the data! |
| 23 | |
| 24 | |
| 25 | vector<tape_block*> tape; |
| 26 | vector<vector<tape_block *> > objects; |
| 27 | vector<tape_block *> current_object; |
| 28 | |
| 29 | tape_block * block=0; |
| 30 | bool read_ahead=true; |
| 31 | int read_pointer=0; |
| 32 | int block_start=0; |
| 33 | int block_end=0; |
| 34 | int blocks_read=0; |
| 35 | bool in_object=false; |
| 36 | int errors=0; |
| 37 | int warnings=0; |
| 38 | int errcode=0; |
| 39 | |
| 40 | while(read_ahead){ |
| 41 | block=tape_block::gen_from_fd(in_fd); |
| 42 | |
| 43 | block_start=read_pointer+block->get_discarded_bytes(); |
| 44 | block_end=block_start+block->get_raw_size()-1; |
| 45 | read_pointer=block_end+1; |
| 46 | //printf("Discarded:%2x, Raw Size:%2x\n",block->get_discarded_bytes(),block->get_raw_size()); |
| 47 | switch(block->get_state()){ // switchy |
| 48 | case tape_block::TBS_EOF_LEGAL: |
| 49 | delete block; |
| 50 | if (!in_object){ |
| 51 | fprintf(stderr,"File successfully read.\n"); |
| 52 | } else { |
| 53 | if (cfg_ignore_object_integrity_errors){ |
| 54 | fprintf(stderr,"Warning: Object integrity check failed!\n"); |
| 55 | warnings++; |
| 56 | } else { |
| 57 | fprintf(stderr,"Error: Object integrity check failed!\n"); |
| 58 | errors++; |
| 59 | errcode=6; |
| 60 | } |
| 61 | } |
| 62 | read_ahead=false; |
| 63 | break; |
| 64 | case tape_block::TBS_EOF_ILLEGAL: |
| 65 | delete block; |
| 66 | if (cfg_ignoreblock_errors){ |
| 67 | fprintf(stderr,"Warning: Block integrity check failed!\n"); |
| 68 | warnings++; |
| 69 | } else { |
| 70 | fprintf(stderr,"Error: Block integrity check failed!\n"); |
| 71 | errors++; |
| 72 | errcode=3; |
| 73 | } |
| 74 | read_ahead=false; |
| 75 | break; |
| 76 | case tape_block::TBS_CHECKSUM: |
| 77 | delete block; |
| 78 | char buffer[200]; |
| 79 | snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", |
| 80 | blocks_read-1, |
| 81 | block_start, |
| 82 | block_end, |
| 83 | block->get_raw_size()); |
| 84 | if (cfg_ignorechecksum_errors){ |
| 85 | char buffer[200]; |
| 86 | snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", |
| 87 | blocks_read-1, |
| 88 | block_start, |
| 89 | block_end, |
| 90 | block->get_raw_size()); |
| 91 | write (out_fd,buffer,strlen(buffer)); |
| 92 | fprintf(stderr,"Warning: Block checksum wrong!\n"); |
| 93 | fprintf(stderr,buffer); |
| 94 | warnings++; |
| 95 | } else { |
| 96 | fprintf(stderr,"Error: Block checksum wrong!\n"); |
| 97 | fprintf(stderr,buffer); |
| 98 | errors++; |
| 99 | read_ahead=false; |
| 100 | errcode=4; |
| 101 | } |
| 102 | break; |
| 103 | case tape_block::TBS_DEFAULT: |
| 104 | delete block; |
| 105 | fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n"); |
| 106 | errors++; |
| 107 | errcode=100; |
| 108 | read_ahead=false; |
| 109 | break; |
| 110 | case tape_block::TBS_IOERR: |
| 111 | delete block; |
| 112 | if (in_fd){ |
| 113 | fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str()); |
| 114 | } else { |
| 115 | fprintf(stderr,"Error: Could not read from stdin!\n"); |
| 116 | } |
| 117 | errors++; |
| 118 | read_ahead=false; |
| 119 | errcode=2; |
| 120 | case tape_block::TBS_OK: |
| 121 | tape.insert(tape.end(),block); |
| 122 | blocks_read++; |
| 123 | |
| 124 | if (cfg_verbose){ |
| 125 | char buffer[200]; |
| 126 | snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", |
| 127 | blocks_read-1, |
| 128 | block_start, |
| 129 | block_end, |
| 130 | block->get_raw_size()); |
| 131 | write (out_fd,buffer,strlen(buffer)); |
| 132 | } |
| 133 | |
| 134 | if(cfg_list_contents) |
| 135 | dump_vector(block->get_description()); |
| 136 | |
| 137 | if (block->get_type()==0x10){ // Unknown data block! |
| 138 | if (cfg_ignore_unknown_block_errors){ |
| 139 | fprintf(stderr,"Warning: Unknown block type!\n"); |
| 140 | warnings++; |
| 141 | }else { |
| 142 | fprintf(stderr,"Error: Unknown block type!\n"); |
| 143 | errors++; |
| 144 | errcode=5; |
| 145 | read_ahead=false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | current_object.insert(current_object.end(),block); |
| 150 | |
| 151 | if ((block->get_type()==data_block::TBT_EOT) |
| 152 | ||((block->get_type()==0)&&(block->get_subtype()==3)) |
| 153 | ||((block->get_type()==4)||(block->get_type()==3)) |
| 154 | ||((block->get_type()==0)&&(block->get_subtype()==014))){ |
| 155 | in_object=false; |
| 156 | objects.insert(objects.end(),current_object); |
| 157 | current_object.clear(); |
| 158 | } else { |
| 159 | in_object=true; |
| 160 | } |
| 161 | |
| 162 | break; |
| 163 | } // switch |
| 164 | } // generate loop |
| 165 | close(in_fd); |
| 166 | |
| 167 | // Now we have our blocks, tell the user about that! |
| 168 | char buffer[200]; |
| 169 | sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n", |
| 170 | read_pointer,blocks_read,errors,warnings); |
| 171 | if(cfg_output_info){ |
| 172 | write (out_fd,buffer,strlen(buffer)); |
| 173 | // } else { |
| 174 | // fprintf(stderr,buffer); |
| 175 | } |
| 176 | |
| 177 | if (errors>0){ |
| 178 | fprintf(stderr,"Errors make me sick. Goodbye!\n"); |
| 179 | exit(errcode); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | return 0; |
| 184 | } // main() |
| 185 | |