1 // -*-c++-*-
2 #ifndef GRAP_H
3 #define GRAP_H
4 // This file is (c) 1998-2001 Ted Faber (faber@lunabase.org) see COPYRIGHT
5 // for the full copyright and limitations of liabilities.
6 #ifndef DEFINES
7 #define DEFINES "/usr/share/grap/grap.defines"
8 #endif
9 
10 #ifndef STDC_HEADERS
11 extern "C" {
12 #ifndef __GNUC__
13     size_t strlen(const char*);
14     char *strcpy(char *, const char *);
15     int strcmp(const char *, const char *);
16 #endif
17     char *strcat(char *, const char *);
18     char *strncpy(char *, const char *, const size_t);
19     int strncmp(const char *, const char *, const size_t);
20 };
21 #endif
22 
23 #ifndef HAVE_SNPRINTF
24 #include "snprintf.h"
25 #else
26 // AIX seems to have snprintf, but not prototype it..
27 #ifndef SNPRINTF_DECLARED
28 extern "C" {
29     int snprintf(char *, size_t, const char *, ...);
30 }
31 #endif
32 #endif
33 
34 using namespace std;
35 
36 #include <vector>
37 #include <string>
38 #include <list>
39 #include <algorithm>
40 
41 class DisplayString;
42 class line;
43 class coord;
44 class macro;
45 class plot;
46 class tick;
47 class grid;
48 class circle;
49 class shiftdesc;
50 class keyword;
51 
52 
53 #if HAVE_HASH_MAP||HAVE_EXT_HASH_MAP||HAVE_UNORDERED_MAP
54 
55 #ifdef HAVE_UNORDERED_MAP
56 #include <unordered_map>
57 
58 typedef HASH_SPACE::unordered_map<string, double *> doubleDictionary;
59 typedef HASH_SPACE::unordered_map<string, coord *> coordinateDictionary;
60 typedef HASH_SPACE::unordered_map<string, line *> lineDictionary;
61 typedef HASH_SPACE::unordered_map<string, macro *> macroDictionary;
62 typedef HASH_SPACE::unordered_map<string, keyword> keywordDictionary;
63 #else
64 #ifdef HAVE_HASH_MAP
65 #include <hash_map>
66 #else
67 #include <ext/hash_map>
68 #endif
69 // A functor for hashing strings - it is an adapter to get to the
70 // standard library char * hash function.
71 class Strhash : public unary_function<const string&, size_t> {
72 private:
73     HASH_SPACE::hash<const char *> h;
74 public:
operator()75     size_t operator()(const string& s) const {
76 	return h(s.c_str());
77     }
78 };
79 
80 typedef HASH_SPACE::hash_map<string, double *, Strhash> doubleDictionary;
81 typedef HASH_SPACE::hash_map<string, coord *, Strhash> coordinateDictionary;
82 typedef HASH_SPACE::hash_map<string, line *, Strhash> lineDictionary;
83 typedef HASH_SPACE::hash_map<string, macro *, Strhash> macroDictionary;
84 typedef HASH_SPACE::hash_map<string, keyword, Strhash> keywordDictionary;
85 #endif
86 
87 #else
88 #include <map>
89 typedef less<string> Strcmp;
90 
91 typedef map<string, double *, Strcmp> doubleDictionary;
92 typedef map<string, coord *, Strcmp> coordinateDictionary;
93 typedef map<string, line *, Strcmp> lineDictionary;
94 typedef map<string, macro *, Strcmp> macroDictionary;
95 typedef map<string, keyword, Strcmp> keywordDictionary;
96 #endif
97 
98 typedef list<plot *> plotSequence;
99 typedef vector<double> doublevec;
100 typedef list<double> doublelist;
101 typedef list<tick *> ticklist;
102 typedef list<grid *> gridlist;
103 typedef list<string *> linelist;
104 typedef list<string *> stringSequence;
105 typedef list<circle *> circleSequence;
106 typedef list<class grap_buffer_state*> lexStack;
107 typedef list<DisplayString *> stringlist;
108 typedef list<shiftdesc *> shiftlist;
109 
110 // number of functions taking 0,1,2 args.  The names of those
111 // functions are in grap_lex.l and the implementations in the
112 // jumptables in grap.y
113 const int NVF1=1;
114 const int NF0=2;
115 const int NF1=10;
116 const int NF2=3;
117 
118 enum size { ht = 0, wid};
119 
120 typedef struct {
121     int op;
122     double expr;
123 } bydesc;
124 
125 // this is complex enough to need a constructor/destructor
126 class for_descriptor {
127  public:
128     double *loop_var;
129     int dir;
130     double limit;
131     double by;
132     int by_op;
133     string *anything;
for_descriptor()134     for_descriptor() :
135 	loop_var(0), dir(0), limit(0.0), by(0.0), by_op(0), anything(0) { }
for_descriptor(double * lv,int d,double l,double b,int bo,string * a)136     for_descriptor(double *lv, int d, double l, double b, int bo, string *a) :
137 	loop_var(lv), dir(d), limit(l), by(b), by_op(bo), anything(a) { }
~for_descriptor()138     ~for_descriptor() {
139 	if ( anything) {
140 	    delete anything;
141 	    anything = 0;
142 	}
143     }
144 };
145 
146 typedef enum { GFILE=1,  GMACRO, GINTERNAL } grap_input;
147 
148 class grap_buffer_state {
149  public:
150     struct yy_buffer_state *yy;
151     for_descriptor *f;
152     string *name;
153     int line;
154     int report_start;
155     grap_input type;
156     int tokenpos;
grap_buffer_state()157     grap_buffer_state() :
158 	yy(0), f(0), name(0), line(0), report_start(0), type(GFILE),
159 	tokenpos(0) { }
160     grap_buffer_state(struct yy_buffer_state *yyb, for_descriptor *fo,
161 		      string *n, int l, int rs, grap_input t, int tp=0) :
yy(yyb)162 	yy(yyb), f(fo), name(n), line(l), report_start(rs), type(t),
163 	tokenpos(tp) { }
164     // this does *not* call yy_delete_buffer
~grap_buffer_state()165     ~grap_buffer_state() {
166         if ( f ) {
167 	    delete f;
168 	    f = 0;
169 	}
170 	if ( name ) {
171 	    delete name;
172 	    name = 0;
173 	}
174     }
175 };
176 
177 
178 extern lexStack lexstack;
179 
180 // Set the coarse and fine limits for double comparisons.  COARSE comparisons
181 // are always within one millionth.  If fine comarisons are requested, either
182 // the values in limits are used or one trillionth (1e-12).
183 #ifdef HAVE_LIMITS
184 #include <limits>
185 #define FINE_EPSILON numeric_limits<double>::epsilon()
186 #define FINE_MIN_DOUBLE numeric_limits<double>::min()
187 #define COARSE_EPSILON		1e-6
188 #define COARSE_MIN_DOUBLE	1e-6
189 #else
190 #define FINE_EPSILON		1e-12
191 #define FINE_MIN_DOUBLE		1e-12
192 #define COARSE_EPSILON		1e-6
193 #define COARSE_MIN_DOUBLE	1e-6
194 #endif
195 
196 extern double epsilon;
197 extern double min_double;
198 
199 
200 #ifdef HAVE_RANDOM
201 #ifndef RANDOM_DECLARED
202 extern "C" {
203     long random();
204     void srandom(unsigned long);
205 }
206 #endif
207 #else
208 #ifdef HAVE_RAND
209 #define random rand
210 #define srandom srand
211 #ifndef RAND_DECLARED
212 extern "C" {
213     long rand();
214     void srand(unsigned);
215 }
216 #endif
217 #endif
218 #endif
219 
220 #endif
221