1 // scriptmgr.cxx - run user scripts
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
7 #include "scriptmgr.hxx"
13 #include <Main/globals.hxx>
14 #include <Main/fg_props.hxx>
20 ////////////////////////////////////////////////////////////////////////
22 ////////////////////////////////////////////////////////////////////////
25 do_print (int argc, pslValue * argv, pslProgram * p)
27 for (int i = 0; i < argc; i++) {
28 switch(argv[i].getType()) {
30 cout << argv[i].getInt();
33 cout << argv[i].getFloat();
36 cout << argv[i].getString();
42 cout << "(**bad value**)";
53 do_get_property (int argc, pslValue * argv, pslProgram * p)
56 SGPropertyNode * prop = fgGetNode(argv[0].getString());
58 switch (prop->getType()) {
59 case SGPropertyNode::BOOL:
60 case SGPropertyNode::INT:
61 case SGPropertyNode::LONG:
62 result.set(prop->getIntValue());
64 case SGPropertyNode::FLOAT:
65 case SGPropertyNode::DOUBLE:
66 result.set(prop->getFloatValue());
68 case SGPropertyNode::STRING:
69 case SGPropertyNode::UNSPECIFIED:
70 result.set(prop->getStringValue());
84 do_set_property (int argc, pslValue * argv, pslProgram * p)
87 SGPropertyNode * prop = fgGetNode(argv[0].getString(), true);
88 switch (argv[1].getType()) {
90 prop->setIntValue(argv[1].getInt());
93 prop->setFloatValue(argv[1].getFloat());
96 prop->setStringValue(argv[1].getString());
99 prop->setUnspecifiedValue("");
102 // TODO: report an error.
111 extensions[] = { {"print", -1, do_print},
112 {"get_property", 1, do_get_property},
113 {"set_property", 2, do_set_property},
118 ////////////////////////////////////////////////////////////////////////
119 // Implementation of FGScriptMgr.
120 ////////////////////////////////////////////////////////////////////////
122 FGScriptMgr::FGScriptMgr ()
126 FGScriptMgr::~FGScriptMgr ()
137 FGScriptMgr::update (double delta_time_sec)
142 FGScriptMgr::run (const char * script) const
144 // FIXME: detect and report errors
145 pslProgram program(extensions);
146 if (program.compile(script, globals->get_fg_root().c_str()) > 0)
148 while (program.step() != PSL_PROGRAM_END)
154 FGScriptMgr::run_inline (const char * script) const
156 string s = "int main () {\n";
158 s += "\n return 0;\n}\n";
159 return run(s.c_str());
163 // end of scriptmgr.cxx