rulimine/src/lib/config.c

59 lines
1.4 KiB
C
Raw Normal View History

2020-01-22 09:13:19 +03:00
#include <stddef.h>
#include <lib/config.h>
#include <lib/libc.h>
2020-03-30 22:24:36 +03:00
#include <lib/blib.h>
#include <fs/echfs.h>
2020-01-22 09:13:19 +03:00
#define SEPARATOR '\n'
2020-03-30 22:24:36 +03:00
#define CONFIG_NAME "qloader2.cfg"
#define MAX_CONFIG_SIZE 4096
2020-01-22 09:13:19 +03:00
2020-03-30 22:24:36 +03:00
static char *config_addr;
int init_config(int drive, int part) {
struct echfs_file_handle f;
if (echfs_open(&f, drive, part, CONFIG_NAME)) {
return -1;
}
2020-03-30 23:27:15 +03:00
if (f.dir_entry.size >= MAX_CONFIG_SIZE) {
2020-03-30 22:24:36 +03:00
print("Config file is too big!\n");
for (;;);
}
config_addr = balloc(MAX_CONFIG_SIZE);
memset(config_addr, 0, MAX_CONFIG_SIZE);
echfs_read(&f, config_addr, 0, f.dir_entry.size);
return 0;
}
2020-03-30 23:27:15 +03:00
char *config_get_value(char *buf, size_t index, size_t limit, const char *key) {
2020-03-30 22:24:36 +03:00
if (!limit || !buf || !key)
2020-01-22 09:13:19 +03:00
return NULL;
size_t key_len = strlen(key);
2020-03-30 22:24:36 +03:00
for (size_t i = 0; config_addr[i]; i++) {
if (!strncmp(&config_addr[i], key, key_len) && config_addr[i + key_len] == '=') {
if (i && config_addr[i - 1] != SEPARATOR)
2020-01-22 09:13:19 +03:00
continue;
2020-03-30 23:27:15 +03:00
if (index--)
continue;
2020-01-22 09:13:19 +03:00
i += key_len + 1;
size_t j;
2020-03-30 22:24:36 +03:00
for (j = 0; config_addr[i + j] != SEPARATOR && config_addr[i + j]; j++) {
2020-01-22 09:13:19 +03:00
if (j == limit - 1)
break;
2020-03-30 22:24:36 +03:00
buf[j] = config_addr[i + j];
2020-01-22 09:13:19 +03:00
}
buf[j] = 0;
return buf;
}
}
return NULL;
}