home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / plotting / contour / contour.lha / Contour / parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-28  |  9.4 KB  |  322 lines

  1. /*** parser.c - parses the input data */
  2.  
  3. #include <stdio.h>
  4. #include <strings.h>
  5. #include <math.h>
  6. #include "common.h"
  7. #include "plot.h"
  8.  
  9. /* Parser */
  10. parse(op) 
  11. FILE *op;
  12. {
  13.    char *strcpy();
  14.    int  len,match,i;
  15.    char line[MAXCHAR];
  16.    char name[MAXCHAR],value[MAXCHAR];
  17.    char word[100][MAXCHAR];
  18.    char *keywords[MAXKEY];
  19.  
  20.    initkeywords(keywords);
  21.  
  22.    while (len=getline(op,line, MAXCHAR) > 0)
  23.       if (getwords(line,len,word) >= 2) {
  24.          strcpy(name, word[0]);
  25.          strcpy(value,word[1]);
  26.          match = NO;
  27.          i = 0;
  28.          while (i < MAXKEY && match==NO ) {
  29.             if (strcmp(keywords[i],name) == 0) {
  30.                match = YES;
  31.                assignvalue(i,value);
  32.             }
  33.             i++;
  34.          }
  35.          if (match==NO) fprintf(stderr,"%s: Unknown keyword\n",name);
  36.       }
  37. }
  38.  
  39. /* Initialize the keywords */
  40. initkeywords(keywords)
  41. char *keywords[];
  42. {
  43.    keywords[0] = "xlabel";
  44.    keywords[1] = "ylabel";
  45.    keywords[2] = "toplabel";
  46.    keywords[3] = "grid";
  47.    keywords[4] = "equalscale";
  48.    keywords[5] = "postscript";
  49.    keywords[6] = "printplot";
  50.    keywords[7] = "linetypes";
  51.    keywords[8] = "scale";
  52.    keywords[9] = "xticks";
  53.    keywords[10]= "yticks";
  54.    keywords[11]= "contlabel";
  55.    keywords[12]= "joinlevel";
  56.    keywords[13]= "printer";
  57.    keywords[14]= "landscape";
  58. }
  59.  
  60. /* Assign a value to a keyword */
  61. assignvalue(index,value) 
  62. int index;
  63. char *value;
  64. {
  65.    double atof();
  66.    char   *strcpy();
  67.    char   *sprintf();
  68.    extern char xlabel[MAXCHAR];
  69.    extern char ylabel[MAXCHAR];
  70.    extern char toplabel[MAXCHAR];
  71.    extern char printer[MAXCHAR];
  72.    extern int  grid,equalscale,postscript,printplot;
  73.    extern int  linetypes,contlabel,joincurve,landscape;
  74.    extern char *joinlevel;
  75.    extern int  xticks,yticks;
  76.    extern double scale;
  77.  
  78.    switch (index) {
  79.    case 0 : strcpy(xlabel , value); 
  80.             fprintf(stdout,"   Options : Xlabel          = %s\n",xlabel);
  81.             break;
  82.    case 1 : strcpy(ylabel , value);
  83.             fprintf(stdout,"   Options : Ylabel          = %s\n",ylabel);
  84.             break;
  85.    case 2 : strcpy(toplabel , value); 
  86.             fprintf(stdout,"   Options : Top label       = %s\n",toplabel);
  87.             break;
  88.    case 3 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
  89.                 (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
  90.                grid = ON;
  91.                if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
  92.                   grid = OFF;
  93.                fprintf(stdout,"   Options : grid            = %s\n",value);
  94.                }
  95.             else
  96.                fprintf(stdout,"   Options : Unknown keyword = %s\n",value);
  97.             break;
  98.    case 4 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
  99.                 (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
  100.                equalscale = ON;
  101.                if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
  102.                   equalscale = OFF;
  103.                fprintf(stdout,"   Options : equalscale      = %s\n",value);
  104.                }
  105.             else
  106.                fprintf(stdout,"   Options : Unknown keyword = %s\n",value);
  107.             break;
  108.    case 5 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
  109.                 (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
  110.                postscript = ON;
  111.                if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
  112.                   postscript = OFF;
  113.                fprintf(stdout,"   Options : postscript      = %s\n",value);
  114.                }
  115.             else
  116.                fprintf(stdout,"   Options : Unknown keyword = %s\n",value);
  117.             break;
  118.    case 6 : if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
  119.                 (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
  120.                printplot  = ON;
  121.                if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
  122.                   printplot  = OFF;
  123.                fprintf(stdout,"   Options : printplot       = %s\n",value);
  124.                }
  125.             else
  126.                fprintf(stdout,"   Options : Unknown keyword = %s\n",value);
  127.             break;
  128.    case 7 : linetypes = atoi(value);
  129.             if (linetypes <=0 || linetypes >= 4) linetypes = 2;
  130.             fprintf(stdout,"   Options : linetypes       = %d\n",linetypes);
  131.             break;
  132.    case 8 : scale = atof(value); 
  133.             if (scale < 0.1 || scale > 1) scale = 1.0;
  134.             fprintf(stdout,"   Options : scale           = %f\n",scale);
  135.             break;
  136.    case 9 : xticks = atoi(value); 
  137.             if (xticks <= 0 || xticks > 20) xticks = 4;
  138.             fprintf(stdout,"   Options : xticks          = %d\n",xticks);
  139.             break;
  140.    case 10: yticks = atoi(value); 
  141.             if (yticks <= 0 || yticks > 20) yticks = 4;
  142.             fprintf(stdout,"   Options : yticks          = %d\n",yticks);
  143.             break;
  144.    case 11: if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
  145.                 (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
  146.                contlabel = ON;
  147.                if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
  148.                   contlabel = OFF;
  149.                fprintf(stdout,"   Options : contlabel       = %s\n",value);
  150.                }
  151.             else
  152.                fprintf(stdout,"   Options : Unknown keyword = %s\n",value);
  153.             break;
  154.    case 12: if ((strcmp(value,"HIGH")==0)||(strcmp(value,"high")==0)||
  155.                 (strcmp(value,"LOW" )==0)||(strcmp(value,"low" )==0)) {
  156.                joincurve = ON;
  157.                fprintf(stdout,"   Options : joincurve       = ON\n");
  158.                fprintf(stdout,"   Options : joinlevel       = %s\n",value);
  159.             } else {
  160.                joinlevel = OFF;
  161.                fprintf(stdout,"   Warning : Invalid keyword -");
  162.                fprintf(stdout," [-j] [HIGH/high/LOW/low]\n");
  163.             }
  164.             break;
  165.    case 13: sprintf(printer,"-P%s",value);
  166.             fprintf(stdout,"   Commands: Printer         = %s\n",printer);
  167.             break;
  168.    case 14: if ((strcmp(value,"on")==0) || (strcmp(value,"off")==0) ||
  169.                 (strcmp(value,"ON")==0) || (strcmp(value,"OFF")==0)) {
  170.                landscape = ON;
  171.                if ((strcmp(value,"off")==0) || (strcmp(value,"OFF")==0))
  172.                   landscape = OFF;
  173.                fprintf(stdout,"   Options : landscape       = %s\n",value);
  174.                }
  175.             else
  176.                fprintf(stdout,"   Options : Unknown keyword = %s\n",value);
  177.             break;
  178.    default: break;
  179.    }
  180. }
  181.  
  182. /* Convert a character into a number */
  183. chartoint(ch) 
  184. char ch;
  185.    int num;
  186.  
  187.    switch (ch) {
  188.    case '0' : num = 0; break;
  189.    case '1' : num = 1; break;
  190.    case '2' : num = 2; break;
  191.    case '3' : num = 3; break;
  192.    case '4' : num = 4; break;
  193.    case '5' : num = 5; break;
  194.    case '6' : num = 6; break;
  195.    case '7' : num = 7; break;
  196.    case '8' : num = 8; break;
  197.    case '9' : num = 9; break;
  198.    default  : fprintf(stderr,"Warning : Nonnumeric character %c in string",ch);
  199.               num = 0; break;
  200.    }
  201.    return(num);
  202. }
  203.  
  204. /* get line into s */
  205. getline(op,s,lim) 
  206. FILE *op;
  207. char s[];
  208. int lim;
  209. {
  210.     int c,i,j;
  211.  
  212.     /* read the line until termination marks ';' or '#' */
  213.     for (i=0; i<lim-1 && (c=getc(op))!=EOF && c!='\n'&&c!='#'&& c!=';' ; ++i) 
  214.        s[i] = c;
  215.     if (i==lim-1 || c==';' ||c=='#' || c=='\n') s[i++] = '\n'; 
  216.     if (c==';'||c=='#')
  217.        for (j=0; (c=getc(op))!=EOF && c!='\n'; ++j);
  218.     s[i] = '\0';
  219.  
  220.     return(i);
  221. }
  222.  
  223. /* Get the words in a line */
  224. getwords(s,length,word)  
  225. char s[];
  226. int length;
  227. char word[100][MAXCHAR];
  228. {
  229.    int i,nc,nw,inword,collectword;
  230.    char c;
  231.  
  232.    nc = nw = 0;
  233.    inword = NO;
  234.    collectword = NO;
  235.  
  236.    if (length==1 && s[0] == '\n') return(nw);
  237.    for (i=0; s[i] != '\0'; i++) {
  238.       c = s[i];
  239.       if (c == ' ' || c=='\n' || c == '\t' || c=='=') {
  240.          if (inword == YES && (collectword==NO || c=='\n') ) {
  241.             word[nw++][nc++] = '\0';
  242.             inword = NO;
  243.             nc = 0;
  244.          } else if (collectword == YES)
  245.             word[nw][nc++] = c;
  246.       } else if (c == '\'' || c == '"') {
  247.          if (collectword == NO) collectword = YES; 
  248.          else {
  249.             collectword = NO; 
  250.             word[nw++][nc++] = '\0';
  251.             inword = NO;
  252.             nc = 0;
  253.          }
  254.       } else {
  255.          word[nw][nc++] = c;
  256.          if (inword == NO) inword = YES;
  257.       }
  258.    }
  259.    return(nw);
  260. }
  261.  
  262. /* Convert a word into a number - don't need this */
  263. /* use the library function atof() instead */
  264. double word_to_number(word) 
  265.  
  266. char word[];
  267.  
  268. {
  269.   int i;
  270.   int decimal_flag;
  271.   int exponent_flag;
  272.   int decimal_count;
  273.   int new_term;
  274.   double number,sign,exponent,exp_sign;
  275.  
  276.   sign = 1.;
  277.   exp_sign = 1.;
  278.   decimal_flag = OFF;
  279.   exponent_flag = OFF;
  280.   decimal_count = 0;
  281.   number = 0.;
  282.   exponent = 0.;
  283.  
  284.   for (i = 0; word[i] != '\0'; i++)
  285.   {
  286.     if (word[i] == '-' && exponent_flag == OFF)
  287.       sign = -1.;
  288.     else if (word[i] != '+')
  289.     {
  290.       if (word[i] == '.')
  291.         decimal_flag = ON;
  292.       else
  293.       if (word[i] == 'e')
  294.         exponent_flag = ON;
  295.       else
  296.       {
  297.         new_term = word[i] - '0';
  298.         if (exponent_flag)
  299.         {
  300.           if (word[i] == '-')
  301.             exp_sign = -1.;
  302.           else 
  303.             exponent = exponent * 10. + (double) new_term;
  304.         }
  305.         else {
  306.           number = number * 10. + (double) new_term;
  307.           if (decimal_flag) 
  308.             decimal_count++;
  309.           }
  310.       }
  311.     }
  312.   }
  313.   for (; decimal_count > 0; decimal_count--)
  314.     number /= 10.; 
  315.   
  316.   number = number * sign * pow((double)10.,(exp_sign * exponent));
  317.  
  318.   return number;
  319. }
  320.  
  321.