| 1 |
|
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <ctype.h> |
| 6 |
|
| 7 |
#include <xml.h> |
| 8 |
|
| 9 |
#define TS (4*1024*1024) |
| 10 |
|
| 11 |
#define PRINT(a) \ |
| 12 |
tag = (a); if (tag) { strncat(s, " <tag>", TS-strlen(s)); strncat(s, tag, TS-strlen(s)); strncat(s, "</tag>\n", TS-strlen(s)); } |
| 13 |
|
| 14 |
static unsigned int tags_level = 0; |
| 15 |
static unsigned int print_tags = 0; |
| 16 |
|
| 17 |
char * |
| 18 |
getCommandLineOption(int argc, char **argv, char const *option) |
| 19 |
{ |
| 20 |
int slen = strlen(option); |
| 21 |
char *rv = 0; |
| 22 |
int i; |
| 23 |
|
| 24 |
for (i=0; i<argc; i++) |
| 25 |
{ |
| 26 |
if (strncmp(argv[i], option, slen) == 0) |
| 27 |
{ |
| 28 |
rv = ""; |
| 29 |
i++; |
| 30 |
if (i<argc) rv = argv[i]; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return rv; |
| 35 |
} |
| 36 |
|
| 37 |
void |
| 38 |
show_help() |
| 39 |
{ |
| 40 |
printf("Usage: metdata [options]\n"); |
| 41 |
printf("Updates the metadata of a FlightGear aircraft configuration.\n"); |
| 42 |
printf("\nOptions:\n"); |
| 43 |
printf(" -i, --input <file>\t\taircraft configuration file to update\n"); |
| 44 |
|
| 45 |
printf("\n"); |
| 46 |
exit(-1); |
| 47 |
} |
| 48 |
|
| 49 |
char *strlwr(char *str) |
| 50 |
{ |
| 51 |
unsigned char *p = (unsigned char *)str; |
| 52 |
|
| 53 |
while (*p) { |
| 54 |
*p = tolower((unsigned char)*p); |
| 55 |
p++; |
| 56 |
} |
| 57 |
|
| 58 |
return str; |
| 59 |
} |
| 60 |
|
| 61 |
void print_xml(FILE *fd, void *id, char *tags) |
| 62 |
{ |
| 63 |
static int level = 1; |
| 64 |
void *xid = xmlMarkId(id); |
| 65 |
unsigned int num; |
| 66 |
|
| 67 |
num = xmlNodeGetNum(xid, "*"); |
| 68 |
if (num == 0) |
| 69 |
{ |
| 70 |
char *s; |
| 71 |
s = xmlGetString(xid); |
| 72 |
if (s) |
| 73 |
{ |
| 74 |
fprintf(fd, "%s", s); |
| 75 |
free(s); |
| 76 |
} |
| 77 |
} |
| 78 |
else |
| 79 |
{ |
| 80 |
unsigned int i, q; |
| 81 |
for (i=0; i<num; i++) |
| 82 |
{ |
| 83 |
if (xmlNodeGetPos(id, xid, "*", i) != 0) |
| 84 |
{ |
| 85 |
unsigned int anum, a; |
| 86 |
char name[256]; |
| 87 |
xmlNodeCopyName(xid, (char *)&name, 256); |
| 88 |
|
| 89 |
if (tags_level == 1 && !strcmp(name, "sim")) tags_level++; |
| 90 |
|
| 91 |
fprintf(fd, "\n"); |
| 92 |
for(q=0; q<level; q++) fprintf(fd, " "); |
| 93 |
fprintf(fd, "<%s", name); |
| 94 |
|
| 95 |
anum = xmlAttributeGetNum(xid); |
| 96 |
for (a=0; a<anum; a++) |
| 97 |
{ |
| 98 |
char name[256], attrib[256]; |
| 99 |
if (xmlAttributeCopyName(xid, (char *)&name, 256, a) != 0) |
| 100 |
{ |
| 101 |
xmlAttributeCopyString(xid, name, (char *)&attrib, 256); |
| 102 |
fprintf(fd, " %s=\"%s\"", name, attrib); |
| 103 |
} |
| 104 |
} |
| 105 |
fprintf(fd, ">"); |
| 106 |
|
| 107 |
level++; |
| 108 |
print_xml(fd, xid, tags); |
| 109 |
|
| 110 |
level--; |
| 111 |
fprintf(fd, "</%s>", name); |
| 112 |
|
| 113 |
if (tags_level == 2 && !strcmp(name, "aero")) { |
| 114 |
fprintf(fd, "\n%s\n", tags); |
| 115 |
} |
| 116 |
} |
| 117 |
else printf("error\n"); |
| 118 |
} |
| 119 |
fprintf(fd, "\n"); |
| 120 |
for(q=1; q<level; q++) fprintf(fd, " "); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
updateFile(char *fname, char *tags) |
| 126 |
{ |
| 127 |
char bfname[4096]; |
| 128 |
FILE *fd = NULL; |
| 129 |
void *rid; |
| 130 |
|
| 131 |
snprintf(bfname, 4096, "%s.bak", fname); |
| 132 |
rename(fname, bfname); |
| 133 |
|
| 134 |
rid = xmlOpen(bfname); |
| 135 |
if (rid) |
| 136 |
{ |
| 137 |
fd = fopen(fname, "w"); |
| 138 |
if (!fd) |
| 139 |
{ |
| 140 |
xmlClose(rid); |
| 141 |
rid = NULL; |
| 142 |
rename(bfname, fname); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (rid) |
| 147 |
{ |
| 148 |
unsigned int i, num; |
| 149 |
void *xid; |
| 150 |
|
| 151 |
fprintf(fd, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); |
| 152 |
|
| 153 |
xid = xmlMarkId(rid); |
| 154 |
num = xmlNodeGetNum(xid, "*"); |
| 155 |
for (i=0; i<num; i++) |
| 156 |
{ |
| 157 |
if (xmlNodeGetPos(rid, xid, "*", i) != 0) |
| 158 |
{ |
| 159 |
unsigned int anum, a; |
| 160 |
char name[256]; |
| 161 |
xmlNodeCopyName(xid, (char *)&name, 256); |
| 162 |
|
| 163 |
if (strcmp(name, "PropertyList")) break; |
| 164 |
|
| 165 |
tags_level++; |
| 166 |
fprintf(fd, "<%s", name); |
| 167 |
anum = xmlAttributeGetNum(xid); |
| 168 |
for (a=0; a<anum; a++) |
| 169 |
{ |
| 170 |
char name[256], attrib[256]; |
| 171 |
if (xmlAttributeCopyName(xid, (char *)&name, 256, a) != 0) |
| 172 |
{ |
| 173 |
xmlAttributeCopyString(xid, name, (char *)&attrib, 256); |
| 174 |
fprintf(fd, " %s=\"%s\"", name, attrib); |
| 175 |
} |
| 176 |
} |
| 177 |
fprintf(fd, ">"); |
| 178 |
print_xml(fd, xid, tags); |
| 179 |
fprintf(fd, "\n</%s>\n", name); |
| 180 |
} |
| 181 |
} |
| 182 |
free(xid); |
| 183 |
|
| 184 |
xmlClose(rid); |
| 185 |
fclose(fd); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
const char* |
| 190 |
vendor_tag(char *desc) |
| 191 |
{ |
| 192 |
char *p = strlwr(desc); |
| 193 |
if (strstr(p, "boeing")) return "boeing"; |
| 194 |
if (strstr(p, "airbus")) return "airbus"; |
| 195 |
if (strstr(p, "antonov")) return "antonov"; |
| 196 |
if (strstr(p, "tupolev")) return "tupolev"; |
| 197 |
if (strstr(p, "ilyoushin")) return "ilyoushin"; |
| 198 |
if (strstr(p, "bombardier")) return "bombardier"; |
| 199 |
if (strstr(p, "fokker")) return "fokker"; |
| 200 |
if (strstr(p, "lockheed")) return "lockheed"; |
| 201 |
if (strstr(p, "general-dynamics")) return "general-dynamics"; |
| 202 |
if (strstr(p, "general dynamics")) return "general-dynamics"; |
| 203 |
if (strstr(p, "mig")) return "mikoyan-gurevich"; |
| 204 |
if (strstr(p, "mikoyan gurevich")) return "mikoyan-gurevich"; |
| 205 |
if (strstr(p, "mikoyan-gurevich")) return "mikoyan-gurevich"; |
| 206 |
if (strstr(p, "sukoi")) return "sukoi"; |
| 207 |
if (strstr(p, "cessna")) return "cessna"; |
| 208 |
if (strstr(p, "fairchild")) return "fairchild"; |
| 209 |
if (strstr(p, "dassult")) return "dassult"; |
| 210 |
if (strstr(p, "dornier")) return "dornier"; |
| 211 |
if (strstr(p, "arado")) return "arado"; |
| 212 |
if (strstr(p, "schleicher")) return "schleicher"; |
| 213 |
if (strstr(p, "avro")) return "avro"; |
| 214 |
if (strstr(p, "saab")) return "saab"; |
| 215 |
if (strstr(p, "dassault")) return "dassault"; |
| 216 |
if (strstr(p, "aermacchi")) return "aermacchi"; |
| 217 |
if (strstr(p, "arsenal")) return "arsenal"; |
| 218 |
if (strstr(p, "rockwell")) return "rockwell"; |
| 219 |
if (strstr(p, "northrop")) return "northrop"; |
| 220 |
if (strstr(p, "grumman")) return "grumman"; |
| 221 |
if (strstr(p, "mc donnell") && strstr(p, "douglas")) return "mc-donnell-douglas"; |
| 222 |
if (strstr(p, "douglas")) return "douglas"; |
| 223 |
if (strstr(p, "mc donnell")) return "mc-donnell"; |
| 224 |
if (strstr(p, "mc-donnell")) return "mc-donnell"; |
| 225 |
|
| 226 |
if (strstr(p, "junkers")) return "junkers"; |
| 227 |
if (strstr(p, "kawasaki")) return "kawasaki"; |
| 228 |
if (strstr(p, "de havilland")) return "de-havilland"; |
| 229 |
if (strstr(p, "de-havilland")) return "de-havilland"; |
| 230 |
if (strstr(p, "diamond")) return "diamond"; |
| 231 |
if (strstr(p, "bell")) return "bell"; |
| 232 |
if (strstr(p, "hughes")) return "hughes"; |
| 233 |
if (strstr(p, "kamov")) return "kamov"; |
| 234 |
if (strstr(p, "mil")) return "mil"; |
| 235 |
if (strstr(p, "eurocopter")) return "eurocopter"; |
| 236 |
if (strstr(p, "alouette")) return "alouette"; |
| 237 |
if (strstr(p, "aerospatiale")) return "aerospatiale"; |
| 238 |
if (strstr(p, "sikorsky")) return "sikorsky"; |
| 239 |
if (strstr(p, "bernard")) return "bernard"; |
| 240 |
if (strstr(p, "bleriot")) return "bleriot"; |
| 241 |
if (strstr(p, "bristol")) return "bristol"; |
| 242 |
if (strstr(p, "breguet")) return "breguet"; |
| 243 |
if (strstr(p, "wright")) return "wright"; |
| 244 |
if (strstr(p, "breda")) return "breda"; |
| 245 |
if (strstr(p, "rutan")) return "rutan"; |
| 246 |
if (strstr(p, "vought")) return "vought"; |
| 247 |
if (strstr(p, "fiat")) return "fiat"; |
| 248 |
if (strstr(p, "focke wulf")) return "focke-wulf"; |
| 249 |
if (strstr(p, "focke-wulf")) return "focke-wulf"; |
| 250 |
if (strstr(p, "gloster")) return "gloster"; |
| 251 |
if (strstr(p, "hawker")) return "hawker"; |
| 252 |
if (strstr(p, "heinkel")) return "heinkel"; |
| 253 |
if (strstr(p, "messerschmitt")) return "messerschmitt"; |
| 254 |
if (strstr(p, "north american")) return "north-american"; |
| 255 |
if (strstr(p, "north-american")) return "north-american"; |
| 256 |
if (strstr(p, "piaggio")) return "piaggio"; |
| 257 |
if (strstr(p, "pilatus")) return "pilatus"; |
| 258 |
if (strstr(p, "supermarine")) return "supermarine"; |
| 259 |
if (strstr(p, "beechcraft")) return "beechcraft"; |
| 260 |
if (strstr(p, "beech")) return "beechcraft"; |
| 261 |
if (strstr(p, "vickers")) return "vickers"; |
| 262 |
if (strstr(p, "westland")) return "westland"; |
| 263 |
if (strstr(p, "yakovlev")) return "yakovlev"; |
| 264 |
if (strstr(p, "schweizer")) return "schweizer"; |
| 265 |
|
| 266 |
|
| 267 |
if (strstr(p, "b7")) return "boeing"; |
| 268 |
if (strstr(p, "airbus")) return "airbus"; |
| 269 |
if (strstr(p, "an-")) return "antonov"; |
| 270 |
if (strstr(p, "tu-")) return "tupolev"; |
| 271 |
if (strstr(p, "il-")) return "ilyoushin"; |
| 272 |
if (strstr(p, "su-")) return "sukoi"; |
| 273 |
if (strstr(p, "dc-")) return "douglas"; |
| 274 |
if (strstr(p, "md-")) return "mc-donnell-douglas"; |
| 275 |
if (strstr(p, "ju-")) return "junkers"; |
| 276 |
if (strstr(p, "ec-")) return "eurocopter"; |
| 277 |
if (strstr(p, "he-")) return "heinkel"; |
| 278 |
if (strstr(p, "as-")) return "aerospatiale"; |
| 279 |
if (strstr(p, "me-")) return "messerschmitt"; |
| 280 |
if (strstr(p, "fw-")) return "focke-wulf"; |
| 281 |
if (strstr(p, "yak-")) return "yakovlev"; |
| 282 |
if (strstr(p, "sgs-")) return "schweizer"; |
| 283 |
if (strstr(p, "ask-")) return "schleicher"; |
| 284 |
if (strstr(p, "mirage")) return "dassault"; |
| 285 |
|
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
const char* |
| 291 |
jsb_wing_tag(void *xid, void *path) |
| 292 |
{ |
| 293 |
void *xlid, *xmid, *xeid; |
| 294 |
double aero_z = 0.0; |
| 295 |
double eye_z = 0.0; |
| 296 |
int i, num; |
| 297 |
|
| 298 |
xeid = xmlNodeGet(xid, "/fdm_config/propulsion/engine/thruster"); |
| 299 |
if (xeid) |
| 300 |
{ |
| 301 |
char *rv = NULL; |
| 302 |
char *file = xmlAttributeGetString(xeid, "file"); |
| 303 |
char *fname = calloc(1, strlen(path)+strlen("Engines/")+ |
| 304 |
strlen(file)+strlen(".xml")+1); |
| 305 |
if (fname) |
| 306 |
{ |
| 307 |
void *xfid; |
| 308 |
|
| 309 |
memcpy(fname, path, strlen(path)); |
| 310 |
memcpy(fname+strlen(path), "Engines/", strlen("Engines/")); |
| 311 |
memcpy(fname+strlen(path)+strlen("Engines/"), file, strlen(file)); |
| 312 |
memcpy(fname+strlen(path)+strlen("Engines/")+strlen(file), ".xml", strlen(".xml")); |
| 313 |
|
| 314 |
xfid = xmlOpen(fname); |
| 315 |
if (xfid) |
| 316 |
{ |
| 317 |
if (xmlNodeTest(xfid, "rotor")) rv = "helicopter"; |
| 318 |
xmlClose(xfid); |
| 319 |
} |
| 320 |
free(fname); |
| 321 |
} |
| 322 |
xmlFree(file); |
| 323 |
xmlFree(xeid); |
| 324 |
if (rv) return rv; |
| 325 |
} |
| 326 |
|
| 327 |
xmid = xmlNodeGet(xid, "/fdm_config/metrics"); |
| 328 |
if (!xmid) return NULL; |
| 329 |
|
| 330 |
xlid = xmlMarkId(xmid); |
| 331 |
num = xmlNodeGetNum(xmid, "location"); |
| 332 |
for (i=0; i<num; ++i) |
| 333 |
{ |
| 334 |
if (xmlNodeGetPos(xmid, xlid, "location", i) != 0) |
| 335 |
{ |
| 336 |
if (!xmlAttributeCompareString(xlid, "name", "AERORP")) { |
| 337 |
aero_z = xmlNodeGetDouble(xlid, "z"); |
| 338 |
} else if (!xmlAttributeCompareString(xlid, "name", "EYEPOINT")) { |
| 339 |
eye_z = xmlNodeGetDouble(xlid, "z"); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
xmlFree(xlid); |
| 344 |
xmlFree(xmid); |
| 345 |
|
| 346 |
if (aero_z > eye_z) return "high-wing"; |
| 347 |
else return "low-wing"; |
| 348 |
} |
| 349 |
|
| 350 |
const char* |
| 351 |
jsb_gear_tag(void *xid) |
| 352 |
{ |
| 353 |
void *xcid, *xgid = xmlNodeGet(xid, "/fdm_config/ground_reactions"); |
| 354 |
double nose_x = 0.0; |
| 355 |
double main_x = 0.0; |
| 356 |
int bogeys = 0; |
| 357 |
int i, num; |
| 358 |
|
| 359 |
if (!xgid) return NULL; |
| 360 |
|
| 361 |
xcid = xmlMarkId(xgid); |
| 362 |
num = xmlNodeGetNum(xgid, "contact"); |
| 363 |
for (i=0; i<num; ++i) |
| 364 |
{ |
| 365 |
if (xmlNodeGetPos(xgid, xcid, "contact", i) != 0) |
| 366 |
{ |
| 367 |
if (!xmlAttributeCompareString(xcid, "type", "BOGEY")) |
| 368 |
{ |
| 369 |
if (xmlNodeGetDouble(xcid, "location/y") == 0.0) { |
| 370 |
nose_x = xmlNodeGetDouble(xcid, "location/x"); |
| 371 |
} else { |
| 372 |
main_x = xmlNodeGetDouble(xcid, "location/x"); |
| 373 |
} |
| 374 |
bogeys++; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
xmlFree(xcid); |
| 379 |
xmlFree(xgid); |
| 380 |
|
| 381 |
if (bogeys == 0) return "skids"; |
| 382 |
else if (bogeys < 3 || main_x < nose_x) return "tail-dragger"; |
| 383 |
return "tricycle"; |
| 384 |
} |
| 385 |
|
| 386 |
const char* |
| 387 |
jsb_gear_retract_tag(void *xid) |
| 388 |
{ |
| 389 |
int r = xmlNodeGetInt(xid, "/fdm_config/ground_reactions/contact/retractable"); |
| 390 |
if (r) return "retractable-gear"; |
| 391 |
else return "fixed-gear"; |
| 392 |
} |
| 393 |
|
| 394 |
const char* |
| 395 |
jsb_gear_steering_tag(void *xid) |
| 396 |
{ |
| 397 |
void *xcid, *xgid = xmlNodeGet(xid, "/fdm_config/ground_reactions"); |
| 398 |
char *rv = NULL; |
| 399 |
int found = 0; |
| 400 |
int i, num; |
| 401 |
|
| 402 |
if (!xgid) return NULL; |
| 403 |
|
| 404 |
xcid = xmlMarkId(xgid); |
| 405 |
num = xmlNodeGetNum(xgid, "contact"); |
| 406 |
for (i=0; i<num; ++i) |
| 407 |
{ |
| 408 |
if (xmlNodeGetPos(xgid, xcid, "contact", i) != 0) |
| 409 |
{ |
| 410 |
if (!xmlAttributeCompareString(xcid, "type", "BOGEY")) |
| 411 |
{ |
| 412 |
if (xmlNodeGetDouble(xcid, "location/y") == 0.0) |
| 413 |
{ |
| 414 |
double m = xmlNodeGetDouble(xcid, "max_steer"); |
| 415 |
int c = xmlNodeGetInt(xcid, "castered"); |
| 416 |
|
| 417 |
if (c || (m == 360.0 && !c)) { |
| 418 |
rv = "castering"; |
| 419 |
break; |
| 420 |
} else if (m > 0.0) { |
| 421 |
|
| 422 |
break; |
| 423 |
} |
| 424 |
else { |
| 425 |
|
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
xmlFree(xcid); |
| 432 |
xmlFree(xgid); |
| 433 |
|
| 434 |
return rv; |
| 435 |
} |
| 436 |
|
| 437 |
const char* |
| 438 |
jsb_engines_tag(void *xid) |
| 439 |
{ |
| 440 |
void *xeid, *xpid = xmlNodeGet(xid, "/fdm_config/propulsion"); |
| 441 |
const char* rv = NULL; |
| 442 |
int engines = 0; |
| 443 |
int i, num; |
| 444 |
char *main; |
| 445 |
|
| 446 |
if (!xpid) return rv; |
| 447 |
|
| 448 |
xeid = xmlMarkId(xpid); |
| 449 |
|
| 450 |
num = xmlNodeGetNum(xpid, "engine"); |
| 451 |
for (i=0; i<num; ++i) |
| 452 |
{ |
| 453 |
if (xmlNodeGetPos(xpid, xeid, "engine", i) != 0) |
| 454 |
{ |
| 455 |
if (i == 0) |
| 456 |
{ |
| 457 |
main = xmlAttributeGetString(xeid, "file"); |
| 458 |
engines++; |
| 459 |
} |
| 460 |
else if (!xmlAttributeCompareString(xeid, "file", main)) { |
| 461 |
engines++; |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
xmlFree(main); |
| 466 |
xmlFree(xeid); |
| 467 |
xmlFree(xpid); |
| 468 |
|
| 469 |
switch (engines) |
| 470 |
{ |
| 471 |
case 0: |
| 472 |
rv = "glider"; |
| 473 |
break; |
| 474 |
case 1: |
| 475 |
rv = "single-engine"; |
| 476 |
break; |
| 477 |
case 2: |
| 478 |
rv = "twin-engine"; |
| 479 |
break; |
| 480 |
case 3: |
| 481 |
rv = "three-engine"; |
| 482 |
break; |
| 483 |
case 4: |
| 484 |
rv = "four-engine"; |
| 485 |
break; |
| 486 |
default: |
| 487 |
break; |
| 488 |
} |
| 489 |
|
| 490 |
return rv; |
| 491 |
} |
| 492 |
|
| 493 |
const char* |
| 494 |
jsb_engine_tag(void *xid, char *path) |
| 495 |
{ |
| 496 |
void *xeid = xmlNodeGet(xid, "/fdm_config/propulsion/engine"); |
| 497 |
char *engine, *fname; |
| 498 |
const char *rv = NULL; |
| 499 |
|
| 500 |
if (!xeid) return rv; |
| 501 |
|
| 502 |
engine = xmlAttributeGetString(xeid, "file"); |
| 503 |
fname = calloc(1, strlen(path)+strlen("Engines/")+ |
| 504 |
strlen(engine)+strlen(".xml")+1); |
| 505 |
if (fname) |
| 506 |
{ |
| 507 |
void *xfid; |
| 508 |
|
| 509 |
memcpy(fname, path, strlen(path)); |
| 510 |
memcpy(fname+strlen(path), "Engines/", strlen("Engines/")); |
| 511 |
memcpy(fname+strlen(path)+strlen("Engines/"), engine, strlen(engine)); |
| 512 |
memcpy(fname+strlen(path)+strlen("Engines/")+strlen(engine), ".xml", strlen(".xml")); |
| 513 |
|
| 514 |
xfid = xmlOpen(fname); |
| 515 |
if (xfid) |
| 516 |
{ |
| 517 |
if (xmlNodeTest(xfid, "turbine_engine")) rv = "jet"; |
| 518 |
else if (xmlNodeTest(xfid, "turboprop_engine")) rv = "turboprop"; |
| 519 |
else if (xmlNodeTest(xfid, "piston_engine")) rv = "piston"; |
| 520 |
else if (xmlNodeTest(xfid, "electric_engine")) rv = "electric"; |
| 521 |
else if (xmlNodeTest(xfid, "rocket_engine")) rv = "rocket"; |
| 522 |
|
| 523 |
xmlClose(xfid); |
| 524 |
} |
| 525 |
else { |
| 526 |
printf("JSBSim engine file not found: %s\n", fname); |
| 527 |
} |
| 528 |
free(fname); |
| 529 |
} |
| 530 |
else { |
| 531 |
printf("Unable to allocate memory\n"); |
| 532 |
} |
| 533 |
xmlFree(engine); |
| 534 |
xmlFree(xeid); |
| 535 |
|
| 536 |
return rv; |
| 537 |
} |
| 538 |
|
| 539 |
const char* |
| 540 |
jsb_propulsion_tag(void *xid, char *path) |
| 541 |
{ |
| 542 |
void *xeid = xmlNodeGet(xid, "fdm_config/propulsion/engine"); |
| 543 |
char *file, *fname; |
| 544 |
const char *rv = NULL; |
| 545 |
|
| 546 |
if (!xeid) return rv; |
| 547 |
|
| 548 |
file = xmlAttributeGetString(xeid, "file"); |
| 549 |
fname = calloc(1, strlen(path)+strlen("Engines/")+ |
| 550 |
strlen(file)+strlen(".xml")+1); |
| 551 |
if (fname) |
| 552 |
{ |
| 553 |
void *xfid; |
| 554 |
|
| 555 |
memcpy(fname, path, strlen(path)); |
| 556 |
memcpy(fname+strlen(path), "Engines/", strlen("Engines/")); |
| 557 |
memcpy(fname+strlen(path)+strlen("Engines/"), file, strlen(file)); |
| 558 |
memcpy(fname+strlen(path)+strlen("Engines/")+strlen(file), ".xml", strlen(".xml")); |
| 559 |
|
| 560 |
xfid = xmlOpen(fname); |
| 561 |
if (xfid) |
| 562 |
{ |
| 563 |
if (xmlNodeGetInt(xfid, "turbine_engine/augmented")) { |
| 564 |
rv = "afterburner"; |
| 565 |
} |
| 566 |
else if (xmlNodeGetInt(xfid, "piston_engine/numboostspeeds")) { |
| 567 |
rv = "supercharged"; |
| 568 |
} |
| 569 |
xmlClose(xfid); |
| 570 |
} |
| 571 |
else { |
| 572 |
printf("JSBSim engine file not found: %s\n", fname); |
| 573 |
} |
| 574 |
free(fname); |
| 575 |
} |
| 576 |
xmlFree(xeid); |
| 577 |
xmlFree(file); |
| 578 |
|
| 579 |
return rv; |
| 580 |
} |
| 581 |
|
| 582 |
const char* |
| 583 |
jsb_thruster_tag(void *xid, char *path) |
| 584 |
{ |
| 585 |
void *xeid = xmlNodeGet(xid, "/fdm_config/propulsion/engine/thruster"); |
| 586 |
char *file, *fname; |
| 587 |
const char *rv = NULL; |
| 588 |
|
| 589 |
if (!xeid) return rv; |
| 590 |
|
| 591 |
file = xmlAttributeGetString(xeid, "file"); |
| 592 |
fname = calloc(1, strlen(path)+strlen("Engines/")+ |
| 593 |
strlen(file)+strlen(".xml")+1); |
| 594 |
if (fname) |
| 595 |
{ |
| 596 |
void *xfid; |
| 597 |
|
| 598 |
memcpy(fname, path, strlen(path)); |
| 599 |
memcpy(fname+strlen(path), "Engines/", strlen("Engines/")); |
| 600 |
memcpy(fname+strlen(path)+strlen("Engines/"), file, strlen(file)); |
| 601 |
memcpy(fname+strlen(path)+strlen("Engines/")+strlen(file), ".xml", strlen(".xml")); |
| 602 |
|
| 603 |
xfid = xmlOpen(fname); |
| 604 |
if (xfid) |
| 605 |
{ |
| 606 |
if (xmlNodeTest(xfid, "propeller")) |
| 607 |
{ |
| 608 |
double min = xmlNodeGetDouble(xfid, "propeller/minpitch"); |
| 609 |
double max = xmlNodeGetDouble(xfid, "propeller/maxpitch"); |
| 610 |
if (min == max) rv = "fixed-pitch"; |
| 611 |
else rv = "variable-pitch"; |
| 612 |
} |
| 613 |
xmlClose(xfid); |
| 614 |
} |
| 615 |
else { |
| 616 |
printf("JSBSim thruster file not found: %s\n", fname); |
| 617 |
} |
| 618 |
free(fname); |
| 619 |
} |
| 620 |
else { |
| 621 |
printf("Unable to allocate memory\n"); |
| 622 |
} |
| 623 |
xmlFree(file); |
| 624 |
xmlFree(xeid); |
| 625 |
|
| 626 |
return rv; |
| 627 |
} |
| 628 |
|
| 629 |
void |
| 630 |
update_metadata_jsb(char *sfname, char *path, char *aero, char *desc) |
| 631 |
{ |
| 632 |
const char *tag; |
| 633 |
char *s, *fname; |
| 634 |
void *xid; |
| 635 |
|
| 636 |
fname = calloc(1, strlen(path)+strlen(aero)+strlen(".xml")+1); |
| 637 |
if (!fname) |
| 638 |
{ |
| 639 |
printf("Unable to allocate memory\n"); |
| 640 |
return; |
| 641 |
} |
| 642 |
|
| 643 |
memcpy(fname, path, strlen(path)); |
| 644 |
memcpy(fname+strlen(path), aero, strlen(aero)); |
| 645 |
memcpy(fname+strlen(path)+strlen(aero), ".xml", strlen(".xml")); |
| 646 |
|
| 647 |
xid = xmlOpen(fname); |
| 648 |
if (!xid) |
| 649 |
{ |
| 650 |
printf("JSBSim aero file not found: '%s'\n", fname); |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
s = malloc(65*1024); |
| 655 |
if (s) |
| 656 |
{ |
| 657 |
sprintf(s, " <tags>\n"); |
| 658 |
PRINT("auto-generated"); |
| 659 |
PRINT(vendor_tag(desc ? desc : aero)); |
| 660 |
PRINT(strlwr(desc ? desc : aero)); |
| 661 |
PRINT(jsb_wing_tag(xid, path)); |
| 662 |
PRINT(jsb_gear_tag(xid)); |
| 663 |
PRINT(jsb_gear_retract_tag(xid)); |
| 664 |
PRINT(jsb_gear_steering_tag(xid)); |
| 665 |
PRINT(jsb_engines_tag(xid)); |
| 666 |
PRINT(jsb_engine_tag(xid, path)); |
| 667 |
PRINT(jsb_propulsion_tag(xid, path)); |
| 668 |
PRINT(jsb_thruster_tag(xid, path)); |
| 669 |
s = strncat(s, " </tags>", TS-strlen(s)); |
| 670 |
|
| 671 |
if (print_tags) { |
| 672 |
printf( "%s\n", s); |
| 673 |
} else { |
| 674 |
updateFile(sfname, s); |
| 675 |
} |
| 676 |
free(s); |
| 677 |
} |
| 678 |
xmlClose(xid); |
| 679 |
free(fname); |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
const char* |
| 685 |
yasim_wing_tag(void *xid) |
| 686 |
{ |
| 687 |
void *xaid = xmlNodeGet(xid, "/airplane"); |
| 688 |
void *xwid, *xcid; |
| 689 |
int n_wings; |
| 690 |
double wing_z = 0.0; |
| 691 |
double eye_z = 0.0; |
| 692 |
|
| 693 |
if (xmlNodeTest(xaid, "rotor")) { |
| 694 |
return "helicopter"; |
| 695 |
} |
| 696 |
|
| 697 |
xwid = xmlNodeGet(xaid, "wing"); |
| 698 |
if (xwid) |
| 699 |
{ |
| 700 |
wing_z = xmlAttributeGetDouble(xwid, "z"); |
| 701 |
xmlFree(xwid); |
| 702 |
} |
| 703 |
|
| 704 |
xcid = xmlNodeGet(xaid, "cockpit"); |
| 705 |
if (xcid) |
| 706 |
{ |
| 707 |
eye_z = xmlAttributeGetDouble(xcid, "z"); |
| 708 |
xmlFree(xcid); |
| 709 |
} |
| 710 |
|
| 711 |
n_wings = xmlNodeGetNum(xaid, "mstab"); |
| 712 |
xmlFree(xaid); |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
if (wing_z > eye_z) return "high-wing"; |
| 718 |
return "low-wing"; |
| 719 |
} |
| 720 |
|
| 721 |
const char* |
| 722 |
yasim_gear_tag(void *xid) |
| 723 |
{ |
| 724 |
void *xaid = xmlNodeGet(xid, "/airplane"); |
| 725 |
void *xgid = xmlMarkId(xaid); |
| 726 |
double nose_x = 0.0; |
| 727 |
double main_x = 0.0; |
| 728 |
int gears = 0; |
| 729 |
int i, num; |
| 730 |
|
| 731 |
num = xmlNodeGetNum(xaid, "gear"); |
| 732 |
for (i=0; i<num; ++i) |
| 733 |
{ |
| 734 |
if (xmlNodeGetPos(xaid, xgid, "gear", i) != 0) |
| 735 |
{ |
| 736 |
if (xmlAttributeGetDouble(xgid, "y") == 0.0) { |
| 737 |
nose_x = xmlAttributeGetDouble(xgid, "x"); |
| 738 |
} else { |
| 739 |
main_x = xmlAttributeGetDouble(xgid, "x"); |
| 740 |
} |
| 741 |
gears++; |
| 742 |
} |
| 743 |
} |
| 744 |
xmlFree(xgid); |
| 745 |
xmlFree(xaid); |
| 746 |
|
| 747 |
if (gears == 0) return "skids"; |
| 748 |
else if (gears < 3 || main_x > nose_x) return "tail-dragger"; |
| 749 |
return "tricycle"; |
| 750 |
} |
| 751 |
|
| 752 |
const char* |
| 753 |
yasim_gear_retract_tag(void *xid) |
| 754 |
{ |
| 755 |
void *xaid = xmlNodeGet(xid, "/airplane"); |
| 756 |
void *xgid = xmlMarkId(xaid); |
| 757 |
char *rv = "fixed-gear"; |
| 758 |
int found = 0; |
| 759 |
int i, num; |
| 760 |
|
| 761 |
num = xmlNodeGetNum(xaid, "gear"); |
| 762 |
for (i=0; i<num; ++i) |
| 763 |
{ |
| 764 |
if (xmlNodeGetPos(xaid, xgid, "gear", i) != 0) |
| 765 |
{ |
| 766 |
void *xgcid = xmlMarkId(xgid); |
| 767 |
int j, cnum = xmlNodeGetNum(xgid, "control-input"); |
| 768 |
for (j=0; j<cnum; ++j) |
| 769 |
{ |
| 770 |
if (xmlNodeGetPos(xgid, xgcid, "control-input", j)) |
| 771 |
{ |
| 772 |
if (!xmlAttributeCompareString(xgcid, "control", "EXTEND")) { |
| 773 |
rv = "retractable-gear"; |
| 774 |
found = 1; |
| 775 |
break; |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
xmlFree(xgcid); |
| 780 |
} |
| 781 |
} |
| 782 |
xmlFree(xgid); |
| 783 |
xmlFree(xaid); |
| 784 |
|
| 785 |
return rv; |
| 786 |
} |
| 787 |
|
| 788 |
const char* |
| 789 |
yasim_gear_steering_tag(void *xid) |
| 790 |
{ |
| 791 |
void *xaid = xmlNodeGet(xid, "/airplane"); |
| 792 |
void *xgid = xmlMarkId(xaid); |
| 793 |
char *rv = NULL; |
| 794 |
int found = 0; |
| 795 |
int i, num; |
| 796 |
|
| 797 |
num = xmlNodeGetNum(xaid, "gear"); |
| 798 |
for (i=0; i<num; ++i) |
| 799 |
{ |
| 800 |
if (xmlNodeGetPos(xaid, xgid, "gear", i) != 0) |
| 801 |
{ |
| 802 |
void *xgcid = xmlMarkId(xgid); |
| 803 |
int j, cnum = xmlNodeGetNum(xgid, "control-input"); |
| 804 |
for (j=0; j<cnum; ++j) |
| 805 |
{ |
| 806 |
if (xmlNodeGetPos(xgid, xgcid, "control-input", j)) |
| 807 |
{ |
| 808 |
if (!xmlAttributeCompareString(xgcid, "control", "STEER")) { |
| 809 |
|
| 810 |
found = 1; |
| 811 |
break; |
| 812 |
} |
| 813 |
else if (!xmlAttributeCompareString(xgcid, "control", "CASTERING")) { |
| 814 |
rv = "castering"; |
| 815 |
found = 1; |
| 816 |
break; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
xmlFree(xgcid); |
| 821 |
} |
| 822 |
} |
| 823 |
xmlFree(xgid); |
| 824 |
xmlFree(xaid); |
| 825 |
|
| 826 |
return rv; |
| 827 |
} |
| 828 |
|
| 829 |
const char* |
| 830 |
yasim_engines_tag(void *xid) |
| 831 |
{ |
| 832 |
void *xeid = xmlNodeGet(xid, "/airplane"); |
| 833 |
const char* rv = "multi-engine"; |
| 834 |
int engines = 0; |
| 835 |
|
| 836 |
if (xmlNodeTest(xeid, "propeller")) { |
| 837 |
engines = xmlNodeGetNum(xeid, "propeller"); |
| 838 |
} |
| 839 |
else if (xmlNodeTest(xeid, "jet")) { |
| 840 |
engines = xmlNodeGetNum(xeid, "jet"); |
| 841 |
} |
| 842 |
else if (xmlNodeTest(xeid, "rotor")) { |
| 843 |
engines = 1; |
| 844 |
} |
| 845 |
|
| 846 |
switch (engines) |
| 847 |
{ |
| 848 |
case 0: |
| 849 |
rv = "glider"; |
| 850 |
break; |
| 851 |
case 1: |
| 852 |
rv = "single-engine"; |
| 853 |
break; |
| 854 |
case 2: |
| 855 |
rv = "twin-engine"; |
| 856 |
break; |
| 857 |
case 3: |
| 858 |
rv = "three-engine"; |
| 859 |
break; |
| 860 |
case 4: |
| 861 |
rv = "four-engine"; |
| 862 |
break; |
| 863 |
default: |
| 864 |
break; |
| 865 |
} |
| 866 |
xmlFree(xeid); |
| 867 |
|
| 868 |
return rv; |
| 869 |
} |
| 870 |
|
| 871 |
const char* |
| 872 |
yasim_engine_tag(void *xid, char *path) |
| 873 |
{ |
| 874 |
const char* rv = NULL; |
| 875 |
|
| 876 |
if (xmlNodeTest(xid, "/airplane/propeller/piston-engine")) { |
| 877 |
rv = "piston"; |
| 878 |
} |
| 879 |
else if (xmlNodeTest(xid, "/airplane/propeller/turbine-engine")) { |
| 880 |
rv = "turboprop"; |
| 881 |
} |
| 882 |
else if (xmlNodeTest(xid, "/airplane/jet")) { |
| 883 |
rv = "jet"; |
| 884 |
} |
| 885 |
|
| 886 |
return rv; |
| 887 |
} |
| 888 |
|
| 889 |
const char* |
| 890 |
yasim_propulsion_tag(void *xid, char *path) |
| 891 |
{ |
| 892 |
const char* rv = NULL; |
| 893 |
void *xpid; |
| 894 |
|
| 895 |
if ((xpid = xmlNodeGet(xid, "/airplane/jet")) != NULL) |
| 896 |
{ |
| 897 |
if (xmlAttributeGetDouble(xpid, "afterburner") > 0.0) { |
| 898 |
rv = "afterburner"; |
| 899 |
} |
| 900 |
xmlFree(xpid); |
| 901 |
} |
| 902 |
else if ((xpid = xmlNodeGet(xid, "/airplane/propeller/piston-engine")) != NULL) |
| 903 |
{ |
| 904 |
if (xmlAttributeGetInt(xpid, "supercharger") > 0) { |
| 905 |
rv = "supercharger"; |
| 906 |
} |
| 907 |
xmlFree(xpid); |
| 908 |
} |
| 909 |
|
| 910 |
return rv; |
| 911 |
} |
| 912 |
|
| 913 |
const char* |
| 914 |
yasim_thruster_tag(void *xid, char *path) |
| 915 |
{ |
| 916 |
const char* rv = NULL; |
| 917 |
void *xtid; |
| 918 |
|
| 919 |
xtid = xmlNodeGet(xid, "/airplane/propeller/control-input"); |
| 920 |
if (xtid) |
| 921 |
{ |
| 922 |
if (!xmlAttributeCompareString(xtid, "control", "ADVANCE")) { |
| 923 |
rv = "variable-pitch"; |
| 924 |
} else rv = "fixed-pitch"; |
| 925 |
xmlFree(xtid); |
| 926 |
} |
| 927 |
|
| 928 |
return rv; |
| 929 |
} |
| 930 |
|
| 931 |
void |
| 932 |
update_metadata_yasim(char *sfname, char *path, char *aero, char *desc) |
| 933 |
{ |
| 934 |
const char *tag; |
| 935 |
char *s, *fname; |
| 936 |
void *xid; |
| 937 |
|
| 938 |
fname = calloc(1, strlen(path)+strlen(aero)+strlen(".xml")+1); |
| 939 |
if (!fname) |
| 940 |
{ |
| 941 |
printf("Unable to allocate memory\n"); |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
memcpy(fname, path, strlen(path)); |
| 946 |
memcpy(fname+strlen(path), aero, strlen(aero)); |
| 947 |
memcpy(fname+strlen(path)+strlen(aero), ".xml", strlen(".xml")); |
| 948 |
|
| 949 |
xid = xmlOpen(fname); |
| 950 |
if (!xid) |
| 951 |
{ |
| 952 |
printf("Yasim aero file not found: '%s'\n", fname); |
| 953 |
return; |
| 954 |
} |
| 955 |
|
| 956 |
s = malloc(65*1024); |
| 957 |
if (s) |
| 958 |
{ |
| 959 |
sprintf(s, " <tags>\n"); |
| 960 |
PRINT("auto-generated"); |
| 961 |
PRINT(vendor_tag(desc ? desc : aero)); |
| 962 |
PRINT(strlwr(desc ? desc : aero)); |
| 963 |
PRINT(yasim_wing_tag(xid)); |
| 964 |
PRINT(yasim_gear_tag(xid)); |
| 965 |
PRINT(yasim_gear_retract_tag(xid)); |
| 966 |
PRINT(yasim_gear_steering_tag(xid)); |
| 967 |
PRINT(yasim_engines_tag(xid)); |
| 968 |
PRINT(yasim_engine_tag(xid, path)); |
| 969 |
PRINT(yasim_propulsion_tag(xid, path)); |
| 970 |
PRINT(yasim_thruster_tag(xid, path)); |
| 971 |
s = strncat(s, " </tags>", TS-strlen(s)); |
| 972 |
|
| 973 |
if (print_tags) { |
| 974 |
printf( "%s\n", s); |
| 975 |
} else { |
| 976 |
updateFile(sfname, s); |
| 977 |
} |
| 978 |
free(s); |
| 979 |
} |
| 980 |
xmlClose(xid); |
| 981 |
free(fname); |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
void |
| 987 |
update_metadata(const char *fname) |
| 988 |
{ |
| 989 |
void *xsid, *xid = xmlOpen(fname); |
| 990 |
|
| 991 |
if (!xid) |
| 992 |
{ |
| 993 |
printf("File not found: '%s'\n", fname); |
| 994 |
return; |
| 995 |
} |
| 996 |
|
| 997 |
xsid = xmlNodeGet(xid, "/PropertyList/sim"); |
| 998 |
if (!xsid) |
| 999 |
{ |
| 1000 |
printf("path '/PropertyList/sim' not found in '%s'\n", fname); |
| 1001 |
xmlClose(xid); |
| 1002 |
return; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (xmlNodeTest(xsid, "tags") == 0) |
| 1006 |
{ |
| 1007 |
char *desc = NULL, *str = NULL; |
| 1008 |
char *path, *pend; |
| 1009 |
|
| 1010 |
pend = strrchr(fname, '/'); |
| 1011 |
if (!pend) pend = strrchr(fname, '\\'); |
| 1012 |
if (!pend) path = strdup("./"); |
| 1013 |
else |
| 1014 |
{ |
| 1015 |
pend++; |
| 1016 |
path = calloc(1, pend-fname+1); |
| 1017 |
memcpy(path, fname, pend-fname); |
| 1018 |
} |
| 1019 |
|
| 1020 |
desc = xmlNodeGetString(xsid, "description"); |
| 1021 |
str = xmlNodeGetString(xsid, "aero"); |
| 1022 |
if (!xmlNodeCompareString(xsid, "flight-model", "jsb")) { |
| 1023 |
update_metadata_jsb(fname, path, str, desc); |
| 1024 |
} |
| 1025 |
else if (!xmlNodeCompareString(xsid, "flight-model", "yasim")) { |
| 1026 |
update_metadata_yasim(fname, path, str, desc); |
| 1027 |
} |
| 1028 |
else |
| 1029 |
{ |
| 1030 |
str = xmlNodeGetString(xsid, "flight-model"); |
| 1031 |
printf("The '%s' flightmodel is unsupported at this time\n", str); |
| 1032 |
} |
| 1033 |
|
| 1034 |
xmlFree(xsid); |
| 1035 |
|
| 1036 |
xmlFree(desc); |
| 1037 |
xmlFree(str); |
| 1038 |
free(path); |
| 1039 |
} |
| 1040 |
else { |
| 1041 |
printf("'%s' already contains metadata tags\n", fname); |
| 1042 |
} |
| 1043 |
|
| 1044 |
xmlClose(xid); |
| 1045 |
} |
| 1046 |
|
| 1047 |
int |
| 1048 |
main(int argc, char **argv) |
| 1049 |
{ |
| 1050 |
const char *setFile; |
| 1051 |
|
| 1052 |
if (argc == 1 || getCommandLineOption(argc, argv, "-h") || |
| 1053 |
getCommandLineOption(argc, argv, "--help")) { |
| 1054 |
show_help(); |
| 1055 |
} |
| 1056 |
|
| 1057 |
if (getCommandLineOption(argc, argv, "-p") || |
| 1058 |
getCommandLineOption(argc, argv, "--print")) { |
| 1059 |
print_tags = 1; |
| 1060 |
} |
| 1061 |
|
| 1062 |
setFile = getCommandLineOption(argc, argv, "-i"); |
| 1063 |
if (!setFile) { |
| 1064 |
setFile = getCommandLineOption(argc, argv, "-input"); |
| 1065 |
} |
| 1066 |
if (!setFile) { |
| 1067 |
show_help(); |
| 1068 |
} |
| 1069 |
|
| 1070 |
printf("%s\n", setFile); |
| 1071 |
update_metadata(setFile); |
| 1072 |
printf("\n"); |
| 1073 |
return 0; |
| 1074 |
} |