Convert char* to string to prevent stdup/malloc/free

This commit is contained in:
ehofman
2003-05-19 15:40:11 +00:00
parent 42b4ec310f
commit 8be760b594
5 changed files with 23 additions and 33 deletions

View File

@@ -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<GeoCoord*> GeoCoordVector;

View 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)
@@ -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 );
}

View File

@@ -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__)

View File

@@ -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;
}

View File

@@ -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(); };
};
/************************************************************************