/**************************************************************************/
/*                                ReFormat Utility                        */
/*                                                                        */
/*                                                                        */
/*                                     M\Cooper                           */
/*                                    PO Box 237                          */
/*                            St. David, AZ 85630-0237                    */
/*                        -------------------------------                 */
/*                        Email:  thegrendel@theriver.com                 */
/*                                                                        */
/*      Reformats the files created by the pattern matching utilities     */
/*          so that they print in an "optimum" number of columns.         */
/*                                                                        */
/**************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define FILE_OPENING_ERROR 3
#define FILENAME_MAXLEN 8
#define CR "\n"
#define MAXLEN 82
#define LINE_LEN 80
#define NOARGS 1
#define FULLARGS 3
#define INCREMENT 1
#define SPACE ' '
#define MAXWORDLEN 24
#define WORDSPACING 2
#define BUFFERSIZE 4096
#define NULL_ 0

typedef enum { LESS_THAN, EQUAL, GREATER_THAN, ANOMALY } A_flag;
typedef enum { OFF, ON } Mod_flag;


void reformat( char *fname, int maxwordlength, int thr_wlen, A_flag action );
A_flag parse_action( char *second_arg );
int parse_threshhold ( char *second_arg );
int get_max_word_length( char *fname );
void center( char *strg ); 




typedef enum { FALSE, TRUE } Boolean;

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

   char filename[ MAXLEN ];
   int mwl,
       threshhold = MAXWORDLEN;
   A_flag action_flag;

	 if( argc == NOARGS )
	    {
	    printf( "\n" );
	    puts("Enter a FILENAME to reformat... ");
	    gets( filename );
	    }
	 else
	    strcpy( filename, *( argv + 1 ) );

      if( argc == FULLARGS )
          {
          action_flag = parse_action( *( argv + 2 ) );
          threshhold =  parse_threshhold( *( argv + 2 ) );
          }
      else
         {
         action_flag = ANOMALY;
         threshhold = MAXWORDLEN;
         }

	 mwl = get_max_word_length( filename );
	 reformat( filename, mwl, threshhold, action_flag );
}




/*************************************************************/
void reformat( char *filename, int maxwl, int threshlen, A_flag action )
{

	char	word [ MAXLEN ],
      desc_messg [ MAXLEN ],
      count_messg [ MAXLEN ],
		tempstr [ MAXLEN + 3 ];

	FILE *fptr,
		*tfile;
	long wcount = 0L;
     int columns,
         interval,
         t_up = MAXWORDLEN,
         t_down = 0;



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


		 /*********************Check for word limits*********************/	 

      threshlen += 1; 
                    /*Compensate for CR at end of each
                      word.*/

      switch ( action )
             {
             case EQUAL:
                  t_up = threshlen + 1;
                  t_down = threshlen - 1;
                  sprintf( desc_messg, "List restricted to words exactly %d letters long.", threshlen - 1 );
                  break;
             case LESS_THAN:
                  t_up = threshlen;
                  t_down = 0;
                  sprintf( desc_messg, "List restricted to words less than %d letters long.", threshlen - 1 );
                  break;
             case GREATER_THAN:
                  t_up = MAXWORDLEN;
                  t_down = threshlen;
                  sprintf( desc_messg, "List restricted to words more that %d letters long.", threshlen - 1 );     
                  break;
             case ANOMALY:        /*accept all...*/
                  t_up = MAXWORDLEN;
                  t_down = 0;
             default: 
                  break;

             }

         strcat( desc_messg,"" );   
      maxwl = maxwl > t_up ? t_up : maxwl;
      /*Readjust column width, as necessary.*/

	   columns = LINE_LEN / ( maxwl + WORDSPACING );
      interval = maxwl + WORDSPACING;

		 /*********************Main Loop*************/	 

		  while( fgets( word, MAXLEN, fptr ) != NULL )

                              /*****888copy action888***/
    if( strlen( word ) > t_down &&
       strlen( word ) < t_up &&
       !isspace( *word )  )


			   {
      wcount++;

			   *( word + strlen( word ) -1 ) = NULL_;  
                           /*Gets rid of CR*/
			   

			   printf( "%-*s", interval, word );


			   if( !( wcount % columns ) )
				  printf( "\n" );
			   }
			else
    if( strlen( word)  >= MAXWORDLEN  && !iscntrl( *word ) )
			   printf( "%s", word );

     /**************999*************/


      sprintf( count_messg, "From the original list, %ld word(s) have been selected.", wcount );
      center( count_messg );
      printf( "\n\n%s\n\n", count_messg );

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

		  fclose( fptr );

      return;

}

int get_max_word_length( char *filename )
{
   int wlen,
	  maxwlen = 0; /* Longest word in file */
   char word [ MAXLEN ];
   FILE *fp;

      if( !( fp = fopen( filename, "rt" ) ) )
	        {
		    printf( "\7\7\7Cannot open file to reformat!" );
		    exit( FILE_OPENING_ERROR );
		    }


	  while( fgets( word, MAXLEN, fp ) != NULL )
		{
		wlen = strlen( word );
		if( wlen > maxwlen && wlen < MAXWORDLEN )
		   maxwlen = wlen;  /*Bump up to new
                                      value.*/
		}

	 return( maxwlen );
}

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

A_flag parse_action( char *arg2 )
{

      switch ( *arg2 )
         {
         case 'g':
         case 'G':
         case 'm':
         case 'M':
         case 'O':
         case 'o':
              return( GREATER_THAN );
         case 'l':
         case 'L':
         case 'f':
         case 'F':
         case 'U':
         case 'u':
         case 's':
         case 'S':
              return( LESS_THAN );
        case 'e':
        case 'E':
        case '=':
             return( EQUAL );
        }

      return( ANOMALY );
}

int parse_threshhold ( char *arg2b )
{

      return( atoi( arg2b + 1 ) );
}
