timezone.cxx

00001 /* -*- Mode: C++ -*- *****************************************************
00002  * timezone.cc
00003  * Written by Durk Talsma. Started July 1999.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  *
00019  **************************************************************************/
00020 
00021 /*************************************************************************
00022  *
00023  * SGTimeZone is derived from geocoord, and stores the timezone centerpoint,
00024  * as well as the countrycode and the timezone descriptor. The latter is 
00025  * used in order to get the local time. 
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 /* Build a timezone object from a textline in zone.tab */
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 /* the copy constructor */
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 /********* Member functions for SGTimeZoneContainer class ********/
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 }

Generated on Mon Dec 17 09:30:56 2007 for SimGear by  doxygen 1.5.1