summaryrefslogtreecommitdiffstats
path: root/debian/fireflies/fireflies-2.08/libgfx/tests/t-script.cxx
blob: 42b9ad831d8a5143ec54500fb7ca63127b99d7cb (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
/************************************************************************

  Test the libgfx scripting facility.

  by Michael Garland, 2000.
  
  $Id: t-script.cxx 426 2004-09-27 04:34:55Z garland $

 ************************************************************************/

#include <gfx/gfx.h>
#include <gfx/script.h>
#include <gfx/vec3.h>

using namespace std;

// usage: add <x>*
//     Adds all numbers listed on the line and prints the result
// 
// usage: avg <x>*
//     Averages all numbers listed on the line and prints the result
//
int proc_add(const CmdLine &cmd)
{
    std::vector<double> values;
    double sum = 0.0;

    cmd.collect_as_numbers(values);
    std::vector<double>::size_type count;
    for(count=0; count<values.size(); count++)
	sum += values[count];

    if( cmd.opname() == "avg" && count>0 )
	sum /= (double)count;

    cout << sum << endl;
    return SCRIPT_OK;
}

// usage: vec3 <x> <y> <z>
//     Constructs a 3-vector and prints the result
int proc_vec3(const CmdLine &cmd)
{
    if( cmd.argcount() != 3 )  return SCRIPT_ERR_SYNTAX;

    Vec3 v;
    cmd.collect_as_numbers(v, 3);

    cout << v << endl;
    return SCRIPT_OK;
}

// usage: echo <msg>
//     Prints all the text following the command name verbatim
int proc_echo(const CmdLine &cmd)
{
    cout << cmd.argline() << endl;
    return SCRIPT_OK;
}

int main(int argc, char *argv[])
{
    CmdEnv env;

    env.register_command("add", proc_add);
    env.register_command("avg", proc_add);
    env.register_command("echo", proc_echo);
    env.register_command("vec3", proc_vec3);

    for(int i=1; i<argc; i++)
	env.do_file(argv[i]);

    return 0;
}