home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / WORDY442.ZIP / SUFFIX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-20  |  6.5 KB  |  242 lines

  1. /**************************************************************************/
  2. /*                                Suffix  Utility                         */
  3. /*                                                                        */
  4. /*                                                                        */
  5. /*                                     M\Cooper                           */
  6. /*                                    PO Box 237                          */
  7. /*                            St. David, AZ 85630-0237                    */
  8. /*                        -------------------------------                 */
  9. /*                        Email:  thegrendel@theriver.com                 */
  10. /*                                                                        */
  11. /*                   $2.00 to register the entire WORDY package           */
  12. /*                                                                        */
  13. /**************************************************************************/
  14.  
  15.  
  16. #include <conio.h>
  17. #include "srch.h"
  18.  
  19.  
  20. #define FILE_OPENING_ERROR 3
  21. #define FILENAME_MAXLEN 8
  22. #define CR "\n"
  23. #define FILE_SUFFIX ".suf"
  24. #define MAXLEN 30
  25. #define LINE_LEN 80
  26. #define NOARGS 1
  27. #define INCREMENT 1
  28. #define WD 10
  29. #define SPACE ' '
  30.  
  31. #define BUFFERSIZE 8192
  32.  
  33. char ad[] =
  34. "SUFFIX utility by M\\Cooper, PO Box 237, St. David, AZ 85630-0237";
  35.  
  36. typedef enum { FALSE, TRUE } Boolean;
  37.  
  38. void getword( char *lset, char *filenam );
  39. void center( char *strng );
  40. Boolean wordtest( char *lset, char *wd, size_t len );
  41.  
  42.  
  43. void main( int argc, char **argv )
  44. {
  45.  
  46.    char letterset[ MAXLEN ],
  47.         filename [ MAXLEN ];
  48.  
  49.  
  50.  
  51.      if( argc == NOARGS )
  52.         {
  53.         clrscr();
  54.         puts("Enter a LETTERSET to test ... ");
  55.         gets( letterset );
  56.      strcpy( filename, "word.lst" );
  57.         }
  58.   else
  59.         if( argc == NOARGS + 1 )
  60.         {
  61.            strcpy( letterset, *( argv + 1 ) );
  62.         strcpy( filename, "word.lst" );
  63.         }
  64.      else
  65.          {
  66.          strcpy( letterset, *( argv + 1 ) );
  67.          strcpy( filename,  *( argv + 2 ) );
  68.          }
  69.  
  70.      getword( letterset, filename );
  71.  
  72. }
  73.  
  74.  
  75. /**********************************WORDTEST********************************/
  76. /*       Function tests if word is constructible from Letterset           */
  77. /*                 Args in: char *letterset, char *word                   */
  78. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  79. /**************************************************************************/
  80.  
  81. Boolean wordtest( char *letterset, char *word, size_t l_len )
  82. {
  83.     Boolean error_flag;
  84.  
  85.       if( !strncmp( letterset, word + strlen( word ) - 1 - l_len, l_len ) )
  86.          error_flag = TRUE;
  87.       else
  88.          error_flag = FALSE;
  89.         return( error_flag );
  90. }
  91.  
  92. /*************************************************************/
  93. void getword( char *letter_set, char *filename )
  94. {
  95.  
  96.     char    l_set [ MAXLEN ],
  97.         word [ MAXLEN ],
  98.         tempstr [ MAXLEN + 1 ],
  99.         targetfile [ MAXLEN ],
  100.         bar [ LINE_LEN + 1 ],
  101.         double_bar [ LINE_LEN + 1 ],
  102.   msg1 [ WD ],
  103.   msg2 [ WD ];
  104.    size_t l_len;
  105.  
  106.     FILE *fptr,
  107.         *tfile;
  108.     int fnamelen;
  109.     long wcount = 0L;
  110.  
  111.  
  112.       l_len = strlen( letter_set );
  113.        memset( bar, '-', LINE_LEN );
  114.        *( bar + LINE_LEN ) = NULL;
  115.        memset( double_bar, '=', LINE_LEN );
  116.        *( double_bar + LINE_LEN ) = NULL;
  117.  
  118.        /*************opening credits*************/
  119.        clrscr();
  120.        printf( double_bar );
  121.        strcpy( tempstr, ad );
  122.        center ( tempstr );
  123.        printf( tempstr );
  124.        printf( CR );
  125.        printf( double_bar );
  126.        printf( CR );
  127.        /****************************************/
  128.  
  129.  
  130.        strcpy ( l_set, letter_set );
  131. //       strcat ( letter_set, CR );
  132.  
  133.        /*   Create name of file to store derived words in   */
  134.        /*********************************************************/
  135.        fnamelen = strlen( l_set );
  136.        if( fnamelen  > FILENAME_MAXLEN )
  137.           fnamelen = FILENAME_MAXLEN;
  138.        strncpy( targetfile, l_set, fnamelen );
  139.        *( targetfile + fnamelen ) = NULL;
  140.        //NULL-terminate string, so strcat works, ha, ha.
  141.        strcat( targetfile, FILE_SUFFIX );
  142.        /*********************************************************/
  143.  
  144.        if( !( fptr = fopen( filename, "rt" ) ) )
  145.          {
  146.          printf( "\7\7\7Cannot open Wordfile!" );
  147.          exit( FILE_OPENING_ERROR );
  148.          }
  149.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  150.          exit( FILE_OPENING_ERROR + 1 );
  151.  
  152.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  153.          {
  154.          printf( "\7\7\7Cannot open file to save words in!" );
  155.          exit ( FILE_OPENING_ERROR + 2 );
  156.          }
  157.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  158.          exit( FILE_OPENING_ERROR + 3 );
  159.  
  160.        /**************'Wait' Message************/
  161.        printf( CR CR );
  162.        printf( "WORKING...\n\n" );
  163.        printf( "This will take a few seconds...\n" );
  164.        printf( "Please be patient.\n\n" );
  165.        printf(
  166. "Now searching 100,000+ word file \nand writing file of valid words suffixed -%s.\n\n",
  167.             letter_set );
  168.        /*****************************************/
  169.  
  170.  
  171.  
  172.  
  173.  
  174.        sprintf( tempstr, "Word(s) generated with suffix: %s\n", strupr( l_set ) );
  175.        center( tempstr );
  176.        fprintf( tfile, double_bar );
  177.       fprintf( tfile, CR );
  178.        fprintf( tfile, tempstr );
  179.        fprintf( tfile, double_bar );
  180.        fprintf( tfile, CR );
  181.  
  182.  
  183.          /*********************Main Loop*************/     
  184.           while( fgets( word, MAXLEN, fptr ) != NULL )
  185.  
  186.             if( wordtest( letter_set, word, l_len ) )
  187.                {
  188.                fprintf( tfile, "%s", word );
  189.                wcount++;
  190.                }
  191.           /*******************************************/
  192.  
  193.       if( wcount == INCREMENT )
  194.          {
  195.          strcpy( msg1, "word" );
  196.          strcpy( msg2, "has" );
  197.          }
  198.       else
  199.          {
  200.          strcpy( msg1, "words" );
  201.          strcpy( msg2, "have" );
  202.          }
  203.  
  204.  
  205.           fprintf( tfile, bar );
  206.       fprintf( tfile, CR );
  207.           sprintf( tempstr, "%ld %s suffixed %s %s been found.",
  208.                  wcount, msg1, l_set, msg2 );
  209.           center( tempstr );              
  210.           fprintf( tfile, tempstr );
  211.           fprintf( tfile, "\n\n" );
  212.  
  213.           center( ad );
  214.           fprintf( tfile, ad );
  215.  
  216.           fcloseall();
  217.  
  218.           sprintf( tempstr,
  219.                  "The file %s has %ld %s suffixed by %s\7.",
  220.                  targetfile, wcount, msg1, l_set );
  221.           center( tempstr );
  222.           printf( CR CR );
  223.           printf( tempstr );
  224.  
  225. }
  226.  
  227.  
  228.  
  229. void center( char *str )
  230. {
  231.    int padding;
  232.    char st [ LINE_LEN + INCREMENT ];
  233.  
  234.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  235.      memset( st, SPACE, padding );
  236.      *( st + padding ) = NULL;  //Terminate string
  237.      strcat( st, str );
  238.      strcpy( str, st );
  239.  
  240.      return;
  241. }
  242.