/**********************************************************************/
/***  vefc.c						    WA8YCD  ***/
/***  Ver 2.1						 25 May 89  ***/
/***								    ***/
/***  This utility is made available to all amateurs or potential   ***/
/***  amateurs for use in training and examinations. For VEs, it    ***/
/***  can be used to create legal tests for any level. For the	    ***/
/***  student, it can be used to generate sample tests. 	    ***/
/***								    ***/
/***  Compiled using Turbo C Version 1.0 under MS-DOS 3.2	    ***/
/***  ___________________________________________		    ***/
/***  Copyright (c) 1989 Robert L. West, Jr.			    ***/
/***								    ***/
/***  Distribution of this program is allowed provided no monetary  ***/
/***  compensation beyond reasonable reproduction costs is made.    ***/
/***  The question pools are in the public domain. The program is   ***/
/***  provided as a public service to the amateur community.	    ***/
/**********************************************************************/

#include <stdio.h>

#define  MIN_LTR      65
#define  MAX_LTR      91
#define  MIN_NUM      48
#define  MAX_NUM      58
#define  PERIOD       46
#define  COMMA	      44
#define  DN	      47
#define  QUERY	      63
#define  BT	      45
#define  AR	      64
#define  SK	      36
#define  LENGTH_5     25
#define  LENGTH_13    65
#define  LENGTH_20   100
#define  TRUE	       1
#define  FALSE	       0

