/**************************************************************************/
/*                           Multi-word Anagram Utility                   */
/*                                                                        */
/*                                     M\Cooper                           */
/*                                    PO Box 237                          */
/*                            St. David, AZ 85630-0237                    */
/*                        -------------------------------                 */
/*                        Email:  thegrendel@theriver.com                 */
/*                Web: http://personal.riverusers.com/~thegrendel         */ 
/*                                                                        */
/*                                                                        */
/**************************************************************************/


#include <ctype.h>
#include <conio.h>
#include "srch.h"

#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 8
#define CR "\n"
#define BASEFILENAME "anagram_.bf"
#define TMPFILENAME "anagram_.000"
#define TMPFILENAME2 "anagram_.001"
#define FILE_SUFFIX ".mwa"
#define MAXLEN 40
#define LINE_LEN 80
#define NOARGS 1
#define INCREMENT 1
#define BINGOLEN 8
#define SPACE ' '
#define XOUT '@'
#define WILDCARD '?'
#define FILLCHAR '_'
#define ASTERISK "*"
#define WD 12
#define CMDLINEERR 6

#define BUFFERSIZE 8192

   char ad [] = "MULTI-word unscramble  - by <thegrendel@theriver.com>";

typedef enum { FALSE, TRUE } Boolean;


void getword( char *lset, char *filename );
void center( char *strng );
Boolean isanagram( char *letterset, char *word );
void mtest( char *lettset, int Number_of_words );
void cleanup( void );
void opening_screen( void );



void main( int argc, char **argv )
{

   char letterset[ MAXLEN ],
        filename[ MAXLEN ],
        wordqty[ BINGOLEN ];
   int wordno;


	 if( argc == NOARGS )
	    {
	    puts("Enter a LETTERSET to test ... ");
	    gets( letterset );
            printf( "\n\nHow many words in each anagram? ");
            gets( wordqty );
	    }
	 else
	    strcpy( letterset, *( argv + 1 ) );

         if( argc == NOARGS + 1 )
            {
            printf( "\n\nHow many words in each anagram? ");
            gets( wordqty );
            }


            if( argc > NOARGS + 1 )         
            strcpy( wordqty, *( argv + 2 ) );


         if( argc >= NOARGS + 2 )
            if( ( !islower( **( argv + 1 ) ) ) && **( argv + 1 ) != '?' ||
                !isdigit( **( argv + 2 ) ) )
                 {           
                 printf( "\nform: multi letterset number_of_words\n" );
                 exit( CMDLINEERR ); 
                 }



        if( strlen( wordqty ) > 1 )
                 {
                 printf( "\nToo many words, it would take forever!\n" );
                 exit( CMDLINEERR );
                 }

        if( *( wordqty ) == '1' )
                 {
                 printf( "\nNumber of words must be greater than 1.\n" );
                 exit( CMDLINEERR );
                 }



         if( argc == NOARGS + 3 )
            strcpy( filename, *( argv + 3 ) );
         else
            strcpy( filename, Wordfile );





	 wordno = atoi( wordqty );

	 opening_screen();

	 getword( letterset, filename );

         mtest( letterset, wordno );

         cleanup();

      printf( "\7\7\7" );  /****Alert, program end***/


}


/**********************************ISANAGRAM*******************************/
/*       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 isanagram( char *letterset, char *word )
{
	Boolean error_flag = TRUE;
	static char dup_lset[ MAXLEN ];
	register char *letpos;

	 strcpy( dup_lset, letterset );
         strcat( dup_lset, "\n" );

		while( *word )
			{

                        if( *word == SPACE )
                           word++;
 

			if( ( letpos  = strchr( dup_lset, *word++ ) ) != NULL )
				*letpos = XOUT;
      else
         if( ( letpos = strchr( dup_lset, WILDCARD ) ) != NULL ) 
            *letpos = XOUT;

			else
				{ error_flag = FALSE; break; }
			}

		return( error_flag );
}

/*************************************************************/
void getword( char *letter_set, char *fname )
{

	char	l_set [ MAXLEN ],
		word [ MAXLEN ],
                asterisk [ MAXLEN ],
		tempstr [ MAXLEN + 1 ],
		targetfile [ MAXLEN ],
                msg [ WD ],
                *ppos;

	FILE *fptr,
             *tfile,
	     *t2file;
        int i;
	long wcount = 0L;



	   strcpy ( l_set, letter_set );

	   /*********************************************************/

	   if( !( fptr = fopen( fname, "rt" ) ) )
		 {
		 printf( "\7\7\7Cannot open Wordfile!" );
		 exit( FILE_OPENING_ERROR );
		 }
      if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE * 2 ) )
         exit ( FILE_OPENING_ERROR );  /*Extra buffering.*/



	   if( !( tfile = fopen( BASEFILENAME, "wt" ) ) )
		 {
		 printf( "\7\7\7Cannot open intermediate file" );
		 exit( FILE_OPENING_ERROR );
		 }
      if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
         exit ( FILE_OPENING_ERROR );  /*Extra buffering.*/

	   if( !( t2file = fopen( TMPFILENAME, "wt" ) ) )
		 {
		 printf( "\7\7\7Cannot open intermediate file" );
		 exit( FILE_OPENING_ERROR );
		 }
      if( setvbuf( t2file, NULL, _IOFBF, BUFFERSIZE ) )
         exit ( FILE_OPENING_ERROR );  /*Extra buffering.*/




		 /*********************Main Loop*************/	 
		  while( fgets( word, MAXLEN, fptr ) != NULL )

			if( isanagram( letter_set, word ) )
			   {
			   fprintf( tfile, "%s", word );
			   fprintf( t2file, "%s", word );
			   wcount++;
			   }
		  /*******************************************/





      fclose( fptr );
      fclose( tfile );
      fclose( t2file );

}



