home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / html_gen.zip / html_gen.c < prev    next >
C/C++ Source or Header  |  1997-12-27  |  4KB  |  164 lines

  1. /************************************************************************
  2. *  HTML generation - Main file
  3. *
  4. *  Author:      U.A.Mueller
  5. *  Last update: 27.12.1997
  6. *************************************************************************/
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define prog_version "1.0"
  12. #define prog_date    "27. Dec. 1997"
  13.  
  14. FILE  *fout,
  15.       *fp,
  16.       *fmac;
  17.  
  18. int strpos (const char *source, const char *comp)
  19. {
  20.   char temp[255] = "",
  21.        walk[255] = "";
  22.   int  i = 0;
  23.  
  24.   strcpy (temp, comp);
  25.   strcpy (walk, source);
  26.  
  27.   while (walk[i] != temp[0])
  28.     i++;
  29.  
  30.   return (i);
  31. }
  32.  
  33. char *substr (const char *source, int first, int second)
  34. {
  35.   char temp[255] = "",
  36.        *temp2;
  37.  
  38.   strcpy (temp, source);
  39.   temp[second - first + 1] = '\0';
  40.   temp2 = &temp[first - 1];
  41.   return (temp2);
  42. }
  43.  
  44. FILE *openFile (char *fname, char *extend, char *mode)
  45. {
  46.   char *buffer = "";
  47.   FILE *fpp;
  48.  
  49.   strcpy (buffer, fname);
  50.   strcat (buffer, extend);
  51.   fpp = fopen (buffer, mode);
  52.  
  53.   if (fpp == NULL)
  54.   {
  55.     printf ("Error opening file %s\n\n", buffer);
  56.     exit (1);
  57.   }
  58.  
  59.   return (fpp);
  60. }
  61.  
  62. void insertMacro (char *macroWithPar)
  63. {
  64.   char buffer[255],
  65.        mfline[255];
  66.   char *macline,
  67.        *param,
  68.        *macpar;
  69.  
  70.   macline = macroWithPar;
  71.   param = strstr (macline, "(");
  72.  
  73.   if (param != NULL)
  74.   {
  75.     param++;                              /* remove opening parathesis */
  76.     param[strlen (param) - 2] = '\0';     /* remove closing parathesis */
  77.   }
  78.  
  79.   /* get macro name and open macro file */
  80.   if (strcspn (macline, "(") == strlen (macline))
  81.     strcpy (buffer, substr (macline, 1, strlen (macline) - 1));
  82.   else
  83.     strcpy (buffer, substr (macline, 1, strcspn (macline, "(")));
  84.  
  85.   fmac = openFile (buffer, ".macro", "rt");
  86.   fseek (fmac, 0, SEEK_SET);
  87.  
  88.   while (NULL != fgets (mfline, 255, fmac))
  89.   {
  90.     macpar = strstr (mfline, "!A!A!");
  91.     strcpy (buffer, "");
  92.  
  93.     if (macpar != NULL)
  94.     { /* line contains parameter wilcard */
  95.       while (macpar != NULL)  /* repeat until line contains no wildcards anymore */
  96.       {
  97.         if (param == NULL)
  98.         { /* no parameter left from macro call */
  99.           printf ("Error in macro %s (no parameters)\n\n", macline);
  100.           exit (1);
  101.         }
  102.  
  103.         strcat (buffer, substr (mfline, 1, strpos (mfline, "!A!A!")));  /* copy line till wildcard to buffer */
  104.         strcat (buffer, substr (param, 1, strcspn (param, ",")));       /* insert parameter */
  105.         param = strstr (param, ",");               /* remove used parameter and comma from param line */
  106.  
  107.         if (param != NULL)
  108.          param++;
  109.  
  110.         macpar += 5;
  111.         strcpy (mfline, macpar);             /* skip wildcard string from input line */
  112.         macpar = strstr (mfline , "!A!A!");  /* test for further wildcards */
  113.     }
  114.  
  115.     strcat (buffer, mfline);                 /* add remainder of line to translated line */
  116.     fputs (buffer, fout);
  117.     }
  118.     else
  119.       fputs (mfline, fout);
  120.   }
  121.  
  122.   fputs ("\n", fout);
  123.   fclose (fmac);
  124. }
  125.  
  126. void parse_file (char *fname)
  127. {
  128.   char fline[255] = "";
  129.   char *macline;
  130.  
  131.   fp   = openFile (fname, ".source", "rt");
  132.   fout = openFile (fname, ".html", "wt");
  133.   fseek (fp, 0, SEEK_SET);
  134.   fseek (fout, 0, SEEK_SET);
  135.  
  136.   while (NULL != fgets (fline, 255, fp))
  137.   {
  138.     macline = strstr (fline, "M!A!C!R!O");
  139.  
  140.     if (macline != NULL)                       /* line contains macro */
  141.     {
  142.       macline += 9;                            /* remove macro sign */
  143.       insertMacro (macline);
  144.     }
  145.     else
  146.       fputs (fline, fout);
  147.   }
  148.  
  149.   fclose (fp);
  150.   fclose (fout);
  151. }
  152.  
  153. int main (int argc, char *argv[])
  154. {
  155.   if (argc < 2)
  156.   {
  157.     printf ("HTML parser v%s by U.A.Mueller %s\n", prog_version, prog_date);
  158.     printf ("Usage: %s <file name>\n\n", argv[0]);
  159.   }
  160.   else
  161.     parse_file (argv[1]);
  162.  
  163.   return (0);
  164. }