/**************************************************************************/
/*                                  XFIND UTILITY                         */
/*                                                                        */
/*                                     M\Cooper                           */
/*                                    PO Box 237                          */
/*                            St. David, AZ 85630-0237                    */
/*                        -------------------------------                 */
/*                        Email:  thegrendel@theriver.com                 */
/*                                                                        */
/*                   $2.00 to register the entire WORDY package           */
/*                                                                        */
/**************************************************************************/

#include "srch.h"
/*srch.h (source) is part of the WORDY package*/


#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 40
#define CR "\n"
#define FILE_SUFFIX ".fnd"
#define MAXLEN 30
#define LINE_LEN 80
#define NOARGS 1
#define INCREMENT 1
#define SPACE ' '
#define DBLBAR 205
#define WILDCARD '_'
/*Wildcard character in command line, may be changed*/
#define NULL__ 0

#define BUFFERSIZE 8192


void getword( char *lset, char *filename );
void center( char *strng );

typedef enum { FALSE, TRUE } Boolean;

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

   char letterset [MAXLEN],
        filename [FILENAME_MAXLEN];

	 if( argc == NOARGS )
	    {
	    puts( "Enter a LETTER PATTERN to test [example ab_c__e...
"
);
	    gets( letterset );
     strcpy( filename, "word.lst" );  /*default*/
	    }
	 else
     if( argc == NOARGS + 1 )
       {
	      strcpy( letterset, *(argv + 1) );
       strcpy( filename, "word.lst" ); /*default*/
       }
   else
      {
      strcpy( letterset, *(argv + 1) );
      strcpy( filename, *(argv + 2) );
      }

	 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;
	static char dup_lset[ MAXLEN ];
	register unsigned int cnt = 0,
					  u,
					  v;

	 strcpy( dup_lset, letterset );
		 
	 u = strlen( word );
      v = strlen( letterset );

	 while( ( *letterset == *word ) || *letterset == WILDCARD )
	    {
	    letterset++;
	    word++;
	    cnt++;
	    }

	 if( u <= cnt && u == v )
		error_flag = TRUE;
	 else
		error_flag = FALSE;


		return( error_flag );
}

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

void getword( char *letter_set, char *filename )
{

	char	l_set [ MAXLEN ],
		word [ MAXLEN ],
		tempstr [ MAXLEN + 1 ],
		targetfile [ MAXLEN ],
		bar [ LINE_LEN + 1 ],
		double_bar [ LINE_LEN + 1 ],
		messg [7],
  msg2 [16];

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

	   memset( bar, '*', LINE_LEN );
	   *( bar + LINE_LEN ) = NULL__;
	   memset( double_bar, DBLBAR, LINE_LEN );
	   *( double_bar + LINE_LEN ) = NULL__;


	   strcpy ( l_set, letter_set );
	   strcat ( letter_set, CR );

	   /*   Create name of file to store derived words in   */
	   /*********************************************************/
	   strcpy( targetfile, "matched.wds" );
	   /*********************************************************/

	   if( !( fptr = fopen( filename, "rt" ) ) )
		 {
		 printf( "\7\7\7Cannot open word file %s!", filename );
		 exit( FILE_OPENING_ERROR );
		 }
      if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
         exit( FILE_OPENING_ERROR + 1 );




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

			if( wordtest( letter_set, word ) )
			   {
			   printf( "%s", word );
			   wcount++;
			   }
		  /*******************************************/

      if( wcount == 1 )
         strcpy( msg2, "word fits" );
      else
         strcpy( msg2, "words fit" );

		  fclose( fptr );

      printf( CR );

}



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;
}
