Search
lxdream.org :: lxdream/test/bin2c.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename test/bin2c.c
changeset 185:6755a04c447f
next561:533f6b478071
author nkeynes
date Fri Dec 29 00:24:43 2006 +0000 (17 years ago)
permissions -rw-r--r--
last change Limit number of sectors read at once (linux driver seems to throw a hissy
fit if you request too many at a time)
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/bin2c.c Fri Dec 29 00:24:43 2006 +0000
1.3 @@ -0,0 +1,63 @@
1.4 +/* Converts a binary file into a C integer array (for inclusion in
1.5 + a source file)
1.6 +
1.7 + (c)2000 Dan Potter
1.8 +
1.9 + $Id: bin2c.c,v 1.1 2006-07-11 01:35:23 nkeynes Exp $
1.10 + Note: Licensed under the new BSD license, see README.KOS -NK
1.11 +*/
1.12 +
1.13 +#include <stdio.h>
1.14 +
1.15 +void convert(char *ifn, char *ofn, char *prefix) {
1.16 + FILE *i, *o;
1.17 + unsigned char buffer[2048];
1.18 + int red, left, lc, q;
1.19 + char buf[BUFSIZ];
1.20 +
1.21 + i = fopen(ifn, "rb");
1.22 + o = fopen(ofn, "w");
1.23 + if (!i || !o) {
1.24 + printf("error: can't open input or output file\n");
1.25 + return;
1.26 + }
1.27 +
1.28 + fseek(i, 0, SEEK_END); left = ftell(i); fseek(i, 0, SEEK_SET);
1.29 + setbuf(o, buf);
1.30 +
1.31 + fprintf(o, "const int %s_size = %d;\n", prefix, left);
1.32 + fprintf(o, "const unsigned char %s_data[%d] =", prefix, left);
1.33 + fprintf(o, "{\n\t");
1.34 +
1.35 + lc = 0;
1.36 + while(left > 0) {
1.37 + red = fread(buffer, 1, 2048, i);
1.38 + left -= red;
1.39 + for (q=0; q<red; q++) {
1.40 + fprintf(o, "0x%02x, ", buffer[q]);
1.41 + if ((++lc) >= 8) {
1.42 + lc = 0;
1.43 + fprintf(o, "\n\t");
1.44 + }
1.45 + }
1.46 + }
1.47 +
1.48 + fprintf(o, "\n};\n");
1.49 + fclose(i); fclose(o);
1.50 +}
1.51 +
1.52 +int main(int argc, char **argv) {
1.53 +
1.54 + char *prefix;
1.55 +
1.56 + argc--;
1.57 + if (argc != 2 && argc != 3) {
1.58 + printf("usage: bin2c <input> <output> [prefix]\n");
1.59 + return 0;
1.60 + }
1.61 +
1.62 + prefix = (argc == 3) ? argv[3] : "file";
1.63 + convert(argv[1], argv[2], prefix);
1.64 + return 0;
1.65 +}
1.66 +
.