r246: More modifications for MSVC
[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.8 2000/12/06 01:46:43 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         sys_error(ERR_WARNING, "Illegal EZSET command: %s", str);
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_SYMBOL:
288     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
289       if (n > 0 && n <= MAXSYMBOL)
290         o_symbol = n;
291       else
292         o_symbol = 1;
293     } else {
294       if (pol_word("every",5) == TRUE) {
295         if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
296           if (n > 0)
297             o_symfreq = n;
298           else
299             o_symfreq = 1;
300         }
301       } else if (pol_word ("none",4) == TRUE) {
302         o_symbol = -1;
303       }
304     }
305     break;
306   case S_XTICKS:
307     if (pol_usertok(str,&lx) == FALSE)
308       break;
309     if (lx == S_ABOVE)
310       o_xticks = ABOVE;
311     else if (lx == S_BELOW)
312       o_xticks = BELOW;
313     else if (lx == S_NOLABEL)
314       o_xtlabel = FALSE;
315     else if (lx == S_LABEL)
316       o_xtlabel = TRUE;
317     else if (lx == S_MAJOR) {
318       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
319         if (n > 1 && n < 100)
320           o_xmajortick = n;
321     } else if (lx == S_MINOR)
322       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
323         if (n >= 0 && n < 100)
324           o_xminortick = n;
325     break;
326   case S_YTICKS:
327     if (pol_usertok(str,&lx) == FALSE)
328       break;
329     if (lx == S_RIGHT)
330       o_yticks = RIGHT;
331     else if (lx == S_LEFT)
332       o_yticks = LEFT;
333     else if (lx == S_NOLABEL)
334       o_ytlabel = FALSE;
335     else if (lx == S_LABEL)
336       o_ytlabel = TRUE;
337     else if (lx == S_MAJOR) {
338       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
339         if (n > 1 && n < 100)
340           o_ymajortick = n;
341     } else if (lx == S_MINOR)
342       if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
343         if (n >= 0 && n < 100)
344           o_yminortick = n;
345     break;
346   case S_LXFRAC:
347     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
348       if (n >= 0) {
349         v_lxfrac = n;
350         s_lxfrac = TRUE;
351       }
352     } else
353       s_lxfrac = FALSE;
354     break;
355   case S_LYFRAC:
356     if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
357       if (n >= 0) {
358         v_lyfrac = n;
359         s_lyfrac = TRUE;
360       }
361     } else
362       s_lyfrac = FALSE;
363     break;
364     break;
365   default:
366     fprintf (stderr, "Unimplemented EZPLOT command\n");
367     break;
368   }
369
370   pol_reader ();
371   return (TRUE);
372 }
373
374
375
376 void 
377 EZPlot::bad_option (char *opt)
378 {
379   sys_error (ERR_WARNING, "INVALID option: %s", opt);
380 }
381
382 /*----------------------------------------------------------------------*/
383 /*                      LEXIGRAPHICAL CODES                             */
384 /*----------------------------------------------------------------------*/
385
386 static struct key {
387   char *keyword;
388   int code;
389 } keytab[] = {
390     {"solid",   S_SOLID},
391         {"dash", S_DASH},
392             {"noline",  S_NOLINE},
393                 {"black",       S_BLACK},
394                     {"red",             S_RED},
395                         {"blue",                S_BLUE},
396                             {"green",   S_GREEN},
397                                 {"pen",         S_PEN},
398                                     {"symbol",  S_SYMBOL},
399                                         {"every",       S_EVERY},    
400                                             {"none",            S_NONE},  
401                         {"legend",      S_LEGEND},
402                           {"xlegend",   S_XLEGEND},
403                             {"ylegend", S_YLEGEND},
404
405                               {"xlin",          S_XLIN},
406                                 {"ylin",                S_YLIN},
407                                   {"xlog",              S_XLOG},
408                                     {"ylog",            S_YLOG},
409                                       {"xlabel",        S_XLABEL},
410                                         {"ylabel",      S_YLABEL},
411                                           {"xlength",   S_XLENGTH},
412                                             {"ylength", S_YLENGTH},
413
414                                               {"xticks",        S_XTICKS},
415                                                 {"yticks",      S_YTICKS},
416                                                   {"above",     S_ABOVE},
417                                                     {"label",   S_LABEL},
418                                                       {"below", S_BELOW},
419                                                         {"nolabel",     S_NOLABEL},
420                                                           {"right",     S_RIGHT},
421                                                             {"left",            S_LEFT},
422
423                                                               {"xautoscale",    S_XAUTOSCALE},
424                                                                 {"yautoscale",  S_YAUTOSCALE},
425                                                                   {"xmin",              S_XMIN},
426                                                                     {"ymin",            S_YMIN},
427                                                                       {"xmax",          S_XMAX},
428                                                                         {"ymax",                S_YMAX},
429                                                                           {"lxfrac",    S_LXFRAC},
430                                                                             {"lyfrac",  S_LYFRAC},
431                                                                               {"xcross",        S_XCROSS},
432                                                                                 {"ycross",      S_YCROSS},
433                                                                                   {"noxaxis",   S_NOXAXIS},
434                                                                                     {"noyaxis", S_NOYAXIS},
435                                                                                       {"xporigin",      S_XPORIGIN},
436                                                                                         {"yporigin",    S_YPORIGIN},
437                                                                                           {"title",     S_TITLE},
438                                                                                             {"xtitle",  S_XTITLE},
439                                                                                               {"ytitle",        S_YTITLE},
440
441                                                                                                 {"replot",      S_REPLOT},
442                                                                                                   {"clear",     S_CLEAR},
443                                                                                                     {"store",   S_STORE},
444                                                                                                       {"restore",       S_RESTORE},
445                                                                                                             {"amark",   S_AMARK},
446                                                                                                                 {"units",       S_UNITS},
447                                                                                                                   {"inches",    S_INCHES},
448                                                                                                                     {"user",            S_USER},
449
450                                                                                                                       {"data",          S_DATA},
451                                                                                                                         {"help",                S_HELP},
452                                                                                                                           {"exit",              S_EXIT},
453
454                                                                                                                             {"box",             S_BOX},
455                                                                                                                               {"nobox", S_NOBOX},
456                                                                                                                                 {"grid",                S_GRID},
457                                                                                                                                   {"nogrid",    S_NOGRID},
458                                                                                                                                     {"major",   S_MAJOR},
459                                                                                                                                       {"minor", S_MINOR},
460                                                                                                                                         {"color",       S_COLOR},
461                                                                                                                                           {"legendbox", S_LEGENDBOX},
462
463                                                                                                                                                 {"no",          S_NO},
464
465                                                                                                                                                   {"textsize",  S_TEXTSIZE},
466                                                                                                                                                         };
467
468 static const unsigned int NKEYS=(sizeof(keytab) / sizeof(struct key));
469
470 void 
471 EZPlot::initkw(void)
472 {
473   for (unsigned int i = 0; i < NKEYS; i++)
474     if (! pol_install(keytab[i].keyword, keytab[i].code))
475       sys_error(ERR_SEVERE, "error installing ezset keywords [initkw]");
476 }