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 cs130_alex 1.5 my $readcount; #number of chars actually read
16
|
17 cs130_alex 1.4
18 # no input record separator to just slurp everything
19 undef $INPUT_RECORD_SEPARATOR;
|
20 rizwank 1.1
21 foreach $file (@ARGV) {
|
22 cs130_alex 1.4 open(FILE, $file) or die("Can't open $file : $OS_ERROR");
|
23 rizwank 1.1 binmode FILE;
24
25 # munge filename into C variable name
26 $varname = $file;
27 $varname =~ s/\./_/;
28
|
29 cs130_alex 1.4 #name of the file
|
30 rizwank 1.1 print "const static char name_$varname"."[] = \"$file\";\n";
|
31 cs130_alex 1.4 #hex contents of the file
32 #BUG:have to get rid of the last comma
33 print "const static char file_$varname"."[] = {\n";
|
34 cs130_alex 1.5
35
36
37 while ( $readcount=read(FILE, $buffer, 16) ) {
38
39 die("Read error in $file:$OS_ERROR") unless defined($readcount);
|
40 rizwank 1.1 $buffer =~ s/(.|\n)/'0x' . unpack('H2', $1).', '/ge;
|
41 cs130_alex 1.5 $buffer =~ s/(, )$//g if($readcount < 16);
42 print "\t$buffer\n";
43
|
44 rizwank 1.1 }
|
45 rizwank 1.3 print "};\n";
|
46 cs130_alex 1.4 close FILE;
47 #size of the file
|
48 rizwank 1.3 print "const static int size_$varname"." = sizeof(file_$varname);\n\n";
|
49 rizwank 1.1 }
|