Search
lxdream.org :: lxdream/test/bin2c.c
lxdream 0.9.1
released Jun 29
Download Now
filename test/bin2c.c
changeset 185:6755a04c447f
next561:533f6b478071
author nkeynes
date Sat Aug 05 00:18:21 2006 +0000 (16 years ago)
permissions -rw-r--r--
last change Add error lines to tests with incomplete polys
Split clip tests to separate data file
Add tests for cmd bit 23 ("use list size field")
view annotate diff log raw
     1 /* Converts a binary file into a C integer array (for inclusion in
     2    a source file) 
     4    (c)2000 Dan Potter
     6    $Id: bin2c.c,v 1.1 2006-07-11 01:35:23 nkeynes Exp $
     7    Note: Licensed under the new BSD license, see README.KOS  -NK
     8 */
    10 #include <stdio.h>
    12 void convert(char *ifn, char *ofn, char *prefix) {
    13 	FILE *i, *o;
    14 	unsigned char buffer[2048];
    15 	int red, left, lc, q;
    16 	char buf[BUFSIZ];
    18 	i = fopen(ifn, "rb");
    19 	o = fopen(ofn, "w");
    20 	if (!i || !o) {
    21 		printf("error: can't open input or output file\n");
    22 		return;
    23 	}
    25 	fseek(i, 0, SEEK_END); left = ftell(i); fseek(i, 0, SEEK_SET);
    26 	setbuf(o, buf);
    28 	fprintf(o, "const int %s_size = %d;\n", prefix, left);
    29 	fprintf(o, "const unsigned char %s_data[%d] =", prefix, left);
    30 	fprintf(o, "{\n\t");
    32 	lc = 0;
    33 	while(left > 0) {
    34 		red = fread(buffer, 1, 2048, i);
    35 		left -= red;
    36 		for (q=0; q<red; q++) {
    37 			fprintf(o, "0x%02x, ", buffer[q]);
    38 			if ((++lc) >= 8) {
    39 				lc = 0;
    40 				fprintf(o, "\n\t");
    41 			}
    42 		}
    43 	}
    45 	fprintf(o, "\n};\n");
    46 	fclose(i); fclose(o);
    47 }
    49 int main(int argc, char **argv) {
    51 	char *prefix;
    53 	argc--;
    54 	if (argc != 2 && argc != 3) {
    55 		printf("usage: bin2c <input> <output> [prefix]\n");
    56 		return 0;
    57 	}
    59 	prefix = (argc == 3) ? argv[3] : "file";
    60 	convert(argv[1], argv[2], prefix);
    61 	return 0;
    62 }
.