home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 577a.lha / TownMaze_v1.1 / src.lzh / getargs.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  6KB  |  215 lines

  1. /*
  2. ** getargs.c  Copyright 1991 Kent Paul Dolan,
  3. **            Mountain View, CA, USA 94039-0755
  4. **
  5. ** Written to satisfy an inquiry on USENet's rec.games.programmer newsgroup.
  6. ** May be freely used or modified in any non-commercial work.  Copyrighted
  7. ** only to prevent patenting by someone else.
  8. */
  9.  
  10.  
  11. #include <stdio.h>
  12. #include "townmaze.h"
  13. #include "townproto.h"
  14.  
  15.  
  16. #ifdef __STDC__
  17. void getargs(int argc,char *argv[])
  18. #else
  19. int getargs(argc,argv)
  20.   int argc;
  21.   char *argv[];
  22. #endif
  23. {
  24.   int scantemp;
  25.   int i;
  26.  
  27. /*
  28. ** The next two data items and the code at the bottom to use them are
  29. ** adapted, and mostly stolen wholesale and reformatted, from suggestions
  30. ** submitted by "GLENN E. HOST" <host@ccf4.nrl.navy.mil> along with a
  31. ** very prompt bug report.  Besides, his changes made me go back and look
  32. ** harder at this interface; I hope this error reporting is more useful.
  33. ** Thanks, Glenn!
  34. */
  35.  
  36.   int errctr;
  37.  
  38.   static char *quit_messages[] =
  39.   {
  40.     " (-h): Maze height must be 11 or greater",
  41.     " (-h): Maze height must be an odd number",
  42.     " (-w): Maze width must be 11 or greater",
  43.     " (-w): Maze width must be an odd number",
  44.     " (-g): Maze gates must be a non-negative number",
  45.     " (-g): Too many maze gates to fit",
  46.     " (-l): Gates left must be a non-negative number",
  47.     "s: Gates left (-l) must be no greater than gates at start (-g)",
  48.     " (-c): Maze courts must be a non-negative number",
  49.     " (-c): Too many maze courts to fit",
  50.     "s: At least one court (-c) or one gate (-g) must exist",
  51.     " (-u): Maze unused must be a non-negative number",
  52.     " (-u): Too many unused cells to fit",
  53.     " (-s): Straightness must be a non-negative number",
  54.     " (-s): Straightness must be less than 999"
  55.   };
  56.  
  57. #if PROGRESS
  58.   fprintf(stderr,"Get arguments ");
  59. #endif
  60.  
  61.   if (argc > 1)
  62.   {
  63.     if ((argc%2) != 1)
  64.     {
  65.       fprintf(stderr,"\n*** Parameters must come in flag/value pairs\n\n");
  66.       usage();
  67.       exit(1);
  68.     }
  69.     i = 1;
  70.     while (i < argc)
  71.     {
  72.       if ((*argv[i]) != '-')
  73.       {
  74.         fprintf(stderr,
  75.           "\n*** Expected flag, didn't see '-' for argument %d\n\n",i);
  76.    
  77.         usage();
  78.         exit(1);
  79.       }
  80.       switch (*(argv[i]+1))
  81.       {
  82.       case 'h':
  83.         if (   ((scantemp = sscanf(argv[i+1],"%d",&mazeheight)) == EOF)
  84.             || (scantemp == 0))
  85.         {
  86.           fprintf(stderr,"\n*** Unable to read value after '-h'\n\n");
  87.           usage();
  88.           exit(1);
  89.         }
  90.         break;
  91.  
  92.       case 'w':
  93.         if (   ((scantemp = sscanf(argv[i+1],"%d",&mazewidth)) == EOF)
  94.             || (scantemp == 0))
  95.         {
  96.           fprintf(stderr,"\n*** Unable to read value after '-w'\n\n");
  97.           usage();
  98.           exit(1);
  99.         }
  100.         break;
  101.  
  102.       case 'g':
  103.         if (   ((scantemp = sscanf(argv[i+1],"%d",&mazegates)) == EOF)
  104.             || (scantemp == 0))
  105.         {
  106.           fprintf(stderr,"\n*** Unable to read value after '-g'\n\n");
  107.           usage();
  108.           exit(1);
  109.         }
  110.         break;
  111.  
  112.       case 'l':
  113.         if (   ((scantemp = sscanf(argv[i+1],"%d",&leftgates)) == EOF)
  114.             || (scantemp == 0))
  115.         {
  116.           fprintf(stderr,"\n*** Unable to read value after '-l'\n\n");
  117.           usage();
  118.           exit(1);
  119.         }
  120.         break;
  121.  
  122.       case 'c':
  123.         if (   ((scantemp = sscanf(argv[i+1],"%d",&mazecourts)) == EOF)
  124.             || (scantemp == 0))
  125.         {
  126.           fprintf(stderr,"\n*** Unable to read value after '-c'\n\n");
  127.           usage();
  128.           exit(1);
  129.         }
  130.         break;
  131.  
  132.       case 'u':
  133.         if (   ((scantemp = sscanf(argv[i+1],"%d",&mazeunused)) == EOF)
  134.             || (scantemp == 0))
  135.         {
  136.           fprintf(stderr,"\n*** Unable to read value after '-u'\n\n");
  137.           usage();
  138.           exit(1);
  139.         }
  140.         break;
  141.  
  142.       case 's':
  143.         if (   ((scantemp = sscanf(argv[i+1],"%d",&mazestraightness)) == EOF)
  144.             || (scantemp == 0))
  145.         {
  146.           fprintf(stderr,"\n*** Unable to read value after '-s'\n\n");
  147.           usage();
  148.           exit(1);
  149.         }
  150.         break;
  151.  
  152.       case 'r':
  153.         if (   ((scantemp = sscanf(argv[i+1],"%ld",&randomseed)) == EOF)
  154.             || (scantemp == 0))
  155.         {
  156.           fprintf(stderr,"\n*** Unable to read value after '-r'\n\n");
  157.           usage();
  158.           exit(1);
  159.         }
  160.         SEEDRANDOM(randomseed); /* override clock seed in main() */
  161.         break;
  162.  
  163.       default:
  164.         usage();
  165.         exit(1);
  166.       }
  167.       i += 2;
  168.     }
  169.   }
  170. /*
  171. ** Thanks to "GLENN E. HOST" <host@ccf4.nrl.navy.mil> for the idea, and
  172. ** most of the new code here, to dump out the reason a command line
  173. ** failed when it does. The functionality was pretty obviously needed
  174. ** with this many parameters and ways to fail.  Don't blame Glenn for the
  175. ** way the code looks now, it's back in what he called my "interesting
  176. ** coding style".
  177. */
  178.   if (   (errctr=0, mazeheight < 11)
  179.       || (errctr++, (mazeheight%2) != 1)
  180.       || (errctr++, mazewidth < 11)
  181.       || (errctr++, (mazewidth%2) != 1)
  182.       || (errctr++, mazegates < 0)
  183.       || (errctr++, mazegates > (  2*((mazeheight - 5)/6)
  184.                                  + 2*((mazewidth - 5)/6)))
  185.       || (errctr++, leftgates < 0)
  186.       || (errctr++, leftgates > mazegates)
  187.       || (errctr++, mazecourts < 0)
  188.       || (errctr++, mazecourts > (((mazeheight - 5)/6)*((mazewidth - 5)/6)))
  189.       || (errctr++, (mazecourts + mazegates) < 1)
  190.       || (errctr++, mazeunused < 0)
  191.       || (errctr++, mazeunused > (((mazeheight - 5)/14)*((mazewidth - 5)/14)))
  192.       || (errctr++, mazestraightness < 0)
  193.       || (errctr++, mazestraightness > 998)
  194.      )
  195.   {
  196.     fprintf(stderr,"\n*** Bad argument%s. You said:\n\n",
  197.                    quit_messages[errctr]);
  198.     fprintf(stderr,"ht %d wd %d gates %d left %d courts %d",
  199.                     mazeheight,mazewidth,mazegates,leftgates,mazecourts);
  200.     fprintf(stderr," unused %d straight %d seed %ld\n\n",
  201.                    mazeunused, mazestraightness,randomseed);
  202.     usage();
  203.     exit(1);
  204.   }
  205.   listsize = ((mazeheight - 1)/2)*((mazewidth - 1)/2);
  206.  
  207. #if HEADER
  208.   fprintf(stdout,
  209.    "ht %d wd %d gates %d left %d courts %d unused %d straight %d seed %ld\n",
  210.    mazeheight,mazewidth,mazegates,leftgates,mazecourts,mazeunused,
  211.    mazestraightness,randomseed);
  212. #endif
  213.   return;
  214. }
  215.