FreeCalypso > hg > freecalypso-tools
comparison ffstools/cal2text/main.c @ 142:d41edd329670
fc-cal2text utility written, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 27 Feb 2017 03:37:11 +0000 |
parents | |
children | 329c31f7c797 |
comparison
equal
deleted
inserted
replaced
141:6b01d4ef85c3 | 142:d41edd329670 |
---|---|
1 /* | |
2 * The main() function for fc-cal2text lives here. | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <sys/param.h> | |
7 #include <sys/file.h> | |
8 #include <sys/stat.h> | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <unistd.h> | |
12 | |
13 char *input_dir_base, *output_dir_base; | |
14 | |
15 extern void write_afcdac_ascii(); | |
16 extern void write_stdmap_ascii(); | |
17 extern void write_afcparams_table(); | |
18 extern void write_agcwords_table(); | |
19 extern void write_agcglobals_table(); | |
20 extern void write_il2agc_table(); | |
21 extern void write_tx_levels_table(); | |
22 extern void write_tx_calchan_table(); | |
23 extern void write_tx_caltemp_table(); | |
24 extern void write_rx_calchan_table(); | |
25 extern void write_rx_caltemp_table(); | |
26 extern void write_rx_agcparams_table(); | |
27 extern void write_tx_ramp(); | |
28 | |
29 struct output_chunk { | |
30 unsigned offset; | |
31 char *outfile; | |
32 void (*conv_func)(); | |
33 }; | |
34 | |
35 static struct output_chunk afcdac_handling = { | |
36 0, "afcdac", write_afcdac_ascii | |
37 }; | |
38 | |
39 static struct output_chunk stdmap_handling = { | |
40 0, "stdmap", write_stdmap_ascii | |
41 }; | |
42 | |
43 static struct output_chunk afcparams_handling = { | |
44 0, "afcparams", write_afcparams_table | |
45 }; | |
46 | |
47 static struct output_chunk agcglobals_handling = { | |
48 0, "agcglobals", write_agcglobals_table | |
49 }; | |
50 | |
51 static struct output_chunk il2agc_handling[3] = { | |
52 {0, "il2agc-pwr", write_il2agc_table}, | |
53 {121, "il2agc-max", write_il2agc_table}, | |
54 {242, "il2agc-av", write_il2agc_table}, | |
55 }; | |
56 | |
57 static struct output_chunk agcwords_handling = { | |
58 0, "agcwords", write_agcwords_table | |
59 }; | |
60 | |
61 static struct output_chunk tx_ramps_handling[16] = { | |
62 {0x000, "ramp00", write_tx_ramp}, | |
63 {0x020, "ramp01", write_tx_ramp}, | |
64 {0x040, "ramp02", write_tx_ramp}, | |
65 {0x060, "ramp03", write_tx_ramp}, | |
66 {0x080, "ramp04", write_tx_ramp}, | |
67 {0x0A0, "ramp05", write_tx_ramp}, | |
68 {0x0C0, "ramp06", write_tx_ramp}, | |
69 {0x0E0, "ramp07", write_tx_ramp}, | |
70 {0x100, "ramp08", write_tx_ramp}, | |
71 {0x120, "ramp09", write_tx_ramp}, | |
72 {0x140, "ramp10", write_tx_ramp}, | |
73 {0x160, "ramp11", write_tx_ramp}, | |
74 {0x180, "ramp12", write_tx_ramp}, | |
75 {0x1A0, "ramp13", write_tx_ramp}, | |
76 {0x1C0, "ramp14", write_tx_ramp}, | |
77 {0x1E0, "ramp15", write_tx_ramp}, | |
78 }; | |
79 | |
80 static struct output_chunk tx_levels_handling = { | |
81 0, "levels", write_tx_levels_table | |
82 }; | |
83 | |
84 static struct output_chunk tx_calchan_handling = { | |
85 0, "calchan", write_tx_calchan_table | |
86 }; | |
87 | |
88 static struct output_chunk tx_caltemp_handling = { | |
89 0, "caltemp", write_tx_caltemp_table | |
90 }; | |
91 | |
92 static struct output_chunk rx_calchan_handling = { | |
93 0, "calchan", write_rx_calchan_table | |
94 }; | |
95 | |
96 static struct output_chunk rx_caltemp_handling = { | |
97 0, "caltemp", write_rx_caltemp_table | |
98 }; | |
99 | |
100 static struct output_chunk rx_agcparams_handling = { | |
101 0, "agcparams", write_rx_agcparams_table | |
102 }; | |
103 | |
104 static struct input_file { | |
105 char *filename; | |
106 unsigned expect_size; | |
107 char *outdir; | |
108 struct output_chunk *handling; | |
109 unsigned nchunks; | |
110 } input_file_list[] = { | |
111 {"afcdac", 2, "global", &afcdac_handling, 1}, | |
112 {"stdmap", 2, "global", &stdmap_handling, 1}, | |
113 {"afcparams", 24, "global", &afcparams_handling, 1}, | |
114 {"rx/agcglobals", 8, "global", &agcglobals_handling, 1}, | |
115 {"rx/il2agc", 363, "global", il2agc_handling, 3}, | |
116 {"rx/agcwords", 40, "global", &agcwords_handling, 1}, | |
117 | |
118 {"tx/ramps.850", 512, "tx-850", tx_ramps_handling, 16}, | |
119 {"tx/levels.850", 128, "tx-850", &tx_levels_handling, 1}, | |
120 {"tx/calchan.850", 128, "tx-850", &tx_calchan_handling, 1}, | |
121 {"tx/caltemp.850", 40, "tx-850", &tx_caltemp_handling, 1}, | |
122 {"rx/calchan.850", 40, "rx-850", &rx_calchan_handling, 1}, | |
123 {"rx/caltemp.850", 44, "rx-850", &rx_caltemp_handling, 1}, | |
124 {"rx/agcparams.850", 8, "rx-850", &rx_agcparams_handling, 1}, | |
125 | |
126 {"tx/ramps.900", 512, "tx-900", tx_ramps_handling, 16}, | |
127 {"tx/levels.900", 128, "tx-900", &tx_levels_handling, 1}, | |
128 {"tx/calchan.900", 128, "tx-900", &tx_calchan_handling, 1}, | |
129 {"tx/caltemp.900", 40, "tx-900", &tx_caltemp_handling, 1}, | |
130 {"rx/calchan.900", 40, "rx-900", &rx_calchan_handling, 1}, | |
131 {"rx/caltemp.900", 44, "rx-900", &rx_caltemp_handling, 1}, | |
132 {"rx/agcparams.900", 8, "rx-900", &rx_agcparams_handling, 1}, | |
133 | |
134 {"tx/ramps.1800", 512, "tx-1800", tx_ramps_handling, 16}, | |
135 {"tx/levels.1800", 128, "tx-1800", &tx_levels_handling, 1}, | |
136 {"tx/calchan.1800", 128, "tx-1800", &tx_calchan_handling, 1}, | |
137 {"tx/caltemp.1800", 40, "tx-1800", &tx_caltemp_handling, 1}, | |
138 {"rx/calchan.1800", 40, "rx-1800", &rx_calchan_handling, 1}, | |
139 {"rx/caltemp.1800", 44, "rx-1800", &rx_caltemp_handling, 1}, | |
140 {"rx/agcparams.1800", 8, "rx-1800", &rx_agcparams_handling, 1}, | |
141 | |
142 {"tx/ramps.1900", 512, "tx-1900", tx_ramps_handling, 16}, | |
143 {"tx/levels.1900", 128, "tx-1900", &tx_levels_handling, 1}, | |
144 {"tx/calchan.1900", 128, "tx-1900", &tx_calchan_handling, 1}, | |
145 {"tx/caltemp.1900", 40, "tx-1900", &tx_caltemp_handling, 1}, | |
146 {"rx/calchan.1900", 40, "rx-1900", &rx_calchan_handling, 1}, | |
147 {"rx/caltemp.1900", 44, "rx-1900", &rx_caltemp_handling, 1}, | |
148 {"rx/agcparams.1900", 8, "rx-1900", &rx_agcparams_handling, 1}, | |
149 | |
150 {0, 0, 0, 0, 0} | |
151 }; | |
152 | |
153 static | |
154 try_read_input_file(ip, buf) | |
155 struct input_file *ip; | |
156 u_char *buf; | |
157 { | |
158 char pathname[MAXPATHLEN]; | |
159 int fd; | |
160 struct stat st; | |
161 | |
162 sprintf(pathname, "%s/%s", input_dir_base, ip->filename); | |
163 fd = open(pathname, O_RDONLY); | |
164 if (fd < 0) | |
165 return(0); | |
166 fstat(fd, &st); | |
167 if (!S_ISREG(st.st_mode)) { | |
168 close(fd); | |
169 fprintf(stderr, "%s is not a regular file, skipping\n", | |
170 pathname); | |
171 return(0); | |
172 } | |
173 if (st.st_size != ip->expect_size) { | |
174 close(fd); | |
175 fprintf(stderr, | |
176 "%s has the wrong length (expected %u bytes), skipping\n", | |
177 pathname, ip->expect_size); | |
178 return(0); | |
179 } | |
180 read(fd, buf, ip->expect_size); | |
181 close(fd); | |
182 return(1); | |
183 } | |
184 | |
185 static void | |
186 write_outputs(ip, buf) | |
187 struct input_file *ip; | |
188 u_char *buf; | |
189 { | |
190 char pathname[MAXPATHLEN]; | |
191 struct output_chunk *op; | |
192 unsigned nc; | |
193 FILE *of; | |
194 | |
195 sprintf(pathname, "%s/%s", output_dir_base, ip->outdir); | |
196 mkdir_existok(pathname); | |
197 op = ip->handling; | |
198 for (nc = 0; nc < ip->nchunks; op++, nc++) { | |
199 sprintf(pathname, "%s/%s/%s", output_dir_base, ip->outdir, | |
200 op->outfile); | |
201 of = fopen(pathname, "w"); | |
202 if (!of) { | |
203 perror(pathname); | |
204 exit(1); | |
205 } | |
206 op->conv_func(buf + op->offset, of); | |
207 fclose(of); | |
208 } | |
209 } | |
210 | |
211 main(argc, argv) | |
212 char **argv; | |
213 { | |
214 struct input_file *ip; | |
215 u_char buf[512]; | |
216 | |
217 if (argc != 3) { | |
218 fprintf(stderr, "usage: %s input-dir output-dir\n", argv[0]); | |
219 exit(1); | |
220 } | |
221 check_directory_exists(argv[1]); | |
222 input_dir_base = argv[1]; | |
223 check_directory_exists(argv[2]); | |
224 output_dir_base = argv[2]; | |
225 | |
226 for (ip = input_file_list; ip->filename; ip++) { | |
227 if (try_read_input_file(ip, buf)) | |
228 write_outputs(ip, buf); | |
229 } | |
230 exit(0); | |
231 } |