From 9df0a95f1c903026fe546a94a34413106f7e093d Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Tue, 4 Sep 2007 14:20:27 +0000 Subject: 2007-09-04 Joel Sherrill * bin2c.c: New file. http://www.wxwidgets.org/wiki/index.php/Embedding_PNG_Images-Bin2c_In_C --- tools/build/ChangeLog | 5 +++ tools/build/rtems-bin2c.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tools/build/rtems-bin2c.c diff --git a/tools/build/ChangeLog b/tools/build/ChangeLog index 8913304bff..57dccef41b 100644 --- a/tools/build/ChangeLog +++ b/tools/build/ChangeLog @@ -1,3 +1,8 @@ +2007-09-04 Joel Sherrill + + * bin2c.c: New file. + http://www.wxwidgets.org/wiki/index.php/Embedding_PNG_Images-Bin2c_In_C + 2007-09-04 Joel Sherrill * Makefile.am: Add bin2c so we can have a more reliable way to convert diff --git a/tools/build/rtems-bin2c.c b/tools/build/rtems-bin2c.c new file mode 100644 index 0000000000..3bc8dd13a9 --- /dev/null +++ b/tools/build/rtems-bin2c.c @@ -0,0 +1,108 @@ +// bin2c.c +// +// convert a binary file into a C source vector +// +// put into the public domain by Sandro Sigala +// +// syntax: bin2c [-c] [-z] +// +// -c add the "const" keyword to definition +// -z terminate the array with a zero (useful for embedded C strings) +// +// examples: +// bin2c -c myimage.png myimage_png.cpp +// bin2c -z sometext.txt sometext_txt.cpp + +#include +#include +#include +#include + +#ifndef PATH_MAX +#define PATH_MAX 1024 +#endif + +int useconst = 0; +int zeroterminated = 0; + +int myfgetc(FILE *f) +{ + int c = fgetc(f); + if (c == EOF && zeroterminated) { + zeroterminated = 0; + return 0; + } + return c; +} + +void process(const char *ifname, const char *ofname) +{ + FILE *ifile, *ofile; + ifile = fopen(ifname, "rb"); + if (ifile == NULL) { + fprintf(stderr, "cannot open %s for reading\n", ifname); + exit(1); + } + ofile = fopen(ofname, "wb"); + if (ofile == NULL) { + fprintf(stderr, "cannot open %s for writing\n", ofname); + exit(1); + } + char buf[PATH_MAX], *p; + const char *cp; + if ((cp = strrchr(ifname, '/')) != NULL) + ++cp; + else { + if ((cp = strrchr(ifname, '\\')) != NULL) + ++cp; + else + cp = ifname; + } + strcpy(buf, cp); + for (p = buf; *p != '\0'; ++p) + if (!isalnum(*p)) + *p = '_'; + fprintf(ofile, "static %sunsigned char %s[] = {\n", useconst ? "const " : "", buf); + int c, col = 1; + while ((c = myfgetc(ifile)) != EOF) { + if (col >= 78 - 6) { + fputc('\n', ofile); + col = 1; + } + fprintf(ofile, "0x%.2x, ", c); + col += 6; + + } + fprintf(ofile, "\n};\n"); + + fclose(ifile); + fclose(ofile); +} + +void usage(void) +{ + fprintf(stderr, "usage: bin2c [-cz] \n"); + exit(1); +} + +int main(int argc, char **argv) +{ + while (argc > 3) { + if (!strcmp(argv[1], "-c")) { + useconst = 1; + --argc; + ++argv; + } else if (!strcmp(argv[1], "-z")) { + zeroterminated = 1; + --argc; + ++argv; + } else { + usage(); + } + } + if (argc != 3) { + usage(); + } + process(argv[1], argv[2]); + return 0; +} -- cgit v1.2.3