forked from flash/temporary-satori-hole
65 lines
2 KiB
C
65 lines
2 KiB
C
#include "config.h"
|
|
|
|
sat_config_ptr sat_config_scoped_scope_to(sat_config_ptr ctx, char *prefix) {
|
|
sat_config_scoped_ptr info = (sat_config_scoped_ptr)ctx->info;
|
|
|
|
int pfxLength = strlen(prefix);
|
|
bool appendColon = prefix[pfxLength - 1] != ':';
|
|
|
|
int length = strlen(info->prefix) + pfxLength + 1;
|
|
if(appendColon) length += 1;
|
|
|
|
char *newPrefix = malloc(length);
|
|
memset(newPrefix, 0, length);
|
|
strcat(newPrefix, info->prefix);
|
|
strcat(newPrefix, prefix);
|
|
if(appendColon) strcat(newPrefix, ":");
|
|
|
|
sat_config_ptr scoped = sat_config_alloc_scoped();
|
|
sat_config_scoped_ptr scopedInfo = (sat_config_scoped_ptr)scoped->info;
|
|
scopedInfo->parent = info->parent;
|
|
scopedInfo->prefix = newPrefix;
|
|
|
|
return scoped;
|
|
}
|
|
|
|
char* sat_config_scoped_read_value(sat_config_ptr ctx, char *name, char *fallback) {
|
|
sat_config_scoped_ptr info = (sat_config_scoped_ptr)ctx->info;
|
|
|
|
int length = strlen(info->prefix) + strlen(name) + 1;
|
|
char *realName = malloc(strlen(info->prefix) + strlen(name) + 1);
|
|
memset(realName, 0, length);
|
|
strcat(realName, info->prefix);
|
|
strcat(realName, name);
|
|
|
|
char *value = sat_config_read_value(info->parent, realName, fallback);
|
|
|
|
free(realName);
|
|
|
|
return value;
|
|
}
|
|
|
|
sat_config_ptr sat_config_alloc_scoped(void) {
|
|
sat_config_ptr ctx = malloc(sizeof(sat_config));
|
|
memset(ctx, 0, sizeof(sat_config));
|
|
ctx->type = SAT_CONFIG_TYPE_SCOPED;
|
|
ctx->scopeTo = sat_config_scoped_scope_to;
|
|
ctx->readValue = sat_config_scoped_read_value;
|
|
ctx->info = malloc(sizeof(sat_config_scoped));
|
|
memset(ctx->info, 0, sizeof(sat_config_scoped));
|
|
return ctx;
|
|
}
|
|
|
|
void sat_config_free_scoped(sat_config_ptr ctx) {
|
|
if(ctx == NULL || ctx->info == NULL || ctx->type != SAT_CONFIG_TYPE_SCOPED) return;
|
|
|
|
// freeing parent will cause explosions beyond your imagination
|
|
sat_config_scoped_ptr info = (sat_config_scoped_ptr)ctx->info;
|
|
|
|
if(info->prefix != NULL)
|
|
free(info->prefix);
|
|
|
|
free(ctx->info);
|
|
free(ctx);
|
|
}
|