diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx index fb94e6179..cae81a4d4 100644 --- a/src/Main/fg_init.cxx +++ b/src/Main/fg_init.cxx @@ -508,6 +508,38 @@ do_options (int argc, char ** argv) } +static string fgFindAircraftPath( const SGPath &path, const string &aircraft ) { + ulDirEnt* dire; + ulDir *dirp = ulOpenDir(path.str().c_str()); + if (dirp == NULL) { + cerr << "Unable to open aircraft directory." << endl; + exit(-1); + } + + while ((dire = ulReadDir(dirp)) != NULL) { + if (dire->d_isdir) { + if ( strcmp("CVS", dire->d_name) && strcmp(".", dire->d_name) + && strcmp("..", dire->d_name) ) + { + SGPath next = path; + next.append(dire->d_name); + + string result = fgFindAircraftPath( next, aircraft ); + if ( ! result.empty() ) { + return result; + } + } + } else if ( !strcmp(dire->d_name, aircraft.c_str()) ) { + return path.str(); + } + } + + ulCloseDir(dirp); + + return ""; +} + + // Read in configuration (file and command line) bool fgInitConfig ( int argc, char **argv ) { @@ -527,42 +559,35 @@ bool fgInitConfig ( int argc, char **argv ) { // Scan user config files and command line for a specified aircraft. fgInitFGAircraft(argc, argv); - string aircraft = fgGetString("/sim/aircraft", ""); + string aircraft = fgGetString( "/sim/aircraft", "" ); if ( aircraft.size() > 0 ) { - SGPath aircraft_path(globals->get_fg_root()); - aircraft_path.append("Aircraft"); - aircraft_path.append(aircraft); - aircraft_path.concat("-set.xml"); + SGPath aircraft_search( globals->get_fg_root() ); + aircraft_search.append( "Aircraft" ); - if ( !ulFileExists(aircraft_path.c_str()) ) { - string adir = aircraft; - int pos, alen = adir.length(); + string aircraft_set = aircraft + "-set.xml"; - if ( ((pos = adir.rfind("-jsbsim")) != string::npos) || - ((pos = adir.rfind("-yasim")) != string::npos) || - ((pos = adir.rfind("-uiuc")) != string::npos) ) - { - adir.erase(pos, alen); + string result = fgFindAircraftPath( aircraft_search, aircraft_set ); + if ( !result.empty() ) { + SGPath full_name( result ); + full_name.append( aircraft_set ); + + SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft + << " from " << full_name.str()); + try { + readProperties( full_name.str(), globals->get_props() ); + } catch ( const sg_exception &e ) { + string message = "Error reading default aircraft: "; + message += e.getFormattedMessage(); + SG_LOG(SG_INPUT, SG_ALERT, message); + exit(2); } + } else { + SG_LOG( SG_INPUT, SG_ALERT, "Cannot find specified aircraft: " + << aircraft ); + } - aircraft_path = globals->get_fg_root(); - aircraft_path.append("Aircraft"); - aircraft_path.append(adir); - aircraft_path.append(aircraft); - aircraft_path.concat("-set.xml"); - } - SG_LOG(SG_INPUT, SG_INFO, "Reading default aircraft: " << aircraft - << " from " << aircraft_path.str()); - try { - readProperties(aircraft_path.str(), globals->get_props()); - } catch (const sg_exception &e) { - string message = "Error reading default aircraft: "; - message += e.getFormattedMessage(); - SG_LOG(SG_INPUT, SG_ALERT, message); - exit(2); - } } else { - SG_LOG(SG_INPUT, SG_ALERT, "No default aircraft specified"); + SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" ); } // parse options after loading aircraft to ensure any user diff --git a/src/Main/options.cxx b/src/Main/options.cxx index 006fea4e4..4f20adf11 100644 --- a/src/Main/options.cxx +++ b/src/Main/options.cxx @@ -1521,7 +1521,7 @@ fgParseArgs (int argc, char **argv) else if (result == FG_OPTIONS_SHOW_AIRCRAFT) { SGPath path( globals->get_fg_root() ); path.append("Aircraft"); - fgShowAircraft(path); + fgShowAircraft(path, true); exit(0); } } @@ -1714,17 +1714,9 @@ fgUsage (bool verbose) } } -/* - * Search in the current directory, and in on directory deeper - * for -set.xml configuration files and show the aircaft name - * and the contents of the tag in a sorted manner. - * - * @parampath the directory to search for configuration files - * @param recursive defines whether the directory should be searched recursively - */ -void fgShowAircraft(const SGPath &path, bool recursive) { - static vector aircraft; - +static void fgSearchAircraft(const SGPath &path, string_list &aircraft, + bool recursive) +{ ulDirEnt* dire; ulDir *dirp = ulOpenDir(path.str().c_str()); if (dirp == NULL) { @@ -1742,7 +1734,7 @@ void fgShowAircraft(const SGPath &path, bool recursive) { SGPath next = path; next.append(dire->d_name); - fgShowAircraft(next, false); + fgSearchAircraft(next, aircraft, true); } } else if ((ptr = strstr(dire->d_name, "-set.xml")) && (ptr[8] == '\0')) { @@ -1778,14 +1770,26 @@ void fgShowAircraft(const SGPath &path, bool recursive) { } } - if (recursive) { - sort(aircraft.begin(), aircraft.end()); - cout << "Available aircraft:" << endl; - for ( unsigned int i = 0; i < aircraft.size(); i++ ) { - cout << aircraft[i] << endl; - } - - aircraft.clear(); - } - ulCloseDir(dirp); + ulCloseDir(dirp); +} + + +/* + * Search in the current directory, and in on directory deeper + * for -set.xml configuration files and show the aircaft name + * and the contents of the tag in a sorted manner. + * + * @parampath the directory to search for configuration files + * @param recursive defines whether the directory should be searched recursively + */ +void fgShowAircraft(const SGPath &path, bool recursive) { + string_list aircraft; + + fgSearchAircraft( path, aircraft, recursive ); + + sort(aircraft.begin(), aircraft.end()); + cout << "Available aircraft:" << endl; + for ( unsigned int i = 0; i < aircraft.size(); i++ ) { + cout << aircraft[i] << endl; + } } diff --git a/src/Main/options.hxx b/src/Main/options.hxx index 110051907..23e288069 100644 --- a/src/Main/options.hxx +++ b/src/Main/options.hxx @@ -33,6 +33,6 @@ extern void fgSetDefaults (); extern void fgParseArgs (int argc, char ** argv); extern void fgParseOptions (const string &file_path); extern void fgUsage (bool verbose = false); -extern void fgShowAircraft(const SGPath &path, bool recursive = true); +extern void fgShowAircraft(const SGPath &path, bool recursive); #endif /* _OPTIONS_HXX */