core/weather/wx_cond.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file wx_cond.c | ||
| 3 | * @brief Turns the condition token the phone sends into its WxCond. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "weather/wx_cond.h" | ||
| 8 | |||
| 9 | #include <string.h> | ||
| 10 | |||
| 11 | #define TOKEN_ROW(name) [WX_COND_##name] = #name, ///< One condition's name as its token | ||
| 12 | |||
| 13 | /** | ||
| 14 | * @brief Each condition's token in a fixed width row, indexed by its WxCond. The rows are plain bytes | ||
| 15 | * rather than pointers, so the watch has no pointer per token to fix up when it loads the app. | ||
| 16 | */ | ||
| 17 | static const char s_tokens[WX_COND_COUNT][WX_COND_TOKEN_LEN] = {WX_COND_EACH(TOKEN_ROW)}; | ||
| 18 | |||
| 19 | 35 | WxCond wx_cond_parse(const char *token, bool *night) | |
| 20 | { | ||
| 21 |
2/2✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 4 taken 30 times.
|
35 | if (night) |
| 22 | { | ||
| 23 | 5 | *night = false; | |
| 24 | } | ||
| 25 | |||
| 26 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 34 times.
|
35 | if (!token) |
| 27 | { | ||
| 28 | 1 | return WX_COND_UNKNOWN; | |
| 29 | } | ||
| 30 | |||
| 31 | 34 | size_t len = strlen(token); | |
| 32 |
3/4✓ Branch 6 → 7 taken 3 times.
✓ Branch 6 → 9 taken 31 times.
✓ Branch 7 → 8 taken 3 times.
✗ Branch 7 → 9 not taken.
|
34 | bool dark = len > WX_COND_NIGHT_LEN && !strcmp(token + len - WX_COND_NIGHT_LEN, WX_COND_NIGHT_ENDING); |
| 33 |
2/2✓ Branch 10 → 11 taken 3 times.
✓ Branch 10 → 12 taken 31 times.
|
34 | if (dark) |
| 34 | { | ||
| 35 | 3 | len -= WX_COND_NIGHT_LEN; | |
| 36 | } | ||
| 37 | |||
| 38 | // a token too long for a row cannot be one, and checking first keeps the end check inside the row | ||
| 39 |
4/4✓ Branch 12 → 13 taken 33 times.
✓ Branch 12 → 14 taken 1 time.
✓ Branch 13 → 14 taken 2 times.
✓ Branch 13 → 15 taken 31 times.
|
34 | if (len == 0 || len >= WX_COND_TOKEN_LEN) |
| 40 | { | ||
| 41 | 3 | return WX_COND_UNKNOWN; | |
| 42 | } | ||
| 43 | |||
| 44 |
2/2✓ Branch 22 → 16 taken 225 times.
✓ Branch 22 → 23 taken 5 times.
|
230 | for (int i = 0; i < WX_COND_COUNT; i++) |
| 45 | { | ||
| 46 |
4/4✓ Branch 16 → 17 taken 27 times.
✓ Branch 16 → 21 taken 198 times.
✓ Branch 17 → 18 taken 26 times.
✓ Branch 17 → 21 taken 1 time.
|
225 | if (!strncmp(s_tokens[i], token, len) && s_tokens[i][len] == '\0') |
| 47 | { | ||
| 48 |
2/2✓ Branch 18 → 19 taken 2 times.
✓ Branch 18 → 20 taken 24 times.
|
26 | if (night) |
| 49 | { | ||
| 50 | 2 | *night = dark; | |
| 51 | } | ||
| 52 | 26 | return (WxCond)i; | |
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 5 | return WX_COND_UNKNOWN; | |
| 57 | } | ||
| 58 |