-
Notifications
You must be signed in to change notification settings - Fork 1
/
makefsdata.pl
168 lines (130 loc) · 4.24 KB
/
makefsdata.pl
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl
use warnings;
# This script creates our output file were we will store all the converted html data to C arrays.
#
# The data is saved into the file in HEX values but the data is written to corespond to the
# following structure. Actually at the end of the script the arrays are pointed to the structures.
#
# struct httpd_fsdata_file_noconst
# {
# struct httpd_fsdata_file *next;
# unsigned char *name;
# unsigned char *data;
# int len;
# };
#
# open the out file for writing
open(OUTPUT, "> fsdata.c");
# change to were all the files are located.
chdir("fs");
# open directory and serch all the working files. Ignore cvs. svn or other
# files not needed to convert
opendir(DIR, ".");
# collecting the file names in the root and all subdirectories to proccess.
@files = grep { !/^\./ && !/(CVS|~)/ } readdir(DIR);
print "\nopening root directory, found the following files and all subdirectories to process:\n ";
print_file_names(@files);
closedir(DIR);
# process directories and add their files as well.
foreach $file (@files)
{
if(-d $file && $file !~ /^\./)
{
print "\nProcessing directory $file\n";
opendir(DIR, $file);
@newfiles = grep { !/^\./ && !/(CVS|~)/ } readdir(DIR);
closedir(DIR);
print " Adding files: @newfiles\n";
@files = (@files, map { $_ = "$file/$_" } @newfiles);
next;
}
}
print "\n\nStarting convert files to arrays process:\n";
foreach $file (@files)
{
if(-f $file)
{
print " Adding file: $file\n";
# open the data file and convert it to HEX valuse...
open(FILE, $file) || die "Could not open file $file\n";
binmode(FILE);
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
$file =~ s-^-/-;
$fvar = $file;
$fvar =~ s-/-_-g;
$fvar =~ s-\.-_-g;
# for AVR, add PROGMEM here
# write the prefix and array name to hold the converted data
print(OUTPUT "static const unsigned char data".$fvar."[] = {\n");
# write the original directory and the file name source
print(OUTPUT "\t/* File: $file */\n\t");
# process the file. Convert its data to HEX values.
# first write the file name to the structure
$file_len = length($file);
for($j=0; $j<$file_len; $j++)
{
printf(OUTPUT "0x%02X, ", unpack("C", substr($file, $j, 1)));
}
printf(OUTPUT "0,\n");
# process the file data and convert it to binary
print(OUTPUT "\t/* here starts the data */\n\t");
$i = 0;
$items_per_line = 10;
# this loop proccesses the input file. It converts every byte
# to a binary value and stors it in file.
for($bytesread=0; $bytesread<$size; $bytesread++)
{
# read one byte from file
read(FILE, $data, 1);
# print it in HEX format
printf(OUTPUT "0x%02X", unpack("C", $data));
# if the are more bytes to process add a seperator !
if($bytesread < ($size-1))
{
print(OUTPUT ", ");
}
$i++;
if($i == $items_per_line)
{
print(OUTPUT "\n\t");
$i = 0;
}
}
# no more bytes to process close the array.
print(OUTPUT "};\n\n");
print " $file, Bytesread = $bytesread\n\n";
close(FILE);
push(@fvars, $fvar);
push(@pfiles, $file);
}
}
# here we start creating the structures definitions. See at begining of the script.
for($i = 0; $i < @fvars; $i++)
{
$file = $pfiles[$i];
$fvar = $fvars[$i];
# the first structure does not point to any somone else so we write NULL.
if($i == 0)
{
$prevfile = "NULL";
}
else
{
$prevfile = "file" . $fvars[$i - 1];
}
print(OUTPUT "const struct fsdata_file file".$fvar."[] = {{$prevfile, data$fvar, ");
print(OUTPUT "data$fvar + ". (length($file) + 1) .", ");
print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) ."}};\n\n");
}
print(OUTPUT "#define FS_ROOT file$fvars[$i - 1]\n\n");
print(OUTPUT "#define FS_NUMFILES $i\n");
# subrutine...
sub print_file_names()
{
my $size = @_;
for($count=0; $count<$size; $count++)
{
printf("%s ", $_[$count]);
}
print "\n";
}