798b0c1d |
1 | /* ldc2 preliminary main program */ |
2 | |
3 | #include <vector> |
4 | #include <string> |
f9d603d2 |
5 | |
ea4c19a4 |
6 | #include "config.hh" |
7 | #include "tool.hh" |
97b26985 |
8 | |
9 | #include "tape_block.hh" |
fed2c751 |
10 | #include "data_block_0.hh" |
11 | |
798b0c1d |
12 | using namespace std; |
13 | |
fed2c751 |
14 | /*! |
15 | *\brief The main routine. |
fed2c751 |
16 | */ |
798b0c1d |
17 | int main(int argc, char ** args){ |
fed2c751 |
18 | |
ea4c19a4 |
19 | // Do all the configuration stuff |
20 | do_config(argc,args); |
50c86ded |
21 | |
ea4c19a4 |
22 | // Now we go to read the data! |
ad324d29 |
23 | |
909d3603 |
24 | |
ad324d29 |
25 | vector<tape_block*> tape; |
fed2c751 |
26 | vector<vector<tape_block *> > objects; |
27 | vector<tape_block *> current_object; |
ad324d29 |
28 | |
29 | tape_block * block=0; |
fed2c751 |
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){ |
ad324d29 |
41 | block=tape_block::gen_from_fd(in_fd); |
fed2c751 |
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 { |
ea4c19a4 |
53 | if (cfg_ignore_object_integrity_errors){ |
fed2c751 |
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; |
ea4c19a4 |
66 | if (cfg_ignoreblock_errors){ |
fed2c751 |
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; |
ea4c19a4 |
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)); |
fed2c751 |
92 | fprintf(stderr,"Warning: Block checksum wrong!\n"); |
ea4c19a4 |
93 | fprintf(stderr,buffer); |
fed2c751 |
94 | warnings++; |
95 | } else { |
96 | fprintf(stderr,"Error: Block checksum wrong!\n"); |
ea4c19a4 |
97 | fprintf(stderr,buffer); |
fed2c751 |
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){ |
ea4c19a4 |
113 | fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str()); |
fed2c751 |
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: |
ad324d29 |
121 | tape.insert(tape.end(),block); |
fed2c751 |
122 | blocks_read++; |
123 | |
ea4c19a4 |
124 | if (cfg_verbose){ |
fed2c751 |
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 | |
ea4c19a4 |
134 | if(cfg_list_contents) |
fed2c751 |
135 | dump_vector(block->get_description()); |
136 | |
137 | if (block->get_type()==0x10){ // Unknown data block! |
ea4c19a4 |
138 | if (cfg_ignore_unknown_block_errors){ |
fed2c751 |
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); |
798b0c1d |
166 | |
fed2c751 |
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); |
ea4c19a4 |
171 | if(cfg_output_info){ |
fed2c751 |
172 | write (out_fd,buffer,strlen(buffer)); |
ea4c19a4 |
173 | // } else { |
174 | // fprintf(stderr,buffer); |
fed2c751 |
175 | } |
176 | |
177 | if (errors>0){ |
178 | fprintf(stderr,"Errors make me sick. Goodbye!\n"); |
179 | exit(errcode); |
ad324d29 |
180 | } |
181 | |
fed2c751 |
182 | |
ad324d29 |
183 | return 0; |
184 | } // main() |
798b0c1d |
185 | |