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>
|
2020-04-14 06:20:55 +03:00
|
|
|
#include <fs/file.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) {
|
2020-04-14 06:20:55 +03:00
|
|
|
struct file_handle f;
|
2020-03-30 22:24:36 +03:00
|
|
|
|
2020-04-14 06:20:55 +03:00
|
|
|
if (fopen(&f, drive, part, CONFIG_NAME)) {
|
2020-03-30 22:24:36 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-04-14 06:20:55 +03:00
|
|
|
if (f.size >= MAX_CONFIG_SIZE) {
|
2020-04-15 23:34:09 +03:00
|
|
|
panic("Config file is too big!\n");
|
2020-03-30 22:24:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
config_addr = balloc(MAX_CONFIG_SIZE);
|
|
|
|
memset(config_addr, 0, MAX_CONFIG_SIZE);
|
|
|
|
|
2020-04-14 06:20:55 +03:00
|
|
|
fread(&f, config_addr, 0, f.size);
|
2020-03-30 22:24:36 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|