home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff384.lzh / NorthC / Example2.LZH / fortune / fortune.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  6KB  |  212 lines

  1. /*  (c) 1990 S.Hawtin.
  2.   Permission is granted to copy this file provided
  3.    1) It is not used for commercial gain
  4.    2) This notice is included in all copies
  5.    3) Altered copies are marked as such
  6.  
  7.   No liability is accepted for the contents of the file.
  8.  
  9. */
  10.  
  11. /**********************************************************************/
  12.  
  13. /* 
  14.   This is a short example program that selects a phrase from a file and
  15. prints it.
  16. */
  17.  
  18. /* We are going to print stuff to files so we must have "stdio.h" */
  19. #include <stdio.h>
  20.  
  21. /**********************************************************************/
  22.  
  23. /* First a set of constants */
  24.  
  25. /* 
  26.   The data file that all the phrases come from.
  27. This file must be at least MIN_LENGTH charcters long, and have 
  28. more than 3 phrases in.
  29. */
  30. #define MIN_LENGTH 100
  31. #define SRC_FILE "fortune.txt"
  32.  
  33. /* 
  34.   Some special "characters", to indicate conditions other than 
  35. normal characters read in from the file. 
  36. */
  37. #define MY_EOF  -1
  38. #define MY_EOL  -2
  39.  
  40. /**********************************************************************/
  41.  
  42. /* Some special NorthC things */
  43.  
  44. #ifdef NORTHC
  45.  
  46. extern long _fromWB;    /* Set to 0 if called from the CLI */
  47.  
  48. /* 
  49.   The description of a console window to use if called from the 
  50. workbench
  51. */ 
  52. char *_WBConsole="con:20/40/600/70/NorthC: Fortune Program";
  53.  
  54. #endif
  55.  
  56. /**********************************************************************/
  57.  
  58. /* The functions */
  59.  
  60. static int
  61. tyi(fptr)
  62.     FILE *fptr;
  63.    {/* 
  64.       Input a single character from the file, if we find the end of 
  65.     a phrase return MY_EOL, if we find the end of a file return MY_EOF.
  66.     */
  67.  
  68.     /* Somewhere to keep the characters as they are read */
  69.     int next;
  70.  
  71.     /* Just get the next character from the file */
  72.     next = fgetc(fptr);
  73.  
  74.     /* 
  75.       Now we have three special cases,
  76.          1) the '\' charcter:  Read the next character without 
  77.             translating it.  This lets us embed newlines in a phrase.
  78.          2) the newline charcter:  Tell the caller we have got the
  79.             end of the phrase.
  80.          3) the end of file:  Tell the caller we have an end of file.
  81.     */
  82.     if(next=='\\')
  83.        {next = fgetc(fptr);
  84.         if(next=='n')
  85.             next = '\n';
  86.         }
  87.       else if(next=='\n')
  88.         next = MY_EOL;
  89.       else if (next==EOF || feof(fptr))
  90.         next = MY_EOF;
  91.     return(next);
  92.     }
  93.  
  94. static int
  95. find_next(fptr)
  96.     FILE *fptr;
  97.    {/* 
  98.       Keep reading until we find the end of the current phrase, when 
  99.     we have the end of phrase return true if we did not get the end 
  100.     of file as well.
  101.     */
  102.     int next;
  103.  
  104.     next = tyi(fptr);
  105.     while(next!=MY_EOL && next!=MY_EOF)
  106.         next = tyi(fptr);
  107.     return(next!=MY_EOF);
  108.     }
  109.  
  110. static int
  111. out_next(fptr)
  112.     FILE *fptr;
  113.    {/* 
  114.       Output the next phrase to the terminal, this involves reading the
  115.     phrase and outputting until we run out of characters.
  116.     Again if we found the end of the file return false.
  117.     */
  118.     int next;
  119.  
  120.     next = tyi(fptr);
  121.     while(next!=MY_EOL && next!=MY_EOF)
  122.        {fputc(next,stdout);
  123.  
  124.         /* Put four spaces at the start of each line */
  125.         if(next=='\n')
  126.             puts("    ");
  127.  
  128.         /* Get the next charcter from the file */
  129.         next = tyi(fptr);
  130.         }
  131.  
  132.     return(next!=MY_EOF);
  133.     }
  134.  
  135. void
  136. main(argc,argv)
  137.     int argc;        /* Both arguments are unused in this example */
  138.     char **argv;
  139.    {/* Pick a single saying from a file and send it to the user */
  140.     FILE *fptr;        /* The data file */
  141.     long length;    /* The total length of the data file */
  142.     long where;        /* Where we are going to look for our phrase */
  143.  
  144.     /* First open the data file */
  145.     fptr = fopen(SRC_FILE,"r");
  146.  
  147.     /* Check to make sure that we got it */
  148.     if(fptr==NULL)
  149.        {printf("Remember:\n\n    A good user always keeps\n");
  150.         printf("    \"%s\" with his fortune program\n",SRC_FILE);
  151.         exit(20);
  152.         }
  153.  
  154.     /* 
  155.       Seed the random number generator, use the current time as a 
  156.     good source of random numbers 
  157.     */
  158.     srand(time(NULL));
  159.  
  160.     /* Find out how long the file is, first move to the end */
  161.     fseek(fptr,0L,SEEK_END);
  162.     /* Then ask where we now are */
  163.     length = ftell(fptr);
  164.  
  165.     /* 
  166.       Check that the file is long enough, this is not really 
  167.     required but we might get problems with small files
  168.     */
  169.     if(length<MIN_LENGTH)
  170.        {printf("Remember:\n\n    A good user makes sure \"%s\"\n",SRC_FILE);
  171.         printf("    is longer than %d charcters.\n",MIN_LENGTH);
  172.         exit(20);
  173.         }
  174.  
  175.     /* Output a wrapper to put round the phrase */
  176.     printf("Remember:\n\n    ");
  177.  
  178.     /* Now go look for a phrase in the file */
  179.     do {
  180.         /* Select somewhere in the middle of the file */
  181.         where = rand() % length;
  182.  
  183.         /* Move there */
  184.         fseek(fptr,where,SEEK_SET);
  185.  
  186.         /* 
  187.           Now move to the end of the prase we were in the middle of,
  188.         then output the next phrase.  If either function returns
  189.         false then we hit the end of the data file and must go round
  190.         and try again another place in the file.
  191.         */
  192.         } while (!find_next(fptr) || !out_next(fptr));
  193.  
  194.     /* Round the phrase off with a couple of newlines */
  195.     puts("\n\n");
  196.  
  197. #ifdef NORTHC
  198.  
  199.     if(_fromWB)
  200.        {/* Wait for the user to hit a key before quitting the program */
  201.         printf("Hit <Return>:");
  202.         /* OK make sure that the last bit gets sent for buffered stdout */
  203.         fflush(stdout);
  204.         fgetc(stdin);
  205.         }
  206.  
  207. #endif
  208.  
  209.     /* Close the data file */
  210.     fclose(fptr);
  211.     }
  212.