00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <errno.h>
00030 #include <string.h>
00031 #include <stdio.h>
00032
00033 #include <simgear/structure/exception.hxx>
00034
00035 #include "timezone.h"
00036
00037 SGTimeZone::SGTimeZone(float la, float lo, char* cc, char* desc) :
00038 SGGeoCoord(la, lo)
00039 {
00040 countryCode = cc;
00041 descriptor = desc;
00042 }
00043
00044
00045 SGTimeZone::SGTimeZone(const char *infoString) :
00046 SGGeoCoord()
00047 {
00048 int i = 0;
00049 while (infoString[i] != '\t')
00050 i++;
00051 char buffer[128];
00052 char latlon[128];
00053 strncpy(buffer, infoString, i);
00054 buffer[i] = 0;
00055 countryCode = buffer;
00056 i ++;
00057 int start = i;
00058 while (infoString[i] != '\t') {
00059 i++;
00060 }
00061 int size = i - start;
00062 strncpy(latlon, (&infoString[start]), size);
00063 latlon[size] = 0;
00064 char sign;
00065 sign = latlon[0];
00066 strncpy(buffer, &latlon[1], 2);
00067 buffer[2] = 0;
00068 lat = atof(buffer);
00069 strncpy(buffer, &latlon[3], 2);
00070 buffer[2] = 0;
00071 lat += (atof(buffer) / 60);
00072 int nextPos;
00073 if (strlen(latlon) > 12) {
00074 nextPos = 7;
00075 strncpy(buffer, &latlon[5], 2);
00076 buffer[2] = 0;
00077 lat += (atof(buffer) / 3600.0);
00078 } else {
00079 nextPos = 5;
00080 }
00081 if (sign == '-') {
00082 lat = -lat;
00083 }
00084
00085 sign = latlon[nextPos];
00086 nextPos++;
00087 strncpy(buffer, &latlon[nextPos], 3);
00088 buffer[3] = 0;
00089 lon = atof(buffer);
00090 nextPos += 3;
00091 strncpy(buffer, &latlon[nextPos], 2);
00092 buffer[2] = 0;
00093
00094 lon += (atof(buffer) / 60);
00095 if (strlen(latlon) > 12) {
00096 nextPos += 2;
00097 strncpy(buffer, &latlon[nextPos], 2);
00098 buffer[2] = 0;
00099 lon += (atof (buffer) / 3600.00);
00100 }
00101 if (sign == '-') {
00102 lon = -lon;
00103 }
00104 i ++;
00105 start = i;
00106 while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
00107 i++;
00108 }
00109 size = i - start;
00110 strncpy(buffer, (&infoString[start]), size);
00111 buffer[size] = 0;
00112 descriptor = buffer;
00113 }
00114
00115
00116 SGTimeZone::SGTimeZone(const SGTimeZone& other)
00117 {
00118 lat = other.getLat();
00119 lon = other.getLon();
00120 countryCode = other.countryCode;
00121 descriptor = other.descriptor;
00122 }
00123
00124
00125
00126
00127 SGTimeZoneContainer::SGTimeZoneContainer(const char *filename)
00128 {
00129 char buffer[256];
00130 FILE* infile = fopen(filename, "rb");
00131 if (!(infile)) {
00132 string e = "Unable to open time zone file '";
00133 throw sg_exception(e + filename + '\'');
00134
00135 } else {
00136 errno = 0;
00137
00138 while (1) {
00139 fgets(buffer, 256, infile);
00140 if (feof(infile)) {
00141 break;
00142 }
00143 for (char *p = buffer; *p; p++) {
00144 if (*p == '#') {
00145 *p = 0;
00146 break;
00147 }
00148 }
00149 if (buffer[0]) {
00150 data.push_back(new SGTimeZone(buffer));
00151 }
00152 }
00153 if ( errno ) {
00154 perror( "SGTimeZoneContainer()" );
00155 errno = 0;
00156 }
00157 }
00158 fclose(infile);
00159 }
00160
00161 SGTimeZoneContainer::~SGTimeZoneContainer()
00162 {
00163 }