home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / ABCPLOT.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1991-04-02  |  9KB  |  198 lines

  1. #include <stdio.h>                 
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <process.h>
  6. #define num_error -9
  7. #define option -4
  8. #define ok 2
  9. #define MAXLENGTH 80                 /* Maximum length of filenames          */
  10. #define MAXCURVE 10                  /* Maximum number of curves .           */
  11. #define tempfil1 "plot_tmp.zz1"      /* Working file if data in std input    */
  12. #define tempfil2 "plot_tmp.zz2"      /* Working file, used in subrtn. COUNT  */
  13. /*---------------------------------------------------------------------------*/
  14. /* C front end for the ABCPLOT program                                                            */ 
  15. /* DATE   : 11/3/88                                                          */
  16. /* AUTHOR : RYAN RUMANANG                                                    */
  17. /*                                                                           */
  18. /* MAIN : This is the main driver for the "abcplot" program . The purpose of  */
  19. /*    this driver is to parse the command issued by the user, then call the  */
  20. /*    fortran modules of abcplot.                                             */
  21. /* Modification History:                                                     */
  22. /* April 2 1991 modified by Robert L. Dougherty                              */
  23. /*     -n option added to allow output of data as numbers.                   */
  24. /*     Arguments are no longer case-sensitive.                               */
  25. /* December 5 1989 modified by Robert L. Dougherty                           */
  26. /*     adapted to MS-DOS (microsoft compilers).                              */
  27. /*  Note: Lines #151-153 are system dependent for MS-DOS.                    */
  28. /*    The "del" command should be modified if running on other computers.    */
  29. /* April 29 1988 modified by Rudy Ting Liang                                  */
  30. /*  Old version: User has to provide the data file.                          */
  31. /*                   Command format: abcplot <datafile> [options]             */
  32. /*  Modified version: The program can accept data from a process.            */
  33. /*                   Format: <process name> | abcplot [options]               */
  34. /*                                                                           */
  35. /* November 3 1988 modified by Robert L. Dougherty                           */
  36. /*     -s switch added.  If present, it initializes the iswtch parameter     */
  37. /*        to 1, which signals the subroutines to switch the x and y axes.    */
  38.  
  39. /*---------------------------------------------------------------------------*/
  40.  
  41. main(argc,argv)
  42.  
  43. int argc ;
  44. char *argv[] ;
  45. {
  46.   FILE *fdata;                   /* file pointer to data file           */
  47.   extern void fortran abcplt (char near *, long near *, long near *, long near *, long near *, long near *, long near *, long near *, char near *) ;
  48.   int status, index, check_arg();
  49.   long i,j, ncurve, ix, icat, iy[MAXCURVE] ,itype, iswtch, itext;
  50.   char fname[MAXLENGTH], outfile[MAXLENGTH], line[MAXLENGTH] ; 
  51.   char *command;
  52.   short flag=0;                  /* indicate if temporary file is used   */
  53.  
  54.   ncurve = 1 ;                   /* Default values .                     */
  55.   ix = 1 ;             
  56.   itype = 1 ;
  57.   for(i=0;i<MAXCURVE;i++) iy[i] = 0 ;
  58.   iy[0] = 2;
  59.   icat=0;
  60.   iswtch=0;
  61.   itext=0;
  62.  
  63.   /* The following block checks for user options .                           */
  64.  
  65.   if (argc == 1 || argv[1][0] == '-') {   /* data from standard input        */
  66.       fdata = fopen(tempfil1,"w");        /* open file for data              */
  67.       while (gets(line) != NULL) {        /* do until eof                    */
  68.         fputs(line,fdata);                /* copy data from standard input   */
  69.         fputc('\n',fdata);                /*   to a temporary file           */
  70.       }
  71.       fclose(fdata);
  72.       strcpy(fname,tempfil1);             /* Get the input file name.        */
  73.       for(j=strlen(tempfil1);j<MAXLENGTH;j++) fname[j] = ' ' ; 
  74.       flag = 1;
  75.       index = 1;
  76.     }
  77.   else {                                      /* user provides data file             */
  78.     strcpy(fname,argv[1]);                    /* Get the input file name.            */
  79.     for(i=0;fname[i] != '\0'; i++) ;          /* Strip-off null character for compa- */
  80.     for(j=i;j<MAXLENGTH;j++) fname[j] = ' ' ; /* tability with the FORTRAN routines  */
  81.     index = 2;
  82.   }
  83.  
  84.   while(index < argc) {
  85.     if(!strcmpi(argv[index],"-x"))  {           /* '-x' option ?             */
  86.        if(index+1 >= argc ) break ;
  87.        status = check_arg(argv[++index]) ;
  88.        if(status == num_error) 
  89.          error("in x-option ...\n") ;
  90.        else if(status == ok)
  91.          ix = atoi(argv[index++]) ;
  92.     } 
  93.     else if(!strcmpi(argv[index],"-o")) {       /* '-o' option ?             */
  94.        itype = 0 ;
  95.        if(index+1 >= argc) 
  96.           error("Need output file name\n");
  97.        strcpy(outfile,argv[++index]) ;
  98.        for(i=0;outfile[i] != '\0';i++) ;
  99.        for(j=i;j < MAXLENGTH;j++)
  100.          outfile[j] = ' ' ;
  101.        index++ ;
  102.     }                   
  103.     else if(!strcmpi(argv[index],"-c"))  {      /* '-c' option ?             */
  104.        if(index+1 >= argc ) break ;
  105.        status = check_arg(argv[++index]) ;
  106.        if(status == num_error) 
  107.          error("in c-option ...\n") ;
  108.        else if(status == ok)
  109.          icat = atoi(argv[index++]) ;
  110.          if(icat == ix)
  111.            error("in c-option ...\n") ;
  112.     } 
  113.  
  114.     else if(!strcmpi(argv[index],"-y")) {       /* '-y' option ?             */
  115.       status = ncurve = j = 0 ;
  116.       index++ ;
  117.       while((status != num_error) && (status != option) && (index < argc)) {
  118.          status = check_arg(argv[index]) ;
  119.          switch(status) {
  120.            case num_error :  error("in y-options");
  121.         
  122.            case option    :  break ; 
  123.  
  124.            case ok        :  iy[j++] = atoi(argv[index++]) ;
  125.                              ncurve++ ; 
  126.                              if(iy[j-1] <= 0) 
  127.                                 error("Y values must be > 0") ;
  128.                              if(ncurve >MAXCURVE) 
  129.                                 error("number of curves must be < 10");
  130.                              break ;
  131.  
  132.            default        :  break ;
  133.          }
  134.        }
  135.        if(ncurve <= 0) ncurve = 1 ;
  136.     }
  137.     else if(!strcmpi(argv[index],"-s"))  {      /* '-s' option ?             */
  138.        iswtch = 1 ;
  139.        if(index+1 >= argc ) break ;
  140.        status = check_arg(argv[++index]) ;
  141.        if(status == num_error) 
  142.          error("in s-option ...\n") ;
  143.        else if(status == ok)
  144.          iswtch = 1 ;
  145.     } 
  146.     else if(!strcmpi(argv[index],"-n"))  {      /* '-n' option ?             */
  147.        itext = 1 ;
  148.        if(index+1 >= argc ) break ;
  149.        status = check_arg(argv[++index]) ;
  150.        if(status == num_error) 
  151.          error("in t-option ...\n") ;
  152.        else if(status == ok)
  153.          itext = 1 ;
  154.     } 
  155.     else  error("ILLEGAL OPTION") ;    
  156.   }
  157.  
  158.   abcplt(fname, &ncurve, &ix, iy, &icat, &itype, &iswtch, &itext, outfile);
  159.   if (flag == 1) {                         /* created a temporary data file */
  160.     fname[strlen(tempfil1)] = '\0';        /* put a null character at end   */
  161.     /* the following three lines are used to remove a file.                 */
  162.     /* NOTE: they are system dependent; only runs on MS-DOS computers!      */
  163.     command = "del ";                      /* generate command to delete    */
  164.     strcat(command,fname);                 /*   the created temporary data  */
  165.     system(command);                       /*   file                        */
  166.   }
  167. }
  168.  
  169. /*--------------------------------------------------------------------------*/
  170. /* ERROR : This routine simply prints a string given by its input parameter */
  171. /*--------------------------------------------------------------------------*/
  172. error(str)
  173. char *str ;
  174. {   
  175.   printf("ERROR ! %s\n",str) ;
  176.   exit(1) ;
  177. }
  178.  
  179. /*--------------------------------------------------------------------------*/
  180. /* CHECK_ARG : This routine checks an argument. If the argument starts with */
  181. /*   a '-' then it should be treated as another option flag . Otherwise, the*/
  182. /*   argument should consist of digits. A flag is returned according to the */
  183. /*   argument .                                                             */
  184. /*--------------------------------------------------------------------------*/
  185.  
  186. int check_arg(inarg)
  187. char inarg[] ;
  188. {
  189.   int len ,i ;
  190.   len = strlen(inarg) ;     
  191.   if(inarg[0] == '-') return(option) ;
  192.   else {
  193.     for(i=0;i < len;i++) 
  194.       if(!isdigit(inarg[i])) return(num_error) ;  
  195.   }
  196.   return(ok) ;
  197. }
  198.