1 rizwank 1.1 #!/usr/bin/perl
|
2 cs130_alex 1.4 # chexify.pl
|
3 rizwank 1.1 # Takes a list of filenames on stdin and create a C data array for each one.
|
4 rizwank 1.2 # Copyright Dan Kegel, Rizwan Kassim 2005
|
5 rizwank 1.1 # Licensed under LGPL
6
|
7 cs130_alex 1.4
8 use English;
9 use strict;
10
11
12 my $file; #each of the files we are working on
13 my $varname; #file name converted to variable name
14 my $buffer; #buffer to store the hexified contents of a file till output
15
16 # no input record separator to just slurp everything
17 undef $INPUT_RECORD_SEPARATOR;
|
18 rizwank 1.1
19 foreach $file (@ARGV) {
|
20 cs130_alex 1.4 open(FILE, $file) or die("Can't open $file : $OS_ERROR");
|
21 rizwank 1.1 binmode FILE;
22
23 # munge filename into C variable name
24 $varname = $file;
25 $varname =~ s/\./_/;
26
|
27 cs130_alex 1.4 #name of the file
|
28 rizwank 1.1 print "const static char name_$varname"."[] = \"$file\";\n";
|
29 cs130_alex 1.4 #hex contents of the file
30 #BUG:have to get rid of the last comma
31 print "const static char file_$varname"."[] = {\n";
32 while ( read(FILE, $buffer, 16) ) {
|
33 rizwank 1.1 $buffer =~ s/(.|\n)/'0x' . unpack('H2', $1).', '/ge;
34 print "\t$buffer\n";
35 }
|
36 rizwank 1.3 print "};\n";
|
37 cs130_alex 1.4 close FILE;
38 #size of the file
|
39 rizwank 1.3 print "const static int size_$varname"." = sizeof(file_$varname);\n\n";
|
40 rizwank 1.1 }
|