]> git.mxchange.org Git - simgear.git/blobdiff - simgear/timing/sg_time.cxx
Convert char* to string to prevent stdup/malloc/free
[simgear.git] / simgear / timing / sg_time.cxx
index e87d19475886ed4a48fa66a04f437cf23806a928..3f43c9a7e5c7b732d31e7aba307651655d8c7fb3 100644 (file)
@@ -40,6 +40,8 @@
 #  include <stdlib.h>
 #endif
 
+#include <string>
+
 #ifdef HAVE_SYS_TIME_H
 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
 #endif
@@ -60,7 +62,6 @@
 #include "timezone.h"
 #include "lowleveltime.h"
 
-
 #define DEGHR(x)        ((x)/15.)
 #define RADHR(x)        DEGHR(x*SGD_RADIANS_TO_DEGREES)
 
@@ -83,12 +84,12 @@ void SGTime::init( double lon, double lat,
        cur_time = time(NULL); 
     }
 
-    cout << "Current greenwich mean time = " << asctime(gmtime(&cur_time))
-         << endl;
-    cout << "Current local time          = " 
-         << asctime(localtime(&cur_time)) << endl;
+    SG_LOG( SG_EVENT, SG_INFO,
+                "Current greenwich mean time = " << asctime(gmtime(&cur_time)));
+    SG_LOG( SG_EVENT, SG_INFO,
+             "Current local time          = " << asctime(localtime(&cur_time)));
 
-    if ( root != (string)"" ) {
+    if ( !root.empty()) {
         SGPath zone( root );
         zone.append( "zone.tab" );
         SG_LOG( SG_EVENT, SG_INFO, "Reading timezone info from: "
@@ -100,12 +101,12 @@ void SGTime::init( double lon, double lat,
 
         SGPath name( root );
         name.append( nearestTz->getDescription() );
-        zonename = strdup( name.c_str() );
+        zonename = name.str();
         SG_LOG( SG_EVENT, SG_INFO, "Using zonename = " << zonename );
     } else {
         SG_LOG( SG_EVENT, SG_INFO, "*** NO TIME ZONE NAME ***" );
         tzContainer = NULL;
-        zonename = NULL;
+        zonename.erase();
     }
 }
 
@@ -132,12 +133,6 @@ SGTime::~SGTime()
         tzContainer = NULL;
        delete tmp;
     }
-
-    if ( zonename != NULL ) {
-        char *tmp = zonename;
-        zonename = NULL;
-       delete tmp;
-    }
 }
 
 
@@ -299,17 +294,12 @@ void SGTime::updateLocal( double lon, double lat, const string& root ) {
     GeoCoord* nearestTz = tzContainer->getNearest(location);
     SGPath zone( root );
     zone.append ( nearestTz->getDescription() );
-    if ( zonename ) {
-        char *ptr = zonename;
-        zonename = NULL;
-        delete ptr;
-    }
-    zonename = strdup( zone.c_str() );
+    zonename = zone.str();
 
     //Avoid troubles when zone.tab hasn't got the right line endings
-    if (zonename[strlen(zonename)-1] == '\r')
+    if (zonename[zonename.size()-1] == '\r')
     {
-      zonename[strlen(zonename)-1]=0;
+      zonename[zonename.size()-1]=0;
       zone.set( zonename );
     }