]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timezone.cxx
Clean up constructor a bit.
[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 #include <stdio.h>
30 #include "timezone.h"
31
32
33 Timezone::Timezone(float la, float lo, char* cc, char* desc) :
34   GeoCoord(la, lo)
35
36   countryCode = strdup(cc);
37   descriptor = strdup(desc);
38 }
39
40 /* Build a timezone object from a textline in zone.tab */
41 Timezone::Timezone(const char *infoString) :
42   GeoCoord()
43 {
44   int i = 0;
45   while (infoString[i] != '\t')
46     i++;
47   char buffer[128];
48   char latlon[128];
49   strncpy(buffer, infoString, i);
50   buffer[i] = 0;
51   countryCode = strdup(buffer);
52   i ++;
53   int start = i;
54   while (infoString[i] != '\t')
55     i++;
56   int size = i - start;
57   strncpy(latlon, (&infoString[start]), size);
58   latlon[size] = 0;
59   char sign;
60   sign = latlon[0];
61   strncpy(buffer, &latlon[1], 2);
62   lat = atof(buffer);
63   strncpy(buffer, &latlon[3], 2);
64   lat += (atof(buffer) / 60);
65   int nextPos;
66   if (strlen(latlon) > 12)
67     {
68       nextPos = 7;
69       strncpy(buffer, &latlon[5], 2);
70       lat += (atof(buffer) / 3600.0);
71     }
72   else
73     nextPos = 5;
74   if (sign == '-')
75     lat = -lat;
76
77   sign = latlon[nextPos];
78   nextPos++;
79   strncpy(buffer, &latlon[nextPos], 3);
80   lon = atof(buffer);
81   nextPos += 3;
82   strncpy(buffer, &latlon[nextPos], 2);
83   buffer[2] = 0;
84  
85   lon  += (atof(buffer) / 60);
86   if (strlen(latlon) > 12)
87     {
88       nextPos += 2;
89       strncpy(buffer, &latlon[nextPos], 2); 
90       lon +=  (atof (buffer) / 3600.00);
91     }
92   if (sign == '-')
93     lon = -lon;
94   i ++;
95   start = i;
96   while (!((infoString[i] == '\t') || (infoString[i] == '\n')))
97     i++;
98   size = i - start;
99   strncpy(buffer, (&infoString[start]), size);
100   buffer[size] = 0;
101   descriptor = strdup(buffer);
102 }
103
104 /* the copy constructor */
105 Timezone::Timezone(const Timezone& other)
106 {
107   lat = other.getLat();
108   lon = other.getLon();
109   countryCode = strdup(other.countryCode);
110   descriptor = strdup(other.descriptor);
111 }
112
113
114 /********* Member functions for TimezoneContainer class ********/
115
116 TimezoneContainer::TimezoneContainer(const char *filename)
117 {
118   char buffer[256];
119   FILE* infile = fopen(filename, "r");
120   if (!(infile))
121     {
122       fprintf(stderr, "Unable to open file %s\n", filename);
123       exit(1);
124     }
125   else
126     { 
127       
128       while (1)
129         {
130           fgets(buffer, 256, infile);
131           if (feof(infile))
132             break;
133           for (int i = 0; i < 256; i++)
134             {
135               if (buffer[i] == '#')
136                 buffer[i] = 0;
137             }
138           if (buffer[0])
139             {
140               data.push_back(new Timezone(buffer));
141             }
142         }
143     }
144 }
145
146 TimezoneContainer::~TimezoneContainer()
147 {
148 }