1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-14 19:39:47 +00:00

weather: changed ifs in parsing routine to switch/case and corrected TCU handling

This commit is contained in:
Cesare Tirabassi 2009-07-07 11:13:07 +02:00
parent 1171ab38a5
commit 476296be5c

View File

@ -37,9 +37,9 @@
#define MAX_LOCATIONS 3 #define MAX_LOCATIONS 3
/* Possible sky conditions */ /* Possible sky conditions */
#define NUM_CC_CODES 7 #define NUM_CC_CODES 6
const char *CC_CODES[NUM_CC_CODES] = const char *CC_CODES[NUM_CC_CODES] =
{"SKC", "CLR", "FEW", "SCT", "BKN", "OVC", "TCU"}; {"SKC", "CLR", "FEW", "SCT", "BKN", "OVC"};
/* Possible weather conditions */ /* Possible weather conditions */
#define NUM_WC_CODES 17 #define NUM_WC_CODES 17
@ -137,154 +137,163 @@ static inline void parse_token(PWEATHER *res, char *token) {
int i; int i;
char s_tmp[64]; char s_tmp[64];
//Check all tokens 2 chars long switch (strlen(token)) {
if (strlen(token) == 2 ) {
//Check if token is a weather condition //Check all tokens 2 chars long
for (i=0; i<2; i++) { case 2:
if (!isalpha(token[i])) break;
}
if (i==2) {
for(i=0; i<NUM_WC_CODES; i++) {
if (!strncmp(token, WC_CODES[i], 2)) {
res->wc=i+1;
break;
}
}
return;
}
//Check for CB //Check if token is a weather condition
if (!strcmp(token, "CB")) { for (i=0; i<2; i++) {
res->cc = 8;
return;
}
}
//Check all tokens 3 chars long
if (strlen(token) == 3 ) {
//Check if token is a modified weather condition
if ((token[0] == '+') || (token[0] == '-')) {
for (i=1; i<3; i++) {
if (!isalpha(token[i])) break; if (!isalpha(token[i])) break;
} }
if (i==3) { if (i==2) {
for(i=0; i<NUM_WC_CODES; i++) { for(i=0; i<NUM_WC_CODES; i++) {
if (!strncmp(&token[1], WC_CODES[i], 2)) { if (!strncmp(token, WC_CODES[i], 2)) {
res->wc=i+1; res->wc=i+1;
break; break;
} }
} }
return; return;
} }
}
//Check for NCD
if (!strcmp(token, "NCD")) {
res->cc = 1;
return;
}
}
//Check all tokens 4 chars long
if (strlen(token) == 4 ) {
//Check if token is an icao
for (i=0; i<4; i++) {
if (!isalpha(token[i])) break;
}
if (i==4) return;
}
//Check all tokens 5 chars long
if (strlen(token) == 5 ) {
//Check for CAVOK
if (!strcmp(token, "CAVOK")) {
res->cc = 1;
return;
}
//Check if token is the temperature
for (i=0; i<2; i++) {
if (!isdigit(token[i])) break;
}
if ((i==2) && (token[2] == '/')) {
for (i=3; i<5; i++) {
if (!isdigit(token[i])) break;
}
if (i==5) {
//First 2 digits gives the air temperature
res->tmpC=atoi(token);
//4th and 5th digits gives the dew point temperature
res->dew=atoi(&token[3]);
//Compute humidity
res->hmid = rel_humidity(res->dew, res->tmpC);
//Convert to Fahrenheit (faster here than in conky.c)
res->tmpF = (res->tmpC*9)/5 + 32;
//Check for CB
if (!strcmp(token, "CB")) {
res->cc = 8;
return; return;
} }
}
break;
//Check if token is the pressure //Check all tokens 3 chars long
if ((token[0] == 'Q') || (token[0] == 'A')) { case 3:
for (i=1; i<5; i++) {
if (!isdigit(token[i])) break; //Check if token is a modified weather condition
} if ((token[0] == '+') || (token[0] == '-')) {
if (i==5) { for (i=1; i<3; i++) {
if (token[0] == 'A') { if (!isalpha(token[i])) break;
//Convert inches of mercury to mbar }
res->bar = (int)(atoi(&token[1])*0.338637526f); if (i==3) {
for(i=0; i<NUM_WC_CODES; i++) {
if (!strncmp(&token[1], WC_CODES[i], 2)) {
res->wc=i+1;
break;
}
}
return; return;
} }
}
//Last 4 digits is pressure im mbar //Check for NCD or NCD
res->bar = atoi(&token[1]); if ((!strcmp(token, "NCD")) || (!strcmp(token, "NSC"))) {
res->cc = 1;
return; return;
} }
}
}
//Check all tokens 6 chars long //Check for TCU
if (strlen(token) == 6 ) { if (!strcmp(token, "TCU")) {
res->cc = 7;
return;
}
//Check if token is the cloud cover break;
for (i=0; i<3; i++) {
if (!isalpha(token[i])) break; //Check all tokens 4 chars long
} case 4:
if (i==3) {
for (i=3; i<6; i++) { //Check if token is an icao
for (i=0; i<4; i++) {
if (!isalpha(token[i])) break;
}
if (i==4) return;
break;
//Check all tokens 5 chars long
case 5:
//Check for CAVOK
if (!strcmp(token, "CAVOK")) {
res->cc = 1;
return;
}
//Check if token is the temperature
for (i=0; i<2; i++) {
if (!isdigit(token[i])) break; if (!isdigit(token[i])) break;
} }
if (i==6) { if ((i==2) && (token[2] == '/')) {
//Check if first 3 digits gives the cloud cover condition for (i=3; i<5; i++) {
for(i=0; i<NUM_CC_CODES; i++) { if (!isdigit(token[i])) break;
if (!strncmp(token, CC_CODES[i], 3)) {
res->cc=i+1;
break;
}
} }
return; if (i==5) {
} //First 2 digits gives the air temperature
} res->tmpC=atoi(token);
//Check if token is positive temp and negative dew //4th and 5th digits gives the dew point temperature
for (i=0; i<2; i++) { res->dew=atoi(&token[3]);
if (!isdigit(token[i])) break;
} //Compute humidity
if ((i==2) && (token[2] == '/') && (token[3] == 'M')) { res->hmid = rel_humidity(res->dew, res->tmpC);
for (i=4; i<6; i++) {
//Convert to Fahrenheit (faster here than in conky.c)
res->tmpF = (res->tmpC*9)/5 + 32;
return;
}
}
//Check if token is the pressure
if ((token[0] == 'Q') || (token[0] == 'A')) {
for (i=1; i<5; i++) {
if (!isdigit(token[i])) break;
}
if (i==5) {
if (token[0] == 'A') {
//Convert inches of mercury to mbar
res->bar = (int)(atoi(&token[1])*0.338637526f);
return;
}
//Last 4 digits is pressure im mbar
res->bar = atoi(&token[1]);
return;
}
}
break;
//Check all tokens 6 chars long
case 6:
//Check if token is the cloud cover
for (i=0; i<3; i++) {
if (!isalpha(token[i])) break;
}
if (i==3) {
for (i=3; i<6; i++) {
if (!isdigit(token[i])) break;
}
if (i==6) {
//Check if first 3 digits gives the cloud cover condition
for(i=0; i<NUM_CC_CODES; i++) {
if (!strncmp(token, CC_CODES[i], 3)) {
res->cc=i+1;
break;
}
}
return;
}
}
//Check if token is positive temp and negative dew
for (i=0; i<2; i++) {
if (!isdigit(token[i])) break; if (!isdigit(token[i])) break;
} }
if (i==6) { if ((i==2) && (token[2] == '/') && (token[3] == 'M')) {
for (i=4; i<6; i++) {
if (!isdigit(token[i])) break;
}
if (i==6) {
//1st and 2nd digits gives the temperature //1st and 2nd digits gives the temperature
res->tmpC = atoi(token); res->tmpC = atoi(token);
@ -299,96 +308,99 @@ static inline void parse_token(PWEATHER *res, char *token) {
return; return;
} }
} }
}
//Check all tokens 7 chars long break;
if (strlen(token) == 7 ) {
//Check if token is the observation time //Check all tokens 7 chars long
for (i=0; i<6; i++) { case 7:
if (!isdigit(token[i])) break;
}
if ((i==6) && (token[6] == 'Z')) return;
//Check if token is the wind speed/direction in knots //Check if token is the observation time
for (i=0; i<5; i++) { for (i=0; i<6; i++) {
if (!isdigit(token[i])) break;
}
if ((i==5) && (token[5] == 'K') && (token[6] == 'T')) {
//First 3 digits are wind direction
strncpy(s_tmp, token, 3);
res->wind_d=atoi(s_tmp);
//4th and 5th digit are wind speed in knots (convert to km/hr)
res->wind_s = (int)(atoi(&token[3])*1.852);
return;
}
//Check if token is negative temperature
if ((token[0] == 'M') && (token[4] == 'M')) {
for (i=1; i<3; i++) {
if (!isdigit(token[i])) break; if (!isdigit(token[i])) break;
} }
if ((i==3) && (token[3] == '/')) { if ((i==6) && (token[6] == 'Z')) return;
for (i=5; i<7; i++) {
//Check if token is the wind speed/direction in knots
for (i=0; i<5; i++) {
if (!isdigit(token[i])) break;
}
if ((i==5) && (token[5] == 'K') && (token[6] == 'T')) {
//First 3 digits are wind direction
strncpy(s_tmp, token, 3);
res->wind_d=atoi(s_tmp);
//4th and 5th digit are wind speed in knots (convert to km/hr)
res->wind_s = (int)(atoi(&token[3])*1.852);
return;
}
//Check if token is negative temperature
if ((token[0] == 'M') && (token[4] == 'M')) {
for (i=1; i<3; i++) {
if (!isdigit(token[i])) break; if (!isdigit(token[i])) break;
} }
if (i==7) { if ((i==3) && (token[3] == '/')) {
//2nd and 3rd digits gives the temperature for (i=5; i<7; i++) {
res->tmpC = -atoi(&token[1]); if (!isdigit(token[i])) break;
}
if (i==7) {
//2nd and 3rd digits gives the temperature
res->tmpC = -atoi(&token[1]);
//6th and 7th digits gives the dew point temperature //6th and 7th digits gives the dew point temperature
res->dew = -atoi(&token[5]); res->dew = -atoi(&token[5]);
//Compute humidity //Compute humidity
res->hmid = rel_humidity(res->dew, res->tmpC); res->hmid = rel_humidity(res->dew, res->tmpC);
//Convert to Fahrenheit (faster here than in conky.c) //Convert to Fahrenheit (faster here than in conky.c)
res->tmpF = (res->tmpC*9)/5 + 32; res->tmpF = (res->tmpC*9)/5 + 32;
return; return;
}
} }
} }
}
//Check if token is wind variability //Check if token is wind variability
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
if (!isdigit(token[i])) break;
}
if ((i==3) && (token[3] == 'V')) {
for (i=4; i<7; i++) {
if (!isdigit(token[i])) break; if (!isdigit(token[i])) break;
} }
if (i==7) return; if ((i==3) && (token[3] == 'V')) {
for (i=4; i<7; i++) {
if (!isdigit(token[i])) break;
}
if (i==7) return;
}
break;
//Check all tokens 8 chars long
case 8:
//Check if token is the wind speed/direction in m/s
for (i=0; i<5; i++) {
if (!isdigit(token[i])) break;
}
if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
//First 3 digits are wind direction
strncpy(s_tmp, token, 3);
res->wind_d=atoi(s_tmp);
//4th and 5th digit are wind speed in m/s (convert to km/hr)
res->wind_s = (int)(atoi(&token[3])*3.6);
return;
}
default:
//printf("token : %s\n", token);
break;
} }
}
//Check all tokens 8 chars long
if (strlen(token) == 8 ) {
//Check if token is the wind speed/direction in m/s
for (i=0; i<5; i++) {
if (!isdigit(token[i])) break;
}
if ((i==5)&&(token[5] == 'M')&&(token[6] == 'P')&&(token[7] == 'S')) {
//First 3 digits are wind direction
strncpy(s_tmp, token, 3);
res->wind_d=atoi(s_tmp);
//4th and 5th digit are wind speed in m/s (convert to km/hr)
res->wind_s = (int)(atoi(&token[3])*3.6);
return;
}
}
//printf("token : %s\n", token);
} }
static inline PWEATHER *parse_weather(const char *data) static inline PWEATHER *parse_weather(const char *data)