c28c76180ac131390c8da4c2b9e875671e772d28
[ctsim.git] / libctgraphics / ezset.cpp
1 /*****************************************************************************
2  **  FILE IDENTIFICATION
3  **
4  **      EZSET - Parameter control for EZPLOT           
5  **
6  **  This is part of the CTSim program
7  **  Copyright (C) 1983-2000 Kevin Rosenberg
8  **
9  **  $Id: ezset.cpp,v 1.6 2000/07/28 10:51:31 kevin Exp $
10  **
11  **  This program is free software; you can redistribute it and/or modify
12  **  it under the terms of the GNU General Public License (version 2) as
13  **  published by the Free Software Foundation.
14  **
15  **  This program is distributed in the hope that it will be useful,
16  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  **  GNU General Public License for more details.
19  **
20  **  You should have received a copy of the GNU General Public License
21  **  along with this program; if not, write to the Free Software
22  **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  ******************************************************************************/
24
25 #include "ctsupport.h"
26 #include "ezplot.h"
27 #include "pol.h"
28
29 bool EZPlot::ezset_initialized = false;
30
31 int 
32 EZPlot::ezset (char *command)
33 {
34   if (! ezset_initialized) {
35     pol_init();
36     initkw();
37     pol_skpword ("please");
38     pol_skpword ("use");
39     pol_skpword ("are");
40     pol_skpword ("and");
41     pol_skpword ("is");
42     pol_skpword ("the");
43     pol_skpword ("equals");
44     pol_skpchar ("=");
45
46     pol_usefile (P_USE_STR,"");
47     set_inputline ("!eoc ,");
48     pol_reader ();
49     pol_closefile ();
50     ezset_initialized = true;
51   }
52   return (ezcmd (command));
53 }
54
55 bool 
56 EZPlot::ezcmd (char *comm)
57 {
58   pol_usefile (P_USE_STR, "");
59   set_inputline (comm);
60
61   char str[MAXTOK+1];
62   int code;
63   bool retval = true;
64   if (! pol_usertok (str, &code)) {
65     cerr << "Illegal EZSET command: " << str << endl;
66     pol_reader();
67     retval = false;
68   }
69   else
70     retval = do_cmd (code);
71
72   pol_closefile();                      /* close input string file */
73   return (retval);
74 }
75
76
77 int 
78 EZPlot::do_cmd (int lx)
79 {
80   char str [1024];
81   char strIn [1024];
82   int n;
83   double f;
84
85   switch (lx) {
86   case S_TEXTSIZE:
87     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
88       {
89         if (f >= 0.0 && f <= 1.0) {
90           v_textsize = f;
91           s_textsize = TRUE;
92         } else
93           s_textsize = FALSE;
94       }
95     break;
96   case S_REPLOT:
97     plot ();
98     break;
99   case S_CLEAR:
100     clearCurves ();
101     break;
102   case S_TITLE:
103     gettext (strIn, sizeof(strIn));
104     c_title = strIn;
105     break;
106   case S_LEGEND:
107     gettext (strIn, sizeof(strIn));
108     c_legend = strIn;
109     break;
110   case S_XLABEL:
111     gettext (strIn, sizeof(strIn));
112     c_xlabel = strIn;
113     break;
114   case S_YLABEL:
115     gettext (strIn, sizeof(strIn));
116     c_ylabel = strIn;
117     break;
118   case S_XCROSS:
119     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE) {
120       v_xcross = f;
121       s_xcross = TRUE;
122     } else
123       s_xcross = FALSE;
124     break;
125   case S_YCROSS:
126     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE) {
127       v_ycross = f;
128       s_ycross = TRUE;
129     } else
130       s_ycross = FALSE;
131     break;
132   case S_NOXAXIS:
133     o_xaxis = NOAXIS;
134     break;
135   case S_NOYAXIS:
136     o_yaxis = NOAXIS;
137     break;
138   case S_XLIN:
139     o_xaxis = LINEAR;
140     break;
141   case S_YLIN:
142     o_yaxis = LINEAR;
143     break;
144   case S_XLOG:
145     o_xaxis = LOG;
146     break;
147   case S_YLOG:
148     o_yaxis = LOG;
149     break;
150   case S_XAUTOSCALE:
151     s_xmin = FALSE;
152     s_xmax = FALSE;
153     break;
154   case S_YAUTOSCALE:
155     s_ymin = FALSE;
156     s_ymax = FALSE;
157     break;
158   case S_XMIN:
159     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE) {
160       v_xmin = f;
161       s_xmin = TRUE;
162     }
163     break;
164   case S_XMAX:
165     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE) {
166       v_xmax = f;
167       s_xmax = TRUE;
168     }
169     break;
170   case S_YMIN:
171     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE) {
172       v_ymin = f;
173       s_ymin = TRUE;
174     }
175     break;
176   case S_YMAX:
177     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE) {
178       v_ymax = f;
179       s_ymax = TRUE;
180     }
181     break;
182   case S_SOLID:
183     o_linestyle = SGP::LS_SOLID;
184     break;
185   case S_DASH:
186     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
187       if (n == 1)
188         o_linestyle = SGP::LS_DASH1;
189       else if (n == 2)
190         o_linestyle = SGP::LS_DASH2;
191       else if (n == 3)
192         o_linestyle = SGP::LS_DASH3;
193       else if (n == 4)
194         o_linestyle = SGP::LS_DASH4;
195       else
196         o_linestyle = SGP::LS_DASH1;
197     } else
198       o_linestyle = SGP::LS_DASH1;
199     break;
200   case S_NOLINE:
201     o_linestyle = SGP::LS_NOLINE;
202     break;
203   case S_PEN:
204   case S_COLOR:
205     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
206       {
207         if (n >= 0)
208           o_color = n;
209         else
210           bad_option("The color you picked");
211       }
212     break;
213   case S_BOX:
214     o_box = TRUE;
215     break;
216   case S_NOBOX:
217     o_box = FALSE;
218     break;
219   case S_GRID:
220     o_grid = TRUE;
221     break;
222   case S_NOGRID:
223     o_grid = FALSE;
224     break;
225   case S_XLENGTH:
226     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
227       if (f > 0.0 && f <= 1.0)
228         o_xlength = f;
229     break;
230   case S_YLENGTH:
231     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
232       if (f > 0.0 && f <= 1.0)
233         o_ylength = f;
234     break;
235   case S_XPORIGIN:
236     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
237       if (f >= 0.0 && f < 1.0)
238         o_xporigin = f;
239     break;
240   case S_YPORIGIN:
241     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
242       if (f >= 0.0 && f < 1.0)
243         o_yporigin = f;
244     break;
245   case S_TAG:
246     if (pol_word("no", 2) == TRUE)
247       o_tag = FALSE;
248     else if (pol_word("off", 2) == TRUE)
249       o_tag = FALSE;
250     else
251       o_tag = TRUE;
252     break;
253   case S_LEGENDBOX:
254     if (pol_word("inside", 2) == TRUE)
255       o_legendbox = INSIDE;
256     else if (pol_word("outside", 3) == TRUE)
257       o_legendbox = OUTSIDE;
258     else if (pol_word("none",2) == TRUE)
259       o_legendbox = NOLEGEND;
260     else {
261       gettext (str, MAXTOK);
262       bad_option(str);
263     }
264     break;
265   case S_XLEGEND:
266     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
267       {
268         if (f >= 0.0 && f < 1.0) {
269           v_xlegend = f;
270           s_xlegend = TRUE;
271         }
272         else
273           s_xlegend = FALSE;
274       }
275     break;
276   case S_YLEGEND:
277     if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
278       {
279         if (f >= 0.0 && f < 1.0) {
280           v_ylegend = f;
281           s_ylegend = TRUE;
282         }
283         else
284           s_ylegend = FALSE;
285       }
286     break;
287   case S_CURVES:
288     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
289       if (n < 1)
290         n = 1;
291       //      if (modeinteract == TRUE) {
292       //        curveinteract = n - 1;
293       //        if (o_unknowncurves == FALSE && o_reqcurves < n)
294       //          o_reqcurves = n;
295       //      } else {
296       // clearCurve ();
297       // o_reqcurves = n;
298       // }
299     } else {
300       //      if (pol_word ("unknown", 7) == TRUE)
301         //      o_unknowncurves = TRUE;
302       //      else if (pol_word ("end", 3) == TRUE) {
303         //      o_unknowncurves = FALSE;
304         //      o_reqcurves = i_numcurves;
305         //      i_plotimmediate = TRUE;
306         //      ezplot (static_cast<double*>(NULL), static_cast<double*>(NULL), 0);
307         //      i_plotimmediate = FALSE;
308       // }
309     }
310     break;
311   case S_SYMBOL:
312     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
313       if (n > 0 && n <= MAXSYMBOL)
314         o_symbol = n;
315       else
316         o_symbol = 1;
317       //      if (modeinteract == TRUE && curveinteract >= 0)
318       //        curve[curveinteract].symbol = o_symbol;
319     } else {
320       if (pol_word("every",5) == TRUE) {
321         if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
322           if (n > 0)
323             o_symfreq = n;
324           else
325             o_symfreq = 1;
326           //      if (modeinteract == TRUE && curveinteract >= 0)
327           //        curve[curveinteract].symfreq = o_symfreq;
328         }
329       } else if (pol_word ("none",4) == TRUE) {
330         o_symbol = -1;
331         //      if (modeinteract == TRUE && curveinteract >= 0)
332         //        curve[curveinteract].symbol = o_symbol;
333       }
334     }
335     break;
336   case S_XTICKS:
337     if (pol_usertok(str,&lx) == FALSE)
338       break;
339     if (lx == S_ABOVE)
340       o_xticks = ABOVE;
341     else if (lx == S_BELOW)
342       o_xticks = BELOW;
343     else if (lx == S_NOLABEL)
344       o_xtlabel = FALSE;
345     else if (lx == S_LABEL)
346       o_xtlabel = TRUE;
347     else if (lx == S_MAJOR) {
348       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
349         if (n > 1 && n < 100)
350           o_xmajortick = n;
351     } else if (lx == S_MINOR)
352       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
353         if (n >= 0 && n < 100)
354           o_xminortick = n;
355     break;
356   case S_YTICKS:
357     if (pol_usertok(str,&lx) == FALSE)
358       break;
359     if (lx == S_RIGHT)
360       o_yticks = RIGHT;
361     else if (lx == S_LEFT)
362       o_yticks = LEFT;
363     else if (lx == S_NOLABEL)
364       o_ytlabel = FALSE;
365     else if (lx == S_LABEL)
366       o_ytlabel = TRUE;
367     else if (lx == S_MAJOR) {
368       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
369         if (n > 1 && n < 100)
370           o_ymajortick = n;
371     } else if (lx == S_MINOR)
372       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
373         if (n >= 0 && n < 100)
374           o_yminortick = n;
375     break;
376   case S_LXFRAC:
377     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
378       if (n >= 0) {
379         v_lxfrac = n;
380         s_lxfrac = TRUE;
381       }
382     } else
383       s_lxfrac = FALSE;
384     break;
385   case S_LYFRAC:
386     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
387       if (n >= 0) {
388         v_lyfrac = n;
389         s_lyfrac = TRUE;
390       }
391     } else
392       s_lyfrac = FALSE;
393     break;
394     break;
395   default:
396     fprintf (stderr, "Unimplemented EZPLOT command\n");
397     break;
398   }
399
400   pol_reader ();
401   return (TRUE);
402 }
403
404
405
406 void 
407 EZPlot::bad_option (char *opt)
408 {
409   cerr << opt << " is an INVALID option\n";
410 }
411
412 /*----------------------------------------------------------------------*/
413 /*                      LEXIGRAPHICAL CODES                             */
414 /*----------------------------------------------------------------------*/
415
416 static struct key {
417   char *keyword;
418   int code;
419 } keytab[] = {
420   {"solid",     S_SOLID},
421     {"dash",            S_DASH},
422       {"noline",        S_NOLINE},
423         {"black",       S_BLACK},
424           {"red",               S_RED},
425             {"blue",            S_BLUE},
426               {"green", S_GREEN},
427                 {"pen",         S_PEN},
428                   {"symbol",    S_SYMBOL},
429                     /*  "every",        S_EVERY,        */
430                     /*  "none",         S_NONE,         */
431
432                     {"curves",  S_CURVES},
433                       {"curve", S_CURVES},
434                         /*      "unknown",      S_UNKNOWN,      */
435                         /*      "end",          S_END,          */
436
437                         {"legend",      S_LEGEND},
438                           {"xlegend",   S_XLEGEND},
439                             {"ylegend", S_YLEGEND},
440
441                               {"xlin",          S_XLIN},
442                                 {"ylin",                S_YLIN},
443                                   {"xlog",              S_XLOG},
444                                     {"ylog",            S_YLOG},
445                                       {"xlabel",        S_XLABEL},
446                                         {"ylabel",      S_YLABEL},
447                                           {"xlength",   S_XLENGTH},
448                                             {"ylength", S_YLENGTH},
449
450                                               {"xticks",        S_XTICKS},
451                                                 {"yticks",      S_YTICKS},
452                                                   {"above",     S_ABOVE},
453                                                     {"label",   S_LABEL},
454                                                       {"below", S_BELOW},
455                                                         {"nolabel",     S_NOLABEL},
456                                                           {"right",     S_RIGHT},
457                                                             {"left",            S_LEFT},
458
459                                                               {"xautoscale",    S_XAUTOSCALE},
460                                                                 {"yautoscale",  S_YAUTOSCALE},
461                                                                   {"xmin",              S_XMIN},
462                                                                     {"ymin",            S_YMIN},
463                                                                       {"xmax",          S_XMAX},
464                                                                         {"ymax",                S_YMAX},
465                                                                           {"lxfrac",    S_LXFRAC},
466                                                                             {"lyfrac",  S_LYFRAC},
467                                                                               {"xcross",        S_XCROSS},
468                                                                                 {"ycross",      S_YCROSS},
469                                                                                   {"noxaxis",   S_NOXAXIS},
470                                                                                     {"noyaxis", S_NOYAXIS},
471                                                                                       {"xporigin",      S_XPORIGIN},
472                                                                                         {"yporigin",    S_YPORIGIN},
473                                                                                           {"title",     S_TITLE},
474                                                                                             {"xtitle",  S_XTITLE},
475                                                                                               {"ytitle",        S_YTITLE},
476
477                                                                                                 {"replot",      S_REPLOT},
478                                                                                                   {"clear",     S_CLEAR},
479                                                                                                     {"store",   S_STORE},
480                                                                                                       {"restore",       S_RESTORE},
481                                                                                                             {"amark",   S_AMARK},
482                                                                                                               {"interactive",   S_INTERACTIVE},
483                                                                                                                 {"units",       S_UNITS},
484                                                                                                                   {"inches",    S_INCHES},
485                                                                                                                     {"user",            S_USER},
486
487                                                                                                                       {"data",          S_DATA},
488                                                                                                                         {"help",                S_HELP},
489                                                                                                                           {"exit",              S_EXIT},
490
491                                                                                                                             {"box",             S_BOX},
492                                                                                                                               {"nobox", S_NOBOX},
493                                                                                                                                 {"grid",                S_GRID},
494                                                                                                                                   {"nogrid",    S_NOGRID},
495                                                                                                                                     {"major",   S_MAJOR},
496                                                                                                                                       {"minor", S_MINOR},
497                                                                                                                                         {"color",       S_COLOR},
498                                                                                                                                           {"legendbox", S_LEGENDBOX},
499
500                                                                                                                                                 {"no",          S_NO},
501
502                                                                                                                                                   {"textsize",  S_TEXTSIZE},
503                                                                                                                                                         };
504
505 static const unsigned int NKEYS=(sizeof(keytab) / sizeof(struct key));
506
507 void 
508 EZPlot::initkw(void)
509 {
510   for (unsigned int i = 0; i < NKEYS; i++)
511     if (pol_install(keytab[i].keyword, keytab[i].code) == FALSE)
512       sys_error(ERR_SEVERE, "error installing ezset keywords [initkw]");
513 }