From 82693bfa3eea8fe8a77cf3d00ef3fc06016ace27 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 21 Nov 2024 14:05:33 +0900 Subject: [PATCH] Ensure http_template.c keeps track of _FILE files --- example_config/each_file_one.html | 1 + example_config/each_file_two.html | 1 + example_config/each_file_zero.html | 1 + example_config/example.config | 7 +++ src/html_cache.c | 20 +++++--- src/http_template.c | 80 ++++++++++++++++-------------- src/http_template.h | 7 ++- src/test.c | 54 ++++++++++---------- 8 files changed, 100 insertions(+), 71 deletions(-) create mode 100644 example_config/each_file_one.html create mode 100644 example_config/each_file_two.html create mode 100644 example_config/each_file_zero.html diff --git a/example_config/each_file_one.html b/example_config/each_file_one.html new file mode 100644 index 0000000..7de5aa7 --- /dev/null +++ b/example_config/each_file_one.html @@ -0,0 +1 @@ +Each File ONE
diff --git a/example_config/each_file_two.html b/example_config/each_file_two.html new file mode 100644 index 0000000..6b66147 --- /dev/null +++ b/example_config/each_file_two.html @@ -0,0 +1 @@ +Each File TWO
diff --git a/example_config/each_file_zero.html b/example_config/each_file_zero.html new file mode 100644 index 0000000..b97d444 --- /dev/null +++ b/example_config/each_file_zero.html @@ -0,0 +1 @@ +Each File ZERO
diff --git a/example_config/example.config b/example_config/example.config index c82d4d7..92f3124 100644 --- a/example_config/example.config +++ b/example_config/example.config @@ -100,6 +100,9 @@ Nested FOREACH:
{{{ArrayValueThird}}} {{{!FOREACH ArrayValueFourth}}} {{{ArrayValueFourth}}} + {{{!FOREACH Each_FILE}}} + {{{Each_FILE}}} + {{{!ENDFOREACH}}} {{{!ENDFOREACH}}} {{{!ENDFOREACH}}} {{{!ENDFOREACH}}} @@ -143,6 +146,10 @@ EachTestHead='''Third Entry Head''' EachTestMid='''Third Entry Mid''' EachTestTail='''Third Entry Tail''' +Each_FILE='''example_config/each_file_zero.html''' +Each_FILE='''example_config/each_file_one.html''' +Each_FILE='''example_config/each_file_two.html''' + PATH=/inner HTML_FILE='''example_config/inner.html''' VAR_FILE='''example_config/var.html''' diff --git a/src/html_cache.c b/src/html_cache.c index 4b8403f..82ac5ff 100644 --- a/src/html_cache.c +++ b/src/html_cache.c @@ -38,8 +38,12 @@ #include "helpers.h" #include "http_template.h" -int c_simple_http_internal_write_filenames_to_cache_file(void *data, void *ud) { - char *filename = data; +int c_simple_http_internal_write_filenames_to_cache_file( + const void *key, + __attribute__((unused)) size_t key_size, + __attribute__((unused)) const void *value, + void *ud) { + const char *filename = key; FILE *cache_fd = ud; const size_t filename_size = strlen(filename); @@ -436,8 +440,8 @@ CACHE_FILE_WRITE_CHECK: return -5; } - __attribute__((cleanup(simple_archiver_list_free))) - SDArchiverLinkedList *used_filenames = NULL; + __attribute__((cleanup(simple_archiver_hash_map_free))) + SDArchiverHashMap *used_filenames = NULL; size_t generated_html_size = 0; @@ -452,10 +456,10 @@ CACHE_FILE_WRITE_CHECK: return -4; } - if (simple_archiver_list_get( - used_filenames, - c_simple_http_internal_write_filenames_to_cache_file, - cache_fd)) { + if (simple_archiver_hash_map_iter( + used_filenames, + c_simple_http_internal_write_filenames_to_cache_file, + cache_fd) != 0) { fprintf(stderr, "ERROR Failed to write filenames to cache file!\n"); return -6; } else if (fwrite("--- BEGIN HTML ---\n", 1, 19, cache_fd) != 19) { diff --git a/src/http_template.c b/src/http_template.c index e5eed4b..829f169 100644 --- a/src/http_template.c +++ b/src/http_template.c @@ -14,8 +14,6 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -// TODO: Update files map. - #include "http_template.h" // Standard library includes. @@ -25,6 +23,7 @@ // Third party includes. #include +#include #include // Local includes. @@ -62,7 +61,22 @@ int c_simple_http_internal_handle_inside_delimeters( SDArchiverHashMap *array_vars, const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map, SDArchiverLinkedList *string_part_list, - SDArchiverLinkedList **files_list_out); + SDArchiverHashMap **files_set_out); + +void c_simple_http_internal_set_out_insert(SDArchiverHashMap **set, + const char *str) { + if (set + && *set + && simple_archiver_hash_map_get(*set, str, strlen(str) + 1) == NULL) { + simple_archiver_hash_map_insert( + *set, + (void*)1, + strdup(str), + strlen(str) + 1, + simple_archiver_helper_datastructure_cleanup_nop, + NULL); + } +} void c_simple_http_internal_cleanup_ArrayVar( C_SIMPLE_HTTP_ArrayVar **array_var) { @@ -169,7 +183,7 @@ int c_simple_http_internal_parse_if_expression( char **left_side_out, char **right_side_out, uint_fast8_t *is_equality_out, - SDArchiverLinkedList **files_list_out) { + SDArchiverHashMap **files_set_out) { if (!left_side_out || !right_side_out || !is_equality_out) { fprintf(stderr, "ERROR Internal error! %s\n", var); return 1; @@ -288,11 +302,7 @@ int c_simple_http_internal_parse_if_expression( } c_simple_http_trim_end_whitespace(*left_side_out); left_side_size = strlen(*left_side_out); - if (files_list_out) { - simple_archiver_list_add(*files_list_out, - strdup(config_value->value), - NULL); - } + c_simple_http_internal_set_out_insert(files_set_out, config_value->value); } else { *left_side_out = strdup(config_value->value); c_simple_http_trim_end_whitespace(*left_side_out); @@ -346,7 +356,8 @@ int c_simple_http_internal_populate_array_vars( const C_SIMPLE_HTTP_HashMapWrapper *wrapped_hash_map, const char *var, SDArchiverHashMap *array_vars, - SDArchiverLinkedList *array_vars_keys) { + SDArchiverLinkedList *array_vars_keys, + SDArchiverHashMap **files_set_out) { C_SIMPLE_HTTP_ConfigValue *config_value = simple_archiver_hash_map_get(wrapped_hash_map->hash_map, *for_each_var_name, @@ -378,6 +389,7 @@ int c_simple_http_internal_populate_array_vars( c_simple_http_internal_cleanup_ArrayVar(&array_var); return 1; } + c_simple_http_internal_set_out_insert(files_set_out, config_value->value); } else { array_var->value = strdup(config_value->value); } @@ -400,6 +412,8 @@ int c_simple_http_internal_populate_array_vars( c_simple_http_internal_cleanup_ArrayVar(&array_var); return 1; } + c_simple_http_internal_set_out_insert(files_set_out, + config_value->value); } else { current_array_var->value = strdup(config_value->value); } @@ -438,7 +452,7 @@ int c_simple_http_internal_parse_iterate( SDArchiverHashMap *array_vars, SDArchiverLinkedList *string_part_list, const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map, - SDArchiverLinkedList **files_list_out) { + SDArchiverHashMap **files_set_out) { C_SIMPLE_HTTP_String_Part string_part; if (((*state) & 1) == 0) { // Using 0x7B instead of left curly-brace due to bug in vim navigation. @@ -511,7 +525,7 @@ int c_simple_http_internal_parse_iterate( array_vars, wrapped_hash_map, string_part_list, - files_list_out) != 0) { + files_set_out) != 0) { return 1; } } @@ -561,7 +575,7 @@ int c_simple_http_internal_handle_inside_delimeters( SDArchiverHashMap *array_vars, const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map, SDArchiverLinkedList *string_part_list, - SDArchiverLinkedList **files_list_out) { + SDArchiverHashMap **files_set_out) { C_SIMPLE_HTTP_String_Part string_part; if (var_size == 0) { fprintf(stderr, "ERROR No characters within delimeters!\n"); @@ -588,7 +602,7 @@ int c_simple_http_internal_handle_inside_delimeters( &left_side, &right_side, &is_equality, - files_list_out)) { + files_set_out)) { return 1; } else if (!left_side || !right_side) { fprintf(stderr, "ERROR Internal error! %s\n", var); @@ -662,7 +676,7 @@ int c_simple_http_internal_handle_inside_delimeters( &left_side, &right_side, &is_equality, - files_list_out)) { + files_set_out)) { return 1; } else if (!left_side || !right_side) { fprintf(stderr, "ERROR Internal error! %s\n", var); @@ -824,11 +838,8 @@ int c_simple_http_internal_handle_inside_delimeters( fprintf(stderr, "ERROR Failed to get value from FILE! %s\n", var); return 1; } - if (files_list_out) { - simple_archiver_list_add(*files_list_out, - strdup(config_value->value), - NULL); - } + c_simple_http_internal_set_out_insert(files_set_out, + config_value->value); } else { value_contents = strdup(config_value->value); value_contents_size = strlen(value_contents); @@ -904,7 +915,8 @@ int c_simple_http_internal_handle_inside_delimeters( wrapped_hash_map, var, array_vars, - for_state->array_vars_keys) != 0) { + for_state->array_vars_keys, + files_set_out) != 0) { if (for_each_var_name) { free(for_each_var_name); } @@ -940,7 +952,8 @@ int c_simple_http_internal_handle_inside_delimeters( wrapped_hash_map, var, array_vars, - for_state->array_vars_keys) != 0) { + for_state->array_vars_keys, + files_set_out) != 0) { if (for_each_var_name) { free(for_each_var_name); } @@ -1017,7 +1030,7 @@ int c_simple_http_internal_handle_inside_delimeters( array_vars, string_part_list, wrapped_hash_map, - files_list_out) != 0) { + files_set_out) != 0) { return 1; } } @@ -1113,10 +1126,8 @@ int c_simple_http_internal_handle_inside_delimeters( string_part.extra = html_buf_idx + 1; string_part.buf[string_part.size - 1] = 0; - if (files_list_out) { - char *variable_filename = strdup(config_value->value); - simple_archiver_list_add(*files_list_out, variable_filename, NULL); - } + c_simple_http_internal_set_out_insert(files_set_out, + config_value->value); } else { // Variable data is "config_value->value". string_part.size = strlen(config_value->value) + 1; @@ -1145,12 +1156,12 @@ char *c_simple_http_path_to_generated( const char *path, const C_SIMPLE_HTTP_HTTPTemplates *templates, size_t *output_buf_size, - SDArchiverLinkedList **files_list_out) { + SDArchiverHashMap **files_set_out) { if (output_buf_size) { *output_buf_size = 0; } - if (files_list_out) { - *files_list_out = simple_archiver_list_init(); + if (files_set_out) { + *files_set_out = simple_archiver_hash_map_init(); } size_t path_len_size_t = strlen(path) + 1; if (path_len_size_t > 0xFFFFFFFF) { @@ -1191,11 +1202,8 @@ char *c_simple_http_path_to_generated( } html_buf[html_file_size] = 0; html_buf_size = (size_t)html_file_size; - if (files_list_out) { - char *html_filename_copy = malloc(strlen(html_file_value->value) + 1); - strcpy(html_filename_copy, html_file_value->value); - simple_archiver_list_add(*files_list_out, html_filename_copy, NULL); - } + c_simple_http_internal_set_out_insert(files_set_out, + html_file_value->value); } else { C_SIMPLE_HTTP_ConfigValue *stored_html_config_value = simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5); @@ -1255,7 +1263,7 @@ char *c_simple_http_path_to_generated( array_vars, string_part_list, wrapped_hash_map, - files_list_out) != 0) { + files_set_out) != 0) { return NULL; } } diff --git a/src/http_template.h b/src/http_template.h index 8aa12ba..6fecde4 100644 --- a/src/http_template.h +++ b/src/http_template.h @@ -17,13 +17,16 @@ #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_HTTP_TEMPLATE_H_ #define SEODISPARATE_COM_C_SIMPLE_HTTP_HTTP_TEMPLATE_H_ -#include "http.h" // Standard library includes. #include // Third-party includes. #include +#include + +// Local includes. +#include "http.h" // Returns non-NULL on success, which must be free'd after use. Takes a path // string and templates and returns the generated HTML. If "output_buf_size" is @@ -35,7 +38,7 @@ char *c_simple_http_path_to_generated( const char *path, const C_SIMPLE_HTTP_HTTPTemplates *templates, size_t *output_buf_size, - SDArchiverLinkedList **files_list_out); + SDArchiverHashMap **files_set_out); #endif diff --git a/src/test.c b/src/test.c index 9034a16..5fd5feb 100644 --- a/src/test.c +++ b/src/test.c @@ -99,9 +99,13 @@ void test_internal_cleanup_delete_temporary_file(const char **filename) { } } -int test_internal_check_matching_string_in_list(void *value, void *ud) { - if (value && ud) { - if (strcmp(value, ud) == 0) { +int test_internal_check_matching_string_in_list( + const void *key, + __attribute__((unused)) size_t key_size, + __attribute__((unused)) const void *value, + void *ud) { + if (key && ud) { + if (strcmp(key, ud) == 0) { return 1; } } @@ -278,18 +282,18 @@ int main(int argc, char **argv) { size_t output_buf_size; - __attribute__((cleanup(simple_archiver_list_free))) - SDArchiverLinkedList *filenames_list = NULL; + __attribute__((cleanup(simple_archiver_hash_map_free))) + SDArchiverHashMap *filenames_set = NULL; __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) char *buf = c_simple_http_path_to_generated( - "/", &config, &output_buf_size, &filenames_list); + "/", &config, &output_buf_size, &filenames_set); ASSERT_TRUE(buf != NULL); ASSERT_TRUE(strcmp(buf, "

