Debian Codegraph @

libctgraphics/ezset.cpp

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