lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [lwip-users] makefsdata


From: Pettinato, Jim
Subject: RE: [lwip-users] makefsdata
Date: Sat, 23 Jul 2005 14:42:55 -0500

Apologies in advance for the lengthy response...

I have converted it to C already... with some modifications. I changed
it so the the HTML headers are no longer embedded in the file data but
prepended by the HTTP daemon to allow both hardcoded pages and pages
uploaded to flash EEPROM to be served. Of course I also had to modify
HTTPD.C to recognize the file type by extension and prepend the headers.
Give me an email address and I'll send you the modified HTTP.C as well
if you're interested.

------------------------------------------------------------------------
-------------------
 #include <stdio.h>
 #include <stdlib.h>
 #include <dir.h>
 #include <dos.h>
 #include <string.h>

 typedef enum file_type_enum
 {
   HTML, GIF, PNG, JPEG, OCTET_STREAM, REALAUDIO, TEXT, CGI, PLAIN
 }
 CONTENT_TYPE;

 const char *html_header[] = {
 "Content-type: text/html\r\n\r\n",
 "Content-type: image/gif\r\n\r\n",
 "Content-type: image/png\r\n\r\n",
 "Content-type: image/jpeg\r\n\r\n",
 "Content-type: application/octet-stream\r\n\r\n",
 "Content-type: audio/x-pn-realaudio\r\n\r\n",
 "Content-type: text/plain\r\n\r\n",
 "\r\n",
 "",
 NULL
 };

 typedef enum getResponseEnum
 {
   HTTP_200_OK,
   HTTP_404_NOT_FOUND
 } HTTP_RESPONSE;

 const char *httpResponseText[] = {
 "HTTP/1.0 200 OK\r\n",
 "HTTP/1.0 404 File not found\r\n"
 };

 const char *serverID =
   "Server: (Your server name here)";

 int process_sub(FILE *outfile);
 int process_file(FILE *outfile, char *filename);

 FILE *tmpFile;

 char curSubdir[256];
 char lastFileVar[256];

 int main(int argc, char *argv[])
 {
         struct ffblk fInfo;
   char path[256];
   char *appPath;
   FILE *outfile;
   unsigned char processSubs = 1;
   int filesProcessed;

   // if no subdir named 'fs' exists, spout usage verbiage
   printf("\n HTMLGen - HTML to C source converter\n");
   printf("     by Jim Pettinato  - circa 2003 \n\n");

   if (strstr(argv[0], "-s"))
    processSubs = 0;

   if ((argc < 3) || (processSubs & (argc < 2)))
     strcpy(path, "fs");
   else
     strcpy(path, argv[1]);

   // if command line param or subdir named 'fs' not found spout usage
verbiage
   if (findfirst(path, &fInfo, FA_DIREC) < 0)
   {
        printf(" Usage: htmlgen [targetdir] [-s]\n\n");
        printf("   targetdir: relative or absolute path to files to
convert \n");
        printf("   switch -s: toggle processing of subdirectories
(default is on) \n");
        printf("   if targetdir not specified, htmlgen will attempt to
\n");
        printf("   process files in subdirectory 'fs' \n");
      exit(-1);
   }

   sprintf(curSubdir, "");  // start off in web page's root directory -
relative paths
   printf("  Processing all files in directory %s", path);
   if (processSubs)
     printf(" and subdirectories...\n\n", path);
   else
                 printf("...\n\n");

         appPath = getcwd(NULL, 256);
   outfile = fopen("fsdata.tmp", "wb");
   tmpFile = fopen("fshdr.tmp", "wb");

   chdir(path);

   fprintf(outfile, "#include \"fs.h\"\n");
   fprintf(outfile, "#include \"lwip\\def.h\"\n");
   fprintf(outfile, "#include \"fsdata.h\"\n\n\n");

   fprintf(outfile, "#define file_NULL (struct fsdata_file *)
NULL\n\n\n");

   sprintf(lastFileVar, "NULL");

   filesProcessed = process_sub(outfile);

   // outfile now contains all of the raw data.. now append linked list
of
   // file header structs to allow embedded app to search for a file
name
         fprintf(outfile, "\n\n");
   fclose(outfile);
   fclose(tmpFile);

   chdir(appPath);
         system("copy fsdata.tmp+fshdr.tmp fsdata.c");   // append
tmpFile to outfile
   free(appPath);

   printf("\nProcessed %d files - done.\n\n", filesProcessed);

   // TODO: automate updating of header file...

   return 0;
 }


int process_sub(FILE *outfile)
{
   struct ffblk fInfo;
   int filesProcessed = 0;

   if (findfirst("*.*", &fInfo, FA_ARCH)==0)
   {  // at least one file in directory
      do
      {
        printf("processing %s...\n", fInfo.ff_name);
        if (process_file(outfile, fInfo.ff_name) < 0)
        {
                printf("\nError... aborting\n");
          return -1;
        }
        filesProcessed++;
      }
      while (findnext(&fInfo) == 0);
   }

   // process subs recursively
   if (findfirst("*", &fInfo, FA_DIREC)==0)
   {
         do
     {
       if (strcmp(fInfo.ff_name, ".") == 0) continue;
       if (strcmp(fInfo.ff_name, "..") == 0) continue;
       if ((fInfo.ff_attrib & FA_DIREC) != FA_DIREC) continue;
       chdir(fInfo.ff_name);
       sprintf(curSubdir, fInfo.ff_name);
       printf("\nprocessing subdirectory %s\\...\n", fInfo.ff_name);
         filesProcessed += process_sub(outfile);
       chdir("..");
     }
     while (findnext(&fInfo) == 0);
   }
   return filesProcessed;
}


 int process_file(FILE *outfile, char *filename)
 {
   char *pch;
   char varname[256];
   unsigned int i;
   int ch;
   FILE *sourceFile;
   char qualifiedName[256];
   static int n=0;

   // create qualified name
   if (curSubdir[0] == 0)
     sprintf(qualifiedName,"%s", filename);
   else
     sprintf(qualifiedName,"%s/%s", curSubdir, filename);
   // create variable name
   strcpy(varname, qualifiedName);
   while ((pch = strpbrk(varname, "./\\")) !=NULL) // convert slashes &
dots to underscores
   {
         *pch = '_';
   }
   fprintf(outfile,
        "const unsigned int dummy_%s = %d;\n", varname, n++);    // to
force even alignment of array\n", varname);
   fprintf(outfile, "static const unsigned char data_%s[] = {\n",
varname);
   // encode source file name (used by file system, not returned to
browser)
   fprintf(outfile, "// %s (%d chars) \n", qualifiedName,
strlen(qualifiedName)+1);
   for (i=0;i < strlen(qualifiedName)+1; i++)
   {
         fprintf(outfile, "0x%02.2x,", qualifiedName[i]);
   }
   // pad to even number of bytes to assure payload is on even boundary
   if (i % 2 == 1)
   {
         fprintf(outfile, "0x%02.2x,", 0);
     i++;
   }
   fprintf(outfile, "\n");

   // build declaration of struct fsdata_file in temp file
   fprintf(tmpFile, "const struct fsdata_file file_%s[] = { {\n",
varname);
   fprintf(tmpFile, "file_%s,\n", lastFileVar);
   fprintf(tmpFile, "data_%s,\n", varname);
   fprintf(tmpFile, "data_%s + %d,\n", varname, i);
   fprintf(tmpFile, "sizeof(data_%s) - %d} };\n\n", varname, i);
   strcpy(lastFileVar, varname);

         i = 0;
   fprintf(outfile, "\n// raw file data \n");
         sourceFile = fopen(filename, "rb");
   while ((ch = fgetc(sourceFile)) != EOF)
   {
         fprintf(outfile, "0x%02.2x,", ch);
     if ((++i % 12) == 0)
       fprintf(outfile, "\n");
   }
   fprintf(outfile, "0x00};\n\n");

         return 0;
 }

------------------------------------------------------------------------
--------------------------

The httpd.c file modifications are thus:

------------------------------------------------------------------------
----------------------------

Hope this helps!

- Jim

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf
Of richard soennichsen
Sent: Friday, July 22, 2005 6:00 PM
To: address@hidden
Subject: [lwip-users] makefsdata


Has anyone run this Perl script on a windows machine? 
I am new to Perl and have not had any luck converting
an html file to c using this script.  I have install
activePerl on my XP machine.
Any help would be appreciated.
 
Rich


_______________________________________________
lwip-users mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/lwip-users




reply via email to

[Prev in Thread] Current Thread [Next in Thread]