r307: additions for plotfile
[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.10 2000/12/20 14:39:09 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                 initkw();
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                 {
96                         if (f >= 0.0 && f <= 1.0) {
97                                 v_textsize = f;
98                                 s_textsize = TRUE;
99                         } else
100                                 s_textsize = FALSE;
101                 }
102                 break;
103         case S_REPLOT:
104                 plot ();
105                 break;
106         case S_CLEAR:
107                 clearCurves ();
108                 break;
109         case S_TITLE:
110                 gettext (strIn, sizeof(strIn));
111                 c_title = strIn;
112                 break;
113         case S_LEGEND:
114                 gettext (strIn, sizeof(strIn));
115                 c_legend = 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                 if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
194                         if (n == 1)
195                                 o_linestyle = SGP::LS_DASH1;
196                         else if (n == 2)
197                                 o_linestyle = SGP::LS_DASH2;
198                         else if (n == 3)
199                                 o_linestyle = SGP::LS_DASH3;
200                         else if (n == 4)
201                                 o_linestyle = SGP::LS_DASH4;
202                         else
203                                 o_linestyle = SGP::LS_DASH1;
204                 } else
205                         o_linestyle = SGP::LS_DASH1;
206                 break;
207         case S_NOLINE:
208                 o_linestyle = SGP::LS_NOLINE;
209                 break;
210         case S_PEN:
211         case S_COLOR:
212                 if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
213                 {
214                         if (n >= 0)
215                                 o_color = n;
216                         else
217                                 bad_option("The color you picked");
218                 }
219                 break;
220         case S_BOX:
221                 o_box = TRUE;
222                 break;
223         case S_NOBOX:
224                 o_box = FALSE;
225                 break;
226         case S_GRID:
227                 o_grid = TRUE;
228                 break;
229         case S_NOGRID:
230                 o_grid = FALSE;
231                 break;
232         case S_XLENGTH:
233                 if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
234                         if (f > 0.0 && f <= 1.0)
235                                 o_xlength = f;
236                         break;
237         case S_YLENGTH:
238                 if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
239                         if (f > 0.0 && f <= 1.0)
240                                 o_ylength = f;
241                         break;
242         case S_XPORIGIN:
243                 if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
244                         if (f >= 0.0 && f < 1.0)
245                                 o_xporigin = f;
246                         break;
247         case S_YPORIGIN:
248                 if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
249                         if (f >= 0.0 && f < 1.0)
250                                 o_yporigin = f;
251                         break;
252         case S_TAG:
253                 if (pol_word("no", 2) == TRUE)
254                         o_tag = FALSE;
255                 else if (pol_word("off", 2) == TRUE)
256                         o_tag = FALSE;
257                 else
258                         o_tag = TRUE;
259                 break;
260         case S_LEGENDBOX:
261                 if (pol_word("inside", 2) == TRUE)
262                         o_legendbox = INSIDE;
263                 else if (pol_word("outside", 3) == TRUE)
264                         o_legendbox = OUTSIDE;
265                 else if (pol_word("none",2) == TRUE)
266                         o_legendbox = NOLEGEND;
267                 else {
268                         gettext (str, MAXTOK);
269                         bad_option(str);
270                 }
271                 break;
272         case S_XLEGEND:
273                 if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
274                 {
275                         if (f >= 0.0 && f < 1.0) {
276                                 v_xlegend = f;
277                                 s_xlegend = TRUE;
278                         }
279                         else
280                                 s_xlegend = FALSE;
281                 }
282                 break;
283         case S_YLEGEND:
284                 if (pol_float (&f, TT_REAL, FALSE, 0.0, 0.0) == TRUE)
285                 {
286                         if (f >= 0.0 && f < 1.0) {
287                                 v_ylegend = f;
288                                 s_ylegend = TRUE;
289                         }
290                         else
291                                 s_ylegend = FALSE;
292                 }
293                 break;
294         case S_SYMBOL:
295                 if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
296                         if (n > 0 && n <= MAXSYMBOL)
297                                 o_symbol = n;
298                         else
299                                 o_symbol = 1;
300                 } else {
301                         if (pol_word("every",5) == TRUE) {
302                                 if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
303                                         if (n > 0)
304                                                 o_symfreq = n;
305                                         else
306                                                 o_symfreq = 1;
307                                 }
308                         } else if (pol_word ("none",4) == TRUE) {
309                                 o_symbol = -1;
310                         }
311                 }
312                 break;
313         case S_XTICKS:
314                 if (pol_usertok(str,&lx) == FALSE)
315                         break;
316                 if (lx == S_ABOVE)
317                         o_xticks = ABOVE;
318                 else if (lx == S_BELOW)
319                         o_xticks = BELOW;
320                 else if (lx == S_NOLABEL)
321                         o_xtlabel = FALSE;
322                 else if (lx == S_LABEL)
323                         o_xtlabel = TRUE;
324                 else if (lx == S_MAJOR) {
325                         if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
326                                 if (n > 1 && n < 100)
327                                         o_xmajortick = n;
328                 } else if (lx == S_MINOR)
329                         if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
330                                 if (n >= 0 && n < 100)
331                                         o_xminortick = n;
332                                 break;
333         case S_YTICKS:
334                 if (pol_usertok(str,&lx) == FALSE)
335                         break;
336                 if (lx == S_RIGHT)
337                         o_yticks = RIGHT;
338                 else if (lx == S_LEFT)
339                         o_yticks = LEFT;
340                 else if (lx == S_NOLABEL)
341                         o_ytlabel = FALSE;
342                 else if (lx == S_LABEL)
343                         o_ytlabel = TRUE;
344                 else if (lx == S_MAJOR) {
345                         if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
346                                 if (n > 1 && n < 100)
347                                         o_ymajortick = n;
348                 } else if (lx == S_MINOR)
349                         if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE)
350                                 if (n >= 0 && n < 100)
351                                         o_yminortick = n;
352                                 break;
353         case S_LXFRAC:
354                 if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
355                         if (n >= 0) {
356                                 v_lxfrac = n;
357                                 s_lxfrac = TRUE;
358                         }
359                 } else
360                         s_lxfrac = FALSE;
361                 break;
362         case S_LYFRAC:
363                 if (pol_integer (&n, TT_REAL, FALSE, 0, 0) == TRUE) {
364                         if (n >= 0) {
365                                 v_lyfrac = n;
366                                 s_lyfrac = TRUE;
367                         }
368                 } else
369                         s_lyfrac = FALSE;
370                 break;
371                 break;
372         default:
373                 fprintf (stderr, "Unimplemented EZPLOT command\n");
374                 break;
375   }
376   
377   pol_reader ();
378   return (true);
379 }
380
381
382
383 void 
384 EZPlot::bad_option (char *opt)
385 {
386         sys_error (ERR_WARNING, "INVALID option: %s", opt);
387 }
388
389 /*----------------------------------------------------------------------*/
390 /*                      LEXIGRAPHICAL CODES                             */
391 /*----------------------------------------------------------------------*/
392
393 static struct key {
394         char *keyword;
395         int code;
396 } keytab[] = {
397     {"solid",   S_SOLID},
398         {"dash", S_DASH},
399         {"noline",      S_NOLINE},
400         {"black",       S_BLACK},
401         {"red",         S_RED},
402         {"blue",                S_BLUE},
403         {"green",       S_GREEN},
404                                 {"pen",         S_PEN},
405         {"symbol",      S_SYMBOL},
406         {"every",       S_EVERY},    
407         {"none",                S_NONE},  
408         {"legend",      S_LEGEND},
409         {"xlegend",     S_XLEGEND},
410         {"ylegend",     S_YLEGEND},
411         
412         {"xlin",                S_XLIN},
413                                 {"ylin",                S_YLIN},
414         {"xlog",                S_XLOG},
415         {"ylog",                S_YLOG},
416         {"xlabel",      S_XLABEL},
417         {"ylabel",      S_YLABEL},
418         {"xlength",     S_XLENGTH},
419         {"ylength",     S_YLENGTH},
420         
421         {"xticks",      S_XTICKS},
422         {"yticks",      S_YTICKS},
423         {"above",       S_ABOVE},
424         {"label",       S_LABEL},
425         {"below",       S_BELOW},
426         {"nolabel",     S_NOLABEL},
427         {"right",       S_RIGHT},
428         {"left",                S_LEFT},
429         
430         {"xautoscale",  S_XAUTOSCALE},
431         {"yautoscale",  S_YAUTOSCALE},
432         {"xmin",                S_XMIN},
433         {"ymin",                S_YMIN},
434         {"xmax",                S_XMAX},
435         {"ymax",                S_YMAX},
436         {"lxfrac",      S_LXFRAC},
437         {"lyfrac",      S_LYFRAC},
438         {"xcross",      S_XCROSS},
439         {"ycross",      S_YCROSS},
440         {"noxaxis",     S_NOXAXIS},
441         {"noyaxis",     S_NOYAXIS},
442         {"xporigin",    S_XPORIGIN},
443         {"yporigin",    S_YPORIGIN},
444         {"title",       S_TITLE},
445         {"xtitle",      S_XTITLE},
446         {"ytitle",      S_YTITLE},
447         
448         {"replot",      S_REPLOT},
449         {"clear",       S_CLEAR},
450         {"store",       S_STORE},
451         {"restore",     S_RESTORE},
452         {"amark",       S_AMARK},
453         {"units",       S_UNITS},
454         {"inches",      S_INCHES},
455         {"user",                S_USER},
456         
457         {"data",                S_DATA},
458         {"help",                S_HELP},
459         {"exit",                S_EXIT},
460         
461         {"box",         S_BOX},
462         {"nobox",       S_NOBOX},
463         {"grid",                S_GRID},
464         {"nogrid",      S_NOGRID},
465         {"major",       S_MAJOR},
466         {"minor",       S_MINOR},
467         {"color",       S_COLOR},
468         {"legendbox",   S_LEGENDBOX},
469         
470         {"no",          S_NO},
471         
472         {"textsize",    S_TEXTSIZE},
473 };
474
475 static const unsigned int NKEYS=(sizeof(keytab) / sizeof(struct key));
476
477 void 
478 EZPlot::initkw(void)
479 {
480         for (unsigned int i = 0; i < NKEYS; i++)
481                 if (! pol_install(keytab[i].keyword, keytab[i].code))
482                         sys_error(ERR_SEVERE, "error installing ezset keywords [initkw]");
483 }