Refactoring related to {{{!IF}}}...

This commit is contained in:
Stephen Seo 2024-11-19 15:07:40 +09:00
parent 12a1edc897
commit e9de2682f6

View file

@ -241,7 +241,7 @@ int c_simple_http_internal_handle_inside_delimeters(
const size_t html_buf_idx, const size_t html_buf_idx,
const char *var, const char *var,
const size_t var_size, const size_t var_size,
SDArchiverLinkedList *state_stack, SDArchiverLinkedList *if_state_stack,
const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map, const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
SDArchiverLinkedList *string_part_list, SDArchiverLinkedList *string_part_list,
SDArchiverLinkedList **files_list_out) { SDArchiverLinkedList **files_list_out) {
@ -277,13 +277,13 @@ int c_simple_http_internal_handle_inside_delimeters(
uint32_t *state = malloc(sizeof(uint32_t)); uint32_t *state = malloc(sizeof(uint32_t));
*state = 0; *state = 0;
uint32_t *prev_state = state_stack->count == 0 ? NULL : uint32_t *prev_if_state = if_state_stack->count == 0 ? NULL :
state_stack->head->next->data; if_state_stack->head->next->data;
if (!prev_state if (!prev_if_state
|| (((*prev_state) & 7) == 1 || (((*prev_if_state) & 7) == 1
|| ((*prev_state) & 7) == 3 || ((*prev_if_state) & 7) == 3
|| ((*prev_state) & 7) == 5)) { || ((*prev_if_state) & 7) == 5)) {
if (is_equality) { if (is_equality) {
if (strcmp(left_side, right_side) == 0) { if (strcmp(left_side, right_side) == 0) {
// Is equality is TRUE. // Is equality is TRUE.
@ -302,17 +302,17 @@ int c_simple_http_internal_handle_inside_delimeters(
} }
} }
} else { } else {
// prev_state is not visible, so this nested block shouldn't be. // prev_if_state is not visible, so this nested block shouldn't be.
(*state) |= 0xA; (*state) |= 0xA;
} }
simple_archiver_list_add_front(state_stack, state, NULL); simple_archiver_list_add_front(if_state_stack, state, NULL);
c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1); c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1);
} else if (strncmp(var + 1, "ELSEIF ", 7) == 0) { } else if (strncmp(var + 1, "ELSEIF ", 7) == 0) {
if (state_stack->count == 0) { if (if_state_stack->count == 0) {
fprintf(stderr, "ERROR No previous conditional! %s\n", var); fprintf(stderr, "ERROR No previous conditional! %s\n", var);
return 1; return 1;
} }
uint32_t *state = state_stack->head->next->data; uint32_t *state = if_state_stack->head->next->data;
if (((*state) & 7) != 1 && ((*state) & 7) != 2 if (((*state) & 7) != 1 && ((*state) & 7) != 2
&& ((*state) & 7) != 3 && ((*state) & 7) != 4) { && ((*state) & 7) != 3 && ((*state) & 7) != 4) {
fprintf( fprintf(
@ -373,11 +373,11 @@ int c_simple_http_internal_handle_inside_delimeters(
} }
c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1); c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1);
} else if (strncmp(var + 1, "ELSE", 4) == 0) { } else if (strncmp(var + 1, "ELSE", 4) == 0) {
if (state_stack->count == 0) { if (if_state_stack->count == 0) {
fprintf(stderr, "ERROR No previous IF! %s\n", var); fprintf(stderr, "ERROR No previous IF! %s\n", var);
return 1; return 1;
} }
uint32_t *state = state_stack->head->next->data; uint32_t *state = if_state_stack->head->next->data;
if (((*state) & 8) == 0) { if (((*state) & 8) == 0) {
// No previous expression resolved to true, enabling ELSE block. // No previous expression resolved to true, enabling ELSE block.
(*state) &= 0xFFFFFFF8; (*state) &= 0xFFFFFFF8;
@ -389,12 +389,12 @@ int c_simple_http_internal_handle_inside_delimeters(
} }
c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1); c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1);
} else if (strncmp(var + 1, "ENDIF", 5) == 0) { } else if (strncmp(var + 1, "ENDIF", 5) == 0) {
if (state_stack->count == 0) { if (if_state_stack->count == 0) {
fprintf(stderr, "ERROR No previous IF! %s\n", var); fprintf(stderr, "ERROR No previous IF! %s\n", var);
return 1; return 1;
} }
simple_archiver_list_remove_once( simple_archiver_list_remove_once(
state_stack, c_simple_http_internal_always_return_one, NULL); if_state_stack, c_simple_http_internal_always_return_one, NULL);
c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1); c_simple_http_add_string_part(string_part_list, NULL, html_buf_idx + 1);
} else if (strncmp(var + 1, "INDEX ", 6) == 0) { } else if (strncmp(var + 1, "INDEX ", 6) == 0) {
// Indexing into variable array. // Indexing into variable array.
@ -656,7 +656,7 @@ char *c_simple_http_path_to_generated(
// xxxx x111 - ForEach expression contents. // xxxx x111 - ForEach expression contents.
// xxxx 1xxx - Previous If/ElseIf had true/ALLOW. // xxxx 1xxx - Previous If/ElseIf had true/ALLOW.
__attribute__((cleanup(simple_archiver_list_free))) __attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *state_stack = simple_archiver_list_init(); SDArchiverLinkedList *if_state_stack = simple_archiver_list_init();
for (; idx < html_buf_size; ++idx) { for (; idx < html_buf_size; ++idx) {
if ((state & 1) == 0) { if ((state & 1) == 0) {
@ -666,12 +666,12 @@ char *c_simple_http_path_to_generated(
if (delimeter_count >= 3) { if (delimeter_count >= 3) {
delimeter_count = 0; delimeter_count = 0;
state |= 1; state |= 1;
uint32_t *state_stack_head = state_stack->count == 0 ? NULL : uint32_t *if_state_stack_head = if_state_stack->count == 0 ? NULL :
state_stack->head->next->data; if_state_stack->head->next->data;
if (!state_stack_head if (!if_state_stack_head
|| ((*state_stack_head) & 7) == 1 || ((*if_state_stack_head) & 7) == 1
|| ((*state_stack_head) & 7) == 3 || ((*if_state_stack_head) & 7) == 3
|| ((*state_stack_head) & 7) == 5) { || ((*if_state_stack_head) & 7) == 5) {
if (string_part_list->count != 0) { if (string_part_list->count != 0) {
C_SIMPLE_HTTP_String_Part *last_part = C_SIMPLE_HTTP_String_Part *last_part =
string_part_list->tail->prev->data; string_part_list->tail->prev->data;
@ -719,7 +719,7 @@ char *c_simple_http_path_to_generated(
idx, idx,
var, var,
var_size, var_size,
state_stack, if_state_stack,
wrapped_hash_map, wrapped_hash_map,
string_part_list, string_part_list,
files_list_out) != 0) { files_list_out) != 0) {