1
0
Fork 0
forked from poz/modfetch

general memory and readability improvements

This commit is contained in:
jacekpoz 2024-02-09 15:10:13 +01:00
parent 9cfdb7bea4
commit 21aefd281a
No known key found for this signature in database
GPG key ID: 94E812A8B12AAE3C

View file

@ -16,7 +16,10 @@ static const uint8_t VERSION_PATCH = 0;
static const uint8_t MAX_MODULE_NAME_LENGTH = 128; static const uint8_t MAX_MODULE_NAME_LENGTH = 128;
static const uint8_t MAX_MODULES = 128; static const uint8_t MAX_MODULES = 128;
static const uint8_t MAX_MODULE_VARS = 128;
static const uint8_t INITIAL_CONFIG_SIZE = 8;
static const uint16_t MAX_PATH_LENGTH = 4096;
static const char *version_str(void) { static const char *version_str(void) {
char *ver; char *ver;
@ -44,7 +47,7 @@ void parsing_error(size_t line) {
} }
char *remove_whitespaces(const char *str) { char *remove_whitespaces(const char *str) {
char *tmp = malloc(strlen(str) * sizeof(char)); char tmp[strlen(str)];
size_t i = 0; size_t i = 0;
size_t j = 0; size_t j = 0;
while (str[i] != '\0') { while (str[i] != '\0') {
@ -60,17 +63,17 @@ char *remove_whitespaces(const char *str) {
char *ret = malloc(j * sizeof(char)); char *ret = malloc(j * sizeof(char));
strncpy(ret, tmp, j); strncpy(ret, tmp, j);
free(tmp);
return ret; return ret;
} }
char *resolve_env_vars(const char *str) { char *resolve_env_vars(const char *str) {
size_t len = strlen(str); size_t len = strlen(str);
char *ret = malloc(4096 * sizeof(char)); char *ret = malloc(MAX_PATH_LENGTH * sizeof(char));
size_t ret_offset = 0; size_t ret_offset = 0;
bool in_env_var = false; bool in_env_var = false;
char *env_var = malloc(4096 * sizeof(char)); char env_var[MAX_PATH_LENGTH];
memset(env_var, 0, sizeof(env_var));
size_t env_var_offset = 0; size_t env_var_offset = 0;
for (size_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
@ -86,14 +89,14 @@ char *resolve_env_vars(const char *str) {
continue; continue;
} }
// end of current env var
if (in_env_var && !(isalpha(str[i]) || isdigit(str[i]) || str[i] == '_')) { if (in_env_var && !(isalpha(str[i]) || isdigit(str[i]) || str[i] == '_')) {
in_env_var = false; in_env_var = false;
char *env = getenv(env_var); char *env = getenv(env_var);
strcat(ret, env); strcat(ret, env);
ret_offset += strlen(env); ret_offset += strlen(env);
free(env_var); memset(env_var, 0, sizeof(env_var));
env_var = malloc(4096 * sizeof(char));
env_var_offset = 0; env_var_offset = 0;
} }
@ -107,8 +110,13 @@ char *resolve_env_vars(const char *str) {
++ret_offset; ++ret_offset;
} }
free(env_var); return ret;
}
char *process_str(const char *str) {
char *whitespaceless = remove_whitespaces(str);
char *ret = resolve_env_vars(whitespaceless);
free(whitespaceless);
return ret; return ret;
} }
@ -119,33 +127,36 @@ Config parse_config(FILE *config_file) {
size_t line_index = 0; size_t line_index = 0;
bool in_config = false; bool in_config = false;
char *current_path = malloc(MAX_MODULE_NAME_LENGTH * sizeof(char)); char current_path[MAX_MODULE_NAME_LENGTH];
Module current_module = { Module current_module = {
.path = malloc(4096 * sizeof(char)), .path = NULL,
.config = malloc(MAX_MODULE_VARS * sizeof(char)), .config = NULL,
}; };
size_t config_index = 0; size_t config_index = 0;
size_t config_multiplier = 1;
Config config = { Config config = {
.modules = malloc(MAX_MODULES * sizeof(Module)), .modules = malloc(MAX_MODULES * sizeof(Module)),
.module_count = 0, .module_count = 0,
}; };
size_t module_index = 0;
// TODO rewrite this to read the config char by char
while ((read = getline(&line, &len, config_file)) != -1) { while ((read = getline(&line, &len, config_file)) != -1) {
if (read == 1) if (read <= 1)
continue; continue;
// if config is passed
// if config is passed to this module
if (line[read - 2u] == '{') { if (line[read - 2u] == '{') {
if (in_config) { if (in_config) {
fclose(config_file); fclose(config_file);
parsing_error(line_index); parsing_error(line_index);
} }
in_config = true; in_config = true;
// get rid of the '{'
strncpy(current_path, line, (size_t)read - 2); strncpy(current_path, line, (size_t)read - 2);
current_module.path = resolve_env_vars(remove_whitespaces(current_path));
free(current_path); current_module.path = process_str(current_path);
current_path = malloc(MAX_MODULE_NAME_LENGTH); current_module.config = malloc(INITIAL_CONFIG_SIZE * sizeof(char*));
continue; continue;
} }
@ -156,31 +167,39 @@ Config parse_config(FILE *config_file) {
parsing_error(line_index); parsing_error(line_index);
} }
in_config = false; in_config = false;
if (config_index >= INITIAL_CONFIG_SIZE * config_multiplier) {
config_multiplier *= 2;
current_module.config = realloc(current_module.config, INITIAL_CONFIG_SIZE * config_multiplier);
}
current_module.config[config_index] = NULL; current_module.config[config_index] = NULL;
config.modules[module_index] = current_module; config.modules[config.module_count] = current_module;
++module_index; ++config.module_count;
current_module.path = malloc(4096 * sizeof(char)); config_index = 0;
current_module.config = malloc(MAX_MODULE_VARS * sizeof(char));
continue; continue;
} }
if (in_config) { if (in_config) {
current_module.config[config_index] = remove_whitespaces(line); if (config_index >= INITIAL_CONFIG_SIZE * config_multiplier) {
config_multiplier *= 2;
current_module.config = realloc(current_module.config, INITIAL_CONFIG_SIZE * config_multiplier);
}
current_module.config[config_index] = process_str(line);
++config_index; ++config_index;
continue; continue;
} }
current_module.path = resolve_env_vars(remove_whitespaces(line)); // no config passed to this module
free(current_module.config); current_module.path = process_str(line);
current_module.config = malloc(sizeof(void*)); current_module.config = malloc(sizeof(void*));
current_module.config[0] = NULL; current_module.config[0] = NULL;
config.modules[module_index] = current_module;
++module_index;
current_module.path = malloc(4096 * sizeof(char));
current_module.config = malloc(MAX_MODULE_VARS * sizeof(char));
}
config.module_count = module_index; config.modules[config.module_count] = current_module;
++config.module_count;
free(line);
}
return config; return config;
} }