home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3650 / getitem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-17  |  2.9 KB  |  113 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include "shop.h"
  5.  
  6. static FILE *nounfile, *pluralfile, *adjfile;
  7. static int files_opened = FALSE;
  8. static long int number_of_nouns = -1L;
  9. static long int number_of_plurals = -1L;
  10. static long int number_of_adjs = -1L;
  11.  
  12. extern char *cmdname;
  13.  
  14. char *getitem(buff)
  15. char *buff;
  16. {
  17.         long int my_rand();
  18.  
  19.         long int which_noun1 = 0L;
  20.         long int which_noun2 = 0L;
  21.         long int which_adj1 = 0L;
  22.         long int which_adj2 = 0L;
  23.  
  24.         char adj1[MAX_WORD_LEN], adj2[MAX_WORD_LEN], 
  25.        noun1[MAX_WORD_LEN], noun2[MAX_WORD_LEN];
  26.     char *get_nth_string();
  27.     long int get_count();
  28.  
  29.     if (!files_opened) {
  30.         nounfile = fopen(NOUN_FILE,"r");
  31.             if (nounfile == NULL) {
  32.                     (void) fprintf(stderr,
  33.                            "%s: couldn't open noun file.\n",
  34.                            cmdname);
  35.                     exit(1);
  36.             }
  37.  
  38.             pluralfile = fopen(PLURAL_FILE,"r");
  39.             if (pluralfile == NULL) {
  40.             (void) fprintf(stderr,
  41.                            "%s: couldn't open plural file.\n",
  42.                            cmdname);
  43.                     exit(1);
  44.             }
  45.  
  46.             adjfile = fopen(ADJ_FILE,"r");
  47.             if (adjfile == NULL) {
  48.                     (void) fprintf(stderr,
  49.                            "%s: couldn't open adjective file.\n",
  50.                     cmdname);
  51.                     exit(1);
  52.             }
  53.         files_opened = TRUE;
  54.     }
  55.  
  56.     if (number_of_nouns < 0) {
  57.         number_of_adjs = get_count(adjfile);
  58.             number_of_nouns = get_count(nounfile);
  59.             number_of_plurals = get_count(pluralfile);
  60.             if (number_of_plurals != number_of_nouns) {
  61.                     (void) fprintf(stderr,
  62.     "%s: plural file has %d elements; noun file, %d\n",
  63.         cmdname,number_of_plurals, number_of_nouns);
  64.                     exit(1);
  65.             }
  66.     }
  67.  
  68.         noun1[0] = '\0';
  69.         noun2[0] = '\0';
  70.         adj1[0] = '\0';
  71.         adj2[0] = '\0';
  72.  
  73.     switch(my_rand() % 5) {
  74.         case 4: /*FALLTHROUGH*/
  75.         case 3:
  76.             which_noun1 = (long) (my_rand() % number_of_nouns);
  77.             get_nth_string(nounfile,
  78.                            which_noun1,noun1,MAX_WORD_LEN);
  79.             (void) strcat(noun1," ");
  80.                 /*FALLTHROUGH*/
  81.         case 2: /*FALLTHROUGH*/
  82.         case 1: /*FALLTHROUGH*/
  83.         case 0: 
  84.             while ((which_noun2 = 
  85.                     (long) (my_rand() % number_of_nouns)) 
  86.                    == which_noun1)
  87.                 /* do nothing */;
  88.             get_nth_string(pluralfile,
  89.                            which_noun2,noun2,MAX_WORD_LEN);
  90.     }
  91.  
  92.     switch(my_rand() % 4) {
  93.         case 3:
  94.             which_adj1 = (long) (my_rand() % number_of_adjs);
  95.             get_nth_string(adjfile,which_adj1,adj1,MAX_WORD_LEN);
  96.             (void) strcat(adj1," ");
  97.                 /*FALLTHROUGH*/
  98.         case 2: /*FALLTHROUGH*/
  99.         case 1: /*FALLTHROUGH*/
  100.         case 0: 
  101.             while ((which_adj2 = 
  102.                (long) (my_rand() % number_of_adjs)) == which_adj1)
  103.                 /* do nothing */;
  104.             get_nth_string(adjfile,which_adj2,adj2,MAX_WORD_LEN);
  105.             (void) strcat(adj2," ");
  106.     }
  107.  
  108.     (void) sprintf(buff,"%s%s%s%s",adj1,adj2,noun1,noun2);
  109.  
  110.     return buff;
  111. }
  112.  
  113.