/**************************************************************************/
/*                             Vowel Number 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 ".5v8"
#define MAXLEN 30
#define FNLEN 40
#define LINE_LEN 80
#define NOARGS 1
#define FILEARGS 2
#define INCREMENT 1
#define SPACE ' '
#define XOUT '@'
#define WILDCARD '?'
#define FILLCHAR '_'
#define BINGO 7
#define NULL_ 0

#define BUFFERSIZE 8192



typedef enum { FALSE, TRUE } Boolean;


void getword( int no_of_vowels, char *sourcefile );
void center( char *strng );
Boolean wordtest( int vn, char *wrd );


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

   char vowelnum [ MAXLEN ],
        wordfile [ FNLEN ];
   int vn,
       wl;

	 if( argc < FILEARGS )
	    {
	    printf( "\n" );
	    puts("Enter number of vowels ");
	    gets( vowelnum );
            vn = atoi( vowelnum );
	    }
   else
     vn = atoi( *( argv + 1 ) );

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

	 getword( vn, wordfile );
}



/**********************************WORDTEST********************************/
/*       Function tests if word contains specified number of vowels       */
/*         Returns: error_flag == TRUE (1) if yes, FALSE (0) if no        */
/**************************************************************************/

Boolean wordtest( int vowelnum, char *word )
{
   char v_lset [] = "aeiou";
   int vowels_counted = 0;

      while( *word )
         {
         if( strchr( v_lset, *word++ ) != NULL )
            vowels_counted++;
         }

      if( vowels_counted == vowelnum )
          return( TRUE );
      else
          return( FALSE );
}

/*************************************************************/
void getword( int vowelnum, char *sourcefile )
{

    char file_name[ MAXLEN ],
         word [ MAXLEN ],
         tempstr [ MAXLEN + 1 ],
         tstr [ MAXLEN ],
        *ppos;

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


       strcpy( file_name, "word.lst" );



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


	   if( !( fptr = fopen( sourcefile, "rt" ) ) ) /***ZZZ***/
		 {
		 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( vowelnum, word )  ) 
			   {
			   printf( "%s", word );
			   wcount++;
			   }
		  /*******************************************/


		  fclose( fptr );

              return;

}



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

