init
This commit is contained in:
70
include/libevconf.h
Normal file
70
include/libevconf.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef EVCONF_H_
|
||||
#define EVCONF_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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 *config = fopen(filename, "r");
|
||||
if (! config) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char line[MAX_LINE];
|
||||
while (fgets(line, sizeof(line), config)) {
|
||||
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(config);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user