X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FScripting%2FNasalSys.cxx;h=991147e790c5619d3b4e0ad14e24f86fe1f47612;hb=50e19ee23970db9bf7314d74f0ed223d8bf702c0;hp=1e832d45f3dc5bc9a98badd3f9686b9867d1cb68;hpb=99885791e99df4cc1cb5214271ed06bea4cd318d;p=flightgear.git diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index 1e832d45f..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" @@ -355,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 }, @@ -387,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 } }; @@ -508,7 +620,7 @@ void FGNasalSys::loadPropertyScripts() loadModule(p, module); } */ - + const char* src = n->getStringValue("script"); if(!n->hasChild("script")) src = 0; // Hrm... if(src) @@ -836,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); +} + +