]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timezone.cxx
Updates to build system to better support automake-1.5
[simgear.git] / simgear / timing / timezone.cxx
1 /* -*- Mode: C++ -*- *****************************************************
2  * timezone.cc
3  * Written by Durk Talsma. Started July 1999.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA  02111-1307, USA.
19  *
20  **************************************************************************/
21
22 /*************************************************************************
23  *
24  * Timezone is derived from geocoord, and stores the timezone centerpoint,
25  * as well as the countrycode and the timezone descriptor. The latter is 
26  * used in order to get the local time. 
27  *
28  ************************************************************************/
29
30 #include <errno.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include "timezone.h"
34
35 Timezone::Timezone(float la, float lo, char* cc, char* desc) :
36     GeoCoord(la, lo)
37
38     countryCode = strdup(cc);
39     descriptor = strdup(desc);
40 }
41
42 /* Build a timezone object from a textline in zone.tab */
43 Timezone::Timezone(const char *infoString) :
44     GeoCoord()
45 {
46     int i = 0;
47     while (infoString[i] != '\t')
48         i++;
49     char buffer[128];
50     char latlon[128];
51     strncpy(buffer, infoString, i);
52     buffer[i] = 0;
53     countryCode = strdup(buffer);
54     i ++;
55     int start = i;
56     while (infoString[i] != '\t') {
57         i++;
58     }
59     int size = i - start;
60     strncpy(latlon, (&infoString[start]), size);
61     latlon[size] = 0;
62     char sign;
63     sign = latlon[0];
64     strncpy(buffer, &latlon[1], 2);
65     lat = atof(buffer);
66     strncpy(buffer, &latlon[3], 2);
67     lat += (atof(buffer) / 60);
68     int nextPos;
69     if (strlen(latlon) > 12) {
70         nextPos = 7;
71         strncpy(buffer, &latlon[5], 2);
72         lat += (atof(buffer) / 3600.0);
73     } else {
74         nextPos = 5;
75     }
76     if (sign == '-') {
77         lat = -lat;
78     }
79
80     sign = latlon[nextPos];
81     nextPos++;
82     strncpy(buffer, &latlon[nextPos], 3);
83     lon = atof(buffer);
84     nextPos += 3;
85     strncpy(buffer, &latlon[nextPos], 2);
86     buffer[2] = 0;
87  
88     lon  += (atof(buffer) / 60);
89     if (strlen(latlon) > 12) {
90         nextPos += 2;
91         strncpy(buffer, &latlon[nextPos], 2); 
92         lon +=  (atof (buffer) / 3600.00);
93     }
94     if (sign == '-') {
95         lon = -lon;
96     }
97     i ++;
98     start = i;
99     while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
100         i++;
101     }
102     size = i - start;
103     strncpy(buffer, (&infoString[start]), size);
104     buffer[size] = 0;
105     descriptor = strdup(buffer);
106 }
107
108 /* the copy constructor */
109 Timezone::Timezone(const Timezone& other)
110 {
111     lat = other.getLat();
112     lon = other.getLon();
113     countryCode = strdup(other.countryCode);
114     descriptor = strdup(other.descriptor);
115 }
116
117
118 /********* Member functions for TimezoneContainer class ********/
119
120 TimezoneContainer::TimezoneContainer(const char *filename)
121 {
122     char buffer[256];
123     FILE* infile = fopen(filename, "rb");
124     if (!(infile)) {
125         fprintf(stderr, "Unable to open file %s\n", filename);
126         exit(1);
127     } else { 
128         errno = 0;
129     
130         while (1) {
131             fgets(buffer, 256, infile);
132             if (feof(infile)) {
133                 break;
134             }
135 #ifdef _MSC_VER
136             if( buffer[0] == '#' )
137                continue;
138 #else
139             for (int i = 0; i < 256; i++) {
140                 if (buffer[i] == '#') {
141                     buffer[i] = 0;
142                 }
143             }
144 #endif
145             if (buffer[0]) {
146                 data.push_back(new Timezone(buffer));
147             }
148         }
149         if ( errno ) {
150             perror( "TimezoneContainer()" );
151             errno = 0;
152         }
153     }
154 }
155
156 TimezoneContainer::~TimezoneContainer()
157 {
158 }