Write a program in PERL that will work on all files in the given folder and its subfolders (see "Recursive cleaning temporary directory" example). The program should read contents of each file and convert it into an entry in an array of structures that would be used by a hypothetical embedded Web server written in C.
Your program should read the location of the folder from STDIN without asking questions and generate the output to STDOUT. (STDOUT could be redirected to a file as necessary.) Please study the provided example. Your program should generate entires created from actual files.
struct PAGE {
const unsigned int maxlen; /* lenght of the allocated bytes array */
unsigned int curlen; /* lenght of current data stored in the array */
const char* mime; /* mime type to be reported */
const char* name; /* source file name */
unsigned char* bytes; /* array of unsigned bytes 0..255, no need to terminate with 0 */
};
unsigned char file00[] = {65, 66, 67, 68, 59, 70, 71, 72};
PAGE page00 = { 8, 8, "text/html", "index.html", file00 };
unsigned char file01[] = {65, 66, 67, 68, 59, 70, 13, 72};
PAGE page01 = { 8, 8, "text/html", "folder/page.html", file01 };
unsigned char file02[] = {12, 0, 27, 254, 59, 70, 71, 72, 13, 28, 11, 12};
PAGE page02 = { 12, 12, "image/gif", "picture.gif", file02 };
unsigned char file03[] = {12, 0, 27, 254, 59, 70, 71, 72, 13, 28, 11, 12};
PAGE page03 = { 12, 12, "application/pdf", "doc.pdf", file03 };
/*
unsigned char dfile01[2048] = {0};
PAGE dpage01 = { 2048, 0, "text/html", "status.html", dfile01 };
*/
struct PAGE* const website[] =
{
&page00
, &page01
, &page02
, &page03
};
Please note that:
/ instead of \\ in folder
names.text/html for files with .html or .htm
extension, text/plain for files with .txt extension,
image/gif for files with .gif extension, image/jpg for
files with .jpg extension, image/png for files with .png extension,
application/anything for files with .anything else extension.Hins: Use the coding pattern used in the example mentioned above. Print the PAGE definition and the prologue to the array of filesystem first and then call the function recursively, and then print the array epilogue. Inside the function read each file into a $string variable, convert into an array of unsigned bytes using pack function and print as fit.
Consider testing your program by pointing it to a folder with data unpacked from this ZIP file.
Attach all source code in files with proper names and extensions (*.pl, e.t.c.). Do not ZIP files together or include compiled files (e.g. *.class). Send your homework via e- mail to the instructor at olekmali at bradley.edu. Please set the message subject to: wbc-hwB.
Thank you.