Fix to file2c to ensure valid C identifiers

A couple of small bug fixes.
This commit is contained in:
Andrew Hannam 2013-04-04 17:55:45 +10:00
parent b95151c7c2
commit ce9b930af0
3 changed files with 9 additions and 1 deletions

View File

@ -23,6 +23,13 @@ static char *filenameof(char *fname) {
return fname; return fname;
} }
static char *clean4c(char *fname) {
char *p;
while((p = strpbrk(fname, "-+ `~!@#$%^&*(){}[]|:;'\",<>?/|="))) *p = '_';
return fname;
}
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
char * opt_progname; char * opt_progname;
@ -115,6 +122,7 @@ size_t i;
opt_arrayname = filenameof(opt_inputfile); opt_arrayname = filenameof(opt_inputfile);
if (!opt_arrayname || !opt_arrayname[0]) if (!opt_arrayname || !opt_arrayname[0])
opt_arrayname = "filearray"; opt_arrayname = "filearray";
opt_arrayname = clean4c(opt_arrayname);
} }
/* Read the file processing 1K at a time */ /* Read the file processing 1K at a time */
@ -125,7 +133,7 @@ size_t i;
else if (opt_breakblocks) else if (opt_breakblocks)
fprintf(f_output, "\n};\n%s%sunsigned char %s_p%u[] = {", opt_static, opt_const, opt_arrayname, blocknum); fprintf(f_output, "\n};\n%s%sunsigned char %s_p%u[] = {", opt_static, opt_const, opt_arrayname, blocknum);
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
fprintf(f_output, (i & 0x0F) ? ", 0x%02X" : "\n\t0x%02X", buf[i]); fprintf(f_output, (i & 0x0F) ? " 0x%02X," : "\n\t0x%02X,", buf[i]);
} }
} }
fprintf(f_output, "\n};\n"); fprintf(f_output, "\n};\n");