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