/**************************************************************************/
/*                    Check for no-good chars in word file                */
/**************************************************************************/


#include "srch.h"
#include <ctype.h>

#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 8
#define CR "\n"
#define MAXLEN 60
#define LINE_LEN 80
#define NOARGS 1
#define INCREMENT 1
#define SPACE ' '
#define NOT '_'
#define SINGLE 1
#define MINLEN 4
#define NULL_ 0
#define ARGCNT 2

#define BUFFERSIZE 8192
/*******8K buffer******/

typedef enum { FALSE, TRUE } Boolean;

void getword( char *wrdfile );
void center( char *strng );
Boolean wordtest( char *word );


void main(  int argc, char **argv )
{

   char letterset[ MAXLEN ],
	   input_set[ MAXLEN ],
           wfile[ MAXLEN ];  


         if( argc < ARGCNT )
             strcpy( wfile, "word.lst" );
         else
             strcpy( wfile, *(argv+1) );

      printf( "The following words test bad in the file %s:\n",
               wfile );

	 getword( wfile );
}



/**********************************WORDTEST********************************/
/*       Function tests if word is constructible from Letterset           */
/*                 Args in: char *letterset, char *word                   */
/*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
/**************************************************************************/

Boolean wordtest( char *word )
{
   char temp [MAXLEN],
	   *t;
   Boolean error_flag = TRUE;


	 strcpy( temp, word );  /*Preserve WORD*/
         t = temp;

	 while( *t )
	    {
            if( !islower( *t ) && *t != '\n' && *t != '\r' )
               return( FALSE );
            t++;
	    }

		return( error_flag );
}


/*************************************************************/

void getword( char *wordfile )
{

	char	l_set [ MAXLEN ],
		word [ MAXLEN ],
		tempstr [ LINE_LEN + 1 ],
		targetfile [ MAXLEN ],
		bar [ LINE_LEN + 1 ],
		double_bar [ LINE_LEN + 1 ],
  msg1 [ MAXLEN ], 
  msg2 [ MAXLEN ];

	FILE *fptr,
		*tfile;
	int fnamelen;
	long wcount = 0L;





	   if( !( fptr = fopen( wordfile, "rt" ) ) )
		 {
		 printf( "\7\7\7Cannot open Wordfile!" );
		 exit( FILE_OPENING_ERROR );
		 }


		 /*********************Main Loop*************/	 
		  while( fgets( word, MAXLEN, fptr ) != NULL_ )

			if( !wordtest( word ) )
				 {
				 printf( "%s\n", word );
				 wcount++;
				 }
		  /*******************************************/
           fclose( fptr );

}



void center( char *str )
{
   int padding;
   char st [ LINE_LEN + INCREMENT ];

	 padding = LINE_LEN / 2 - strlen( str ) / 2;
	 memset( st, SPACE, padding );
	 *( st + padding ) = NULL_;  /*Terminate
                                       string*/
	 strcat( st, str );
	 strcpy( str, st );

	 return;
}
