/**************************************************************************/
/*                              Select 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 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;



	   strcpy ( l_set, letter_set );


	   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( letter_set, num_lets, word ) )
			   if( !nw_test( nlset, word ) )
				  {
               			  printf( "%s", 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;
}

void parse( char *arg, char *letterset, char *nls )
{
   char *p;


	 if( *arg == '_' )
	    {
	    *letterset = NULL_;
	    strcpy( nls, ++arg );
	    return;
	    }


	 if( ( p = strchr( arg, NOT ) ) )    
             {
             *p = NULL_;
             strcpy( letterset, arg );
	     strcpy( nls, p + 1 );
             }

/*	 p = strtok( NULL_, NULL_ ); 


	 if( *p )
	    strcpy( nls, p );
*/

  else
     {
     *nls = NULL_;
     strcpy( letterset, arg );
     }

	 return;
}
