X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Ftiming%2Ftimezone.cxx;h=4fa6746ca2cd742fc245a10bb440606d7207fd3c;hb=a131f442477a11894169933755591674bb7ad5e9;hp=aac090a4466dfb4824b2a2e311758be801d012fe;hpb=51ef4568dd248a6917720a386e658610958e1512;p=simgear.git diff --git a/simgear/timing/timezone.cxx b/simgear/timing/timezone.cxx index aac090a4..4fa6746c 100644 --- a/simgear/timing/timezone.cxx +++ b/simgear/timing/timezone.cxx @@ -12,10 +12,9 @@ * 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. * **************************************************************************/ @@ -27,22 +26,31 @@ * ************************************************************************/ +#ifdef HAVE_CONFIG_H +# include +#endif + #include #include #include +#include + +#include + #include "timezone.h" -SGTimeZone::SGTimeZone(float la, float lo, char* cc, char* desc) : - SGGeoCoord(la, lo) +SGTimeZone::SGTimeZone(const SGGeod& geod, char* cc, char* desc) : + centerpoint(SGVec3d::fromGeod(geod)) { countryCode = cc; descriptor = desc; } /* Build a timezone object from a textline in zone.tab */ -SGTimeZone::SGTimeZone(const char *infoString) : - SGGeoCoord() +SGTimeZone::SGTimeZone(const char *infoString) { + double lat = 0.0, lon = 0.0; + int i = 0; while (infoString[i] != '\t') i++; @@ -108,13 +116,14 @@ SGTimeZone::SGTimeZone(const char *infoString) : strncpy(buffer, (&infoString[start]), size); buffer[size] = 0; descriptor = buffer; + + centerpoint = SGVec3d::fromGeod(SGGeod::fromDeg(lon, lat)); } /* the copy constructor */ SGTimeZone::SGTimeZone(const SGTimeZone& other) { - lat = other.getLat(); - lon = other.getLon(); + centerpoint = other.centerpoint; countryCode = other.countryCode; descriptor = other.descriptor; } @@ -127,33 +136,59 @@ SGTimeZoneContainer::SGTimeZoneContainer(const char *filename) char buffer[256]; FILE* infile = fopen(filename, "rb"); if (!(infile)) { - fprintf(stderr, "Unable to open file %s\n", filename); - exit(1); - } else { - errno = 0; + std::string e = "Unable to open time zone file '"; + throw sg_exception(e + filename + '\''); + } - while (1) { - fgets(buffer, 256, infile); - if (feof(infile)) { + errno = 0; + + while (1) { + if (0 == fgets(buffer, 256, infile)) + break; + if (feof(infile)) { + break; + } + for (char *p = buffer; *p; p++) { + if (*p == '#') { + *p = 0; 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; + if (buffer[0]) { + zones.push_back(new SGTimeZone(buffer)); } } + if ( errno ) { + perror( "SGTimeZoneContainer()" ); + errno = 0; + } + + fclose(infile); } SGTimeZoneContainer::~SGTimeZoneContainer() { + TZVec::iterator it = zones.begin(); + for (; it != zones.end(); ++it) { + delete *it; + } } + +SGTimeZone* SGTimeZoneContainer::getNearest(const SGGeod& ref) const +{ + SGVec3d refCart(SGVec3d::fromGeod(ref)); + SGTimeZone* match = NULL; + double minDist2 = HUGE_VAL; + + TZVec::const_iterator it = zones.begin(); + for (; it != zones.end(); ++it) { + double d2 = distSqr((*it)->cartCenterpoint(), refCart); + if (d2 < minDist2) { + match = *it; + minDist2 = d2; + } + } + + return match; +} +