Debian Codegraph @

yamdrv/mcs2h.c

raw
1 #include <stdio.h>
2 #include <time.h>
3 #include <string.h>
4
5 static int in2hex(char *ptr)
6 {
7 char str[3];
8 unsigned int val;
9
10 memcpy(str, ptr, 2);
11 str[2] = '\0';
12
13 sscanf(str, "%x", &val);
14
15 return val;
16 }
17
18 static unsigned char swap(unsigned char c)
19 {
20 unsigned char r = 0;
21 int i;
22
23 for (i = 0 ; i < 8 ; i++)
24 {
25 r <<= 1;
26 if (c & 1)
27 r |= 1;
28 c >>= 1;
29 }
30 return r;
31 }
32
33 int main(int ac, char *av[])
34 {
35 int nb, type, i;
36 int first = 1;
37 time_t temps;
38 FILE *fptr;
39 char buf[256];
40
41 if (ac != 3)
42 {
43 fprintf(stderr, "format : mcs2h 1200|9600 filename\n");
44 return 1;
45 }
46
47 fptr = fopen(av[2], "r");
48 if (fptr == NULL)
49 {
50 fprintf(stderr, "file %s not found\n", av[2]);
51 return 1;
52 }
53
54 time(&temps);
55
56 printf( "/*\n"
57 " *\n"
58 " * File %s converted to h format by mcs2h\n"
59 " *\n"
60 " * (C) F6FBB 1998\n"
61 " *\n"
62 " * %s"
63 " *\n"
64 " */\n\n",
65 av[2], ctime(&temps));
66
67 printf("static unsigned char bits_%s[]= {\n", av[1]);
68
69 while (fgets(buf, sizeof(buf), fptr))
70 {
71 nb = in2hex(buf+1);
72 type = in2hex(buf+7);
73
74 if (type != 0)
75 continue;
76
77 if (first)
78 first = 0;
79 else
80 printf(",\n");
81
82 for (i = 0 ; i < nb ; i++)
83 {
84 printf("0x%02x%s", swap(in2hex(buf+9+i*2)), (i < (nb-1)) ? "," : "");
85 }
86 }
87
88 printf(" };\n");
89
90 fclose(fptr);
91 return 0;
92 }