]> git.mxchange.org Git - flightgear.git/commitdiff
use safer and faster property methods instead of creating property
authormfranz <mfranz>
Tue, 12 May 2009 20:51:02 +0000 (20:51 +0000)
committerTim Moore <timoore@redhat.com>
Mon, 18 May 2009 10:24:17 +0000 (12:24 +0200)
paths via snprintf+strncat+absolute paths every time (more to come)

src/Environment/environment_ctrl.cxx
src/Environment/fgclouds.cxx

index f54fbf6d3e6ed6c016a1aaa09651931927f641db..76f2942e9683486cfad6b4de471cdc41996a5abe 100644 (file)
@@ -861,14 +861,12 @@ void
 FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
 {
     int i;
-    double d;
-    char s[128];
 
     fgSetString("/environment/metar/real-metar", m->getData());
-       // don't update with real weather when we use a custom weather scenario
-       const char *current_scenario = fgGetString("/environment/weather-scenario", "METAR");
-       if( strcmp(current_scenario, "METAR") && strcmp(current_scenario, "none"))
-               return;
+    // don't update with real weather when we use a custom weather scenario
+    const char *current_scenario = fgGetString("/environment/weather-scenario", "METAR");
+    if (strcmp(current_scenario, "METAR") && strcmp(current_scenario, "none"))
+        return;
     fgSetString("/environment/metar/last-metar", m->getData());
     fgSetString("/environment/metar/station-id", m->getId());
     fgSetDouble("/environment/metar/min-visibility-m",
@@ -876,17 +874,15 @@ FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
     fgSetDouble("/environment/metar/max-visibility-m",
                 m->getMaxVisibility().getVisibility_m() );
 
+    SGPropertyNode *metar = fgGetNode("/environment/metar", true);
     const SGMetarVisibility *dirvis = m->getDirVisibility();
-    for (i = 0; i < 8; i++, dirvis++) {
-        const char *min = "/environment/metar/visibility[%d]/min-m";
-        const char *max = "/environment/metar/visibility[%d]/max-m";
 
-        d = dirvis->getVisibility_m();
+    for (i = 0; i < 8; i++, dirvis++) {
+        SGPropertyNode *vis = metar->getChild("visibility", i, true);
+        double v = dirvis->getVisibility_m();
 
-        snprintf(s, 128, min, i);
-        fgSetDouble(s, d);
-        snprintf(s, 128, max, i);
-        fgSetDouble(s, d);
+        vis->setDoubleValue("min-m", v);
+        vis->setDoubleValue("max-m", v);
     }
 
     fgSetInt("/environment/metar/base-wind-range-from",
@@ -907,51 +903,34 @@ FGMetarEnvironmentCtrl::update_metar_properties( const FGMetar *m )
                 m->getPressure_inHg() );
 
     vector<SGMetarCloud> cv = m->getClouds();
-    vector<SGMetarCloud>::const_iterator cloud;
-
-    const char *cl = "/environment/metar/clouds/layer[%i]";
-    for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
-        const char *coverage_string[5] = 
-            { "clear", "few", "scattered", "broken", "overcast" };
-        const double thickness[5] = { 0, 65, 600,750, 1000};
-        int q;
-
-        snprintf(s, 128, cl, i);
-        strncat(s, "/coverage", 128);
-        q = cloud->getCoverage();
-        fgSetString(s, coverage_string[q] );
-
-        snprintf(s, 128, cl, i);
-        strncat(s, "/elevation-ft", 128);
-        fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
-
-        snprintf(s, 128, cl, i);
-        strncat(s, "/thickness-ft", 128);
-        fgSetDouble(s, thickness[q]);
-
-        snprintf(s, 128, cl, i);
-        strncat(s, "/span-m", 128);
-        fgSetDouble(s, 40000.0);
-    }
+    vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
 
-    for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
-        snprintf(s, 128, cl, i);
-        strncat(s, "/coverage", 128);
-        fgSetString(s, "clear");
+    SGPropertyNode *metar_clouds = fgGetNode("/environment/metar/clouds", true);
 
-        snprintf(s, 128, cl, i);
-        strncat(s, "/elevation-ft", 128);
-        fgSetDouble(s, -9999);
+    for (i = 0, cloud = cv.begin(); i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
+        const char *coverage_string[5] = { "clear", "few", "scattered", "broken", "overcast" };
+        const double thickness_value[5] = { 0, 65, 600, 750, 1000 };
 
-        snprintf(s, 128, cl, i);
-        strncat(s, "/thickness-ft", 128);
-        fgSetDouble(s, 0);
+        const char *coverage = "clear";
+        double elevation = -9999.0;
+        double thickness = 0.0;
+        const double span = 40000.0;
 
-        snprintf(s, 128, cl, i);
-        strncat(s, "/span-m", 128);
-        fgSetDouble(s, 40000.0);
-    }
+        if (cloud != cloud_end) {
+            int c = cloud->getCoverage();
+            coverage = coverage_string[c];
+            elevation = cloud->getAltitude_ft() + station_elevation_ft;
+            thickness = thickness_value[c];
+            ++cloud;
+        }
 
+        SGPropertyNode *layer;
+        layer = metar_clouds->getChild("layer", i, true);
+        layer->setStringValue("coverage", coverage);
+        layer->setDoubleValue("elevation-ft", elevation);
+        layer->setDoubleValue("thickness-ft", thickness);
+        layer->setDoubleValue("span-m", span);
+    }
     fgSetDouble("/environment/metar/rain-norm", m->getRain());
     fgSetDouble("/environment/metar/hail-norm", m->getHail());
     fgSetDouble("/environment/metar/snow-norm", m->getSnow());
index 5d7df98b072139e99d8b84f59ed05e0546cc81bc..e6e32e43559792ac1545062eccc106cc6cb55fb0 100644 (file)
@@ -48,13 +48,13 @@ extern SGSky *thesky;
 
 
 FGClouds::FGClouds(FGEnvironmentCtrl * controller) :
-       station_elevation_ft(0.0),
-       _controller( controller ),
-       snd_lightning(NULL),
+    snd_lightning(0),
+    _controller(controller),
+    station_elevation_ft(0.0),
     clouds_3d_enabled(false),
     last_scenario( "unset" ),
-    last_env_config( new SGPropertyNode() ),
-    last_env_clouds( new SGPropertyNode() )
+    last_env_config( new SGPropertyNode ),
+    last_env_clouds( new SGPropertyNode )
 {
        update_event = 0;
 }
@@ -252,8 +252,8 @@ void FGClouds::buildLayer(int iLayer, const string& name, double alt, double cov
 
 void FGClouds::buildCloudLayers(void) {
        SGPropertyNode *metar_root = fgGetNode("/environment", true);
-        
-       double wind_speed_kt     = metar_root->getDoubleValue("wind-speed-kt");
+
+       //double wind_speed_kt   = metar_root->getDoubleValue("wind-speed-kt");
        double temperature_degc  = metar_root->getDoubleValue("temperature-sea-level-degc");
        double dewpoint_degc     = metar_root->getDoubleValue("dewpoint-sea-level-degc");
        double pressure_mb              = metar_root->getDoubleValue("pressure-sea-level-inhg") * SG_INHG_TO_PA / 100.0;
@@ -266,8 +266,6 @@ void FGClouds::buildCloudLayers(void) {
        double cumulus_base = 122.0 * (temperature_degc - dewpoint_degc);
        double stratus_base = 100.0 * (100.0 - rel_humidity) * SG_FEET_TO_METER;
 
-       bool cu_seen = false;
-
        for(int iLayer = 0 ; iLayer < thesky->get_cloud_layer_count(); iLayer++) {
                SGPropertyNode *cloud_root = fgGetNode("/environment/clouds/layer", iLayer, true);
 
@@ -325,9 +323,6 @@ void
 FGClouds::update_metar_properties( const FGMetar *m )
 {
     int i;
-    int j;
-    double d;
-    char s[128];
 
     fgSetString("/environment/metar/station-id", m->getId());
     fgSetDouble("/environment/metar/min-visibility-m",
@@ -335,17 +330,15 @@ FGClouds::update_metar_properties( const FGMetar *m )
     fgSetDouble("/environment/metar/max-visibility-m",
                 m->getMaxVisibility().getVisibility_m() );
 
+    SGPropertyNode *metar = fgGetNode("/environment/metar", true);
     const SGMetarVisibility *dirvis = m->getDirVisibility();
-    for (i = 0; i < 8; i++, dirvis++) {
-        const char *min = "/environment/metar/visibility[%d]/min-m";
-        const char *max = "/environment/metar/visibility[%d]/max-m";
 
-        d = dirvis->getVisibility_m();
+    for (i = 0; i < 8; i++, dirvis++) {
+        SGPropertyNode *vis = metar->getChild("visibility", i, true);
+        double v = dirvis->getVisibility_m();
 
-        snprintf(s, 128, min, i);
-        fgSetDouble(s, d);
-        snprintf(s, 128, max, i);
-        fgSetDouble(s, d);
+        vis->setDoubleValue("min-m", v);
+        vis->setDoubleValue("max-m", v);
     }
 
     fgSetInt("/environment/metar/base-wind-range-from",
@@ -366,57 +359,41 @@ FGClouds::update_metar_properties( const FGMetar *m )
                 m->getPressure_inHg() );
 
     vector<SGMetarCloud> cv = m->getClouds();
-    vector<SGMetarCloud>::const_iterator cloud;
+    vector<SGMetarCloud>::const_iterator cloud, cloud_end = cv.end();
 
     // Load into both the METAR and environment properties to stop interpolation
-    const char *cl[] = {"/environment/metar/clouds/layer[%i]",
-                        "/environment/clouds/layer[%i]"};
-
-    for (j = 0; j < 2; j++)
-    {
-        for (i = 0, cloud = cv.begin(); cloud != cv.end(); cloud++, i++) {
-            const char *coverage_string[5] = 
-                { "clear", "few", "scattered", "broken", "overcast" };
-            const double thickness[5] = { 0, 65, 600,750, 1000};
-            int q;
-    
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/coverage", 128);
-            q = cloud->getCoverage();
-            fgSetString(s, coverage_string[q] );
-            
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/elevation-ft", 128);
-            fgSetDouble(s, cloud->getAltitude_ft() + station_elevation_ft);
-            
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/thickness-ft", 128);
-            fgSetDouble(s, thickness[q]);
-            
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/span-m", 128);
-            fgSetDouble(s, 40000.0);
-        }
-    
-        for (; i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
-            
-            
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/coverage", 128);
-            fgSetString(s, "clear");
-    
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/elevation-ft", 128);
-            fgSetDouble(s, -9999);
-    
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/thickness-ft", 128);
-            fgSetDouble(s, 0);
-    
-            snprintf(s, 128, cl[j], i);
-            strncat(s, "/span-m", 128);
-            fgSetDouble(s, 40000.0);
+    SGPropertyNode *metar_clouds = fgGetNode("/environment/metar/clouds", true);
+    SGPropertyNode *clouds = fgGetNode("/environment/clouds", true);
+
+    for (i = 0, cloud = cv.begin(); i < FGEnvironmentMgr::MAX_CLOUD_LAYERS; i++) {
+        const char *coverage_string[5] = { "clear", "few", "scattered", "broken", "overcast" };
+        const double thickness_value[5] = { 0, 65, 600, 750, 1000 };
+
+        const char *coverage = "clear";
+        double elevation = -9999.0;
+        double thickness = 0.0;
+        const double span = 40000.0;
+
+        if (cloud != cloud_end) {
+            int c = cloud->getCoverage();
+            coverage = coverage_string[c];
+            elevation = cloud->getAltitude_ft() + station_elevation_ft;
+            thickness = thickness_value[c];
+            ++cloud;
         }
+
+        SGPropertyNode *layer;
+        layer = metar_clouds->getChild("layer", i, true);
+        layer->setStringValue("coverage", coverage);
+        layer->setDoubleValue("elevation-ft", elevation);
+        layer->setDoubleValue("thickness-ft", thickness);
+        layer->setDoubleValue("span-m", span);
+
+        layer = clouds->getChild("layer", i, true);
+        layer->setStringValue("coverage", coverage);
+        layer->setDoubleValue("elevation-ft", elevation);
+        layer->setDoubleValue("thickness-ft", thickness);
+        layer->setDoubleValue("span-m", span);
     }
 
     fgSetDouble("/environment/metar/rain-norm", m->getRain());