/* program mconvert.c  */

/* see J.Purdum, p 225 & 242, C Programming Guide */
/* Note that this works by use of passing an address pointer - see K & R */
/* p92/93, for equiv example using integers                              */

/* program to read HPGL (HewlettPackard Graphics Language) files  */
/*  and convert them to drive a Watanabe WX4671 plotter  */

#include <stdio.h>
#include <ctype.h>
#define scr (&_iob[2])  /* this to force prompt message to screen */
#define CLEARS  12      /* control code to clear screen */


main(argc, argv)
int argc;
char **argv;
{
   FILE *f1, *fopen();
   int  c, n2, x, word();
   char s[20], *gets(),  sep[10];
   double atof(), scale, xscale, yscale, y;


   putchar(CLEARS);

   if (argc != 2)
      {
      puts("\n Need a file name to read from \n");
      exit(1);
      }

   if ((f1 = fopen( argv[1], "r")) == NULL)   /* open the file */
      {
      printf("can't open %s\n",argv[1]);
      exit(1);
      }
   fprintf(scr, "\n enter xscale (eg, 0.25)" );
      gets(s);
      xscale = atof(s);
      scale = xscale;
   fprintf(scr, "\n enter yscale (eg, 0.2)" );
      gets(s);
      yscale = atof(s);
 printf("scale= %g xscale = %g yscale= %g\n", scale,xscale,yscale);

   n2 = word(f1, s, &x);

   while  ((n2 != 0) || (s[0] != '\0') )
   {
    if (s[0] == 'P' && s[1] == 'U')
     {
      scale = xscale;
      printf("\nM ");
      strcpy(sep, ", ");
      if (n2 != 0) {printf("%d", (int) (xscale*x + 0.5)); scale = yscale; }
         while (( n2 = word(f1, s, &x)) && ( s[0] == '\0'))
                   {
                     y = scale*x + 0.5;
                     printf("%s%d", sep, (int) y);
                     if (scale == xscale) (scale = yscale);
                        else (scale = xscale);
                   }
     }
    else if ((s[0] == 'P') && (s[1] == 'D'))
     {
      scale = xscale;
      printf("\nD ");
      strcpy(sep, ", ");
      if (n2 != 0) {printf("%d", (int) (xscale*x + 0.5)); scale = yscale; }
         while (( n2 = word(f1, s, &x)) && ( s[0] == '\0'))
                   {
                     y = scale*x + 0.5;
                     printf("%s%d", sep, (int) y);
                     if (scale == xscale) (scale = yscale);
                        else (scale = xscale);
                   }
     }
    else if (s[0] == 'L' && s[1] == 'T')
     {
      printf("\nL");
      if (n2 != 0) printf("%d", 10*x);
      while ( ((s[0] == 'L') && (s[1] == 'T')) || (s[0] == '\0') )
                   {
                     if ((s[0] == 'L') && (s[1] == 'T')) printf("0\nL");
                     if (n2 != 0) printf("1\nB%d", 10*x);
                     n2 = word(f1, s, &x);
                   }
      }
      else n2 = word(f1, s, &x);
   }

   printf("\n");
   fclose(f1);

}



word(fp, str, xy)

FILE *fp;
char str[];
int *xy;

{
int c, p1, p2;
char n[20];
int atoi();

   p1 = 0, p2 = 0, strcpy(str, ""), strcpy(n, "");

/*  look for an alpha or numeric char */

 while ( ((c = fgetc(fp)) != EOF) && isalnum(c) == NULL)
  {  ;  }

/*  if alpha char found, copy it & any following to str[] */
   if (isalpha(c))
      {
       str[p1] = c;
       p1++;
        while ( ((c = fgetc(fp)) != EOF) && isalpha(c))
         {
         str[p1] = c;
         p1++;
         }
      str[p1] = '\0';   /* terminate str[] */
      }

/*  if next char is numeric, copy it & any following to n[]  */
   if (isdigit(c))
      {
      n[p2] = c;
      p2++;
      while ( ((c = fgetc(fp)) != EOF) && isdigit(c))
         {
         n[p2] = c;
         p2++;
         }
      n[p2] = '\0';     /* terminate n[]   */
      }


   *xy = atoi(n);
  return (p2);
}
