1
0
Fork 0

Refactor battery_state()

This commit is contained in:
Laslo Hunhold 2017-08-11 00:17:49 +02:00 committed by Aaron Marcher
parent 6b8384ef2f
commit 178c23e3d2
No known key found for this signature in database
GPG key ID: 74B048E5C2474F9A

View file

@ -142,10 +142,19 @@ battery_power(const char *bat)
static const char * static const char *
battery_state(const char *bat) battery_state(const char *bat)
{ {
char path[PATH_MAX];
char state[12];
FILE *fp; FILE *fp;
struct {
char *state;
char *symbol;
} map[] = {
{ "Charging", "+" },
{ "Discharging", "-" },
{ "Full", "=" },
{ "Unknown", "/" },
};
size_t i;
int n; int n;
char path[PATH_MAX], state[12];
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status"); snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
fp = fopen(path, "r"); fp = fopen(path, "r");
@ -158,17 +167,13 @@ battery_state(const char *bat)
if (n != 1) if (n != 1)
return UNKNOWN_STR; return UNKNOWN_STR;
if (strcmp(state, "Charging") == 0) { for (i = 0; i < sizeof(map) / sizeof(*map); i++) {
return "+"; if (!strcmp(map[i].state, state)) {
} else if (strcmp(state, "Discharging") == 0) { break;
return "-";
} else if (strcmp(state, "Full") == 0) {
return "=";
} else if (strcmp(state, "Unknown") == 0) {
return "/";
} else {
return "?";
} }
}
return (i == sizeof(map) / sizeof(*map)) ? "?" : map[i].symbol;
} }
static const char * static const char *