#!/sw/bin/perl
##
##  bin2c -- convert an binary file into a statically initialised
##           C array of characters
##
##  Copyright (c) Ralf S. Engelschall, All Rights Reserved.
##

$filein  = $ARGV[0];
$fileout = $ARGV[1];
$name    = $ARGV[2];

if ($#ARGV ne 2) {
	printf(STDERR "Usage: $0 binary-file c-file buffer-name\n");
    exit(1);
}

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($filein);

open(IN,   "<$filein");
#open(OUTH, ">$fileout.h");
open(OUTC, ">$fileout.c");

print OUTC "/* $filein.c -- automatically generated by bin2c */\n";
print OUTC "\n";
print OUTC "int  ${name}_size   = " . $size . ";\n";
print OUTC "char ${name}_data[] = {\n";

$i = 1;
while (read(IN, $c, 1)) {
	printf(OUTC "0x%02x", ord($c));
	printf(OUTC ",") if ($i < $size);
	printf(OUTC "\n") if ($i % 15 == 0 && $i < $size);
	$i++;
}

print OUTC " };\n";
print OUTC "\n";
print OUTC "/*EOF*/\n";

$filename = "$fileout.h";
$filename =~ tr|a-z.|A-Z_|;

#print OUTH "/* $fileout.h -- automatically generated by bin2c */\n";
#print OUTH "#ifndef __$filename\n";
#print OUTH "#define __$filename\n";
#print OUTH "\n";
#print OUTH "extern char *$name;\n";
#print OUTH "\n";
#print OUTH "#endif /* __$filename */\n";

close(IN);
#close(OUTH);
close(OUTC);

##EOF##
