* The Shell.. for a long time we dreamed of having a compatible, properly working...
[reactos.git] / reactos / tools / bin2c.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS bin2c
4 * FILE: tools/bin2c/bin2c.c
5 * PURPOSE: Converts a binary file into a byte array
6 * PROGRAMMER: Hermès Bélusca - Maïto
7 */
8
9 #include <stdio.h>
10
11 int main(int argc, char *argv[])
12 {
13 FILE* inFile;
14 FILE* outCFile;
15 FILE* outHFile;
16 unsigned char ch;
17 unsigned char cnt;
18
19 /*
20 * Validate the arguments.
21 */
22 if (argc < 5)
23 {
24 printf("Usage: bin2c infile.bin outfile.c outfile.h array_name [array_attribute [header_for_attribute]]\n");
25 return -1;
26 }
27
28 /*
29 * Open the input and the output files.
30 */
31 inFile = fopen(argv[1], "rb");
32 if (!inFile)
33 {
34 printf("ERROR: Couldn't open data file '%s'.\n", argv[1]);
35 return -1;
36 }
37 outCFile = fopen(argv[2], "w");
38 if (!outCFile)
39 {
40 fclose(inFile);
41 printf("ERROR: Couldn't create output source file '%s'.\n", argv[2]);
42 return -1;
43 }
44 outHFile = fopen(argv[3], "w");
45 if (!outHFile)
46 {
47 fclose(outCFile);
48 fclose(inFile);
49 printf("ERROR: Couldn't create output header file '%s'.\n", argv[3]);
50 return -1;
51 }
52
53 /*
54 * Generate the header file and close it.
55 */
56 fprintf(outHFile, "/* This file is autogenerated, do not edit. */\n\n");
57 fprintf(outHFile, "#ifndef CHAR\n"
58 "#define CHAR char\n"
59 "#endif\n\n");
60 fprintf(outHFile, "extern CHAR %s[];\n", argv[4]);
61 fclose(outHFile);
62
63 /*
64 * Generate the source file and close it.
65 */
66 fprintf(outCFile, "/* This file is autogenerated, do not edit. */\n\n");
67 if (argc >= 7)
68 {
69 /* There is a header to be included for defining the array attribute. */
70 fprintf(outCFile, "#include \"%s\"\n", argv[6]);
71 }
72 fprintf(outCFile, "#include \"%s\"\n\n", argv[3]);
73
74 /* Generate the array. */
75 if (argc >= 6)
76 {
77 /* There is an array attribute. */
78 fprintf(outCFile, "%s ", argv[5]);
79 }
80 fprintf(outCFile, "CHAR %s[] =\n{", argv[4]);
81
82 cnt = 0;
83 ch = fgetc(inFile);
84 while (!feof(inFile))
85 {
86 if ((cnt % 16) == 0)
87 {
88 fprintf(outCFile, "\n ");
89 cnt = 0;
90 }
91 fprintf(outCFile, " 0x%02x,", (unsigned int)ch);
92 ++cnt;
93 ch = fgetc(inFile);
94 }
95 /* Put a final NULL terminator. */
96 fprintf(outCFile, "\n 0x00");
97 fprintf(outCFile, "\n};\n");
98 fclose(outCFile);
99
100 /* Close the input file. */
101 fclose(inFile);
102
103 return 0;
104 }
105
106 /* EOF */