home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / games / wordy / xtract.c < prev    next >
C/C++ Source or Header  |  1994-11-05  |  7KB  |  290 lines

  1. /**************************************************************************/
  2. /*                             Extract Utility                        */
  3. /*                                                            */
  4. /*                                 M\Cooper                            */
  5. /*                        3425 Chestnut Ridge Rd.                        */
  6. /*                        Grantsville, MD 21536-9801                    */
  7. /*                        --------------------------                    */
  8. /*                        Email:  thegrendel@aol.com                    */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package             */
  11. /*                                                            */    
  12. /**************************************************************************/
  13.  
  14.  
  15. /**********************************WORDTEST********************************/
  16. /*       Function tests if word is constructible from Letterset            */
  17. /*                 Args in: char *letterset, char *word                          */
  18. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  19. /**************************************************************************/
  20.  
  21. #include <conio.h>
  22. #include "srch.h"
  23.  
  24.  
  25. #define FILE_OPENING_ERROR 3
  26. #define FILENAME_MAXLEN 8
  27. #define CR "\n"
  28. #define FILE_SUFFIX ".xtr"
  29. #define MAXLEN 30
  30. #define LINE_LEN 80
  31. #define NOARGS 1
  32. #define INCREMENT 1
  33. #define SPACE ' '
  34. #define NOT "~!"
  35.  
  36. char ad[] =
  37. "XTRACT tool by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  38.  
  39. typedef enum { FALSE, TRUE } Boolean;
  40.  
  41. void getword( char *lset, char *nlset );
  42. void center( char *strng );
  43. void parse( char *argument, char *lset, char *not_letterset );
  44. Boolean wordtest( char *letterset, char *word );
  45. char *nw_test( char *nlset, char *word );
  46.  
  47.  
  48. void main( int argc, char **argv )
  49. {
  50.  
  51.    char letterset[ MAXLEN ],
  52.        n_letterset[ MAXLEN ],
  53.        input_set[ MAXLEN ];
  54.  
  55.      strcpy ( n_letterset, NULL );
  56.  
  57.      if( argc == NOARGS )
  58.         {
  59.         clrscr();
  60.         puts("Enter a LETTERSET to test [~ or ! for excluded letters]...");
  61.         gets( input_set );
  62.         parse( input_set, letterset, n_letterset );
  63.         }
  64.      else
  65.         parse( argv[1], letterset, n_letterset );
  66.  
  67.      getword( letterset, n_letterset );
  68. }
  69.  
  70.  
  71.  
  72. Boolean wordtest( char *letterset, char *word )
  73. {
  74.    char temp [MAXLEN],
  75.        *t;
  76.    Boolean error_flag = TRUE;
  77.  
  78.      if( *letterset == NULL )
  79.         return( error_flag );  //All valid if no specs given
  80.  
  81.      strcpy( temp, word );  //Preserve WORD
  82.  
  83.      while( *letterset )
  84.         {
  85.         t = strchr( temp, *letterset++ );
  86.  
  87.         if( !t )
  88.           {
  89.           error_flag = FALSE;
  90.           return( error_flag );
  91.           } 
  92.         
  93.         *t = '*';  //Remove occurrence of letter
  94.         }
  95.  
  96.         return( error_flag );
  97. }
  98.  
  99. char *nw_test( char *nlset, char *word )
  100. {
  101.    char *ptr;
  102.  
  103.      ptr = ( strpbrk( word, nlset ) );
  104.  
  105.      return( ptr );
  106. }
  107. /*************************************************************/
  108. void getword( char *letter_set, char *nlset )
  109. {
  110.  
  111.     char    l_set [ MAXLEN ],
  112.         word [ MAXLEN ],
  113.         tempstr [ MAXLEN + 1 ],
  114.         targetfile [ MAXLEN ],
  115.         bar [ LINE_LEN + 1 ],
  116.         double_bar [ LINE_LEN + 1 ];
  117.  
  118.     FILE *fptr,
  119.         *tfile;
  120.     int fnamelen;
  121.     long wcount = 0L;
  122.  
  123.        memset( bar, '-', LINE_LEN );
  124.        *( bar + LINE_LEN ) = NULL;
  125.        memset( double_bar, '=', LINE_LEN );
  126.        *( double_bar + LINE_LEN ) = NULL;
  127.  
  128.        /*************opening credits*************/
  129.        clrscr();
  130.        printf( double_bar );
  131.        strcpy( tempstr, ad );
  132.        center ( tempstr );
  133.        printf( tempstr );
  134.        printf( CR );
  135.        printf( double_bar );
  136.        printf( CR );
  137.        /****************************************/
  138.  
  139.  
  140.        strcpy ( l_set, letter_set );
  141. //       strcat ( letter_set, CR );
  142.  
  143.        /*   Create name of file to store derived words in   */
  144.        /*********************************************************/
  145.        fnamelen = strlen( l_set );
  146.  
  147.        if( fnamelen > 0 )
  148.          {
  149.          if( fnamelen  > FILENAME_MAXLEN )
  150.             fnamelen = FILENAME_MAXLEN;
  151.          strncpy( targetfile, l_set, fnamelen );
  152.          *( targetfile + fnamelen ) = NULL;
  153.             //NULL-terminate string, so strcat works, ha, ha.
  154.          }
  155.        else
  156.          strcpy( targetfile, "not" );
  157.  
  158.        strcat( targetfile, FILE_SUFFIX );
  159.        /*********************************************************/
  160.  
  161.        if( !( fptr = fopen( Wordfile, "rt" ) ) )
  162.          {
  163.          printf( "\7\7\7Cannot open Wordfile!" );
  164.          exit( FILE_OPENING_ERROR );
  165.          }
  166.  
  167.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  168.          {
  169.          printf( "\7\7\7Cannot open file to save words in!" );
  170.          exit ( FILE_OPENING_ERROR + 1 );
  171.          }
  172.  
  173.        /**************'Wait' Message************/
  174.        printf( CR CR );
  175.        printf( "WORKING...\n\n" );
  176.        printf( "This will take from 10 seconds or less [fast 486 machine]\n" );
  177.        printf( "to 5 minutes or more [slow 8088 with old hard drive].\n\n" );
  178.        printf( "Now searching 94,000 word file \nand writing file of valid words containing -%s-", 
  179.           letter_set );
  180.  
  181.         if( *nlset )
  182.           printf( " but NOT -%s-", nlset );
  183.  
  184.         printf( ".\n\n" );
  185.        /*****************************************/
  186.  
  187.  
  188.  
  189.  
  190.  
  191.        sprintf( tempstr, "Words containing: %s", strupr( l_set ) );
  192.        if( *nlset )
  193.             {
  194.             strcat( tempstr, ", but NOT -" );
  195.             strcat( tempstr, nlset );
  196.             strcat( tempstr, "-" );
  197.             }
  198.        strcat( tempstr, CR );
  199.  
  200.        center( tempstr );
  201.        fprintf( tfile, double_bar );
  202.        fprintf( tfile, tempstr );
  203.        fprintf( tfile, double_bar );
  204.        fprintf( tfile, CR );
  205.  
  206.  
  207.          /*********************Main Loop*************/     
  208.           while( fgets( word, MAXLEN, fptr ) != NULL )
  209.  
  210.             if( wordtest( letter_set, word ) )
  211.                if( !nw_test( nlset, word ) )
  212.                  {
  213.                  fprintf( tfile, "%s", word );
  214.                  wcount++;
  215.                  }
  216.           /*******************************************/
  217.  
  218.           fprintf( tfile, bar );
  219.           sprintf( tempstr, "%ld words contain %s.",
  220.                  wcount, l_set );
  221.           if( *nlset )
  222.             {
  223.             strcat( tempstr, ".. but NOT --- " );
  224.             strcat( tempstr, nlset );
  225.             strcat( tempstr, "." );
  226.             }
  227.  
  228.           center( tempstr );              
  229.           fprintf( tfile, tempstr );
  230.           fprintf( tfile, "\n\n" );
  231.  
  232.           center( ad );
  233.           fprintf( tfile, ad );
  234.  
  235.           fcloseall();
  236.  
  237.           sprintf( tempstr,
  238.                  "The file %s has %ld words containing %s.",
  239.                  targetfile, wcount, l_set );
  240.           if( *nlset )
  241.             {
  242.             strcat( tempstr, ".. but NOT --> " );
  243.             strcat( tempstr, nlset );
  244.             strcat( tempstr, "." );
  245.             }
  246.  
  247.           center( tempstr );
  248.           printf( CR CR );
  249.           printf( tempstr );
  250.           printf( "\7" );  //Bell
  251.  
  252. }
  253.  
  254.  
  255.  
  256. void center( char *str )
  257. {
  258.    int padding;
  259.    char st [ LINE_LEN + INCREMENT ];
  260.  
  261.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  262.      memset( st, SPACE, padding );
  263.      *( st + padding ) = NULL;  //Terminate string
  264.      strcat( st, str );
  265.      strcpy( str, st );
  266.  
  267.      return;
  268. }
  269.  
  270. void parse( char *arg, char *letterset, char *nls )
  271. {
  272.    char *p;
  273.  
  274.      if( *arg == '~' || *arg == '!' )
  275.         {
  276.         *letterset = NULL;
  277.         strcpy( nls, ++arg );
  278.         return;
  279.         }
  280.  
  281.      p = strtok( arg, NOT );
  282.      strcpy( letterset, p );
  283.  
  284.      p = strtok( NULL, NULL );
  285.      if( *p )
  286.         strcpy( nls, p );
  287.  
  288.      return;
  289. }
  290.