/**************************************************************************/
/*                                  Bingo Utility                         */
/*                                                                        */
/*                                                                        */
/*                                     M\Cooper                           */
/*                                    PO Box 237                          */
/*                            St. David, AZ 85630-0237                    */
/*                        -------------------------------                 */
/*                        Email:  thegrendel@theriver.com                 */
/*                                                                        */
/**************************************************************************/


#include "srch.h"


#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 8
#define CR "\n"
#define MAXLEN 30
#define LINE_LEN 80
#define NOARGS 1
#define FILEARGS 3
#define INCREMENT 1
#define SPACE ' '
#define XOUT '@'
#define WILDCARD '_'
#define BINGO 7
#define BUFFERSIZE 8192

char ad[] =
"BINGO utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";


void getword( char *lset, char *file_name );

typedef enum { FALSE, TRUE } Boolean;

void main( int argc, char **argv )
{

   char letterset[ MAXLEN ],
        filename [ MAXLEN ];

	 if( argc == NOARGS )
	    {
	    puts("Enter a LETTERSET to test ... ");
	    gets( letterset );
	    }
	 else
	    strcpy( letterset, *( argv + 1 ) );

     if( argc == FILEARGS ) 
         strcpy( filename, *( argv + 2 ) );
     else
         strcpy( filename, "word.lst" );

	 getword( letterset, filename );
}



/**********************************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 *letterset, char *word )
{
	Boolean error_flag = TRUE;
	static char dup_lset[ MAXLEN ];
	register char *letpos;

	 strcpy( dup_lset, letterset );
         strcat( dup_lset, "\n" );     /***55555***/
		 
		while( *word )
			{
			if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
				*letpos = XOUT;
/*********33333*********/
       else
         if( ( letpos = strchr( dup_lset, WILDCARD ) ) != NULL ) 
            *letpos = XOUT;  
			else
				{ error_flag = FALSE; break; } 
			}

		return( error_flag );
}

/*************************************************************/
void getword( char *letter_set, char *file_name )
{

	char	l_set [ MAXLEN ],
		word [ MAXLEN ],
		tempstr [ MAXLEN + 1 ],
		targetfile [ MAXLEN ],
		bar [ LINE_LEN + 1 ],
		double_bar [ LINE_LEN + 1 ],
   Bnum [ BINGO + 2 ],
   *ppos;

	FILE *fptr;
	int fnamelen,
     i;
	long wcount = 0L,
             tcount = 0L;

	   memset( bar, '-', LINE_LEN );
	   *( bar + LINE_LEN ) = 0;
	   memset( double_bar, '=', LINE_LEN );
	   *( double_bar + LINE_LEN ) = 0;

	   /****************************************/



	   if( !( fptr = fopen( file_name, "rt" ) ) )
		 {
		 printf( "\7\7\7Cannot open Wordfile: %s!", file_name );
		 exit( FILE_OPENING_ERROR );
		 }
      if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
         exit( FILE_OPENING_ERROR );





		 /*********************Main Loop*************/	 
		  while( fgets( word, MAXLEN, fptr ) != NULL )

			if( wordtest( letter_set, word ) && strlen( word ) >= BINGO + 1 ) 
			   {
			   printf( "%s", word );
			   wcount++;
			   }
                         else tcount++;   /***DEBUG***/
		  /*******************************************/




		  fclose( fptr );


}