Test

") == 0); CHECK_TRUE(output_buf_size == 13); - CHECK_TRUE(filenames_list->count == 0); + CHECK_TRUE(filenames_set->count == 0); simple_archiver_helper_cleanup_c_string(&buf); - simple_archiver_list_free(&filenames_list); + simple_archiver_hash_map_free(&filenames_set); __attribute__((cleanup(test_internal_cleanup_delete_temporary_file))) const char *test_http_template_filename2 = @@ -321,7 +325,7 @@ int main(int argc, char **argv) { ASSERT_TRUE(config.paths != NULL); buf = c_simple_http_path_to_generated( - "/", &config, &output_buf_size, &filenames_list); + "/", &config, &output_buf_size, &filenames_set); ASSERT_TRUE(buf != NULL); //printf("%s\n", buf); ASSERT_TRUE( @@ -330,9 +334,9 @@ int main(int argc, char **argv) { "

Some text.


More text.

") == 0); CHECK_TRUE(output_buf_size == 46); - CHECK_TRUE(filenames_list->count == 0); + CHECK_TRUE(filenames_set->count == 0); simple_archiver_helper_cleanup_c_string(&buf); - simple_archiver_list_free(&filenames_list); + simple_archiver_hash_map_free(&filenames_set); __attribute__((cleanup(test_internal_cleanup_delete_temporary_file))) const char *test_http_template_filename3 = @@ -387,7 +391,7 @@ int main(int argc, char **argv) { ASSERT_TRUE(config.paths != NULL); buf = c_simple_http_path_to_generated( - "/", &config, &output_buf_size, &filenames_list); + "/", &config, &output_buf_size, &filenames_set); ASSERT_TRUE(buf != NULL); //printf("%s\n", buf); ASSERT_TRUE( @@ -396,14 +400,14 @@ int main(int argc, char **argv) { "

