1 |
/* Note for input file format: |
2 |
|
3 |
should declare first V,then A, then T all grouped by type |
4 |
|
5 |
*/ |
6 |
#include <stdio.h> |
7 |
#include <stdlib.h> |
8 |
|
9 |
#define FLISTNAME "PRH_ParamHandler_INFN_auto.id" |
10 |
#define TRUE 1 |
11 |
#define FALSE 0 |
12 |
#define MAXMODE 2 |
13 |
|
14 |
#define SETPARAMETERGEN 0x20 |
15 |
#define TESTDEBUG 0x43 |
16 |
|
17 |
struct list_el |
18 |
{ |
19 |
struct list_el *next; |
20 |
char string[255]; |
21 |
int id; |
22 |
}; |
23 |
|
24 |
typedef struct |
25 |
{ |
26 |
char str[255]; |
27 |
unsigned char code; |
28 |
} sub_struct; |
29 |
|
30 |
typedef struct list_el listItem; |
31 |
|
32 |
|
33 |
listItem *beginList,*beginCommand=NULL; |
34 |
unsigned char bufferOut[2048]; |
35 |
int subid; |
36 |
sub_struct subCommandMode[MAXMODE]={{"SetParameterGen",SETPARAMETERGEN},{"TestDebug",TESTDEBUG}}; |
37 |
|
38 |
|
39 |
void convertHex2Dec(char *str) |
40 |
{ |
41 |
char tmp[255]=""; |
42 |
char dec[10]=""; |
43 |
char *p,*s; |
44 |
unsigned int value; |
45 |
|
46 |
if ((p = strstr(str,"0x")) != NULL) |
47 |
{ |
48 |
strncat(tmp,str,p-str); |
49 |
sscanf(p+2,"%x",&value); |
50 |
sprintf(dec,"%d",value); |
51 |
strcat(tmp,dec); |
52 |
memcpy(str,tmp,strlen(tmp)); |
53 |
str[strlen(tmp)]=0; |
54 |
} |
55 |
} |
56 |
|
57 |
int loadList(char * filename,listItem **first,int command) |
58 |
{ |
59 |
FILE *fileList; |
60 |
char *s; |
61 |
char tmp[255],tmp_1[255]; |
62 |
|
63 |
int ret = TRUE; |
64 |
|
65 |
listItem *loc=NULL,*cur; |
66 |
|
67 |
if ((fileList = fopen(filename,"r")) != NULL) |
68 |
{ |
69 |
while (fgets(tmp,254,fileList) != NULL ) |
70 |
{ |
71 |
cur = (listItem*)malloc(sizeof(listItem)); |
72 |
|
73 |
if (loc == NULL) |
74 |
{ |
75 |
*first = cur; loc = cur; |
76 |
cur->next = NULL; |
77 |
} |
78 |
else |
79 |
{ |
80 |
loc->next = cur; |
81 |
loc = cur; |
82 |
cur->next = NULL; |
83 |
} |
84 |
|
85 |
if (command == TRUE) |
86 |
{ |
87 |
strncpy(cur->string,tmp,strlen(tmp)-1); |
88 |
} |
89 |
else |
90 |
{ |
91 |
sscanf(tmp,"%s %d\n",tmp_1,&ret); |
92 |
strncpy(cur->string,tmp_1,strlen(tmp_1)); |
93 |
} |
94 |
cur->id = ret; |
95 |
} |
96 |
ret = TRUE; |
97 |
fclose(fileList); |
98 |
} |
99 |
else |
100 |
{ |
101 |
printf("Cannot open file %s!!!\n",FLISTNAME); |
102 |
ret = FALSE; |
103 |
} |
104 |
|
105 |
return(ret); |
106 |
} |
107 |
|
108 |
void parseCommand() |
109 |
{ |
110 |
listItem *loc; |
111 |
char fileName[255]; |
112 |
int count = 0; |
113 |
int VCount = 0,VFirst = TRUE,VSave,VInc; |
114 |
int ACount = 0,AFirst = TRUE,ASave,AInc; |
115 |
int TCount = 0,TFirst = TRUE,TSave,TInc; |
116 |
|
117 |
loc = beginCommand; |
118 |
memset(bufferOut,0,2048); |
119 |
|
120 |
while (loc != NULL) |
121 |
{ |
122 |
if (count == 0) |
123 |
{ |
124 |
if ((loc->string[0] == 0x0a) || (loc->string[0] == 0x0)) |
125 |
{ |
126 |
printf("Finished !"); |
127 |
exit(0); |
128 |
} |
129 |
|
130 |
strncpy(fileName,loc->string,strlen(loc->string)+1); |
131 |
loc = loc->next; |
132 |
count++; |
133 |
|
134 |
if (strcmp(loc->string,"")) |
135 |
printf("Processing file: %s\n",fileName); |
136 |
|
137 |
continue; |
138 |
} |
139 |
|
140 |
if (count == 1) |
141 |
{ |
142 |
if ((subid = returnID(loc->string)) != -1) |
143 |
{ |
144 |
bufferOut[0] = 0; |
145 |
bufferOut[1] = subid; |
146 |
loc = loc->next; |
147 |
count+=3; |
148 |
continue; |
149 |
} |
150 |
else |
151 |
{ |
152 |
printf("Fatal Error: unknown subcommand %s\n",loc->string); |
153 |
exit(1); |
154 |
} |
155 |
} |
156 |
|
157 |
if (strcmp(loc->string,"")) |
158 |
printf("Processing: %s \n",loc->string); |
159 |
|
160 |
convertHex2Dec(loc->string); |
161 |
|
162 |
switch (subid) // last subid |
163 |
{ |
164 |
case SETPARAMETERGEN : |
165 |
|
166 |
switch (loc->string[0]) |
167 |
{ |
168 |
case 'V' : |
169 |
{ |
170 |
char type[255],name[255]; |
171 |
unsigned int value,id; |
172 |
|
173 |
sscanf(loc->string,"%s %s %d\n",type,name,&value); |
174 |
|
175 |
if (VFirst) |
176 |
{ |
177 |
VSave = count; // the place for the command number |
178 |
VFirst = FALSE; |
179 |
VInc = 1; // do not consider N after first time |
180 |
} |
181 |
|
182 |
if ((id = scanList4ID(name)) != -1) |
183 |
{ |
184 |
bufferOut[count+VInc] = id; |
185 |
bufferOut[count+1+VInc] = ((value & 0xff000000) >> 24); |
186 |
bufferOut[count+2+VInc] = ((value & 0x00ff0000) >> 16); |
187 |
bufferOut[count+3+VInc] = ((value & 0x0000ff00) >> 8); |
188 |
bufferOut[count+4+VInc] = value & 0x000000ff; |
189 |
|
190 |
count+=(5+VInc); |
191 |
VCount++; |
192 |
VInc = 0; |
193 |
} |
194 |
else |
195 |
{ |
196 |
printf("Fatal Error: unknown macrocommand %s\n",loc->string); |
197 |
exit(1); |
198 |
} |
199 |
|
200 |
if (loc->next) |
201 |
if (loc->next->string[0] != loc->string[0]) |
202 |
bufferOut[VSave] = VCount; |
203 |
} |
204 |
break; |
205 |
|
206 |
case 'A' : |
207 |
{ |
208 |
char type[255],name[255]; |
209 |
unsigned int value,id,index; |
210 |
|
211 |
if (VFirst) // write 0 if V is not present |
212 |
{ |
213 |
bufferOut[count] = 0; |
214 |
count++; |
215 |
VFirst = 0; |
216 |
} |
217 |
|
218 |
sscanf(loc->string,"%s %s %d %d\n",type,name,&index,&value); |
219 |
|
220 |
if (AFirst) |
221 |
{ |
222 |
ASave = count; // the place for the command number |
223 |
AFirst = FALSE; |
224 |
AInc = 1; |
225 |
} |
226 |
|
227 |
if ((id = scanList4ID(name)) != -1) |
228 |
{ |
229 |
bufferOut[count+AInc] = id; |
230 |
bufferOut[count+1+AInc] = index; |
231 |
bufferOut[count+2+AInc] = ((value & 0xff000000) >> 24); |
232 |
bufferOut[count+3+AInc] = ((value & 0x00ff0000) >> 16); |
233 |
bufferOut[count+4+AInc] = ((value & 0x0000ff00) >> 8); |
234 |
bufferOut[count+5+AInc] = value & 0x000000ff; |
235 |
count+=(6+AInc); |
236 |
ACount++; |
237 |
AInc = 0; |
238 |
} |
239 |
else |
240 |
{ |
241 |
printf("Fatal Error: unknown macrocommand %s\n",loc->string); |
242 |
exit(1); |
243 |
} |
244 |
|
245 |
if (loc->next) |
246 |
if (loc->next->string[0] != loc->string[0]) |
247 |
bufferOut[ASave] = ACount; |
248 |
} |
249 |
break; |
250 |
|
251 |
case 'T' : |
252 |
{ |
253 |
char type[255],name[255]; |
254 |
unsigned int value,id,row,col; |
255 |
|
256 |
sscanf(loc->string,"%s %s %d %d %d\n",type,name,&row,&col,&value); |
257 |
|
258 |
if (VFirst) // write 0 if V is not present |
259 |
{ |
260 |
|
261 |
bufferOut[count] = 0; |
262 |
count++; |
263 |
VFirst = 0; |
264 |
} |
265 |
|
266 |
if (AFirst) // write 0 if V is not present |
267 |
{ |
268 |
bufferOut[count] = 0; |
269 |
count++; |
270 |
AFirst = 0; |
271 |
} |
272 |
|
273 |
if (TFirst) |
274 |
{ |
275 |
TSave = count; // the place for the command number |
276 |
TFirst = FALSE; |
277 |
TInc = 1; |
278 |
} |
279 |
|
280 |
if ((id = scanList4ID(name)) != -1) |
281 |
{ |
282 |
bufferOut[count+TInc] = id; |
283 |
bufferOut[count+1+TInc] = row; |
284 |
bufferOut[count+2+TInc] = col; |
285 |
bufferOut[count+3+TInc] = ((value & 0xff000000) >> 24); |
286 |
bufferOut[count+4+TInc] = ((value & 0x00ff0000) >> 16); |
287 |
bufferOut[count+5+TInc] = ((value & 0x0000ff00) >> 8); |
288 |
bufferOut[count+6+TInc] = value & 0x000000ff; |
289 |
|
290 |
count+=(7+TInc); |
291 |
TCount++; |
292 |
TInc = 0; |
293 |
} |
294 |
else |
295 |
{ |
296 |
printf("Fatal Error: unknown macrocommand %s\n",loc->string); |
297 |
exit(1); |
298 |
} |
299 |
|
300 |
if (loc->next) |
301 |
if (loc->next->string[0] != loc->string[0]) |
302 |
bufferOut[TSave] = TCount; |
303 |
} |
304 |
break; |
305 |
case 0 : |
306 |
{ |
307 |
int c; |
308 |
|
309 |
if (VFirst) // write 0 if V is not present |
310 |
{ |
311 |
bufferOut[count] = 0; |
312 |
count++; |
313 |
} |
314 |
|
315 |
if (AFirst) // write 0 if V is not present |
316 |
{ |
317 |
bufferOut[count] = 0; |
318 |
count++; |
319 |
} |
320 |
|
321 |
if (TFirst) // write 0 if V is not present |
322 |
{ |
323 |
bufferOut[count] = 0; |
324 |
count++; |
325 |
} |
326 |
|
327 |
c = count; |
328 |
|
329 |
while ((count % 4) != 0) |
330 |
count++; |
331 |
|
332 |
memset(&bufferOut[c],0,count-c); |
333 |
|
334 |
bufferOut[2] = (((count-4)/4) & 0x0100) >> 8; |
335 |
bufferOut[3] = ((count-4)/4) & 0x00ff; |
336 |
|
337 |
if (!write2File(fileName,count)) |
338 |
{ |
339 |
printf("Error writing on file %s\n",fileName); |
340 |
exit(1); |
341 |
} |
342 |
|
343 |
count = 0; |
344 |
VCount = 0; VFirst = TRUE; |
345 |
ACount = 0; AFirst = TRUE; |
346 |
TCount = 0; TFirst = TRUE; |
347 |
} |
348 |
break; |
349 |
|
350 |
default : |
351 |
printf("Fatal Error : variable identifier not known !\n"); |
352 |
exit(1); |
353 |
} |
354 |
break; |
355 |
|
356 |
case TESTDEBUG: |
357 |
{ |
358 |
unsigned int value; |
359 |
|
360 |
if (loc->string[0]) |
361 |
{ |
362 |
sscanf(loc->string,"%d\n",&value); |
363 |
|
364 |
bufferOut[count] = ((value & 0xff00) >> 8); |
365 |
bufferOut[count+1] = value & 0x00ff; |
366 |
count+=2; |
367 |
} |
368 |
else |
369 |
{ |
370 |
bufferOut[2] = 0; |
371 |
bufferOut[3] = 1; |
372 |
|
373 |
if (!write2File(fileName,count)) |
374 |
{ |
375 |
printf("Error writing on file %s\n",fileName); |
376 |
exit(1); |
377 |
} |
378 |
|
379 |
count = 0; |
380 |
} |
381 |
|
382 |
} |
383 |
break; |
384 |
} |
385 |
|
386 |
loc = loc->next; |
387 |
|
388 |
} |
389 |
} |
390 |
|
391 |
int returnID(char *key) |
392 |
{ |
393 |
int i; |
394 |
|
395 |
for (i=0; i < MAXMODE; i++) |
396 |
{ |
397 |
if (strncmp(key,subCommandMode[i].str,strlen(subCommandMode[i].str)) == 0) |
398 |
return subCommandMode[i].code; |
399 |
} |
400 |
return(-1); |
401 |
} |
402 |
|
403 |
int scanList4ID(char *name) |
404 |
{ |
405 |
listItem *loc; |
406 |
|
407 |
loc = beginList; |
408 |
|
409 |
while (loc != NULL) |
410 |
{ |
411 |
if (strncmp(name,loc->string,strlen(loc->string)) == 0) |
412 |
return loc->id; |
413 |
loc = loc->next; |
414 |
} |
415 |
return(-1); |
416 |
} |
417 |
|
418 |
|
419 |
int write2File(char *filename,int count) |
420 |
{ |
421 |
FILE *out; |
422 |
int ret = TRUE; |
423 |
int i = 0,c; |
424 |
char binname[255]; |
425 |
unsigned short data; |
426 |
|
427 |
c = count; |
428 |
|
429 |
if ((out = fopen(filename,"w")) != NULL) |
430 |
{ |
431 |
count = count / 2; |
432 |
|
433 |
while (count--) |
434 |
{ |
435 |
fprintf(out,"0x%02x%02x\n",bufferOut[i],bufferOut[i+1]); |
436 |
i+=2; |
437 |
} |
438 |
fclose(out); |
439 |
} |
440 |
else |
441 |
ret = FALSE; |
442 |
|
443 |
|
444 |
strcpy(binname,filename); |
445 |
strcat(binname,".bin"); |
446 |
|
447 |
if ((out = fopen(binname,"w")) != NULL) |
448 |
{ |
449 |
c = c / 2; i = 0; |
450 |
|
451 |
while (c--) |
452 |
{ |
453 |
data = *(unsigned short*)&bufferOut[i]; |
454 |
fputc((unsigned char)((data & 0xff00) >> 8),out); |
455 |
fputc((unsigned char)((data & 0x00ff)),out); |
456 |
i+=2; |
457 |
} |
458 |
|
459 |
fclose(out); |
460 |
} |
461 |
else |
462 |
ret = FALSE; |
463 |
|
464 |
return(ret); |
465 |
} |
466 |
|
467 |
void printList(listItem *first) |
468 |
{ |
469 |
listItem *loc; |
470 |
|
471 |
loc = first; |
472 |
|
473 |
while (loc != NULL) |
474 |
{ |
475 |
printf("%s %d\n",loc->string,loc->id); |
476 |
loc = loc->next; |
477 |
} |
478 |
} |
479 |
int main (int argc,char *argv[]) |
480 |
{ |
481 |
if (argc > 1) |
482 |
{ |
483 |
loadList(FLISTNAME,&beginList,FALSE); |
484 |
loadList(argv[1],&beginCommand,TRUE); |
485 |
|
486 |
printf("Processing file %s\n\n",argv[1]); |
487 |
|
488 |
parseCommand(); |
489 |
} |
490 |
else |
491 |
printf("\nMissing filename !!\n"); |
492 |
return 0; |
493 |
} |
494 |
|
495 |
|