/**************************************************************************/
/*                              Select Utility                            */
/*                                                                        */
/*                                 M\Cooper                               */
/*                                PO Box 237                              */
/*                        St. David, AZ 85630-0237                        */
/*                        -------------------------------                 */
/*                        Email:  thegrendel@theriver.com                 */
/*                                                                        */
/*                                                                        */
/**************************************************************************/


#include "srch.h"
#include <ctype.h>
#include <conio.h>


#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 8
#define CR "\n"
#define FILE_SUFFIX ".sel"
#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 BUFFERSIZE 8192
/*******8K buffer******/

char ad[] =
"SELECT tool by M\\Cooper, thegrendel@theriver.com";

typedef enum { FALSE, TRUE } Boolean;

void getword( char *lset, char *nlset, int num_lets, char *wrdfile );
void center( char *strng );
void parse( char *argument, char *lset, char *not_letterset );
Boolean wordtest( char *letterset, int how_many_hits, char *word );
char *nw_test( char *nlset, char *word );


void main( int argc, char **argv )
{

   char letterset[ MAXLEN ],
	   n_letterset[ MAXLEN ],
	   input_set[ MAXLEN ],
    wfile [ MAXLEN ],
    ninput [ MAXLEN ];
    int number_of_letters;

	   *n_letterset = NULL_;

	 if( argc == NOARGS )
	    {
	    printf( "\n\n\n" );
	    puts( "Enter a LETTERSET to test [~ or ! for excluded letters]..." );
	    gets( input_set );
	    parse( input_set, letterset, n_letterset );

      printf( "\n\n" );
      puts( "Enter the name of the word file to search... " );
      gets( wfile );

      printf( "\n" );
      puts( "How many letter to select from the set? " );
      gets( ninput );
      number_of_letters = atoi( ninput );

	    }

      if( argc == NOARGS + 1 )
         {
         printf( "\n" );
         puts( "How many letter to select from the set? " );
         gets( ninput );
         number_of_letters = atoi( ninput );
         parse( argv[1], letterset, n_letterset );
         strcpy( wfile, "word.lst" );
         }


      if( argc == NOARGS + 2 )
         {
         strcpy( wfile, "word.lst" );
         if( !isalpha( *argv[2] ) )
            {
            number_of_letters = atoi ( argv[2] );
	    parse( argv[1], letterset, n_letterset );
            }
          else
            {
            number_of_letters = atoi ( argv[1] );
	    parse( argv[2], letterset, n_letterset );
            }
          }

      else
         if( argc == NOARGS + 3 )
            {
            strcpy( wfile, argv [3] );

            if( !isalpha( *argv[2] ) )
               {
               number_of_letters = atoi ( argv[2] );
	       parse( argv[1], letterset, n_letterset );
               }
            else
              {
              number_of_letters = atoi ( argv[1] );
	      parse( argv[2], letterset, n_letterset );
              }
            }

	 getword( letterset, n_letterset, number_of_letters, 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 *letterset, int Hits, char *word )
{
   char temp [MAXLEN],
	   *t, *u;
   int hits = 0;

	 if( *letterset == NULL_ )
	    return( TRUE );  /*All valid if no specs
                                     given*/

	 strcpy( temp, word );  /*Preserve WORD*/

	 while( *letterset )
	    {
	    t = strchr( temp, *letterset );

	    if( t )
               {
               hits++;
               u = strchr( t + 1, *letterset );
               if( u )
                  return( FALSE ); /*eliminate doubles of same letter*/
               }	

             letterset++;
	
	 /*   *t = '*'; */   /*Remove occurrence of letter*/
	    }

      if( hits == Hits )
        return( TRUE );
      else
        return( FALSE );
}

char *nw_test( char *nlset, char *word )
{
   char *ptr;

	 ptr = ( strpbrk( word, nlset ) );

	 return( ptr );
}

/*************************************************************/

void getword( char *letter_set, char *nlset, int num_lets, 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;





	   memset( bar, '-', LINE_LEN );
	   *( bar + LINE_LEN ) = NULL;
	   memset( double_bar, '=', LINE_LEN );
	   *( double_bar + LINE_LEN ) = NULL;

	   /*************opening credits*************/
	   clrscr();
	   printf( double_bar );
	   strcpy( tempstr, ad );
	   center ( tempstr );
	   printf( tempstr );
	   printf( CR );
	   printf( double_bar );
	   printf( CR );
	   /****************************************/

	   /*   Create name of file to store derived words in   */
	   /*********************************************************/
    strcpy( l_set, letter_set );

	   fnamelen = strlen( l_set );

	   if( fnamelen > 0 )
		 {
		 if( fnamelen  > FILENAME_MAXLEN )
			fnamelen = FILENAME_MAXLEN;
		 strncpy( targetfile, l_set, fnamelen );
		 *( targetfile + fnamelen ) = NULL;
            //NULL-terminate string, so strcat works, ha, ha.
		 }
	   else
		 strcpy( targetfile, "not" );

	   strcat( targetfile, FILE_SUFFIX );
	   /*********************************************************/

	   /**************'Wait' Message************/
	   printf( CR CR );
	   printf( "WORKING...\n\n" );
	   printf( "This will take a few seconds.\n" );

	   printf( "Please be patient.\n\n" );

	   printf( "Now searching word file %s ", wordfile );
    printf( "\nand writing file of valid words containing %d letters of -%s-",
             num_lets, letter_set );

	   strcpy ( l_set, letter_set );


	   if( !( fptr = fopen( wordfile, "rt" ) ) )
		 {
		 printf( "\7\7\7Cannot open Wordfile!" );
		 exit( FILE_OPENING_ERROR );
		 }

	   if( !( tfile = fopen( targetfile, "wt" ) ) )
		 {
		 printf( "\7\7\7Cannot open file to save words in!" );
		 exit ( FILE_OPENING_ERROR + 1 );
		 }
      if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
         exit( FILE_OPENING_ERROR );




	   sprintf( tempstr, 
    "Word(s) containing: %d letters of %s", num_lets, strupr( l_set ) );
	   if( *nlset )
			{
			strcat( tempstr, ", but NOT -" );
			strcat( tempstr, nlset );
			strcat( tempstr, "-" );
			}
	   strcat( tempstr, CR );

	   center( tempstr );
	   fprintf( tfile, double_bar );
    fprintf( tfile, CR );
	   fprintf( tfile, tempstr );
	   fprintf( tfile, double_bar );
	   fprintf( tfile, CR );



		 /*********************Main Loop*************/	 
		  while( fgets( word, MAXLEN, fptr ) != NULL_ )

			if( wordtest( letter_set, num_lets, word ) )
			   if( !nw_test( nlset, word ) )
				  {
				 fprintf( tfile, "%s", word );
				 wcount++;
				  }
		  /*******************************************/

      if( wcount == SINGLE )
          {
          strcpy( msg1, "word" );
          strcpy( msg2, "word contains" );
          }
      else
          {
          strcpy( msg1, "words" );
          strcpy( msg2, "words contain" );
          }

		  fprintf( tfile, bar );
		  sprintf( tempstr, "%ld %s %d letters of %s.",
				 wcount, msg2, num_lets, l_set );
		  if( *nlset )
			{
			strcat( tempstr, ".. but NOT --- " );
			strcat( tempstr, nlset );
			strcat( tempstr, "." );
			}

    fprintf( tfile, "\n" );
		  center( tempstr );		      
		  fprintf( tfile, tempstr );
		  fprintf( tfile, "\n\n" );

		  center( ad );
		  fprintf( tfile, ad );

		  fcloseall();

		  sprintf( tempstr,
				 "The file %s has %ld %s containing %d letters of %s.",
				 targetfile, wcount, msg1, num_lets, l_set );
		  if( NULL != *nlset )
			{
			strcat( tempstr, ".. but NOT --> " );
			strcat( tempstr, nlset );
			strcat( tempstr, "." );
			}

		  center( tempstr );
		  printf( CR CR );
		  printf( tempstr );
		  printf( "\7" );  //Bell

}



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;
}



void parse( char *arg, char *letterset, char *nls )
{
   char *p;

	 if( *arg == '~' || *arg == '!' )
	    {
	    *letterset = NULL;
	    strcpy( nls, ++arg );
	    return;
	    }


	 p = strtok( arg, NOT );
	 strcpy( letterset, p );

	 p = strtok( NULL, NULL );

	 if( *p )
	    strcpy( nls, p );
  else
     *nls = NULL;

	 return;
}
