#include "stdio.h"
#include "ctype.h"
#include "string.h"

#define MAXTABLE 24
#define QUOTE 0x22

int col_ctr = 1, line_ctr = 0;
int len_of_text[MAXTABLE];
int beg_col[MAXTABLE];
int xpos[MAXTABLE];
int ypos[MAXTABLE];

char byte;
char *sub1;
char text[MAXTABLE][81];

void compresstext (sub1,beg_col,len)
char *sub1;
int *beg_col, *len;
{
   char *sub2, *sub3;
   for (sub2 = sub1; isspace (*sub2); sub2++);
   if (*sub2 != '\0')
   {
      *beg_col = sub2 - sub1 + 1;
      for (sub3 = sub2; *sub3 != '\0'; sub3++);
      for (sub3--; isspace (*sub3); sub3--);
      sub3++;
      *sub3 = '\0';
      *len = sub3 - sub2;
      if (sub2 != sub1)
      {
         for (; sub2 <= sub3; sub2++, sub1++)
            *sub1 = *sub2;
      }
   }
}

void nextline()
{
   line_ctr++;
   if (line_ctr >= MAXTABLE)
      byte = NULL;
   else
   {
      col_ctr = 1;
      sub1 = &text[line_ctr];
   }
}

void getdata (fname)
char *fname;

{
   FILE *infile;
   int a;

   infile = fopen (fname, "r");
   if (infile == NULL)
   {
      printf ("Input Open Error\n");
      printf ("Error Code %d\n", infile);
      exit (1);
   }


   sub1 = &text[0];
   do
   {
      a = fread (&byte, sizeof (byte), 1,infile);
      if (a < 1)
         byte = NULL;

      switch (byte)
      {
         case NULL:
            *sub1 = '\0';
            compresstext (text[line_ctr], &beg_col[line_ctr], &len_of_text[line_ctr]);
            break;
         case '\n':
            *sub1 = '\0';
            compresstext (text[line_ctr], &beg_col[line_ctr], &len_of_text[line_ctr]);
            nextline ();
            break;
         default:
            *sub1 = byte;
            sub1++;
            col_ctr++;
            if (col_ctr > 80)
            {
               compresstext (text[line_ctr], &beg_col[line_ctr], &len_of_text[line_ctr]);
               nextline ();
            }
      }

   }  while (byte != NULL);

   fclose (infile);
}

void calcxy (font)
char *font;
{
   int indx1 = 0;
   int xorg = 0, yorg, baseline, txheight, txwidth;

   if (strcmpi (font, "topaz.8") == 0)
   {
      xorg = 4;
      yorg = 11;
      baseline = 0;     /* is really 6, but is not needed for itext struct */
      txheight = 8;
      txwidth = 8;
   }
   else
   {
      if (strcmpi (font, "topaz.9") == 0)
      {
         xorg = 4;
         yorg = 12;
         baseline = 0;  /* is really 6 but not need for itext structures */
         txheight = 9;
         txwidth = 10;
      }
   }

   if (xorg == 0)
   {
         printf ("%s font is not supported.\nOutput File was not created.\n", font);
         exit (1);
   }

   for (indx1 = 0; indx1 < MAXTABLE; indx1++)
   {
      if (len_of_text[indx1] > 0)
      {
         xpos[indx1] = xorg + (txwidth * (beg_col[indx1] - 1));
         ypos[indx1] = yorg + baseline + (txheight * indx1);
      }
   }
}

void givedata (outfile)
char  *outfile;
{
   FILE *fp;
   int indx1;
   int lines_of_text = 0;
   int first_time = 0;

   for (indx1 = 0; indx1 < MAXTABLE; indx1++)
   {
      if (len_of_text[indx1] > 0)
         lines_of_text++;
   }

   if (lines_of_text == 0)
   {
      printf ("Input File contains no Text\nOutput File is not created\n");
      exit (1);
   }

   fp = fopen (outfile, "w");
   if (fp == NULL)
   {
      printf ("Output Open Error\n");
      printf ("Error Code %d\n", fp);
      exit (1);
   }

   for (indx1 = MAXTABLE - 1; indx1 >= 0; indx1--)
   {
      if (len_of_text[indx1] > 0)
      {
         fprintf (fp, "struct IntuiText itext%d = {\n", lines_of_text);
         fprintf (fp, "             3,0,JAM2,          /* front and back text pens, drawmode and fill byte */\n");
         fprintf (fp, "             %3d,%3d,           /* XY origin relateive to conatiner TOPLEFT */\n", xpos[indx1], ypos[indx1]);
         fprintf (fp, "             NULL,              /* font pointer or NULL for default */\n");
         fprintf (fp, "             %c%s%c,            /* pointer to text */\n", QUOTE, text[indx1], QUOTE);
         if (first_time == 0)
            fprintf (fp, "             NULL               /* next InutiText structure */\n");
         else
            fprintf (fp, "             &itext%d            /* next InutiText structure */\n", lines_of_text + 1);
         fprintf (fp, "};\n\n");
         lines_of_text--;
         first_time = 1;
      }
   }

   fprintf (fp, "#define IntuiTextList1 itext1\n");

   fclose (fp);
}


void main(argc, argv)
int argc;
char *argv[];
{

   if (argc < 3 || argc > 4)
   {
      printf ("Invalid number of arguments received!\nArguments are:\n  1) The name of the file to read (required)\n  2) The name of the file to output (required)\n  3) The font to use (option) (ie:topaz.9) (default = topaz.8)\n");
      exit(1);
   }

   getdata (argv[1]);

   if (argc == 4)
         calcxy (argv[3]);
      else
         calcxy ("topaz.8");

   givedata (argv[2]);

}