main(argc, argv)
int argc;
char *argv[];
{
  int	 i, inch=0, no_zeros=1, output_to_file;
  long	 counts[256];
  float  test_time_5, test_time_13, test_time_20;
  char	c;
  FILE  *fopen(), *infile, *outfile;

/* first, see if the correct number of parameters are given... */
  if (argc < 2)
  { printf("\nusage:\n    freq input-filename [output-filename]\n");
    printf("\n   if output-filename is omitted display appears \n");
    printf("   on the screen.\n\n");
    exit();
  }

/* next, see if the first parameter can be opened as an input file... */
  if ((infile = fopen(argv[1], "rb")) == NULL)
  { printf("\nUnable to open input file, \"%s\"!\n\n", argv[1]);
    printf("vefc:  abnormal termination.\n");
    exit();
  }

/* now, see if the second parameter can be opened as an output file... */
  if (argc == 3)
  { if ((outfile = fopen(argv[2], "w")) == NULL)
    { printf("\nUnable to open output file, \"%s\"!\n\n", argv[2]);
      printf("vefc:  abnormal termination.\n");
      exit();
    }
  }
  else
  { if ((outfile = fopen("CON", "w")) == NULL)
    { printf("\nUnable to open console for output!\n\n");
      printf("vefc:  abnormal termination.\n");
      exit();
    }
  }

/*************************************************************************/
/***								       ***/
/*************************************************************************/

  fprintf(outfile, "- VE CODE EXAM VALIDATER ver 2.1,  05/25/89 -\n");
  fprintf(outfile, "\n");
  fprintf(outfile, "      ....5...10...15...20...25\n");
  fprintf(outfile, "%5d ", inch);

  for (i=0; i<256; i++)
    counts[i]=0;

  while((c=getc(infile)) != EOF)
  {
    if (c>='a' && c<='z')
      c=c-'a'+'A';

    switch((int)c)
    {
      case 'A': case 'F': case 'K': case 'P': case 'U':
      case 'B': case 'G': case 'L': case 'Q': case 'V':
      case 'C': case 'H': case 'M': case 'R': case 'W':
      case 'D': case 'I': case 'N': case 'S': case 'X':
      case 'E': case 'J': case 'O': case 'T': case 'Y':
      case 'Z': case '/': case '@': case '$': case '-':
	counts[(int)c]++;
	fprintf(outfile, "%c", c);
	if  ( !(++inch % 25) )
	  fprintf(outfile, "\n%5d ", inch);
	break;

      case '0': case '1': case '2': case '3': case '4':
      case '5': case '6': case '7': case '8': case '9':
      case '.': case ',': case '?':
	counts[(int)c]++;
	fprintf(outfile, "%c", c);
	if  ( !(++inch % 25) )
	  fprintf(outfile, "\n%5d ", inch);
	fprintf(outfile, "_");
	if  ( !(++inch % 25) )
	  fprintf(outfile, "\n%5d ", inch);
	break;

      default:
	break;
    }
  }
  fclose(infile);     /* We're finished with the input file, so close it */

  no_zeros = TRUE;
  for (i=MIN_LTR; i<MAX_LTR; i++)
    no_zeros = no_zeros && counts[i];
  for (i=MIN_NUM; i<MAX_NUM; i++)
    no_zeros = no_zeros && counts[i];
  no_zeros = no_zeros && counts[PERIOD];
  no_zeros = no_zeros && counts[COMMA];
  no_zeros = no_zeros && counts[DN];
  no_zeros = no_zeros && counts[QUERY];
  no_zeros = no_zeros && counts[BT];
  no_zeros = no_zeros && counts[AR];
  no_zeros = no_zeros && counts[SK];

  if (no_zeros)
  {
    fprintf(outfile, "\n");
    fprintf(outfile, "\n");
    fprintf(outfile, "All required characters are contained in this test.\n");
  }
  else
  {
    fprintf(outfile, "\n");
    fprintf(outfile, "\n");
    fprintf(outfile, "The following characters are in this test:\n");

    for (i=MIN_LTR; i<MAX_LTR; i++)
      if (counts[i])
	fprintf(outfile, "%s ", &i);

    for (i=MIN_NUM; i<MAX_NUM; i++)
      if (counts[i])
	fprintf(outfile, "%s ", &i);

    if (counts[PERIOD])
      fprintf(outfile, "%c ", PERIOD);
    if (counts[COMMA])
      fprintf(outfile, "%c ", COMMA);
    if (counts[DN])
      fprintf(outfile, "%c ", DN);
    if (counts[QUERY])
      fprintf(outfile, "%c ", QUERY);
    if (counts[BT])
      fprintf(outfile, "%c ", BT);
    if (counts[AR])
      fprintf(outfile, "%c ", AR);
    if (counts[SK])
      fprintf(outfile, "%c ", SK);

    fprintf(outfile, "\n");
    fprintf(outfile, "The following characters are not in this test:\n");

    for (i=MIN_LTR; i<MAX_LTR; i++)
      if (!counts[i])
	fprintf(outfile, "%s ", &i);

    for (i=MIN_NUM; i<MAX_NUM; i++)
      if (!counts[i])
	fprintf(outfile, "%s ", &i);

    if (!counts[PERIOD])
      fprintf(outfile, "%c ", PERIOD);
    if (!counts[COMMA])
      fprintf(outfile, "%c ", COMMA);
    if (!counts[DN])
      fprintf(outfile, "%c ", DN);
    if (!counts[QUERY])
      fprintf(outfile, "%c ", QUERY);
    if (!counts[BT])
      fprintf(outfile, "%c ", BT);
    if (!counts[AR])
      fprintf(outfile, "%c ", AR);
    if (!counts[SK])
      fprintf(outfile, "%c ", SK);

    fprintf(outfile, "\n");
  }

  test_time_5  = (float) inch / LENGTH_5;
  test_time_13 = (float) inch / LENGTH_13;
  test_time_20 = (float) inch / LENGTH_20;

  fprintf(outfile, "\n");
  fprintf(outfile, "This test contains %5d characters\n", inch);
  fprintf(outfile, "\n");
  fprintf(outfile, "This test lasts %8.2f minutes at  5 wpm.\n",
		   test_time_5);
  fprintf(outfile, "This test lasts %8.2f minutes at 13 wpm.\n",
		   test_time_13);
  fprintf(outfile, "This test lasts %8.2f minutes at 20 wpm.\n",
		   test_time_20);

  fclose(outfile);
  printf("\n\nvefc:  normal termination.\n");
}
