-
Notifications
You must be signed in to change notification settings - Fork 9
/
filegen
executable file
·95 lines (70 loc) · 1.84 KB
/
filegen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl
use strict;
sub decode {
my $data = shift @_;
my $len = length($data);
my $rc = "";
my $i = 0;
while ($i < $len) {
$rc .= sprintf("0x%02x,", unpack("C", substr($data, $i, 1)));
$i = $i + 1;
if ($i % 16 == 0) { $rc .= "\n" };
}
if ($i % 16 != 0) { $rc .= "\n" };
return $rc;
}
sub slurp {
my $fname = shift @_;
open my $fh, '<', ${fname} or die "Can't open file ($fname) $!";
my $rc = do { local $/; <$fh> };
return $rc;
}
($#ARGV == 2) or die("Usage: fsgen <source_directory> <output_c_file> <output_h_file\n");
my $source = $ARGV[0];
my $dest_c = $ARGV[1];
my $dest_h = $ARGV[2];
(-d $source) or die("Error: source_directory must be a directory\n");
print("Source=$source dest=$dest_c / $dest_h\n");
#
# Now open the output file and write the headers...
#
open(FC, ">${dest_c}") or die("Error: unable to open c file for writing\n");
open(FH, ">${dest_h}") or die("Error: unable to open h file for writing\n");
my $header = <<'EOF';
//
// Autogenerated file - do not edit
//
EOF
print FC $header;
print FH $header;
my $path = "/";
sub do_dir {
my $dir = shift @_;
my $rep = shift @_;
opendir(DH, $dir) or die("Error: unable to open directory ($dir)\n");
my @files = readdir(DH);
closedir(DH);
foreach my $file (@files) {
next if ($file eq ".");
next if ($file eq "..");
if (-d "${dir}/${file}") {
do_dir("${dir}/${file}", "${rep}/${file}");
next;
}
my $sname = "${file}";
$sname =~ s/\./_/g;
$sname =~ s/\//_/g;
print("Got file: [$dir/$file] [$sname]\n");
print FC "const unsigned char ${sname}[] = {\n";
my $flen = -s "${dir}/${file}";
print FC "/* raw file data ($flen bytes) */\n";
my $data = slurp("${dir}/${file}");
print FC decode($data);
print FC "};\n\n";
print FH "extern const unsigned char ${sname}[${flen}];\n";
}
}
do_dir(${source}, "");
close(FC);
close(FH);
exit(0);