85 lines
2.1 KiB
Python
Executable File
85 lines
2.1 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
import sys
|
|
import re
|
|
|
|
def cname(s):
|
|
c = re.sub(r'\W', '_', s)
|
|
if re.search(r'^\d', c):
|
|
c = '_' + c
|
|
return c
|
|
|
|
def main(argv):
|
|
if len(argv) < 3:
|
|
sys.stderr.write('Usage: cfs_gen.py out_file.c path1 [path2...]\n')
|
|
return 2
|
|
|
|
out_fname = argv[1]
|
|
paths = argv[2:]
|
|
|
|
store = {}
|
|
header_fname = re.sub(r'\.cc?', '.h', out_fname)
|
|
if header_fname == out_fname:
|
|
sys.stderr.write('Unrecognized output file name extension\n')
|
|
return 2
|
|
c_file = open(out_fname, 'w')
|
|
h_file = open(header_fname, 'w')
|
|
c_file.write('#include <string.h>\n')
|
|
for p in paths:
|
|
c_name = cname(p)
|
|
c_file.write('static const unsigned char %s[] = {' % c_name)
|
|
src = open(p, 'r')
|
|
s_len = 0
|
|
while 1:
|
|
if s_len % 12 == 0:
|
|
c_file.write('\n ')
|
|
ch = src.read(1)
|
|
if len(ch) < 1:
|
|
break
|
|
s_len += 1
|
|
c_file.write('0x%02x, ' % ord(ch))
|
|
c_file.write('0x00\n')
|
|
src.close()
|
|
c_file.write('};\n')
|
|
store[p] = (c_name, s_len)
|
|
c_file.write('''static const struct {
|
|
const char *fname;
|
|
const unsigned char *data;
|
|
const unsigned int len;
|
|
} store[] = {\n''')
|
|
for ent in store:
|
|
c_file.write(' {"%s", %s, %d},\n' % \
|
|
(ent, store[ent][0], store[ent][1]))
|
|
c_file.write(' {NULL, NULL, 0}\n')
|
|
c_file.write('};\n')
|
|
|
|
c_file.write('''
|
|
const unsigned char *getFile(const char *fname, unsigned int *length)
|
|
{
|
|
int i;
|
|
for (i = 0; store[i].fname != NULL; i++)
|
|
{
|
|
if (strcmp(fname, store[i].fname) == 0)
|
|
{
|
|
if (length != NULL)
|
|
*length = store[i].len;
|
|
return store[i].data;
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
''')
|
|
h_file.write('''#ifndef CFS_GEN_%s
|
|
#define CFS_GEN_%s
|
|
|
|
const unsigned char *getFile(const char *fname, unsigned int *length);
|
|
|
|
#endif
|
|
''' % (cname(header_fname), cname(header_fname)))
|
|
h_file.close()
|
|
c_file.close()
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv))
|