#!/usr/bin/perl
# chexify.pl
# Takes a list of filenames on stdin and create a C data array for each one.
# Copyright Dan Kegel, Rizwan Kassim 2005
# Licensed under LGPL


use English;
use strict; 


my $file;       #each of the files we are working on
my $varname;    #file name converted to variable name
my $buffer;     #buffer to store the hexified contents of a file till output

# no input record separator to just slurp everything
undef $INPUT_RECORD_SEPARATOR;

foreach $file (@ARGV) {
       open(FILE, $file) or die("Can't open $file : $OS_ERROR");
       binmode FILE;

       # munge filename into C variable name
       $varname = $file;
       $varname =~ s/\./_/;

	   #name of the file
       print "const static char name_$varname"."[] = \"$file\";\n";
       #hex contents of the file
	   #BUG:have to get rid of the last comma
	   print "const static char file_$varname"."[] = {\n";
       while ( read(FILE, $buffer, 16) ) {
               $buffer =~ s/(.|\n)/'0x' . unpack('H2', $1).', '/ge;
               print "\t$buffer\n";
       }
       print "};\n";
	   close FILE;
	   #size of the file
       print "const static int size_$varname"." = sizeof(file_$varname);\n\n";
}
