summaryrefslogtreecommitdiffstats
path: root/kweather/stationdatabase.cpp
blob: a30a1f29a2942e9fa13e206b9c8ce1a3728b8e23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
//
// C++ Implementation: $MODULE$
//
// Description:
//
//
// Author: ian reinhart geiser <geiseri@yahoo.com>, (C) 2003
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "stationdatabase.h"

#include <tqstringlist.h>
#include <tqfile.h>
#include <kdebug.h>

class StationInfo
{
	public:
		TQString cityName;
		TQString country;
		TQString longitude;
		TQString latitude;
		StationInfo () {}
};

StationDatabase::StationDatabase(const TQString path) : mPath(path)
{
}


StationDatabase::~StationDatabase()
{
}

bool StationDatabase::loadStation( const TQString & stationID )
{
	TQFile file( mPath );
	bool found = FALSE;

	if ( !file.open( IO_ReadOnly ) )
		return false;

	TQTextStream stream( &file );
	stream.setEncoding( TQTextStream::UnicodeUTF8 );
	TQString line;
	while ( !stream.eof() )
	{
		line = stream.readLine(); // line of text excluding '\n'
		TQStringList data = TQStringList::split( ";", line, true );

		if ( data[ 0 ].stripWhiteSpace() == stationID )
		{
			StationInfo station;
			station.cityName = data[ 3 ].stripWhiteSpace();
			station.country = data[ 5 ].stripWhiteSpace();
			station.latitude = data[ 7 ].stripWhiteSpace();
			station.longitude = data[ 8 ].stripWhiteSpace();

			theDB.insert( data[ 0 ], station );
			found = TRUE;
			break;
		}
	}

	file.close();
	return found;
}

/*!
    \fn StationDatabase::stationNameFromID(const TQString& id)
 */
TQString StationDatabase::stationNameFromID( const TQString & stationID )
{
	TQString result;
	
	if ( theDB.tqfind( stationID ) == theDB.end() )
	{
		if ( loadStation( stationID ) )
			result = theDB[ stationID ].cityName;
		else
			result = i18n( "Unknown Station" );
	}
	else
	{
		result = theDB[ stationID ].cityName;
	}

	return result;
}

/*!
    \fn StationDatabase::stationLongitudeFromID( const TQString &stationID)
 */
TQString StationDatabase::stationLongitudeFromID( const TQString & stationID )
{
	TQString result;
	
	if ( theDB.tqfind( stationID ) == theDB.end() )
	{
		if ( loadStation( stationID ) )
			result = theDB[ stationID ].longitude;
		else
			result = i18n( "Unknown Station" );
	}
	else
	{
		result = theDB[ stationID ].longitude;
	}

	return result;
}

/*!
    \fn StationDatabase::stationLatitudeFromID(const TQString &stationID)
 */
TQString StationDatabase::stationLatitudeFromID( const TQString & stationID )
{
	TQString result;
	
	if ( theDB.tqfind( stationID ) == theDB.end() )
	{
		if ( loadStation( stationID ) )
			result = theDB[ stationID ].latitude;
		else
			result = i18n( "Unknown Station" );
	}
	else
	{
		result = theDB[ stationID ].latitude;
	}

	return result;
}

/*!
    \fn StationDatabase::stationCountryFromID( const TQString &stationID)
 */
TQString StationDatabase::stationCountryFromID( const TQString &stationID )
{
	TQString result;
	
	if ( theDB.tqfind( stationID ) == theDB.end() )
	{
		if ( loadStation( stationID ) )
			result = theDB[ stationID ].country;
		else
			result = i18n( "Unknown Station" );
	}
	else
	{
		result = theDB[ stationID ].country;
	}

	return result;
}

TQString StationDatabase::stationIDfromName( const TQString &name )
{
	TQMap<TQString,StationInfo>::Iterator itr = theDB.begin();
	for( ; itr != theDB.end(); ++itr)
	{
	  kdDebug() << "Checking " << itr.data().cityName << endl;
	  if( itr.data().cityName == name )
		return itr.key();
	}
	return "0000";
}