refactor(ltm): remove some code duplication

Jef Roosens 2023-12-17 09:55:20 +01:00
parent dae8a2f30e
commit f781fbdb89
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 58 additions and 47 deletions

View File

@ -96,6 +96,12 @@ typedef struct ltm_instance_block {
ltm_err ltm_instance_block_init(ltm_instance_block **out);
/**
* Add a new block to the instance with the given type.
*/
ltm_err ltm_instance_block_add(ltm_instance_block **out, ltm_instance *instance,
const char *name, ltm_instance_block_type type);
struct ltm_instance {
const ltm_template *template;
struct {

View File

@ -106,7 +106,9 @@ ltm_err ltm_template_instantiate(ltm_instance **out,
} break;
case ltm_template_block_type_var:
case ltm_template_block_type_nested:
*vars = &instance->blocks.tail->next;
// Account for the possibility a template starts with a placeholder
*vars = instance->blocks.tail != NULL ? &instance->blocks.tail->next
: &instance->blocks.head;
vars++;
break;
}
@ -117,20 +119,21 @@ ltm_err ltm_template_instantiate(ltm_instance **out,
return ltm_err_ok;
}
ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
ltm_instance_block_type type, void *data,
size_t len) {
ltm_err ltm_instance_block_add(ltm_instance_block **out, ltm_instance *instance,
const char *name, ltm_instance_block_type type) {
const ltm_template *template = instance->template;
ltm_template_block_name *block_name = NULL;
size_t i = 0;
for (i = 0; i < template->names.len; i++) {
while (i < template->names.len) {
block_name = &template->names.arr[i];
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
break;
}
i++;
}
if (i == template->names.len) {
@ -138,8 +141,22 @@ ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
}
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
bool correct_type = false;
if (template_block->type != ltm_template_block_type_var) {
switch (type) {
case ltm_instance_block_type_nested:
correct_type = template_block->type == ltm_template_block_type_nested;
break;
case ltm_instance_block_type_buf:
case ltm_instance_block_type_buf_owned:
case ltm_instance_block_type_file:
case ltm_instance_block_type_file_owned:
case ltm_instance_block_type_fn:
correct_type = template_block->type == ltm_template_block_type_var;
break;
}
if (!correct_type) {
return ltm_err_wrong_block_type;
}
@ -147,57 +164,39 @@ ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
LTM_RES(ltm_instance_block_init(&block));
block->type = type;
block->data.ptr = data;
block->data.len = len;
// We insert the block in the linked list and replace its next pointer as the
// new attachment point for this variable
// Insert the block in the linked list
block->next = *instance->vars[i];
*instance->vars[i] = block;
instance->vars[i] = &block->next;
*out = block;
return ltm_err_ok;
}
ltm_err ltm_instance_block_add_var(ltm_instance *instance, const char *name,
ltm_instance_block_type type, void *data,
size_t len) {
ltm_instance_block *block;
LTM_RES(ltm_instance_block_add(&block, instance, name, type));
block->data.ptr = data;
block->data.len = len;
return ltm_err_ok;
}
ltm_err ltm_instance_block_add_var_fn(ltm_instance *instance, const char *name,
ltm_data_fn fn, void *data, size_t len) {
const ltm_template *template = instance->template;
ltm_template_block_name *block_name = NULL;
size_t i = 0;
for (i = 0; i < template->names.len; i++) {
block_name = &template->names.arr[i];
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
break;
}
}
if (i == template->names.len) {
return ltm_err_not_found;
}
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
if (template_block->type != ltm_template_block_type_var) {
return ltm_err_wrong_block_type;
}
ltm_instance_block *block;
LTM_RES(ltm_instance_block_init(&block));
LTM_RES(ltm_instance_block_add(&block, instance, name,
ltm_instance_block_type_fn));
block->type = ltm_instance_block_type_fn;
block->fn = fn;
block->data.ptr = data;
block->data.len = len;
// We insert the block in the linked list and replace its next pointer as the
// new attachment point for this variable
block->next = *instance->vars[i];
*instance->vars[i] = block;
instance->vars[i] = &block->next;
return ltm_err_ok;
}
@ -205,19 +204,25 @@ ltm_err ltm_instance_block_add_nested(ltm_instance **out,
ltm_instance *instance,
const char *name) {
const ltm_template *template = instance->template;
bool matched = false;
ltm_template_block_name *block_name = NULL;
size_t i = 0;
for (i = 0; i < template->names.len && !matched; i++) {
ltm_template_block_name *block_name = &template->names.arr[i];
matched = strncmp(name, block_name->name.s, block_name->name.len) == 0;
while (i < template->names.len) {
block_name = &template->names.arr[i];
if (strncmp(name, block_name->name.s, block_name->name.len) == 0) {
break;
}
i++;
}
if (!matched) {
if (i == template->names.len) {
return ltm_err_not_found;
}
ltm_template_block *template_block = &template->blocks.arr[i];
ltm_template_block *template_block = &template->blocks.arr[block_name->index];
if (template_block->type != ltm_template_block_type_nested) {
return ltm_err_wrong_block_type;