home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 127 / af127sub.adf / WiseCrack.lzx / WiseCrack / WiseCrack.c < prev    next >
C/C++ Source or Header  |  2002-05-30  |  8KB  |  306 lines

  1. /***********************************************************************
  2.     WiseCrack -    A fortune cookie program using Nico Françios
  3.                    ReqTools library for simplicity.
  4.                     
  5.                    Version 1.0 by Lars Magnus Nordeide.
  6.                    
  7.                    This program is written using SAS/C V5.10a.
  8.                    (See SASCOPTS for the compiler options.)
  9.  ***********************************************************************/
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <math.h>
  15. #include <libraries/dos.h>
  16. #include <workbench/startup.h>
  17. #include <workbench/workbench.h>
  18. #include <libraries/reqtools.h>
  19. #include <proto/reqtools.h>
  20. #include <proto/icon.h>
  21.  
  22. /* 
  23.  * Global variables.
  24.  */
  25. struct ReqToolsBase *ReqToolsBase = NULL;
  26. struct Library *IconBase = NULL;
  27. BOOL window = FALSE, fromwb;
  28. char *filepath, *wisetext;
  29.  
  30. /* 
  31.  * Prototypes.
  32.  */
  33. void PrintHelp(char *), CleanExit(char *, int), ReadText(char *, char *);
  34.  
  35. void main(int argc, char **argv)
  36. {
  37.     int next = 1;
  38.     char option, *odata, *tooltype, **toolarray;
  39.     char *opts = "f";
  40.     FILE *fpout;
  41.     struct TagItem reqtags[] = { RTEZ_ReqTitle, (ULONG)"WiseCrack V1.0",
  42.                                  TAG_END };
  43.     struct WBStartup *wbenchmsg;
  44.     struct WBArg *wbarg;
  45.     struct DiskObject *dobj;
  46.     
  47.     /*
  48.      * Allocate space for filepath and assign default datafile name.
  49.      */
  50.     filepath = (char *)malloc(FMSIZE);
  51.     if (!filepath)
  52.         CleanExit("Cannot allocate space.", RETURN_FAIL);
  53.     strcpy(filepath, "S:WiseCrack.dat");
  54.  
  55.     /* 
  56.      * Allocate space for the text. 2400 bytes should be more than
  57.      * enough. This corresponds to a window with size 80x30 characters, 
  58.      * full of text.
  59.      */
  60.     wisetext = (char *)malloc(2400);
  61.     if (!wisetext)
  62.         CleanExit("Cannot allocate space.", RETURN_FAIL);
  63.     
  64.     /* 
  65.      * Are we started from the workbench?
  66.      */
  67.     fromwb = window = (argc == 0);
  68.     
  69.     if (!fromwb) {     /* Program was started from CLI, parse arguments */
  70.         while ((odata = argopt(argc, argv, opts, &next, &option)) 
  71.                 != NULL) {
  72.             switch(option) {
  73.                 case 'h':
  74.                 case '?':
  75.                     PrintHelp(argv[0]);
  76.                     break;
  77.                 case 'w':
  78.                     window = TRUE;
  79.                     break;
  80.                 default:
  81.                     CleanExit("Illegal option!", RETURN_ERROR);
  82.                     break;
  83.             }
  84.         }
  85.         
  86.         if (next < argc) {
  87.             if (argv[next][0] == '?')
  88.                 PrintHelp(argv[0]);
  89.             else
  90.                 strcpy(filepath, argv[next]);
  91.         }
  92.     }
  93.     else {  /* Started from the workbench */
  94.         wbenchmsg = (struct WBStartup *)argv;
  95.         
  96.         /*
  97.          * Get the arguments for this tool.
  98.          */
  99.         wbarg = wbenchmsg->sm_ArgList;
  100.         
  101.         /* 
  102.          * Open icon.library.
  103.          */
  104.         IconBase =(struct Library *) OpenLibrary("icon.library", 33);
  105.         if (!IconBase)
  106.             CleanExit("Cannot open icon.library.", RETURN_FAIL);
  107.         
  108.         /*
  109.          * Read the iconfile.
  110.          */
  111.         dobj = GetDiskObject(wbarg->wa_Name);
  112.         if (!dobj)
  113.             CleanExit("Cannot open icon.", RETURN_FAIL);
  114.         
  115.         /*
  116.          * Get the filename if given.
  117.          */
  118.         toolarray = (char **)dobj->do_ToolTypes;
  119.         tooltype = FindToolType(toolarray, "DATAFILE");
  120.         if(tooltype && tooltype[0] != '\0')
  121.             strcpy(filepath, tooltype);
  122.         
  123.         /*
  124.          * Free the DiskObject.
  125.          */
  126.         FreeDiskObject(dobj);
  127.         
  128.         /* 
  129.          * Close The icon library.
  130.          */
  131.         if (IconBase)
  132.         CloseLibrary((struct Library *)IconBase);
  133.     }
  134.     
  135.     /*
  136.      * Read text to print.
  137.      */
  138.     ReadText(filepath, wisetext);
  139.     
  140.     /*
  141.      * Print out text, either to the console or in a separate window.
  142.      */
  143.     if (window) {
  144.         /*
  145.          * Open ReqTool library.
  146.          */
  147.         ReqToolsBase = (struct ReqToolsBase *)
  148.                     OpenLibrary(REQTOOLSNAME, REQTOOLSVERSION);
  149.         if (!ReqToolsBase)
  150.             CleanExit("Cannot open reqtools.library", RETURN_FAIL);
  151.         
  152.         rtEZRequestA(wisetext, "OK", NULL, NULL, reqtags);
  153.     }
  154.     else {
  155.         /*
  156.          * Open stdout.
  157.          */
  158.         fpout = fopen("*", "w");
  159.         fprintf(fpout, "%s\n", wisetext);
  160.         close(fpout);
  161.     }
  162.         
  163.     /* 
  164.      * Ok, we are finished.
  165.      */
  166.     CleanExit(NULL, RETURN_OK);
  167. }
  168.  
  169. /***********************************************************************/
  170.  
  171. void ReadText(char *filepath, char *text)
  172. {
  173.     struct FILEINFO *info;
  174.     int cnt = 0;
  175.     char ch;
  176.     long size, pos;
  177.     unsigned int clock[2];
  178.     unsigned short seed[3];
  179.     FILE *infile;
  180.         
  181.     /*
  182.      * Initialize stuff.
  183.      */
  184.     info = (struct FILEINFO *)malloc(sizeof(struct FILEINFO));
  185.     if (!info)
  186.         CleanExit("Cannot allocate space.", RETURN_FAIL);
  187.     
  188.     /*
  189.      * Check if datafile exist in specified filepath.
  190.      */
  191.     if (dfind(info, filepath, 0)) {
  192.         sprintf(text, "Cannot find file %s", filepath);
  193.         return;
  194.     }
  195.     
  196.     /*
  197.      * Find the size of the file in bytes.
  198.      */
  199.     size = info->fib_Size;
  200.     
  201.     /*
  202.      * Have no more use for info. Free it.
  203.      */
  204.     free(info);
  205.     
  206.     /*
  207.      * Compute a random position in the file.
  208.      */
  209.     timer(clock);
  210.     seed[0] = clock[0];
  211.     seed[1] = clock[1];
  212.     seed[2] = clock[0];
  213.     pos = nrand48(seed) % size;
  214.     
  215.     /*
  216.      * Open file.
  217.      */
  218.     infile = fopen(filepath, "r");
  219.     if (infile == NULL) {
  220.         sprintf(text, "Cannot open %s.", filepath);
  221.         return;
  222.     }
  223.     
  224.     /*
  225.      * Fast forward to computed position.
  226.      */
  227.     fseek(infile, pos, 0);
  228.     
  229.     /*
  230.      * Find next start of text to print out.
  231.      */
  232.     while(((ch = fgetc(infile)) != '\\') && (ch != EOF));
  233.  
  234.     /*
  235.      * Sometimes we are unlucky and will read past the end of the file. 
  236.      * Instead of trying again, it's easier just to use a standard text. 
  237.      * It's okay as it won't happen to often. 
  238.      */
  239.     if (ch == EOF) {
  240.         strcpy(text,"How many IBM PC's does it take to execute a job?\n");
  241.         strcat(text,"Four. Three to hold it down, and one to rip it's");
  242.         strcat(text," head of.\n");
  243.     }
  244.     else {
  245.         /*
  246.          * Read text until the next '\' or EOF.
  247.          */
  248.         while(((ch = fgetc(infile)) != '\\') && (ch != EOF)) {
  249.             text[cnt] = ch;
  250.             cnt++;
  251.         }
  252.         text[cnt-1] = '\0';
  253.     }
  254.     
  255.     /*
  256.      * Close the file because we are done.
  257.      */
  258.     fclose(infile);
  259. }    
  260.  
  261. /***********************************************************************/
  262.  
  263. void PrintHelp(char *name)
  264. {
  265.     FILE *fpout;
  266.     
  267.     fpout = fopen("*", "w");
  268.     fprintf(fpout, "WiseCrack V1.0 by Lars Magnus Nordeide\n");
  269.     fprintf(fpout, "Usage: %s [-w] [filename]\n", name);
  270.     fclose(fpout);
  271.     
  272.     CleanExit(NULL, RETURN_OK);
  273. }
  274.  
  275. /***********************************************************************/
  276.  
  277. void CleanExit(char *message, int ret_val)
  278. {
  279.     FILE *fpout;
  280.  
  281.     /*
  282.      * Close the opened libraries.
  283.      */
  284.     if (ReqToolsBase)
  285.         CloseLibrary((struct Library *)ReqToolsBase);
  286.  
  287.     /*
  288.      * Free the space allocated for the filename.
  289.      */
  290.     if (wisetext)
  291.         free(wisetext);
  292.     if (filepath)
  293.         free(filepath);
  294.         
  295.     /*
  296.      * If there is an error then print the error message.
  297.      */
  298.     if (message && !fromwb) {
  299.         fpout = fopen("*", "w");
  300.         fprintf(fpout, "WiseCrack: %s\n", message);
  301.         fclose(fpout);
  302.     }
  303.         
  304.     exit(ret_val);
  305. }
  306.