2013-05-01 23:53:28 +00:00
|
|
|
/*
|
2013-06-15 11:37:22 +00:00
|
|
|
* This file is subject to the terms of the GFX License. If a copy of
|
2013-05-03 14:36:17 +00:00
|
|
|
* the license was not distributed with this file, you can obtain one at:
|
|
|
|
*
|
2013-07-21 20:20:37 +00:00
|
|
|
* http://ugfx.org/license.html
|
2013-05-03 14:36:17 +00:00
|
|
|
*/
|
2013-04-03 11:22:27 +00:00
|
|
|
|
2013-04-04 06:58:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2014-02-04 22:45:28 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2013-04-04 06:58:48 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2014-02-04 22:45:28 +00:00
|
|
|
static unsigned char buf[1024];
|
|
|
|
static char tname[FILENAME_MAX];
|
2013-04-04 06:58:48 +00:00
|
|
|
|
|
|
|
static char *filenameof(char *fname) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
if (fname[1] == ':')
|
|
|
|
fname = fname+2;
|
|
|
|
p = strrchr(fname, '\\');
|
|
|
|
if (p) fname = p+1;
|
|
|
|
#endif
|
|
|
|
p = strrchr(fname, '/');
|
|
|
|
if (p) fname = p+1;
|
2014-02-04 22:45:28 +00:00
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *basenameof(char *fname) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
fname = filenameof(fname);
|
2013-04-04 06:58:48 +00:00
|
|
|
p = strchr(fname, '.');
|
|
|
|
if (p) *p = 0;
|
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
2013-04-04 07:55:45 +00:00
|
|
|
static char *clean4c(char *fname) {
|
|
|
|
char *p;
|
|
|
|
|
2013-04-04 11:01:06 +00:00
|
|
|
while((p = strpbrk(fname, "-+ `~!@#$%^&*(){}[]|:;'\",<>?/|=.\\"))) *p = '_';
|
2013-04-04 07:55:45 +00:00
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
2013-04-04 06:58:48 +00:00
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
|
|
|
char * opt_progname;
|
|
|
|
char * opt_inputfile;
|
|
|
|
char * opt_outputfile;
|
|
|
|
char * opt_arrayname;
|
2014-02-04 22:45:28 +00:00
|
|
|
char * opt_dirname;
|
2013-04-04 06:58:48 +00:00
|
|
|
int opt_breakblocks;
|
2014-02-04 22:45:28 +00:00
|
|
|
int opt_romdir;
|
2013-04-04 06:58:48 +00:00
|
|
|
char * opt_static;
|
|
|
|
char * opt_const;
|
|
|
|
FILE * f_input;
|
|
|
|
FILE * f_output;
|
|
|
|
unsigned blocknum;
|
2014-02-04 22:45:28 +00:00
|
|
|
size_t len, totallen;
|
2013-04-04 06:58:48 +00:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* Default values for our parameters */
|
2014-02-04 22:45:28 +00:00
|
|
|
opt_progname = basenameof(argv[0]);
|
|
|
|
opt_inputfile = opt_outputfile = opt_arrayname = opt_dirname = 0;
|
|
|
|
opt_breakblocks = opt_romdir = 0;
|
|
|
|
opt_static = opt_const = "";
|
2013-04-04 06:58:48 +00:00
|
|
|
|
|
|
|
/* Read the arguments */
|
|
|
|
while(*++argv) {
|
|
|
|
if (argv[0][0] == '-') {
|
|
|
|
while (*++(argv[0])) {
|
|
|
|
switch(argv[0][0]) {
|
2014-02-04 22:45:28 +00:00
|
|
|
case '?': case 'h': goto usage;
|
|
|
|
case 'd': opt_romdir = 1; break;
|
|
|
|
case 'b': opt_breakblocks = 1; break;
|
|
|
|
case 'c': opt_const = "const "; break;
|
|
|
|
case 's': opt_static = "static "; break;
|
|
|
|
case 'n': opt_arrayname = *++argv; goto nextarg;
|
|
|
|
case 'f': opt_romdir = 1; opt_dirname = *++argv; goto nextarg;
|
2013-04-04 06:58:48 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown flag -%c\n", argv[0][0]);
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!opt_inputfile)
|
|
|
|
opt_inputfile = argv[0];
|
|
|
|
else if (!opt_outputfile)
|
|
|
|
opt_outputfile = argv[0];
|
|
|
|
else {
|
|
|
|
usage:
|
2014-02-04 22:45:28 +00:00
|
|
|
fprintf(stderr, "Usage:\n\n%s -?\n"
|
|
|
|
"%s [-dbcs] [-n name] [-f file] [inputfile] [outputfile]\n"
|
|
|
|
"\t-?\tThis help\n"
|
|
|
|
"\t-h\tThis help\n"
|
|
|
|
"\t-d\tAdd a directory entry for the ROM file system\n"
|
|
|
|
"\t-b\tBreak the arrays for compilers that won't handle large arrays\n"
|
|
|
|
"\t-c\tDeclare as const (useful to ensure they end up in Flash)\n"
|
|
|
|
"\t-s\tDeclare as static\n"
|
|
|
|
"\t-n name\tUse \"name\" as the name of the array\n"
|
|
|
|
"\t-f file\tUse \"file\" as the filename in the ROM directory entry\n"
|
2013-04-04 06:58:48 +00:00
|
|
|
, opt_progname, opt_progname);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
nextarg: ;
|
|
|
|
}
|
|
|
|
|
2014-02-04 22:45:28 +00:00
|
|
|
/* Make sure we can generate a default directory name if required */
|
|
|
|
if (opt_romdir && !opt_dirname && !opt_inputfile) {
|
|
|
|
fprintf(stderr, "If using -d you must either specify an input filename or use -f to specify a directory entry filename\n");
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
2013-04-04 06:58:48 +00:00
|
|
|
/* Open the input file */
|
|
|
|
if (opt_inputfile) {
|
|
|
|
f_input = fopen(opt_inputfile,
|
|
|
|
#ifdef WIN32
|
|
|
|
"rb");
|
|
|
|
#else
|
|
|
|
"r");
|
|
|
|
#endif
|
|
|
|
if (!f_input) {
|
|
|
|
fprintf(stderr, "Could not open input file '%s'\n", opt_inputfile);
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f_input = stdin;
|
|
|
|
#ifdef WIN32
|
|
|
|
_setmode(_fileno(stdin), _O_BINARY);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the output file */
|
|
|
|
if (opt_outputfile) {
|
|
|
|
f_output = fopen(opt_outputfile, "w");
|
|
|
|
if (!f_output) {
|
|
|
|
fprintf(stderr, "Could not open output file '%s'\n", opt_outputfile);
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
f_output = stdout;
|
|
|
|
|
2013-04-04 11:01:06 +00:00
|
|
|
/* Print the comment header */
|
|
|
|
fprintf(f_output, "/**\n * This file was generated ");
|
|
|
|
if (opt_inputfile) fprintf(f_output, "from \"%s\" ", opt_inputfile);
|
|
|
|
fprintf(f_output, "using...\n *\n *\t%s", opt_progname);
|
2014-02-04 22:45:28 +00:00
|
|
|
if (opt_arrayname || opt_static[0] || opt_const[0] || opt_breakblocks || opt_romdir) {
|
2013-04-04 11:01:06 +00:00
|
|
|
fprintf(f_output, " -");
|
2014-02-26 21:54:51 +00:00
|
|
|
if (opt_romdir) fprintf(f_output, "d");
|
2013-04-04 11:01:06 +00:00
|
|
|
if (opt_breakblocks) fprintf(f_output, "b");
|
|
|
|
if (opt_const[0]) fprintf(f_output, "c");
|
|
|
|
if (opt_static[0]) fprintf(f_output, "s");
|
|
|
|
if (opt_arrayname) fprintf(f_output, "n %s", opt_arrayname);
|
2014-02-04 22:45:28 +00:00
|
|
|
if (opt_dirname) fprintf(f_output, (opt_arrayname ? " -f %s" : "f %s"), opt_dirname);
|
2013-04-04 11:01:06 +00:00
|
|
|
}
|
|
|
|
if (opt_inputfile) fprintf(f_output, " %s", opt_inputfile);
|
|
|
|
if (opt_outputfile) fprintf(f_output, " %s", opt_outputfile);
|
|
|
|
fprintf(f_output, "\n *\n */\n");
|
|
|
|
|
|
|
|
/*
|
2014-02-04 22:45:28 +00:00
|
|
|
* Set the array name and dir name
|
2013-04-04 11:01:06 +00:00
|
|
|
*/
|
2013-04-04 06:58:48 +00:00
|
|
|
if (!opt_arrayname) {
|
|
|
|
if (opt_inputfile)
|
2014-02-04 22:45:28 +00:00
|
|
|
opt_arrayname = basenameof(strcpy(tname, opt_inputfile));
|
|
|
|
if (!opt_arrayname || !opt_arrayname[0]) {
|
2014-05-11 03:10:50 +00:00
|
|
|
srand(time(0));
|
2014-02-04 22:45:28 +00:00
|
|
|
sprintf(tname, "filearray%u", rand());
|
|
|
|
opt_arrayname = tname;
|
|
|
|
}
|
2013-04-04 06:58:48 +00:00
|
|
|
}
|
2013-04-04 11:01:06 +00:00
|
|
|
opt_arrayname = clean4c(opt_arrayname);
|
2014-02-04 22:45:28 +00:00
|
|
|
if (opt_romdir && !opt_dirname)
|
|
|
|
opt_dirname = filenameof(opt_inputfile);
|
2013-04-04 06:58:48 +00:00
|
|
|
|
|
|
|
/* Read the file processing 1K at a time */
|
|
|
|
blocknum = 0;
|
2014-02-04 22:45:28 +00:00
|
|
|
totallen = 0;
|
2013-04-04 06:58:48 +00:00
|
|
|
while((len = fread(buf, 1, sizeof(buf), f_input))) {
|
|
|
|
if (!blocknum++)
|
2014-02-04 22:45:28 +00:00
|
|
|
fprintf(f_output, "%s%schar %s[] = {", opt_static, opt_const, opt_arrayname);
|
2013-04-04 06:58:48 +00:00
|
|
|
else if (opt_breakblocks)
|
2014-02-04 22:45:28 +00:00
|
|
|
fprintf(f_output, "\n};\n%s%schar %s_p%u[] = {", opt_static, opt_const, opt_arrayname, blocknum);
|
2013-04-04 06:58:48 +00:00
|
|
|
for(i = 0; i < len; i++) {
|
2013-04-04 07:55:45 +00:00
|
|
|
fprintf(f_output, (i & 0x0F) ? " 0x%02X," : "\n\t0x%02X,", buf[i]);
|
2013-04-04 06:58:48 +00:00
|
|
|
}
|
2014-02-04 22:45:28 +00:00
|
|
|
totallen += len;
|
2013-04-04 06:58:48 +00:00
|
|
|
}
|
|
|
|
fprintf(f_output, "\n};\n");
|
|
|
|
|
2014-02-04 22:45:28 +00:00
|
|
|
/* Add the directory entry if required */
|
|
|
|
if (opt_romdir) {
|
|
|
|
fprintf(f_output, "\n#ifdef ROMFS_DIRENTRY_HEAD\n");
|
2014-02-07 08:43:39 +00:00
|
|
|
fprintf(f_output, "\t%s%sROMFS_DIRENTRY %s_dir = { 0, 0, ROMFS_DIRENTRY_HEAD, \"%s\", %u, %s };\n", opt_static, opt_const, opt_arrayname, opt_dirname, totallen, opt_arrayname);
|
2014-02-04 22:45:28 +00:00
|
|
|
fprintf(f_output, "\t#undef ROMFS_DIRENTRY_HEAD\n\t#define ROMFS_DIRENTRY_HEAD &%s_dir\n#endif\n", opt_arrayname);
|
|
|
|
}
|
|
|
|
|
2013-04-04 06:58:48 +00:00
|
|
|
/* Clean up */
|
|
|
|
if (ferror(f_input))
|
|
|
|
fprintf(stderr, "Input file read error\n");
|
|
|
|
if (ferror(f_output))
|
|
|
|
fprintf(stderr, "Output file write error - disk full?\n");
|
|
|
|
if (f_input != stdin)
|
|
|
|
fclose(f_input);
|
|
|
|
if (f_output != stdout)
|
|
|
|
fclose(f_output);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-03 10:32:57 +00:00
|
|
|
|