#include #include #include #include #include char * getCommandLineOption(int argc, char **argv, char const *option) { int slen = strlen(option); char *rv = 0; int i; for (i=0; i\t\taircraft configuration file to update\n"); printf("\n"); exit(-1); } char *strlwr(char *str) { unsigned char *p = (unsigned char *)str; while (*p) { *p = tolower((unsigned char)*p); p++; } return str; } /* -- JSBSim ---------------------------------------------------------------- */ const char* jsb_wing_tag(void *xid) { void *xmid = xmlNodeGet(xid, "/fdm_config/metrics"); void *xlid = xmlMarkId(xmid); double aero_z = 0.0; double eye_z = 0.0; int i, num; num = xmlNodeGetNum(xmid, "location"); for (i=0; i eye_z) return "high-wing"; else return "low-wing"; } const char* jsb_gear_tag(void *xid) { void *xgid = xmlNodeGet(xid, "/fdm_config/ground_reactions"); void *xcid = xmlMarkId(xgid); double nose_x = 0.0; double main_x = 0.0; int bogeys = 0; int i, num; num = xmlNodeGetNum(xgid, "contact"); for (i=0; i 0.0) { rv = "normal-steering"; break; } else { rv = "no-steering"; } } } } } xmlFree(xcid); xmlFree(xgid); return rv; } const char* jsb_engines_tag(void *xid) { void *xpid = xmlNodeGet(xid, "/fdm_config/propulsion"); void *xeid = xmlMarkId(xpid); const char* rv = "multi-engine"; int engines = 0; int i, num; char *main; num = xmlNodeGetNum(xpid, "engine"); for (i=0; i\n"); printf(" %s\n", strlwr(aero)); printf(" %s\n", jsb_wing_tag(xid)); printf(" %s\n", jsb_gear_tag(xid)); printf(" %s\n", jsb_gear_retract_tag(xid)); printf(" %s\n", jsb_gear_steering_tag(xid)); printf(" %s\n", jsb_engines_tag(xid)); tag = jsb_engine_tag(xid, path); if (tag) printf(" %s\n", tag); tag = jsb_propulsion_tag(xid, path); if (tag) printf(" %s\n", tag); tag = jsb_thruster_tag(xid, path); if (tag) printf(" %s\n", tag); printf(" \n"); xmlClose(xid); free(fname); } /* -- Yasim ----------------------------------------------------------------- */ const char* yasim_wing_tag(void *xid) { void *xwid, *xcid; double wing_z = 0.0; double eye_z = 0.0; if (xmlNodeTest(xid, "/airplane/rotor")) { return "helicopter"; } xwid = xmlNodeGet(xid, "/airplane/wing"); if (xwid) { wing_z = xmlAttributeGetDouble(xwid, "z"); xmlFree(xwid); } xcid = xmlNodeGet(xid, "/airplane/cockpit"); if (xcid) { eye_z = xmlAttributeGetDouble(xcid, "z"); xmlFree(xcid); } if (wing_z > eye_z) return "high-wing"; else return "low-wing"; } const char* yasim_gear_tag(void *xid) { void *xaid = xmlNodeGet(xid, "/airplane"); void *xgid = xmlMarkId(xaid); double nose_x = 0.0; double main_x = 0.0; int gears = 0; int i, num; num = xmlNodeGetNum(xaid, "gear"); for (i=0; i nose_x) return "tail-dragger"; return "tricycle"; } const char* yasim_gear_retract_tag(void *xid) { void *xaid = xmlNodeGet(xid, "/airplane"); void *xgid = xmlMarkId(xaid); char *rv = "fixed-gear"; int found = 0; int i, num; num = xmlNodeGetNum(xaid, "gear"); for (i=0; i 0.0) { rv = "afterburner"; } xmlFree(xpid); } else if ((xpid = xmlNodeGet(xid, "/airplane/propeller/piston-engine")) != NULL) { if (xmlAttributeGetInt(xpid, "supercharger") > 0) { rv = "supercharger"; } xmlFree(xpid); } return rv; } const char* yasim_thruster_tag(void *xid, char *path) { const char* rv = NULL; void *xtid; xtid = xmlNodeGet(xid, "/airplane/propeller/control-input"); if (xtid) { if (!xmlAttributeCompareString(xtid, "control", "ADVANCE")) { rv = "variable-pitch"; } else rv = "fixed-pitch"; xmlFree(xtid); } return rv; } void update_metadata_yasim(char *path, char *aero) { const char *tag; char *fname; void *xid; fname = calloc(1, strlen(path)+strlen(aero)+strlen(".xml")+1); if (!fname) { printf("Unable to allocate memory\n"); return; } memcpy(fname, path, strlen(path)); memcpy(fname+strlen(path), aero, strlen(aero)); memcpy(fname+strlen(path)+strlen(aero), ".xml", strlen(".xml")); xid = xmlOpen(fname); if (!xid) { printf("Yasim aero file not found: '%s'\n", fname); return; } printf(" \n"); printf(" %s\n", strlwr(aero)); printf(" %s\n", yasim_wing_tag(xid)); printf(" %s\n", yasim_gear_tag(xid)); printf(" %s\n", yasim_gear_retract_tag(xid)); printf(" %s\n", yasim_gear_steering_tag(xid)); printf(" %s\n", yasim_engines_tag(xid)); tag = yasim_engine_tag(xid, path); if (tag) printf(" %s\n", tag); tag = yasim_propulsion_tag(xid, path); if (tag) printf(" %s\n", tag); tag = yasim_thruster_tag(xid, path); if (tag) printf(" %s\n", tag); printf(" \n"); xmlClose(xid); free(fname); } /* -------------------------------------------------------------------------- */ void update_metadata(const char *fname) { void *xsid, *xid = xmlOpen(fname); if (!xid) { printf("File not found: '%s'\n", fname); return; } xsid = xmlNodeGet(xid, "/PropertyList/sim"); if (!xsid) { printf("path '/PropertyList/sim' not found in '%s'\n", fname); xmlClose(xid); return; } if (xmlNodeTest(xsid, "tags") == 0) { char *str = NULL; char *path, *pend; pend = strrchr(fname, '/'); if (!pend) pend = strrchr(fname, '\\'); if (!pend) path = strdup("./"); else { pend++; path = calloc(1, pend-fname+1); memcpy(path, fname, pend-fname); } if (!xmlNodeCompareString(xsid, "flight-model", "jsb")) { str = xmlNodeGetString(xsid, "aero"); update_metadata_jsb(path, str); } else if (!xmlNodeCompareString(xsid, "flight-model", "yasim")) { str = xmlNodeGetString(xsid, "aero"); update_metadata_yasim(path, str); } else { str = xmlNodeGetString(xsid, "flight-model"); printf("The '%s' flightmodel is unsupported at this time\n", str); } xmlFree(xsid); xmlFree(str); free(path); } else { printf("'%s' already contains metadata tags\n", fname); } xmlClose(xid); } int main(int argc, char **argv) { const char *setFile; if (argc == 1 || getCommandLineOption(argc, argv, "-h") || getCommandLineOption(argc, argv, "--help")) { show_help(); } setFile = getCommandLineOption(argc, argv, "-i"); if (!setFile) { setFile = getCommandLineOption(argc, argv, "-input"); } if (!setFile) { show_help(); } printf("%s\n", setFile); update_metadata(setFile); printf("\n"); return 0; }