summaryrefslogtreecommitdiff
path: root/protocols/Weather/src/weather_conv.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'protocols/Weather/src/weather_conv.cpp')
-rw-r--r--protocols/Weather/src/weather_conv.cpp286
1 files changed, 110 insertions, 176 deletions
diff --git a/protocols/Weather/src/weather_conv.cpp b/protocols/Weather/src/weather_conv.cpp
index 649ca37a91..4d2c79f0ce 100644
--- a/protocols/Weather/src/weather_conv.cpp
+++ b/protocols/Weather/src/weather_conv.cpp
@@ -25,12 +25,12 @@ string conversions, display text parsing, etc
#include "stdafx.h"
-//============ SOME HELPER FUNCTIONS ============
-
+/////////////////////////////////////////////////////////////////////////////////////////
// see if a string is a number
// s = the string to be determined
// return value = true if the string is a number, false if it isn't
-BOOL is_number(wchar_t *s)
+
+BOOL is_number(const wchar_t *s)
{
BOOL tag = FALSE;
// looking character by character
@@ -47,7 +47,7 @@ BOOL is_number(wchar_t *s)
return FALSE;
}
-static void numToStr(double num, wchar_t *str, size_t strSize)
+void CWeatherProto::numToStr(double num, wchar_t *str, size_t strSize)
{
int i = (int)(num * (opt.NoFrac ? 10 : 100));
int u = abs(i);
@@ -69,22 +69,18 @@ static void numToStr(double num, wchar_t *str, size_t strSize)
mir_snwprintf(str, strSize, L"%i", w);
}
-//============ UNIT CONVERSIONS ============
-
+/////////////////////////////////////////////////////////////////////////////////////////
// temperature conversion
// tempchar = the string containing the temperature value
// unit = the unit for temperature
// return value = the converted temperature with degree sign and unit; if fails, return N/A
-void GetTemp(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
+
+void CWeatherProto::GetTemp(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str)
{
// unit can be C, F
double temp;
wchar_t tstr[20];
- TrimString(tempchar);
- if (tempchar[0] == '-' && tempchar[1] == ' ')
- memmove(&tempchar[1], &tempchar[2], sizeof(wchar_t) * (mir_wstrlen(&tempchar[2]) + 1));
-
// quit if the value obtained is N/A or not a number
if (!mir_wstrcmp(tempchar, NODATA) || !mir_wstrcmp(tempchar, L"N/A")) {
mir_wstrcpy(str, tempchar);
@@ -123,11 +119,13 @@ void GetTemp(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
}
}
+/////////////////////////////////////////////////////////////////////////////////////////
// temperature conversion
// tempchar = the string containing the pressure value
// unit = the unit for pressure
// return value = the converted pressure with unit; if fail, return the original string
-void GetPressure(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
+
+void CWeatherProto::GetPressure(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str)
{
// unit can be kPa, hPa, mb, in, mm, torr
double tempunit = 0, output;
@@ -180,11 +178,13 @@ void GetPressure(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
}
}
+/////////////////////////////////////////////////////////////////////////////////////////
// speed conversion
// tempchar = the string containing the speed value
// unit = the unit for speed
// return value = the converted speed with unit; if fail, return _T(""
-void GetSpeed(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
+
+void CWeatherProto::GetSpeed(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str)
{
// unit can be km/h, mph, m/s, knots
double tempunit;
@@ -229,11 +229,13 @@ void GetSpeed(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
}
}
+/////////////////////////////////////////////////////////////////////////////////////////
// distance conversion
// tempchar = the string containing the distance value
// unit = the unit for distance
// return value = the converted distance with unit; if fail, return original string
-void GetDist(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
+
+void CWeatherProto::GetDist(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str)
{
// unit can be km, miles
double tempunit = 0, output;
@@ -269,11 +271,13 @@ void GetDist(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
}
}
+/////////////////////////////////////////////////////////////////////////////////////////
// elevation conversion
// tempchar = the string containing the elevation value
// unit = the unit for elevation
// return value = the converted elevation with unit; if fail, return original string
-void GetElev(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
+
+void CWeatherProto::GetElev(const wchar_t *tempchar, const wchar_t *unit, wchar_t *str)
{
// unit can be ft, m
double tempunit = 0, output;
@@ -309,73 +313,9 @@ void GetElev(wchar_t *tempchar, wchar_t *unit, wchar_t *str)
}
}
-//============ CONDITION ICON ASSIGNMENT ============
-
-// assign the contact icon (status) from the condition string
-// the description may be different between different sources
-// cond = the string for weather condition
-// return value = status for the icon (ONLINE, OFFLINE, etc)
-
-static const wchar_t *statusStr[MAX_COND] = { L"Lightning", L"Fog", L"Snow", L"Rain", L"Partly Cloudy", L"Cloudy", L"Sunny", L"N/A", L"Rain Shower", L"Snow Shower"};
-static const uint16_t statusValue[MAX_COND] = { LIGHT, FOG, SNOW, RAIN, PCLOUDY, CLOUDY, SUNNY, NA, RSHOWER, SSHOWER };
-
-uint16_t GetIcon(const wchar_t *cond, WIDATA *Data)
-{
- // set the icon using ini
- for (int i = 0; i < _countof(statusValue); i++)
- if (IsContainedInCondList(cond, &Data->CondList[i]))
- return statusValue[i];
-
- // internal detection
- if (wcsstr(cond, L"mainy sunny") || wcsstr(cond, L"mainy clear") || wcsstr(cond, L"partly sunny") || wcsstr(cond, L"partly cloudy") || wcsstr(cond, L"mostly") || wcsstr(cond, L"clouds"))
- return PCLOUDY;
-
- if (wcsstr(cond, L"sunny") || wcsstr(cond, L"clear") || wcsstr(cond, L"fair"))
- return SUNNY;
-
- if (wcsstr(cond, L"thunder") || wcsstr(cond, L"t-storm"))
- return LIGHT;
-
- if (wcsstr(cond, L"cloud") || wcsstr(cond, L"overcast"))
- return CLOUDY;
-
- if (wcsstr(cond, L"fog") || wcsstr(cond, L"mist") || wcsstr(cond, L"smoke") || wcsstr(cond, L"sand") || wcsstr(cond, L"dust") || wcsstr(cond, L"haze"))
- return FOG;
-
- if (wcsstr(cond, L"snow shower"))
- return SSHOWER;
-
- if (wcsstr(cond, L"snow") || wcsstr(cond, L"ice") || wcsstr(cond, L"freezing") || wcsstr(cond, L"wintry"))
- return SNOW;
-
- if (wcsstr(cond, L"rain shower"))
- return RSHOWER;
-
- if (wcsstr(cond, L"drizzle") || wcsstr(cond, L"rain"))
- return RAIN;
-
- // set the icon using langpack
- for (int i = 0; i < _countof(statusStr)-1; i++) {
- wchar_t LangPackStr[64], LangPackStr1[128];
- int j = 0;
- do {
- j++;
- // using the format _T("# Weather <condition name> <counter> #"
- mir_snwprintf(LangPackStr, L"# Weather %s %i #", statusStr[i], j);
- wcsncpy_s(LangPackStr1, TranslateW(LangPackStr), _TRUNCATE);
- CharLowerBuff(LangPackStr1, (uint32_t)mir_wstrlen(LangPackStr1));
- if (wcsstr(cond, LangPackStr1) != nullptr)
- return statusValue[i];
- // loop until the translation string exists (ie, the translated string is differ from original)
- } while (mir_wstrcmp(TranslateW(LangPackStr), LangPackStr));
- }
-
- return NA;
-}
-
-//============ STRING CONVERSIONS ============
-//
+/////////////////////////////////////////////////////////////////////////////////////////
// this function convert the string to the format with 1 upper case followed by lower case char
+
void CaseConv(wchar_t *str)
{
bool nextUp = true;
@@ -391,9 +331,10 @@ void CaseConv(wchar_t *str)
}
}
+/////////////////////////////////////////////////////////////////////////////////////////
// the next 2 functions are copied from miranda source
// str = the string to modify
-//
+
void TrimString(char *str)
{
size_t len, start;
@@ -414,7 +355,9 @@ void TrimString(wchar_t *str)
memmove(str, str + start, (len - start + 1) * sizeof(wchar_t));
}
+/////////////////////////////////////////////////////////////////////////////////////////
// convert \t to tab and \n to linefeed
+
void ConvertBackslashes(char *str)
{
for (char *pstr = str; *pstr; pstr = CharNextA(pstr)) {
@@ -429,10 +372,12 @@ void ConvertBackslashes(char *str)
}
}
+/////////////////////////////////////////////////////////////////////////////////////////
// replace spaces with _T("%20"
// dis = original string
// return value = the modified string with space -> _T("%20"
-char *GetSearchStr(char *dis)
+
+char* GetSearchStr(char *dis)
{
char *pstr = dis;
size_t len = mir_strlen(dis);
@@ -448,13 +393,13 @@ char *GetSearchStr(char *dis)
return dis;
}
-//============ ICON ASSIGNMENT ============
-//
+/////////////////////////////////////////////////////////////////////////////////////////
// make display and history strings
// w = WEATHERINFO data to be parsed
// dis = the string to parse
// return value = the parsed string
-wchar_t *GetDisplay(WEATHERINFO *w, const wchar_t *dis, wchar_t *str)
+
+CMStringW GetDisplay(WEATHERINFO *w, const wchar_t *dis)
{
wchar_t lpzDate[32], chr;
char name[256], temp[2];
@@ -462,7 +407,7 @@ wchar_t *GetDisplay(WEATHERINFO *w, const wchar_t *dis, wchar_t *str)
size_t i;
// Clear the string
- str[0] = 0;
+ CMStringW str;
// looking character by character
for (i = 0; i < mir_wstrlen(dis); i++) {
@@ -471,10 +416,10 @@ wchar_t *GetDisplay(WEATHERINFO *w, const wchar_t *dis, wchar_t *str)
i++;
chr = dis[i];
switch (chr) {
- case '%': mir_wstrcat(str, L"%"); break;
- case 't': mir_wstrcat(str, L"\t"); break;
- case 'n': mir_wstrcat(str, L"\r\n"); break;
- case '\\': mir_wstrcat(str, L"\\"); break;
+ case '%': str.Append(L"%"); break;
+ case 't': str.Append(L"\t"); break;
+ case 'n': str.Append(L"\r\n"); break;
+ case '\\': str.Append(L"\\"); break;
}
}
@@ -485,29 +430,31 @@ wchar_t *GetDisplay(WEATHERINFO *w, const wchar_t *dis, wchar_t *str)
// turn capitalized characters to small case
if (chr < 'a' && chr != '[' && chr != '%') chr = (char)((int)chr + 32);
switch (chr) {
- case 'c': mir_wstrcat(str, w->cond); break;
+ case 'c': str.Append(w->cond); break;
case 'd': // get the current date
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, nullptr, nullptr, lpzDate, _countof(lpzDate));
- mir_wstrcat(str, lpzDate); break;
- case 'e': mir_wstrcat(str, w->dewpoint); break;
- case 'f': mir_wstrcat(str, w->feel); break;
- case 'h': mir_wstrcat(str, w->high); break;
- case 'i': mir_wstrcat(str, w->winddir); break;
- case 'l': mir_wstrcat(str, w->low); break;
- case 'm': mir_wstrcat(str, w->humid); break;
- case 'n': mir_wstrcat(str, w->city); break;
- case 'p': mir_wstrcat(str, w->pressure); break;
- case 'r': mir_wstrcat(str, w->sunrise); break;
- case 's': mir_wstrcat(str, w->id); break;
- case 't': mir_wstrcat(str, w->temp); break;
+ str.Append(lpzDate); break;
+ case 'e': str.Append(w->dewpoint); break;
+ case 'f': str.Append(w->feel); break;
+ case 'h': str.Append(w->high); break;
+ case 'i': str.Append(w->winddir); break;
+ case 'l': str.Append(w->low); break;
+ case 'm': str.Append(w->humid); break;
+ case 'n': str.Append(w->city); break;
+ case 'p': str.Append(w->pressure); break;
+ case 'r': str.Append(w->sunrise); break;
+ case 's': str.Append(w->id); break;
+ case 't': str.Append(w->temp); break;
case 'u':
- if (mir_wstrcmp(w->update, NODATA)) mir_wstrcat(str, w->update);
- else mir_wstrcat(str, TranslateT("<unknown time>"));
+ if (mir_wstrcmp(w->update, NODATA))
+ str.Append(w->update);
+ else
+ str.Append(TranslateT("<unknown time>"));
break;
- case 'v': mir_wstrcat(str, w->vis); break;
- case 'w': mir_wstrcat(str, w->wind); break;
- case 'y': mir_wstrcat(str, w->sunset); break;
- case '%': mir_wstrcat(str, L"%"); break;
+ case 'v': str.Append(w->vis); break;
+ case 'w': str.Append(w->wind); break;
+ case 'y': str.Append(w->sunset); break;
+ case '%': str.Append(L"%"); break;
case '[': // custom variables
i++;
name[0] = 0;
@@ -519,90 +466,77 @@ wchar_t *GetDisplay(WEATHERINFO *w, const wchar_t *dis, wchar_t *str)
// access the database to get its value
if (!db_get_ws(w->hContact, WEATHERCONDITION, name, &dbv)) {
if (dbv.pwszVal != TranslateW(NODATA) && dbv.pwszVal != TranslateT("<Error>"))
- mir_wstrcat(str, dbv.pwszVal);
+ str.Append(dbv.pwszVal);
db_free(&dbv);
}
break;
}
}
// if the character is not a variable, write the original character to the new string
- else {
- mir_snwprintf(lpzDate, L"%c", dis[i]);
- mir_wstrcat(str, lpzDate);
- }
+ else str.AppendChar(dis[i]);
}
return str;
}
-wchar_t svcReturnText[MAX_TEXT_SIZE];
-INT_PTR GetDisplaySvcFunc(WPARAM wParam, LPARAM lParam)
-{
- WEATHERINFO winfo = LoadWeatherInfo(wParam);
- return (INT_PTR)GetDisplay(&winfo, (wchar_t*)lParam, svcReturnText);
-}
-
-//============ ID MANAGEMENT ============
-//
-// get service data module internal name
-// mod/id <- the mod part
-// pszID = original 2-part id, return the service internal name
-void GetSvc(wchar_t *pszID)
-{
- wchar_t *chop = wcschr(pszID, '/');
- if (chop != nullptr)
- *chop = '\0';
- else
- pszID[0] = 0;
-}
-
-// get the id use for update without the service internal name
-// mod/id <- the id part
-// pszID = original 2-part id, return the single part id
-void GetID(wchar_t *pszID)
-{
- wchar_t *chop = wcschr(pszID, '/');
- if (chop != nullptr)
- mir_wstrcpy(pszID, chop + 1);
- else
- pszID[0] = 0;
-}
-
-//============ WEATHER ERROR CODE ============
-//
+/////////////////////////////////////////////////////////////////////////////////////////
// Get the text when an error code is specified
// code = the error code obtained when updating weather
// str = the string for the error
-//
+
wchar_t *GetError(int code)
{
wchar_t *str, str2[100];
switch (code) {
- case 10: str = E10; break;
- case 11: str = E11; break;
- case 12: str = E12; break;
- case 20: str = E20; break;
- case 30: str = E30; break;
- case 40: str = E40; break;
- case 42: str = E42; break;
- case 43: str = E43; break;
- case 99: str = E99; break;
- case 204: str = E204; break;
- case 301: str = E301; break;
- case 305: str = E305; break;
- case 307: str = E307; break;
- case 400: str = E400; break;
- case 401: str = E401; break;
- case 402: str = E402; break;
- case 403: str = E403; break;
- case 404: str = E404; break;
- case 405: str = E405; break;
- case 407: str = E407; break;
- case 410: str = E410; break;
- case 500: str = E500; break;
- case 502: str = E502; break;
- case 503: str = E503; break;
- case 504: str = E504; break;
+ case 10: str = TranslateT("Invalid ID format, missing \"/\" (10)"); break;
+ case 11: str = TranslateT("Invalid service (11)"); break;
+ case 12: str = TranslateT("Invalid station (12)"); break;
+ case 20: str = TranslateT("Weather service ini for this station is not found (20)"); break;
+ case 30: str = TranslateT("Netlib error - check your internet connection (30)"); break;
+ case 40: str = TranslateT("Empty data is retrieved (40)"); break;
+ case 42: str = TranslateT("Document not found (42)"); break;
+ case 43: str = TranslateT("Document too short to contain any weather data (43)"); break;
+ case 99: str = TranslateT("Unknown error (99)"); break;
+ // 100 Continue
+ // 101 Switching Protocols
+ // 200 OK
+ // 201 Created
+ // 202 Accepted
+ // 203 Non-Authoritative Information
+ case 204: str = TranslateT("HTTP Error: No content (204)"); break;
+ // 205 Reset Content
+ // 206 Partial Content
+ // 300 Multiple Choices
+ case 301: str = TranslateT("HTTP Error: Data moved (301)"); break;
+ // 302 Found
+ // 303 See Other
+ // 304 Not Modified
+ case 305: str = TranslateT("HTTP Error: Use proxy (305)"); break;
+ case 307: str = TranslateT("HTTP Error: Temporary redirect (307)"); break;
+ case 400: str = TranslateT("HTTP Error: Bad request (400)"); break;
+ case 401: str = TranslateT("HTTP Error: Unauthorized (401)"); break;
+ case 402: str = TranslateT("HTTP Error: Payment required (402)"); break;
+ case 403: str = TranslateT("HTTP Error: Forbidden (403)"); break;
+ case 404: str = TranslateT("HTTP Error: Not found (404)"); break;
+ case 405: str = TranslateT("HTTP Error: Method not allowed (405)"); break;
+ // 406 Not Acceptable
+ case 407: str = TranslateT("HTTP Error: Proxy authentication required (407)"); break;
+ // 408 Request Timeout
+ // 409 Conflict
+ case 410: str = TranslateT("HTTP Error: Gone (410)"); break;
+ // 411 Length Required
+ // 412 Precondition Failed
+ // 413 Request Entity Too Large
+ // 414 Request-URI Too Long
+ // 415 Unsupported Media Type
+ // 416 Requested Range Not Satisfiable
+ // 417 Expectation Failed
+ case 500: str = TranslateT("HTTP Error: Internal server error (500)"); break;
+ // 501 Not Implemented
+ case 502: str = TranslateT("HTTP Error: Bad gateway (502)"); break;
+ case 503: str = TranslateT("HTTP Error: Service unavailable (503)"); break;
+ case 504: str = TranslateT("HTTP Error: Gateway timeout (504)"); break;
+ // 505 HTTP Version Not Supported
default:
mir_snwprintf(str2, TranslateT("HTTP Error %i"), code);
str = str2;