void center( char *str )
{
   int padding;
   char st [ LINE_LEN + INCREMENT ];

	 padding = LINE_LEN / 2 - strlen( str ) / 2;
	 memset( st, SPACE, padding );
	 *( st + padding ) = 0;
	 strcat( st, str );
	 strcpy( str, st );

	 return;
}


void mtest( char *letterset, int wordcount )
{
   FILE *baselist,
        *alist,
        *blist,
        *savefile;
   int tlen,
       t2len,
       combowlen,
       lsetlen,
       fnamelen,
       ndex,
       i,
       space_count = 0;
   unsigned Count = 0;
   char baseword [MAXLEN],
        nextword [MAXLEN],
        comboword [LINE_LEN],
        savefilename [MAXLEN],
        tmpstr [LINE_LEN],
        tmpstr2 [LINE_LEN];


            /**************7777777************/


	   /*   Create name of file to store derived words in   */
	   /*********************************************************/
	   fnamelen = strlen( letterset );
	   if( fnamelen  > FILENAME_MAXLEN )
		  fnamelen = FILENAME_MAXLEN;
	   strncpy( savefilename, letterset, fnamelen );
	   *( savefilename + fnamelen ) = NULL;
	   //NULL-terminate string, so strcat works, ha, ha.

      for( i = 0; i < fnamelen; i++ )
         if( *( savefilename + i ) == WILDCARD )
             *( savefilename + i ) = FILLCHAR;


	   strcat( savefilename, FILE_SUFFIX );
	   /*********************************************************/




    sprintf( tmpstr, "The results will be saved in the file --> %s <--.",
             savefilename );
    center( tmpstr );
    printf( "\n\n%s\n\n", tmpstr );

    lsetlen = strlen( letterset );

    savefile = fopen( savefilename, "w" );

    /************1111111***File Header***1111111********/

    strcpy( tmpstr2, ad );
    center( tmpstr2 );
    fprintf( savefile, "%s", tmpstr2 );
    fprintf( savefile, "%s", CR CR );

    sprintf( tmpstr,
    "-%s-, split into %d words, yields:",
     letterset, wordcount );
    strcpy( tmpstr2, tmpstr );
    strset( tmpstr2, '=');
    center( tmpstr );
    center( tmpstr2 );
    fprintf( savefile, "%s", tmpstr2 );
    fprintf( savefile, "%s", CR );
    fprintf( savefile, "%s", tmpstr );   
    fprintf( savefile, "%s", CR );
    fprintf( savefile, tmpstr2 );
    fprintf( savefile, "%s", CR CR );


    for( ndex = 1; ndex < wordcount; ndex++ )
      {
      baselist = fopen( BASEFILENAME, "r" );
      alist = fopen( TMPFILENAME, "r" );
      blist = fopen( TMPFILENAME2, "w" );

      ++space_count; 
      while( fgets( baseword, MAXLEN, baselist ) != NULL )
           {
           tlen = strlen( baseword );
           *( baseword + (tlen - 1) ) = SPACE; 

           while( fgets( nextword, MAXLEN, alist ) != NULL )
               {
               strcpy( comboword, baseword ); 
               strcat( comboword, nextword );
               combowlen = strlen( comboword );
               if( space_count + 1 == wordcount &&
                   combowlen - space_count - 1 == lsetlen &&
                   isanagram( letterset, comboword ) )
                   {
                   fprintf( savefile, "%s", comboword );
                   Count++;
                   }
                   /**************888888**************/
               else
                  if( combowlen - space_count - 1 < lsetlen &&
                      isanagram( letterset, comboword ) )
                          fprintf( blist, "%s", comboword );
               }
           rewind( alist );
           }
           rewind( baselist );

      fclose( baselist );
      fclose( alist );
      fclose( blist );

      unlink( TMPFILENAME );
      rename( TMPFILENAME2, TMPFILENAME );
      }

      /***************22222   File Footer   22222*************/

      sprintf( tmpstr, "A total of %d anagrams found.", Count );
      center( tmpstr );

      fprintf( savefile, "%s", CR CR );
      fprintf( savefile, "%s", tmpstr );
      printf( "%s", CR CR );
      printf( "%s", tmpstr );
      printf( "%s", CR CR );


      fclose( savefile );

}

/*****************************************************************/

void cleanup()
{
      unlink( BASEFILENAME );
      unlink( TMPFILENAME );

}



void opening_screen()
{

   char bar [ LINE_LEN + 1 ],
        double_bar [ LINE_LEN + 1 ],
        tempstr [ LINE_LEN ],
	msg1 [ MAXLEN ],
        msg2 [ MAXLEN ];


	   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 );
	   /****************************************/

	   /**************'Wait' Message************/
	   printf( CR CR );
	   strcpy( tempstr, "WORKING..." );
    center( tempstr );
    printf( tempstr );
    printf( CR CR );

	   strcpy( tempstr, "This may take a while. Please be patient." );
    center( tempstr );
    printf( tempstr );
    printf( CR CR );


    strcpy( tempstr, "You will be alerted by a triple beep when the program ends." );
    center( tempstr );
    printf( tempstr );
    printf( CR );


    strcpy( tempstr, 
   "You may wish to turn off your monitor to prevent screen burn-in.");
   center( tempstr );
   printf( tempstr );
   printf( CR CR );


/************  printf( "\7\7\7" );  //Bell  *******/

}