testVar text.


testVar2 text.

") == 0); CHECK_TRUE(output_buf_size == 53); - CHECK_TRUE(filenames_list->count == 1); - CHECK_TRUE(simple_archiver_list_get( - filenames_list, + CHECK_TRUE(filenames_set->count == 1); + CHECK_TRUE(simple_archiver_hash_map_iter( + filenames_set, test_internal_check_matching_string_in_list, (void*)test_http_template_html_filename) - != NULL); + != 0); simple_archiver_helper_cleanup_c_string(&buf); - simple_archiver_list_free(&filenames_list); + simple_archiver_hash_map_free(&filenames_set); __attribute__((cleanup(test_internal_cleanup_delete_temporary_file))) const char *test_http_template_filename4 = @@ -478,7 +482,7 @@ int main(int argc, char **argv) { ASSERT_TRUE(config.paths != NULL); buf = c_simple_http_path_to_generated( - "/", &config, &output_buf_size, &filenames_list); + "/", &config, &output_buf_size, &filenames_set); ASSERT_TRUE(buf != NULL); //printf("%s\n", buf); ASSERT_TRUE( @@ -487,17 +491,17 @@ int main(int argc, char **argv) { "

some test text in test var file.

") == 0); CHECK_TRUE(output_buf_size == 43); - CHECK_TRUE(filenames_list->count == 2); - CHECK_TRUE(simple_archiver_list_get( - filenames_list, + CHECK_TRUE(filenames_set->count == 2); + CHECK_TRUE(simple_archiver_hash_map_iter( + filenames_set, test_internal_check_matching_string_in_list, (void*)test_http_template_html_filename2) - != NULL); - CHECK_TRUE(simple_archiver_list_get( - filenames_list, + != 0); + CHECK_TRUE(simple_archiver_hash_map_iter( + filenames_set, test_internal_check_matching_string_in_list, (void*)test_http_template_html_var_filename) - != NULL); + != 0); simple_archiver_helper_cleanup_c_string(&buf); } -- 2.49.0