X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Ftiming%2Ftimezone.cxx;h=0b2e74c6c5ad0094a2fcaff6841b5c78f2a56fe8;hb=7e7ce2f38e87d6244e05730fa4382da088bb25f1;hp=51951a15be9d303156728762dfeba73ac5606878;hpb=82459a2802a939434f9fc5b88d2b2aea58f0043d;p=simgear.git diff --git a/simgear/timing/timezone.cxx b/simgear/timing/timezone.cxx index 51951a15..0b2e74c6 100644 --- a/simgear/timing/timezone.cxx +++ b/simgear/timing/timezone.cxx @@ -12,137 +12,152 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * **************************************************************************/ /************************************************************************* * - * Timezone is derived from geocoord, and stores the timezone centerpoint, + * SGTimeZone is derived from geocoord, and stores the timezone centerpoint, * as well as the countrycode and the timezone descriptor. The latter is * used in order to get the local time. * ************************************************************************/ + +#include +#include #include -#include "timezone.h" +#include -Timezone::Timezone(float la, float lo, char* cc, char* desc) : - GeoCoord(la, lo) +#include "timezone.h" + +SGTimeZone::SGTimeZone(float la, float lo, char* cc, char* desc) : + SGGeoCoord(la, lo) { - countryCode = strdup(cc); - descriptor = strdup(desc); + countryCode = cc; + descriptor = desc; } /* Build a timezone object from a textline in zone.tab */ -Timezone::Timezone(const char *infoString) : - GeoCoord() +SGTimeZone::SGTimeZone(const char *infoString) : + SGGeoCoord() { - int i = 0; - while (infoString[i] != '\t') - i++; - char buffer[128]; - char latlon[128]; - strncpy(buffer, infoString, i); - buffer[i] = 0; - countryCode = strdup(buffer); - i ++; - int start = i; - while (infoString[i] != '\t') - i++; - int size = i - start; - strncpy(latlon, (&infoString[start]), size); - latlon[size] = 0; - char sign; - sign = latlon[0]; - strncpy(buffer, &latlon[1], 2); - lat = atof(buffer); - strncpy(buffer, &latlon[3], 2); - lat += (atof(buffer) / 60); - int nextPos; - if (strlen(latlon) > 12) - { - nextPos = 7; - strncpy(buffer, &latlon[5], 2); - lat += (atof(buffer) / 3600.0); + int i = 0; + while (infoString[i] != '\t') + i++; + char buffer[128]; + char latlon[128]; + strncpy(buffer, infoString, i); + buffer[i] = 0; + countryCode = buffer; + i ++; + int start = i; + while (infoString[i] != '\t') { + i++; + } + int size = i - start; + strncpy(latlon, (&infoString[start]), size); + latlon[size] = 0; + char sign; + sign = latlon[0]; + strncpy(buffer, &latlon[1], 2); + buffer[2] = 0; + lat = atof(buffer); + strncpy(buffer, &latlon[3], 2); + buffer[2] = 0; + lat += (atof(buffer) / 60); + int nextPos; + if (strlen(latlon) > 12) { + nextPos = 7; + strncpy(buffer, &latlon[5], 2); + buffer[2] = 0; + lat += (atof(buffer) / 3600.0); + } else { + nextPos = 5; + } + if (sign == '-') { + lat = -lat; } - else - nextPos = 5; - if (sign == '-') - lat = -lat; - sign = latlon[nextPos]; - nextPos++; - strncpy(buffer, &latlon[nextPos], 3); - lon = atof(buffer); - nextPos += 3; - strncpy(buffer, &latlon[nextPos], 2); - buffer[2] = 0; + sign = latlon[nextPos]; + nextPos++; + strncpy(buffer, &latlon[nextPos], 3); + buffer[3] = 0; + lon = atof(buffer); + nextPos += 3; + strncpy(buffer, &latlon[nextPos], 2); + buffer[2] = 0; - lon += (atof(buffer) / 60); - if (strlen(latlon) > 12) - { - nextPos += 2; - strncpy(buffer, &latlon[nextPos], 2); - lon += (atof (buffer) / 3600.00); + lon += (atof(buffer) / 60); + if (strlen(latlon) > 12) { + nextPos += 2; + strncpy(buffer, &latlon[nextPos], 2); + buffer[2] = 0; + lon += (atof (buffer) / 3600.00); + } + if (sign == '-') { + lon = -lon; + } + i ++; + start = i; + while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) { + i++; } - if (sign == '-') - lon = -lon; - i ++; - start = i; - while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) - i++; - size = i - start; - strncpy(buffer, (&infoString[start]), size); - buffer[size] = 0; - descriptor = strdup(buffer); + size = i - start; + strncpy(buffer, (&infoString[start]), size); + buffer[size] = 0; + descriptor = buffer; } /* the copy constructor */ -Timezone::Timezone(const Timezone& other) +SGTimeZone::SGTimeZone(const SGTimeZone& other) { - lat = other.getLat(); - lon = other.getLon(); - countryCode = strdup(other.countryCode); - descriptor = strdup(other.descriptor); + lat = other.getLat(); + lon = other.getLon(); + countryCode = other.countryCode; + descriptor = other.descriptor; } -/********* Member functions for TimezoneContainer class ********/ +/********* Member functions for SGTimeZoneContainer class ********/ -TimezoneContainer::TimezoneContainer(const char *filename) +SGTimeZoneContainer::SGTimeZoneContainer(const char *filename) { - char buffer[256]; - FILE* infile = fopen(filename, "r"); - if (!(infile)) - { - fprintf(stderr, "Unable to open file %s\n", filename); - exit(1); - } - else - { - - while (1) - { - fgets(buffer, 256, infile); - if (feof(infile)) - break; - for (int i = 0; i < 256; i++) - { - if (buffer[i] == '#') - buffer[i] = 0; - } - if (buffer[0]) - { - data.push_back(new Timezone(buffer)); - } - } + char buffer[256]; + FILE* infile = fopen(filename, "rb"); + if (!(infile)) { + string e = "Unable to open time zone file '"; + throw sg_exception(e + filename + '\''); + + } else { + errno = 0; + + while (1) { + fgets(buffer, 256, infile); + if (feof(infile)) { + break; + } + for (char *p = buffer; *p; p++) { + if (*p == '#') { + *p = 0; + break; + } + } + if (buffer[0]) { + data.push_back(new SGTimeZone(buffer)); + } + } + if ( errno ) { + perror( "SGTimeZoneContainer()" ); + errno = 0; + } } + fclose(infile); } -TimezoneContainer::~TimezoneContainer() +SGTimeZoneContainer::~SGTimeZoneContainer() { }