File: variable.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (226 lines) | stat: -rw-r--r-- 6,920 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include	<string.h>
#include	<stdio.h>
#include        "gr.hh"
#include        "extern.hh"
#include        "private.hh"
#include        "Variable.hh"


static vector<GriVariable> variableStack;


// Make new variable
bool
create_variable(const char *name, double value)
{
    GriVariable newVariable(name, value);
    variableStack.push_back(newVariable);
    return true;
}

bool
show_variablesCmd()
{
    const int variables_per_line = 2; // how many columns?
    int             line_break = 0;
    bool            have_some = false;
    ShowStr("Variables...\n");
    vector<GriVariable>::iterator i;
    for (i = variableStack.begin(); i < variableStack.end(); i++) {
	extern char     _grTempString[];
	sprintf(_grTempString, "%25s=%-5g", i->getName(), i->getValue());
	ShowStr(_grTempString);
	if (!(++line_break % variables_per_line)) {
	    ShowStr("\n");
	} else {
	    ShowStr("  ");
	}
	have_some = true;
    }
    ShowStr("\n");
    if (!have_some) {
	ShowStr(" ... none exist\n");
    }
    return true;
}

// display unused user variables
void
display_unused_var()
{
    int i;
    unsigned stackLen = variableStack.size();
    extern char     _grTempString[];
    if (stackLen > 0) {
	char *name;
	for (i = stackLen - 1; i >= 0; i--) {
	    if (0 == variableStack[i].getCount()) {
		name = variableStack[i].getName();
		if (*(name + 1) != '.') { // avoid builtins
		    sprintf(_grTempString, "\
Warning: variable `%s' defined but not used\n", name);
		    ShowStr(_grTempString);
		}
	    }
	}
    }
}

// is_var - return 0 if not a variable, or 1 if is
bool
is_var(const char *w)
{
    int             len = strlen(w);
    return (len > 2 && w[0] == '.' && w[-1 + len] == '.' ? true : false);
}

// for internal debugging
void
show_var_stack()
{
    int i;
    unsigned stackLen = variableStack.size();
    if (stackLen > 0) {
	printf("Variable stack [\n");
	for (i = stackLen - 1; i >= 0; i--) {
	    printf("  %s = %f\n", 
		   variableStack[i].getName(),
		   variableStack[i].getValue());
	}
	printf("]\n");
    }
}

// Delete variable, searching from end of stack
bool
delete_var(const char *name)
{
    unsigned stackLen = variableStack.size();
    for (int i = stackLen - 1; i >= 0; i--) {
	if (!strcmp(name, variableStack[i].getName())) {
	    for (unsigned int j = i; j < stackLen - 1; j++)
		variableStack[j] = variableStack[j + 1];
	    variableStack.pop_back();
	    return true;
	}
    }
    return false;
}

// get_var() - get value of variable (incrementing the 'uses' flag)
// 
// RETURN true if variable is defined and has a value
// RETURN false otherwise
bool
get_var(const char *name, double *value)
{
    *value = 0.0;		// store something in case not found
    if (!is_var(name))
	return false;
    // Following are special cases.  They are not stored in the stack because
    // it would take far too much time.
    //
    // If more variables are added to this list, be sure to make changes to
    // gri.cc and extern.h, under comments which appear as follows:
    // 
    // The following globals have symbolic names associated with them, and
    // MUST be updated whenever these names are assigned to.  See the note in
    // put_var() in variable.c.  The reason for the parallel C storage is
    // that the following are accessed for every data point plotted. Certain
    // other symbolic variables (like ..publication.. for example) are not
    // accessed frequently, and hence have no parallel C storage as the
    // following do.  Thus they are safe against breakage.
    if (!strcmp(name, "..trace..")) {
	*value = (int) _griState.trace();
	return true;
    } else if (!strcmp(name, "..use_default_for_query..")) {
	*value = (int) _use_default_for_query;
	return true;
    } else if (!strcmp(name, "..linewidth..")) {
	*value = _griState.linewidth_line();
	return true;
    } else if (!strcmp(name, "..linewidthaxis..")) {
	*value = _griState.linewidth_axis();
	return true;
    } else if (!strcmp(name, "..linewidthsymbol..")) {
	*value = _griState.linewidth_symbol();
	return true;
    } else if (!strcmp(name, "..superuser..")) {
	*value = _griState.superuser();
	return true;
    } else {
	// Look it up in stack
	int             i;
	unsigned        stackLen = variableStack.size();
	if (stackLen > 0) {
	    for (i = stackLen - 1; i >= 0; i--) {
#ifdef DEBUG_VARIABLE
		printf("debug: check [%s] vs %d-th [%s]\n", name, i, variableStack[i].getName());
#endif
		if (!strcmp(name, variableStack[i].getName())) {
		    *value = variableStack[i].getValue();
		    variableStack[i].incrementCount(); // record the usage
		    return true;
		}
	    }
	}
    }
    return false;
}

// put_var() -- assign value to name, creating new variable if necessary.
// If replace_existing=0 replace existing value.
// If replace_existing=1 create a new variable with the old name.
// RETURN NULL if can't do it.
bool
put_var(const char *name, double value, bool replace_existing)
{
    void            reset_top_of_plot(void);
    int             i;
    unsigned        stackLen = variableStack.size();
    // put_var (): Certain special cases are stored in C variables too,
    // for speed. (An example is ..xleft.., which is stored as _xleft and
    // used all over the code.)  Capture these and store the values. Also,
    // some operations should be intercepted for general use later: e.g.
    // changing ..ymargin.. requires calling reset_top_of_plot(). Note
    // that this code fragment is easy to break, because addition of new
    // internals means addition of code here.  See also the note in gri.cc,
    // where the internals are segmented.
    if (!strcmp(name, "..trace..")) {
	_griState.set_trace((floor(0.5 + value)) ? true : false);
    } else if (!strcmp(name, "..use_default_for_query..")) {
	_use_default_for_query = int(floor(0.5 + value)) ? true : false;
    } else if (!strcmp(name, "..linewidth..")) {
	_griState.set_linewidth_line(value);
    } else if (!strcmp(name, "..linewidthaxis..")) {
	_griState.set_linewidth_axis(value);
    } else if (!strcmp(name, "..linewidthsymbol..")) {
	_griState.set_linewidth_symbol(value);
    } else if (!strcmp(name, "..superuser.."))
	_griState.set_superuser(int(floor(0.5 + value)));
    else if (!strcmp(name, "..xleft.."))
	_xleft = value;
    else if (!strcmp(name, "..xright.."))
	_xright = value;
    else if (!strcmp(name, "..ybottom.."))
	_ybottom = value;
    else if (!strcmp(name, "..ytop.."))
	_ytop = value;
    // Replace if on stack already.
    if (replace_existing) {
	if (stackLen) {
	    for (i = stackLen - 1; i >= 0; i--) {
		if (!strcmp(name, variableStack[i].getName())) {
		    variableStack[i].setValue(value);
		    return true;
		}
	    }
	}
    }
    // Store on end of stack.
    GriVariable     newVariable(name, value);
    variableStack.push_back(newVariable);
    if (!strcmp(name, "..ymargin..") || !strcmp(name, "..ysize.."))
	reset_top_of_plot();
    return true;
}