home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 108.lha / hdlns.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  9KB  |  268 lines

  1. /*    National Enquirer To Buy Hamburger Chain              */
  2. /*    ..."We were really famished.", says top Exec.         */
  3.  
  4. /*******************************************************
  5.   This program prints random National Enquirer type
  6.   headlines. It was inspired by a database that appeared
  7.   on the Net somewhere, but without the driver program.
  8.   This is my first program in C, so have mercy on some
  9.   of the code and casting overkill.
  10.     The database should be in the current directory under
  11.         the name <filename> (defined below). This database may be edited
  12.         quite freely. Capitalized words result in subsearches
  13.         of the dbase until the complete headline is decoded.
  14.         The first string searched is "CODE", which calls up the
  15.         main template, usually "CONST\\", which forms the main
  16.         headline template. 
  17.   Have fun with this. It is a little slow on the searches. 
  18.   A way out of this would be to use a table file which determines
  19.   how many strings are in each category--to set the random
  20.   number range limits for fetches, but this would require 
  21.   running the table maker to ensure that the data was uptodate in
  22.   case the string database had been edited.
  23.  
  24.       Bob Dickow, Lionel Hampton School of Music, U of Idaho,
  25.           Moscow, ID 83843
  26.  
  27.           (...egg-id!ui3!dickow)
  28.  
  29. ***************************************************************/
  30. #include <stdio.h>
  31. #include "errno.h"
  32. #include "ctype.h"
  33.  
  34. #define BOOLEAN     char   /* for True/False checking */
  35. #define MAXBUFF     81     /* a buffer size for parsing various strings */
  36. #define MAXHDLN     300    /* sets size of headline assemble buffer  */
  37. #define NL          0x0a   /* ascii 10 for newline character */
  38. #define IOERR       -1     /* flags file io error */
  39. #define NOTFOUND    -2     /* flags something not located */
  40. #define EQUAL       0      /* synonym. used as ok flag or equivalency */
  41. #define BUFFLIMIT 200 /* Max size of string work buffers in parse() */
  42. #define KEYLIMIT  100 /* Max size of Key_buff in parse() rtn     */
  43.  
  44. char *filename = "headlines.txt"; /* database file */
  45. char *seekstart_str = "CODE"; /* first str to seek in file */
  46. char hdln_buff[MAXHDLN]; /* headline assembled here  */
  47.  
  48. /* prcnt_chck checks to see if first chr in passed string (pointer) is
  49.    a '%' and returns TRUE (boolean) if so, FALSE otherwise           */
  50.  
  51. BOOLEAN prcnt_chck(buffptr)
  52. char *buffptr;
  53. {
  54.    if (*buffptr == (char) '\%') 
  55.      return (BOOLEAN) 1;
  56.    else
  57.      return (BOOLEAN) 0;
  58. }         /* end prcnt_flg()    */
  59.  
  60.  
  61. /* randomize is a routine to randomize the rnd num generator seed.
  62.    This method is probably system dependent. */
  63.  
  64. void randomize()
  65. {
  66.   unsigned int rtime;
  67.  
  68.   rtime = (unsigned int) time(NULL);
  69.   (void) srand(rtime);
  70. }           /* end randomize()  */
  71.  
  72. /* replace NL char with NULL char given pointer to string */
  73.  
  74. void nullput(ptr)
  75. char *ptr;
  76. {
  77.   char *buff = ptr;
  78.  
  79.   while ((*buff != (char) NL) && (buff++ - ptr)<MAXBUFF-1);
  80.   *buff = (char) NULL;
  81. }                 /* end nullput()  */
  82.  
  83.  
  84. /* find_category locates a category identifier & returns 0 if ok, -2 if not 
  85.    found, or -1 if error. The position will be located at the byte just
  86.    past the string. The file handle must be previously opened.  */
  87.  
  88. int find_category(handle,string) 
  89. FILE *handle;     
  90. char *string;
  91. {
  92.   BOOLEAN prcnt_flg, last_flg;
  93.   char buffer[MAXBUFF];  /* for temporary storage */
  94.  
  95.   if (fseek(handle,0L,0) != 0)   /* set file pos to beginning of file */
  96.         return IOERR;
  97.   last_flg = (BOOLEAN) 0;
  98.   while (fgets(buffer,MAXBUFF-1,handle) != NULL) {
  99.  
  100.     (void) nullput(buffer);
  101.         prcnt_flg = prcnt_chck(buffer);
  102.     if ((strcmp(buffer,string)==EQUAL) && (last_flg==1))
  103.           return EQUAL;
  104.     last_flg = prcnt_flg;
  105.   }
  106.   if (ferror(handle)) {
  107.       printf ("I/O ERROR #%d\n",errno);
  108.           exit();
  109.   }
  110.   return (int) NOTFOUND;
  111. }            /* end find_category()  */
  112.  
  113. /* returns a pointer to a buffer which is set to random string */
  114. /* this routine changes buff which is a pointer passed to it   */
  115.  
  116. char *rand_str(handle,buff)
  117. FILE *handle;
  118. char *buff;
  119. {
  120.   long ftell();
  121.  
  122.   char buffer[MAXBUFF]; /* working buffer local to function */
  123.   int count = 0;        /* number of lines input till '%' chr matched */
  124.   long startloc;        /* saves current location in file at entry */
  125.   int rand_index;       /* set to random # bet 0 and # of lines in categ. */
  126.  
  127.   startloc = ftell(handle);
  128.  
  129.   do {
  130.     (void) fgets(buffer,MAXBUFF-1,handle);
  131.     (void) nullput(buffer);
  132.     count++;
  133.   }
  134.   while (prcnt_chck(buffer)==0);  /* end of do loop */
  135.  
  136.   if (--count == 0) 
  137.     return (char *) NULL;
  138.  
  139.   (void) randomize();
  140.   rand_index =  (rand() % count)+1;
  141.   if (fseek(handle,startloc,0) != 0)
  142.     return (char *) NULL;
  143.   for (count=0; count<rand_index; count++)
  144.     (void) fgets(buffer,MAXBUFF-1,handle);
  145.   (void) nullput(buffer);
  146.   (void) strcpy(buff,buffer,MAXBUFF-1);
  147.   return (char *)buff;
  148. }         /* end rand_str()  */
  149.  
  150.  
  151. /*  parse() actually takes the CONST line and parses it, extracting capital-
  152.     ized words and sub-searching the file. The headline is assembled in
  153.         hdln_buff. str is the CONST line, and the routine returns a pointer to
  154.         hdln_buff.  */
  155.  
  156. char *parse(handle,str,hdln_str)
  157. FILE *handle;
  158. char * str, * hdln_str;
  159.  
  160. {
  161.   int ind=0, key_ind=0, hd_ind=0;
  162.   char buff1[BUFFLIMIT], buff2[BUFFLIMIT], key_buff[KEYLIMIT],
  163.        found_str[KEYLIMIT];
  164.   char *temp_str,  *str2 = buff2;
  165.   unsigned char chr;
  166.   int returncode; 
  167.   char * strcat(); /* keep the compiler happy about types */
  168.  
  169.   (void) strcpy(buff1,str);
  170.   str = buff1;
  171.  
  172.   *key_buff = (char) NULL;
  173.  
  174.   while ( *(str + ind) != (char) NULL ) {
  175.     if ( isupper( *(str+ind) ) )  {
  176.           *(key_buff) = (char) NULL;
  177.           key_ind = 0;
  178.           while ( isupper( *(str+ind)) || isdigit( *(str+ind) ) ) {
  179.             *(hdln_str + hd_ind) = *(key_buff+key_ind) = *(str+ind);
  180.                 ind++; key_ind++; hd_ind++;
  181.             if (key_ind == KEYLIMIT) {
  182.                   printf ("\nOverflow error\n");
  183.                   exit();
  184.                 }
  185.           }
  186.           *(key_buff + key_ind) = (char) NULL;
  187.         }
  188.         if ( strlen(key_buff) > 1) {
  189.           /* search */
  190.           returncode = find_category(handle,key_buff);
  191.  
  192.       if (returncode == EQUAL) {
  193.                 (void) rand_str(handle,found_str);
  194.                 hd_ind -= strlen(key_buff);
  195.           } 
  196.           else if (returncode == NOTFOUND)
  197.         *found_str = (char) NULL;
  198.       if ((returncode == EQUAL) || (returncode == NOTFOUND)) {
  199.                 *(key_buff) = *(hdln_str + hd_ind) = (char) NULL;
  200.         (void) strcpy( str2, found_str ); 
  201.       }
  202.       if ( (strlen(str2) + strlen(found_str)) <= BUFFLIMIT)
  203.        temp_str = strcat( str2, str+ind );
  204.            else {
  205.                   printf ("\nOverflow error\n");
  206.                   exit();
  207.        }
  208.         str2 = str;
  209.                 str = temp_str;
  210.         ind = key_ind = 0;   /* reset indices into buffers */
  211.          }   
  212.     else {
  213.           chr = *(str+ind++);
  214.           if (chr == (char) '|') {
  215.             if (*(str+ind) == (char) 's') {
  216.           if (*(hdln_buff+hd_ind-1) == (char) 'y') {
  217.                     *(hdln_buff+hd_ind-1) = (char) 'i';
  218.             *(hdln_buff+hd_ind++) = (char) 'e';
  219.                   }
  220.                   else if (*(hdln_buff+hd_ind-1) == (char) 'o') 
  221.                    { *(hdln_buff+hd_ind++) = (char) 'e'; }
  222.                   else if ((*(hdln_buff+hd_ind-1) == (char) 'h') 
  223.                        && ((*(hdln_buff+hd_ind-2) == (char) 's') 
  224.                            || (*(hdln_buff+hd_ind-2) == (char) 'c'))) {
  225.                     if (*(hdln_buff+hd_ind-3) == (char) 'i') 
  226.                           hd_ind = hd_ind-3;
  227.                     else
  228.                      *(hdln_buff+hd_ind++) = (char) 'e';
  229.                   }
  230.                 }
  231.                  else if (*(str+ind) == (char) 't') {
  232.                   if (*(hdln_buff+hd_ind-2) == (char) 'v')
  233.                     *(hdln_buff+hd_ind-- -2) = (char) 'f';
  234.             }
  235.                          chr = (char) *(str+ind++); 
  236.           }
  237.           if (chr == (char) '\\' )
  238.             chr = (char) NL;
  239.       if (hd_ind < MAXHDLN-1)
  240.             *(hdln_str+hd_ind++) = chr;
  241.           else {
  242.             printf ("\nOverflow error\n");
  243.             exit();
  244.         }
  245.         }
  246.   }
  247. *(hdln_str+hd_ind) = (char) NULL;
  248. return ((char *)hdln_str);
  249. } /* end parse() */
  250.  
  251.  
  252. main()
  253. {
  254.   FILE *handle;
  255.  
  256.   handle = fopen(filename,"r");
  257.   if (handle != (FILE *) NULL) {
  258.         if ( parse(handle,seekstart_str,hdln_buff) != (char *) NULL)
  259.           printf ("%s\n",hdln_buff);
  260.     else if (ferror(handle))
  261.       printf ("I/O ERROR #%d\n",errno);
  262.     if (handle != (FILE *) NULL)
  263.       fclose(handle);
  264.   }
  265.   else
  266.     printf ("%s not found\n",filename);
  267. }        /* end main() */
  268.