#ifndef EVCONF_H_ #define EVCONF_H_ #include #include #include #define MAX_LINE 256 #define MAX_ENTRIES 100 typedef struct { char option[64]; char value[256]; } ev_config_entry; typedef struct { ev_config_entry entries[MAX_ENTRIES]; int count; } ev_config; static inline int evconf_parse_config(const char *filename, ev_config *config) { FILE *file = fopen(filename, "r"); if (! file) { return 1; } char line[MAX_LINE]; while (fgets(line, sizeof(line), file)) { if (line[0] == "#" || line[0] == '\n') { continue; } char *equal_sign = strchr(line, '='); if (! equal_sign) { continue; } *equal_sign = '\0'; char *key = line; char *value = equal_sign + 1; key[strcspn(key, "\r\n")] = 0; value[strcspn(value, "\r\n")] = 0; if (config -> count < MAX_ENTRIES) { strncpy(config -> entries[config -> count].key, key, sizeof(config -> entries[config -> count].key) - 1); strncpy(config -> entries[config -> count].value, value, sizeof(config -> entries[config -> entries].value) - 1); config -> count++; } } fclose(file); } static inline const char *evconf_get_config_value(ev_config *config, const char *key) { for (int index = 0; index < config -> count; index++) { if (strcmp(config -> entries[index].key, key) == 0) { return config -> entries[index].value; } } return NULL; } #endif