summaryrefslogtreecommitdiffstats
path: root/debian/fireflies/fireflies-2.08/libgfx/src/script.cxx
blob: 5f1bdff26d1d802f428901005b8dcf70fabd0ca2 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <gfx/script.h>
#include <fstream>

#if !defined(HAVE_SSTREAM) && defined(HAVE_STRSTREAM)
#  include <strstream>
typedef istrstream istringstream;
#else
#  include <sstream>
#endif

#ifdef HAVE_GZSTREAM
#  include <gzstream.h>
#endif

namespace gfx
{

using namespace std;


////////////////////////////////////////////////////////////////////////
//
// CmdLine methods -- These implement all the basic line manipulations
//

string CmdLine::token_to_string(int i) const { return substr(tokens[i]); }

string CmdLine::rest_to_string(int i) const
{
    return line.substr(tokens[i].first);
}

double CmdLine::token_to_double(int i) const
    { string str = substr(tokens[i]); return atof(str.c_str()); }

float CmdLine::token_to_float(int i) const
    { string str = substr(tokens[i]); return atof(str.c_str()); }

int CmdLine::token_to_int(int i) const
    { string str = substr(tokens[i]); return atoi(str.c_str()); }

string CmdLine::argline() const
{
    if( argcount() == 0 )  return "";

    index_type start = tokens.front().first;
    index_type end = tokens.back().second;
    return substr(range_type(start, end));
}

int CmdLine::collect_as_strings(vector<string> &v, int offset) const
{
    for(int i=offset; i<tokens.size(); i++)
	v.push_back( substr(tokens[i]) );
    return tokens.size();
}

int CmdLine::collect_as_numbers(vector<double> &v, int offset) const
{
    for(int i=offset; i<tokens.size(); i++)
	v.push_back(token_to_double(i));
    return tokens.size();
}

int CmdLine::collect_as_numbers(vector<int> &v, int offset) const
{
    for(int i=offset; i<tokens.size(); i++)
	v.push_back(token_to_int(i));
    return tokens.size();
}

int CmdLine::collect_as_numbers(double *v, int size, int offset) const
{
    int i;
    for(i=0; (i+offset)<tokens.size() && i<size; i++)
	v[i] = token_to_double(i+offset);
    return i;
}

int CmdLine::collect_as_numbers(float *v, int size, int offset) const
{
    int i;
    for(i=0; (i+offset)<tokens.size() && i<size; i++)
	v[i] = token_to_float(i+offset);
    return i;
}

int CmdLine::collect_as_numbers(int *v, int size, int offset) const
{
    int i;
    for(i=0; (i+offset)<tokens.size() && i<size; i++)
	v[i] = token_to_int(i+offset);
    return i;
}

////////////////////////////////////////////////////////////////////////
//
// CmdEnv methods -- Minimal interface supported by all environments
//

void CmdEnv::register_command(const std::string& name, CmdObject *fn)
{
    script_commands[name] = fn;
}

void CmdEnv::register_command(const std::string& name, CmdHandler proc)
{
    register_command(name, new CmdFunction(proc));
}

CmdObject *CmdEnv::lookup_command(const std::string& name)
{
    CmdTable::const_iterator iter = script_commands.find(name);

    return iter!=script_commands.end() ? iter->second : NULL;
}

static int ignored(const CmdLine& line) { return SCRIPT_OK; }

void CmdEnv::ignore_command(const std::string& name)
{
    register_command(name, ignored);
}

void CmdEnv::register_vocabulary(const std::string& name, CmdEnv *env)
{
    register_method(name, env, &CmdEnv::script_eval);
}

CmdEnv::CmdEnv()
{
    register_method("include", this, &CmdEnv::script_include);
    register_method("ignore", this, &CmdEnv::script_ignore);
    register_method("end", this, &CmdEnv::script_end);
}

CmdEnv::~CmdEnv()
{
    // Free all the CmdObject's held by script_commands
    //
    for(CmdTable::iterator i = script_commands.begin();
	i != script_commands.end(); ++i)
    {
	CmdObject *obj = i->second;
	delete obj;
    }
}

int CmdEnv::script_include(const CmdLine& cmd)
{
    if( cmd.argcount() != 1 )  return SCRIPT_ERR_SYNTAX;

    string filename = cmd.token_to_string(0);
    return do_file(cmd.token_to_string(0));
}

int CmdEnv::script_ignore(const CmdLine& cmd)
{
    for(int i=0; i<cmd.argcount(); ++i)
    {
	string name = cmd.token_to_string(i);
	ignore_command(name);
    }

    return SCRIPT_OK;
}

int CmdEnv::script_end(const CmdLine& cmd)
{
    return SCRIPT_END;
}

int CmdEnv::script_eval(const CmdLine& cmd)
{
    return do_line(cmd.argline());
}

void CmdEnv::begin_scope(CmdEnv *sub) { scopes.push_back(sub); }

void CmdEnv::end_scope()
{
    if( scopes.size() > 0 )
    {
	CmdEnv *sub = scopes.back();
	scopes.pop_back();
	delete sub;
    }
}

////////////////////////////////////////////////////////////////////////
//
// Toplevel functions -- extract and execute scripting commands
//

int CmdEnv::do_line(const string &line)
{
    CmdEnv& env = *this;

    // Pass this line off to the sub-scope (if any)
    if( scopes.size() > 0 && scopes.back() )
    {
	int rc = scopes.back()->do_line(line);
	if( rc==SCRIPT_END )
	{
	    end_scope();
	    rc = SCRIPT_OK;
	}
	return rc;
    }

    const char *ws = " \t\n\r";
    string::size_type start, end;

    // First, process the initial (command) token
    start = line.find_first_not_of(ws);

    // Only continue processing if line is a non-empty, non-comment line
    if( start!=string::npos && line[start]!='#' )
    {
	end = line.find_first_of(ws, start);
	string op = line.substr(start, end-start);

	CmdObject *fn = env.lookup_command(op);
	if( !fn )  return SCRIPT_ERR_UNDEF;

	CmdLine argv(line);
	argv.op = CmdLine::range_type(start, end);

	while(1)
	{
	    start = line.find_first_not_of(ws, end);
	    if( start==string::npos )  break;
	    end = line.find_first_of(ws, start);

	    argv.tokens.push_back( CmdLine::range_type(start, end) );
	}

	return (*fn)(argv);
    }

    return SCRIPT_OK;
}

int CmdEnv::do_stream(istream &in)
{
    string line;

    while( !in.eof() )
    {
	getline(in, line);
	if( in.fail() )  break;

	int rc = do_line(line);
	if( rc != SCRIPT_OK )
	{
	    cerr << "Script Error: " << line << endl;
	    return rc;
	}
    }

    return SCRIPT_OK;
}

int CmdEnv::do_file(const std::string& filename)
{
#ifdef HAVE_GZSTREAM
    if( !filename.compare(filename.size()-3, 3, ".gz") ||
        !filename.compare(filename.size()-2, 2, ".z")  ||
        !filename.compare(filename.size()-2, 2, ".Z")  )
    {
	igzstream in(filename.c_str());
	if( in.good() )  return do_stream(in);
	else             return SCRIPT_ERR_NOFILE;
    }
    else
    {
	ifstream in(filename.c_str());
	if( in.good() )  return do_stream(in);
	else             return SCRIPT_ERR_NOFILE;
    }
#else
    ifstream in(filename.c_str());
    if( in.good() )  return do_stream(in);
    else             return SCRIPT_ERR_NOFILE;
#endif
}

int CmdEnv::do_string(const std::string& str)
{
    istringstream in(str.c_str());
    if( in.good() )  return CmdEnv::do_stream(in);
    else             return SCRIPT_ERR_NOFILE;
}

} // namespace gfx