798b0c1d |
1 | /* ldc2 preliminary main program */ |
2 | |
7880ae2d |
3 | |
798b0c1d |
4 | #include <vector> |
5 | #include <string> |
f9d603d2 |
6 | |
ea4c19a4 |
7 | #include "config.hh" |
8 | #include "tool.hh" |
97b26985 |
9 | |
10 | #include "tape_block.hh" |
fed2c751 |
11 | #include "data_block_0.hh" |
12 | |
798b0c1d |
13 | using namespace std; |
14 | |
7880ae2d |
15 | #ifndef BUILD_STAMP |
16 | #define BUILD_STAMP "" |
17 | #endif |
18 | |
19 | #ifndef VERSION |
20 | #define VERSION "0.0" |
21 | #endif |
22 | |
23 | #define ID_STRING "\n ldc2 - The X16 object analyser\n\ |
24 | (C) 2007 Philipp Hachtmann\n\n\ |
25 | Version %s, built %s\n %s\n\n" |
26 | |
27 | |
28 | |
fed2c751 |
29 | /*! |
30 | *\brief The main routine. |
fed2c751 |
31 | */ |
798b0c1d |
32 | int main(int argc, char ** args){ |
7880ae2d |
33 | |
fed2c751 |
34 | |
ea4c19a4 |
35 | // Do all the configuration stuff |
36 | do_config(argc,args); |
50c86ded |
37 | |
7880ae2d |
38 | if (cfg_version){ |
39 | fprintf(stderr, ID_STRING, VERSION, BUILD_DATE, BUILD_STAMP); |
40 | exit(0); |
41 | } |
909d3603 |
42 | |
ad324d29 |
43 | vector<tape_block*> tape; |
fed2c751 |
44 | vector<vector<tape_block *> > objects; |
45 | vector<tape_block *> current_object; |
ad324d29 |
46 | |
47 | tape_block * block=0; |
fed2c751 |
48 | bool read_ahead=true; |
7880ae2d |
49 | |
fed2c751 |
50 | int read_pointer=0; |
51 | int block_start=0; |
52 | int block_end=0; |
53 | int blocks_read=0; |
7880ae2d |
54 | |
fed2c751 |
55 | bool in_object=false; |
7880ae2d |
56 | |
fed2c751 |
57 | int errors=0; |
58 | int warnings=0; |
59 | int errcode=0; |
60 | |
7880ae2d |
61 | bool spaced=false; |
62 | |
fed2c751 |
63 | while(read_ahead){ |
7880ae2d |
64 | try{ |
65 | block=tape_block::gen_from_fd(in_fd); |
66 | blocks_read++; |
67 | tape.insert(tape.end(),block); |
68 | block_start=read_pointer+block->get_discarded_bytes(); |
69 | block_end=block_start+block->get_raw_size()-1; |
70 | read_pointer=block_end+1; |
71 | |
72 | if (cfg_verbose) { |
ea4c19a4 |
73 | char buffer[200]; |
7880ae2d |
74 | if (!spaced) fprintf(stderr,"\n"); |
75 | spaced=false; |
76 | |
77 | snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", |
ea4c19a4 |
78 | blocks_read-1, |
79 | block_start, |
80 | block_end, |
81 | block->get_raw_size()); |
ea4c19a4 |
82 | fprintf(stderr,buffer); |
fed2c751 |
83 | } |
7880ae2d |
84 | if(cfg_list_contents) |
85 | dump_vector(block->get_description()); |
86 | } |
87 | |
88 | catch(tape_block::eof_legal_exception &e){ |
89 | read_pointer+=e.get_consumed(); |
90 | break; // Immediately leave read loop. |
91 | } |
92 | |
93 | catch(tape_block::io_error_exception){ |
fed2c751 |
94 | if (in_fd){ |
7880ae2d |
95 | fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str()); |
fed2c751 |
96 | } else { |
7880ae2d |
97 | fprintf(stderr,"Error: Could not read from stdin!\n"); |
fed2c751 |
98 | } |
99 | errors++; |
fed2c751 |
100 | errcode=2; |
7880ae2d |
101 | break; // Immediately leave read loop. |
102 | } |
103 | |
104 | catch(tape_block::eof_illegal_exception &e){ |
105 | block=e.get_block(); |
fed2c751 |
106 | blocks_read++; |
7880ae2d |
107 | tape.insert(tape.end(),block); |
108 | block_start=read_pointer+block->get_discarded_bytes(); |
109 | block_end=block_start+block->get_raw_size()-1; |
110 | read_pointer=block_end+1; |
fed2c751 |
111 | |
7880ae2d |
112 | if (!spaced) fprintf(stderr,"\n"); |
113 | spaced=false; |
114 | char buffer[200]; |
115 | snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", |
116 | blocks_read-1, |
117 | block_start, |
118 | block_end, |
119 | block->get_raw_size()); |
120 | fprintf(stderr,buffer); |
121 | |
122 | if (cfg_ignore_block_errors){ |
123 | fprintf(stderr,"Warning: Block integrity check failed!\n"); |
124 | warnings++; |
125 | } else { |
126 | fprintf(stderr,"Error: Block integrity check failed!\n"); |
127 | errors++; |
128 | errcode=3; |
129 | read_ahead=false; |
fed2c751 |
130 | } |
ea4c19a4 |
131 | if(cfg_list_contents) |
fed2c751 |
132 | dump_vector(block->get_description()); |
7880ae2d |
133 | fprintf(stderr,"\n"); |
134 | spaced=true; |
135 | } |
136 | |
137 | catch(tape_block::checksum_error_exception &e){ |
138 | block=e.get_block(); |
139 | blocks_read++; |
140 | tape.insert(tape.end(),block); |
141 | block_start=read_pointer+block->get_discarded_bytes(); |
142 | block_end=block_start+block->get_raw_size()-1; |
143 | read_pointer=block_end+1; |
fed2c751 |
144 | |
fed2c751 |
145 | |
7880ae2d |
146 | if (!spaced) fprintf(stderr,"\n"); |
147 | spaced=false; |
148 | char buffer[200]; |
149 | snprintf(buffer,200,"Block No.%03i: Start:0x%04x End:0x%04x Size:0x%02x\n", |
150 | blocks_read-1, |
151 | block_start, |
152 | block_end, |
153 | block->get_raw_size()); |
154 | fprintf(stderr,buffer); |
fed2c751 |
155 | |
7880ae2d |
156 | if (cfg_ignore_checksum_errors){ |
157 | fprintf(stderr,"Warning: Block checksum wrong!\n"); |
158 | warnings++; |
fed2c751 |
159 | } else { |
7880ae2d |
160 | fprintf(stderr,"Error: Block checksum wrong!\n"); |
161 | errors++; |
162 | read_ahead=false; |
163 | errcode=4; |
fed2c751 |
164 | } |
7880ae2d |
165 | if(cfg_list_contents) |
166 | dump_vector(block->get_description()); |
167 | fprintf(stderr,"\n"); |
168 | spaced=true; |
169 | } |
170 | |
171 | |
172 | } // while (read_ahead) |
798b0c1d |
173 | |
fed2c751 |
174 | // Now we have our blocks, tell the user about that! |
7880ae2d |
175 | if (!spaced) fprintf(stderr,"\n"); |
176 | spaced=false; |
fed2c751 |
177 | char buffer[200]; |
178 | sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n", |
7880ae2d |
179 | read_pointer,blocks_read,errors,warnings); |
ea4c19a4 |
180 | if(cfg_output_info){ |
fed2c751 |
181 | write (out_fd,buffer,strlen(buffer)); |
7880ae2d |
182 | } else { |
183 | fprintf(stderr,buffer); |
fed2c751 |
184 | } |
7880ae2d |
185 | |
fed2c751 |
186 | if (errors>0){ |
7880ae2d |
187 | fprintf(stderr,"Failed. (%i)\n",errcode); |
fed2c751 |
188 | exit(errcode); |
ad324d29 |
189 | } |
7880ae2d |
190 | |
191 | if (cfg_verbose){ |
192 | fprintf(stderr,"Data read successfully.\n"); |
193 | } |
194 | close(in_fd); |
195 | |
196 | // switch(block->get_state()){ // switchy |
197 | // case tape_block::TBS_EOF_LEGAL: |
198 | // delete block; |
199 | // if (!in_object){ |
200 | // fprintf(stderr,"File successfully read.\n"); |
201 | // } else { |
202 | // if (cfg_ignore_object_integrity_errors){ |
203 | // fprintf(stderr,"Warning: Object integrity check failed!\n"); |
204 | // warnings++; |
205 | // } else { |
206 | // fprintf(stderr,"Error: Object integrity check failed!\n"); |
207 | // errors++; |
208 | // errcode=6; |
209 | // } |
210 | // } |
211 | // read_ahead=false; |
212 | // break; |
213 | // case tape_block::TBS_EOF_ILLEGAL: |
214 | // break; |
215 | // case tape_block::TBS_CHECKSUM: |
216 | // break; |
217 | // case tape_block::TBS_DEFAULT: |
218 | // delete block; |
219 | // fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n"); |
220 | // errors++; |
221 | // errcode=100; |
222 | // read_ahead=false; |
223 | // break; |
224 | // case tape_block::TBS_IOERR: |
225 | // case tape_block::TBS_OK: |
226 | // tape.insert(tape.end(),block); |
227 | // blocks_read++; |
228 | |
229 | // if (cfg_verbose){ |
230 | // char buffer[200]; |
231 | // snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", |
232 | // blocks_read-1, |
233 | // block_start, |
234 | // block_end, |
235 | // block->get_raw_size()); |
236 | // write (out_fd,buffer,strlen(buffer)); |
237 | // } |
ad324d29 |
238 | |
7880ae2d |
239 | // if(cfg_list_contents) |
240 | // dump_vector(block->get_description()); |
241 | |
242 | // if (block->get_type()==0x10){ // Unknown data block! |
243 | // if (cfg_ignore_unknown_block_errors){ |
244 | // fprintf(stderr,"Warning: Unknown block type!\n"); |
245 | // warnings++; |
246 | // }else { |
247 | // fprintf(stderr,"Error: Unknown block type!\n"); |
248 | // errors++; |
249 | // errcode=5; |
250 | // read_ahead=false; |
251 | // } |
252 | // } |
253 | |
254 | // current_object.insert(current_object.end(),block); |
255 | |
256 | // if ((block->get_type()==data_block::TBT_EOT) |
257 | // ||((block->get_type()==0)&&(block->get_subtype()==3)) |
258 | // ||((block->get_type()==4)||(block->get_type()==3)) |
259 | // ||((block->get_type()==0)&&(block->get_subtype()==014))){ |
260 | // in_object=false; |
261 | // objects.insert(objects.end(),current_object); |
262 | // current_object.clear(); |
263 | // } else { |
264 | // in_object=true; |
265 | // } |
266 | |
267 | // break; |
268 | // } // switch |
269 | // } // generate loop |
270 | |
271 | |
272 | // } |
273 | |
fed2c751 |
274 | |
ad324d29 |
275 | return 0; |
276 | } // main() |
798b0c1d |
277 | |