From 8be760b594f5e245e0a69efdef0baddab64193c8 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 19 May 2003 15:40:11 +0000 Subject: [PATCH] Convert char* to string to prevent stdup/malloc/free --- simgear/timing/geocoord.h | 2 +- simgear/timing/sg_time.cxx | 24 +++++++----------------- simgear/timing/sg_time.hxx | 4 ++-- simgear/timing/timezone.cxx | 12 ++++++------ simgear/timing/timezone.h | 14 +++++++------- 5 files changed, 23 insertions(+), 33 deletions(-) diff --git a/simgear/timing/geocoord.h b/simgear/timing/geocoord.h index c01195ab..ce9dc1e9 100644 --- a/simgear/timing/geocoord.h +++ b/simgear/timing/geocoord.h @@ -69,7 +69,7 @@ public: //double getAngle(const GeoCoord& other) const; virtual void print() {} ; - virtual char *getDescription() {return 0;}; + virtual const char * getDescription() {return 0;}; }; typedef vector GeoCoordVector; diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx index 37dc3454..3f43c9a7 100644 --- a/simgear/timing/sg_time.cxx +++ b/simgear/timing/sg_time.cxx @@ -40,6 +40,8 @@ # include #endif +#include + #ifdef HAVE_SYS_TIME_H # include // 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) @@ -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; - free(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; - free(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 ); } diff --git a/simgear/timing/sg_time.hxx b/simgear/timing/sg_time.hxx index f2bab45a..fa0ab060 100644 --- a/simgear/timing/sg_time.hxx +++ b/simgear/timing/sg_time.hxx @@ -72,7 +72,7 @@ private: TimezoneContainer* tzContainer; // Points to the current local timezone name; - char *zonename; + string zonename; // Unix "calendar" time in seconds time_t cur_time; @@ -170,7 +170,7 @@ public: inline time_t get_cur_time() const { return cur_time; }; /** @return time zone name for your current position*/ - inline char* get_zonename() const { return zonename; } + inline const char * get_zonename() const { return zonename.c_str(); } /** @return GMT in a "brokent down" tm structure */ #if defined(_MSC_VER) || defined(__MINGW32__) diff --git a/simgear/timing/timezone.cxx b/simgear/timing/timezone.cxx index 41c596e8..d8ef91d2 100644 --- a/simgear/timing/timezone.cxx +++ b/simgear/timing/timezone.cxx @@ -35,8 +35,8 @@ Timezone::Timezone(float la, float lo, char* cc, char* desc) : GeoCoord(la, lo) { - countryCode = strdup(cc); - descriptor = strdup(desc); + countryCode = cc; + descriptor = desc; } /* Build a timezone object from a textline in zone.tab */ @@ -50,7 +50,7 @@ Timezone::Timezone(const char *infoString) : char latlon[128]; strncpy(buffer, infoString, i); buffer[i] = 0; - countryCode = strdup(buffer); + countryCode = buffer; i ++; int start = i; while (infoString[i] != '\t') { @@ -107,7 +107,7 @@ Timezone::Timezone(const char *infoString) : size = i - start; strncpy(buffer, (&infoString[start]), size); buffer[size] = 0; - descriptor = strdup(buffer); + descriptor = buffer; } /* the copy constructor */ @@ -115,8 +115,8 @@ Timezone::Timezone(const Timezone& other) { lat = other.getLat(); lon = other.getLon(); - countryCode = strdup(other.countryCode); - descriptor = strdup(other.descriptor); + countryCode = other.countryCode; + descriptor = other.descriptor; } diff --git a/simgear/timing/timezone.h b/simgear/timing/timezone.h index ce3521be..5d3035d5 100644 --- a/simgear/timing/timezone.h +++ b/simgear/timing/timezone.h @@ -37,24 +37,24 @@ class Timezone : public GeoCoord { private: - char* countryCode; - char* descriptor; + string countryCode; + string descriptor; public: Timezone() : GeoCoord() { - countryCode = 0; - descriptor = 0; + countryCode.erase(); + descriptor.erase(); }; Timezone(float la, float lo, char* cc, char* desc); Timezone(const char *infoString); Timezone(const Timezone &other); - virtual ~Timezone() { delete [] countryCode; delete [] descriptor; }; + virtual ~Timezone() { }; - virtual void print() { printf("%s", descriptor);}; - virtual char * getDescription() { return descriptor; }; + virtual void print() { printf("%s", descriptor.c_str());}; + virtual const char * getDescription() { return descriptor.c_str(); }; }; /************************************************************************ -- 2.39.5