summaryrefslogtreecommitdiff
path: root/bin2c.cpp
blob: 69561a12a5443a592c7d837d19d4e7e6d394961e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <Input.h>
#include <Output.h>
#include <Main.h>

CODE_BEGINS
virtual int startup() throw (GeneralException) {
    int i;
    
    if (argc != 4) {
	printm(M_BARE, "Usage: %s <bin file> <c file> <symbol>\n", argv[0]);
	exit(-1);
    }
    
    printm(M_BARE, "Starting converting %s to %s:\n", argv[1], argv[2]);
    
    Input binfile(argv[1]);
    Output cfile(argv[2]);
    
    unsigned char * map = (unsigned char *) binfile.mmap();
    
    cfile << "int " << argv[3] << "_size = " << binfile.GetSize() << ";\n";
    cfile << "unsigned char " << argv[3] << "[] = {";
    
    for (i = 0; i < binfile.GetSize(); i++) {
	String s;
	
	s.set("0x%02x, ", map[i]);
	if (!(i % 16)) {
	    cfile << "\n\t";
	    printm(M_BARE, "%5.2f%%\r", i * 100.0 / binfile.GetSize());
	}
	cfile << s;
    }
    
    cfile << "\n};\n";
    printm(M_BARE, "Done!  \n");
    
    return 0;
}
CODE_ENDS