X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FScripting%2FNasalSys.cxx;h=991147e790c5619d3b4e0ad14e24f86fe1f47612;hb=50e19ee23970db9bf7314d74f0ed223d8bf702c0;hp=133d8f7407c973f36b916da949589f9284a37ee1;hpb=738b40158d7fa6e3d2e7475e646490d9441efb84;p=flightgear.git diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 133d8f740..991147e79 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -19,10 +20,13 @@ #include #include #include +#include #include +#include #include
#include
+#include #include "NasalSys.hxx" @@ -252,10 +256,17 @@ static naRef f_print(naContext c, naRef me, int argc, naRef* args) // an argument. static naRef f_fgcommand(naContext c, naRef me, int argc, naRef* args) { - if(argc < 2 || !naIsString(args[0]) || !naIsGhost(args[1])) + naRef cmd = argc > 0 ? args[0] : naNil(); + naRef props = argc > 1 ? args[1] : naNil(); + if(!naIsString(cmd) || (!naIsNil(props) && !naIsGhost(props))) naRuntimeError(c, "bad arguments to fgcommand()"); - naRef cmd = args[0], props = args[1]; - SGPropertyNode_ptr* node = (SGPropertyNode_ptr*)naGhost_ptr(props); + SGPropertyNode_ptr tmp, *node; + if(!naIsNil(props)) + node = (SGPropertyNode_ptr*)naGhost_ptr(props); + else { + tmp = new SGPropertyNode(); + node = &tmp; + } return naNum(globals->get_commands()->execute(naStr_data(cmd), *node)); } @@ -348,6 +359,40 @@ static naRef f_directory(naContext c, naRef me, int argc, naRef* args) return result; } +// Parse XML file. +// parsexml( [, [, [, [, ]]]]); +// +// ... absolute path of an XML file +// ... callback function with two args: tag name, attribute hash +// ... callback function with one arg: tag name +// ... callback function with one arg: data +// ... callback function with two args: target, data +// (pi = "processing instruction") +// All four callback functions are optional and default to nil. +// The function returns nil on error, and the file name otherwise. +static naRef f_parsexml(naContext c, naRef me, int argc, naRef* args) +{ + if(argc < 1 || !naIsString(args[0])) + naRuntimeError(c, "parsexml(): path argument missing or not a string"); + if(argc > 5) argc = 5; + for(int i=1; iget_scenery()->get_elevation_m(lat, lon, 10000.0, elev, &mat)) + return naNil(); + naRef vec = naNewVector(c); + naVec_append(vec, naNum(elev)); + naRef matdata = naNil(); + if(mat) { + matdata = naNewHash(c); + naRef names = naNewVector(c); + const vector n = mat->get_names(); + for(unsigned int i=0; i(n[i].c_str()), n[i].size())); + HASHSET("names", 5, names); + HASHSET("solid", 5, naNum(mat->get_solid())); + HASHSET("friction_factor", 15, naNum(mat->get_friction_factor())); + HASHSET("rolling_friction", 16, naNum(mat->get_rolling_friction())); + HASHSET("load_resistance", 15, naNum(mat->get_load_resistance())); + HASHSET("bumpiness", 9, naNum(mat->get_bumpiness())); + HASHSET("light_coverage", 14, naNum(mat->get_light_coverage())); + } + naVec_append(vec, matdata); + return vec; +#undef HASHSET +} + // Table of extension functions. Terminate with zeros. static struct { char* name; naCFunction func; } funcs[] = { { "getprop", f_getprop }, @@ -380,7 +495,11 @@ static struct { char* name; naCFunction func; } funcs[] = { { "rand", f_rand }, { "srand", f_srand }, { "directory", f_directory }, + { "parsexml", f_parsexml }, { "systime", f_systime }, + { "carttogeod", f_carttogeod }, + { "geodtocart", f_geodtocart }, + { "geodinfo", f_geodinfo }, { 0, 0 } }; @@ -501,7 +620,7 @@ void FGNasalSys::loadPropertyScripts() loadModule(p, module); } */ - + const char* src = n->getStringValue("script"); if(!n->hasChild("script")) src = 0; // Hrm... if(src) @@ -829,3 +948,59 @@ FGNasalModelData::~FGNasalModelData() } + +// NasalXMLVisitor class: handles EasyXML visitor callback for parsexml() +// +NasalXMLVisitor::NasalXMLVisitor(naContext c, int argc, naRef* args) : + _c(naSubContext(c)), + _start_element(argc > 1 ? args[1] : naNil()), + _end_element(argc > 2 ? args[2] : naNil()), + _data(argc > 3 ? args[3] : naNil()), + _pi(argc > 4 ? args[4] : naNil()) +{ +} + +void NasalXMLVisitor::startElement(const char* tag, const XMLAttributes& a) +{ + if(naIsNil(_start_element)) return; + naRef attr = naNewHash(_c); + for(int i=0; i(s), + n < 0 ? strlen(s) : n); +} + +