]> git.mxchange.org Git - flightgear.git/blobdiff - src/Main/fg_init.cxx
Only test for --fg-root and --aircraft in ~/.fgfsrc et all if it wasn't specified...
[flightgear.git] / src / Main / fg_init.cxx
index e1bb2e56588147861b1dc3b8c9a0fdaeebdba3fb..5ad9476021430d06c3222f18f74720a1bb372b36 100644 (file)
 #include "globals.hxx"
 #include "logger.hxx"
 #include "viewmgr.hxx"
+#include "main.hxx"
 
 #if defined(FX) && defined(XMESA)
 #include <GL/xmesa.h>
@@ -206,46 +207,68 @@ static string fgScanForOption( const string& option, const string& path ) {
     return "";
 }
 
+// Scan the user config files for the specified option and return
+// the value.
+static string fgScanForOption( const string& option ) {
+    string arg("");
 
-// Read in configuration (files and command line options) but only set
-// fg_root
-bool fgInitFGRoot ( int argc, char **argv ) {
-    string root;
-    char* envp;
-
-    // First parse command line options looking for --fg-root=, this
-    // will override anything specified in a config file
-    root = fgScanForOption( "--fg-root=", argc, argv);
+    if (hostname == NULL)
+    {
+        char _hostname[256];
+        gethostname(_hostname, 256);
+        hostname = strdup(_hostname);
+        free_hostname = true;
+    }
 
 #if defined( unix ) || defined( __CYGWIN__ )
     // Next check home directory for .fgfsrc.hostname file
-    if ( root.empty() ) {
-        envp = ::getenv( "HOME" );
-        if ( envp != NULL ) {
-            SGPath config( envp );
+    if ( arg.empty() ) {
+        if ( homedir != NULL ) {
+            SGPath config( homedir );
             config.append( ".fgfsrc" );
-            char name[256];
-            gethostname( name, 256 );
             config.concat( "." );
-            config.concat( name );
-            root = fgScanForOption( "--fg-root=", config.str() );
+            config.concat( hostname );
+            arg = fgScanForOption( option, config.str() );
         }
     }
 #endif
 
     // Next check home directory for .fgfsrc file
-    if ( root.empty() ) {
-        envp = ::getenv( "HOME" );
-        if ( envp != NULL ) {
-            SGPath config( envp );
+    if ( arg.empty() ) {
+        if ( homedir != NULL ) {
+            SGPath config( homedir );
             config.append( ".fgfsrc" );
-            root = fgScanForOption( "--fg-root=", config.str() );
+            arg = fgScanForOption( option, config.str() );
         }
     }
+
+    if ( arg.empty() ) {
+        // Check for $fg_root/system.fgfsrc
+        SGPath config( globals->get_fg_root() );
+        config.append( "system.fgfsrc" );
+        arg = fgScanForOption( option, config.str() );
+    }
+
+    return arg;
+}
+
+
+// Read in configuration (files and command line options) but only set
+// fg_root
+bool fgInitFGRoot ( int argc, char **argv ) {
+    string root;
+
+    // First parse command line options looking for --fg-root=, this
+    // will override anything specified in a config file
+    root = fgScanForOption( "--fg-root=", argc, argv);
+
+    // Check in one of the user configuration files.
+    if (root.empty() )
+        root = fgScanForOption( "--fg-root=" );
     
     // Next check if fg-root is set as an env variable
     if ( root.empty() ) {
-        envp = ::getenv( "FG_ROOT" );
+        char *envp = ::getenv( "FG_ROOT" );
         if ( envp != NULL ) {
             root = envp;
         }
@@ -295,44 +318,15 @@ bool fgInitFGRoot ( int argc, char **argv ) {
 // aircraft
 bool fgInitFGAircraft ( int argc, char **argv ) {
     string aircraft;
-    char* envp;
 
     // First parse command line options looking for --aircraft=, this
     // will override anything specified in a config file
     aircraft = fgScanForOption( "--aircraft=", argc, argv );
 
-#if defined( unix ) || defined( __CYGWIN__ )
-    // Next check home directory for .fgfsrc.hostname file
-    if ( aircraft.empty() ) {
-        envp = ::getenv( "HOME" );
-        if ( envp != NULL ) {
-            SGPath config( envp );
-            config.append( ".fgfsrc" );
-            char name[256];
-            gethostname( name, 256 );
-            config.concat( "." );
-            config.concat( name );
-            aircraft = fgScanForOption( "--aircraft=", config.str() );
-        }
-    }
-#endif
+    // Check in one of the user configuration files.
+    if ( aircraft.empty() )
+        aircraft = fgScanForOption( "--aircraft=" );
 
-    // Next check home directory for .fgfsrc file
-    if ( aircraft.empty() ) {
-        envp = ::getenv( "HOME" );
-        if ( envp != NULL ) {
-            SGPath config( envp );
-            config.append( ".fgfsrc" );
-            aircraft = fgScanForOption( "--aircraft=", config.str() );
-        }
-    }
-
-    if ( aircraft.empty() ) {
-        // Check for $fg_root/system.fgfsrc
-        SGPath sysconf( globals->get_fg_root() );
-        sysconf.append( "system.fgfsrc" );
-        aircraft = fgScanForOption( "--aircraft=", sysconf.str() );
-    }
     // if an aircraft was specified, set the property name
     if ( !aircraft.empty() ) {
         SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
@@ -493,27 +487,22 @@ do_options (int argc, char ** argv)
     fgParseOptions(config.str());
 
 #if defined( unix ) || defined( __CYGWIN__ )
-    char name[256];
-    // Check for $fg_root/system.fgfsrc.hostname
-    gethostname( name, 256 );
     config.concat( "." );
-    config.concat( name );
+    config.concat( hostname );
     fgParseOptions(config.str());
 #endif
 
     // Check for ~/.fgfsrc
-    char* envp = ::getenv( "HOME" );
-    if ( envp != NULL ) {
-        config.set( envp );
+    if ( homedir != NULL ) {
+        config.set( homedir );
         config.append( ".fgfsrc" );
         fgParseOptions(config.str());
     }
 
 #if defined( unix ) || defined( __CYGWIN__ )
     // Check for ~/.fgfsrc.hostname
-    gethostname( name, 256 );
     config.concat( "." );
-    config.concat( name );
+    config.concat( hostname );
     fgParseOptions(config.str());
 #endif
 
@@ -607,15 +596,24 @@ bool fgInitConfig ( int argc, char **argv ) {
         SG_LOG( SG_INPUT, SG_ALERT, "No default aircraft specified" );
     }
 
-
-    char* envp = ::getenv( "HOME" );
-    if ( envp != NULL ) {
-        SGPath config( globals->get_fg_root() );
-        config.set( envp );
+#ifdef _MSC_VER
+    char *envp = ::getenv( "APPDATA" );
+    if (envp != NULL ) {
+        SGPath config( envp );
+        config.append( "flightgear.org" );
+#else
+    if ( homedir != NULL ) {
+        SGPath config( homedir );
         config.append( ".fgfs" );
+#endif
         config.append( "preferences.xml" );
         SG_LOG(SG_INPUT, SG_INFO, "Reading user preferences");
-        fgLoadProps(config.str().c_str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
+        try {
+            fgLoadProps(config.str().c_str(), globals->get_props(), false,
+                        SGPropertyNode::USERARCHIVE);
+        } catch (...) {
+            SG_LOG(SG_INPUT, SG_BULK, "First time reading user preferences.");
+        }
         SG_LOG(SG_INPUT, SG_BULK, "Finished Reading user preferences");
     }