Compare commits

..

No commits in common. "3fc301cde5e77db91cea0917bd96a5ac4ae31ebc" and "e6e9fc16afc5e4120a436856915d6d1c242b1b8a" have entirely different histories.

9 changed files with 75 additions and 106 deletions

View file

@ -4,17 +4,16 @@
Implemented "IF", "ELSEIF", "ELSE", "ENDIF", and "INDEX" for templates.
IF is used like: `{{{!IF Variable==SomeString}}}`.
ELSEIF is used like: `{{{!ELSEIF Variable==AnotherString}}}`.
ELSE is used like: `{{{!ELSE}}}`.
ENDIF is used like: `{{{!ENDIF}}}`.
IF is used like: `{{{!IF Variable==SomeString}}}`.
ELSEIF is used like: `{{{!ELSEIF Variable==AnotherString}}}`.
ELSE is used like: `{{{!ELSE}}}`.
ENDIF is used like: `{{{!ENDIF}}}`.
INDEX is used like: `{{{!INDEX ArrayVar[2]}}}`.
Implemented "FOREACH" and "ENDFOREACH" for templates.
FOREACH is used like:
PATH=/
HTML='''
{{{!FOREACH ArrayVar}}}
{{{ArrayVar}}}
@ -25,7 +24,6 @@ FOREACH is used like:
For multiple variables to expand in FOREACH:
PATH=/
HTML='''
{{{!FOREACH ArrayVar!ArrayVarSecond!ArrayVarThird}}}
{{{ArrayVar}}}

View file

@ -1 +0,0 @@
<b>Each File ONE</b><br>

View file

@ -1 +0,0 @@
<b>Each File TWO</b><br>

View file

@ -1 +0,0 @@
<b>Each File ZERO</b><br>

View file

@ -100,9 +100,6 @@ Nested FOREACH:<br>
{{{ArrayValueThird}}}
{{{!FOREACH ArrayValueFourth}}}
{{{ArrayValueFourth}}}
{{{!FOREACH Each_FILE}}}
{{{Each_FILE}}}
{{{!ENDFOREACH}}}
{{{!ENDFOREACH}}}
{{{!ENDFOREACH}}}
{{{!ENDFOREACH}}}
@ -146,10 +143,6 @@ 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'''

View file

@ -38,12 +38,8 @@
#include "helpers.h"
#include "http_template.h"
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;
int c_simple_http_internal_write_filenames_to_cache_file(void *data, void *ud) {
char *filename = data;
FILE *cache_fd = ud;
const size_t filename_size = strlen(filename);
@ -440,8 +436,8 @@ CACHE_FILE_WRITE_CHECK:
return -5;
}
__attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *used_filenames = NULL;
__attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *used_filenames = NULL;
size_t generated_html_size = 0;
@ -456,10 +452,10 @@ CACHE_FILE_WRITE_CHECK:
return -4;
}
if (simple_archiver_hash_map_iter(
used_filenames,
c_simple_http_internal_write_filenames_to_cache_file,
cache_fd) != 0) {
if (simple_archiver_list_get(
used_filenames,
c_simple_http_internal_write_filenames_to_cache_file,
cache_fd)) {
fprintf(stderr, "ERROR Failed to write filenames to cache file!\n");
return -6;
} else if (fwrite("--- BEGIN HTML ---\n", 1, 19, cache_fd) != 19) {

View file

@ -14,6 +14,8 @@
// 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.
@ -23,7 +25,6 @@
// Third party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
#include <SimpleArchiver/src/data_structures/hash_map.h>
#include <SimpleArchiver/src/helpers.h>
// Local includes.
@ -61,22 +62,7 @@ int c_simple_http_internal_handle_inside_delimeters(
SDArchiverHashMap *array_vars,
const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
SDArchiverLinkedList *string_part_list,
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);
}
}
SDArchiverLinkedList **files_list_out);
void c_simple_http_internal_cleanup_ArrayVar(
C_SIMPLE_HTTP_ArrayVar **array_var) {
@ -183,7 +169,7 @@ int c_simple_http_internal_parse_if_expression(
char **left_side_out,
char **right_side_out,
uint_fast8_t *is_equality_out,
SDArchiverHashMap **files_set_out) {
SDArchiverLinkedList **files_list_out) {
if (!left_side_out || !right_side_out || !is_equality_out) {
fprintf(stderr, "ERROR Internal error! %s\n", var);
return 1;
@ -302,7 +288,11 @@ int c_simple_http_internal_parse_if_expression(
}
c_simple_http_trim_end_whitespace(*left_side_out);
left_side_size = strlen(*left_side_out);
c_simple_http_internal_set_out_insert(files_set_out, config_value->value);
if (files_list_out) {
simple_archiver_list_add(*files_list_out,
strdup(config_value->value),
NULL);
}
} else {
*left_side_out = strdup(config_value->value);
c_simple_http_trim_end_whitespace(*left_side_out);
@ -356,8 +346,7 @@ 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,
SDArchiverHashMap **files_set_out) {
SDArchiverLinkedList *array_vars_keys) {
C_SIMPLE_HTTP_ConfigValue *config_value =
simple_archiver_hash_map_get(wrapped_hash_map->hash_map,
*for_each_var_name,
@ -389,7 +378,6 @@ 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);
}
@ -412,8 +400,6 @@ 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);
}
@ -452,7 +438,7 @@ int c_simple_http_internal_parse_iterate(
SDArchiverHashMap *array_vars,
SDArchiverLinkedList *string_part_list,
const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
SDArchiverHashMap **files_set_out) {
SDArchiverLinkedList **files_list_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.
@ -525,7 +511,7 @@ int c_simple_http_internal_parse_iterate(
array_vars,
wrapped_hash_map,
string_part_list,
files_set_out) != 0) {
files_list_out) != 0) {
return 1;
}
}
@ -575,7 +561,7 @@ int c_simple_http_internal_handle_inside_delimeters(
SDArchiverHashMap *array_vars,
const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
SDArchiverLinkedList *string_part_list,
SDArchiverHashMap **files_set_out) {
SDArchiverLinkedList **files_list_out) {
C_SIMPLE_HTTP_String_Part string_part;
if (var_size == 0) {
fprintf(stderr, "ERROR No characters within delimeters!\n");
@ -602,7 +588,7 @@ int c_simple_http_internal_handle_inside_delimeters(
&left_side,
&right_side,
&is_equality,
files_set_out)) {
files_list_out)) {
return 1;
} else if (!left_side || !right_side) {
fprintf(stderr, "ERROR Internal error! %s\n", var);
@ -676,7 +662,7 @@ int c_simple_http_internal_handle_inside_delimeters(
&left_side,
&right_side,
&is_equality,
files_set_out)) {
files_list_out)) {
return 1;
} else if (!left_side || !right_side) {
fprintf(stderr, "ERROR Internal error! %s\n", var);
@ -838,8 +824,11 @@ int c_simple_http_internal_handle_inside_delimeters(
fprintf(stderr, "ERROR Failed to get value from FILE! %s\n", var);
return 1;
}
c_simple_http_internal_set_out_insert(files_set_out,
config_value->value);
if (files_list_out) {
simple_archiver_list_add(*files_list_out,
strdup(config_value->value),
NULL);
}
} else {
value_contents = strdup(config_value->value);
value_contents_size = strlen(value_contents);
@ -915,8 +904,7 @@ int c_simple_http_internal_handle_inside_delimeters(
wrapped_hash_map,
var,
array_vars,
for_state->array_vars_keys,
files_set_out) != 0) {
for_state->array_vars_keys) != 0) {
if (for_each_var_name) {
free(for_each_var_name);
}
@ -952,8 +940,7 @@ int c_simple_http_internal_handle_inside_delimeters(
wrapped_hash_map,
var,
array_vars,
for_state->array_vars_keys,
files_set_out) != 0) {
for_state->array_vars_keys) != 0) {
if (for_each_var_name) {
free(for_each_var_name);
}
@ -1030,7 +1017,7 @@ int c_simple_http_internal_handle_inside_delimeters(
array_vars,
string_part_list,
wrapped_hash_map,
files_set_out) != 0) {
files_list_out) != 0) {
return 1;
}
}
@ -1126,8 +1113,10 @@ int c_simple_http_internal_handle_inside_delimeters(
string_part.extra = html_buf_idx + 1;
string_part.buf[string_part.size - 1] = 0;
c_simple_http_internal_set_out_insert(files_set_out,
config_value->value);
if (files_list_out) {
char *variable_filename = strdup(config_value->value);
simple_archiver_list_add(*files_list_out, variable_filename, NULL);
}
} else {
// Variable data is "config_value->value".
string_part.size = strlen(config_value->value) + 1;
@ -1156,12 +1145,12 @@ char *c_simple_http_path_to_generated(
const char *path,
const C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *output_buf_size,
SDArchiverHashMap **files_set_out) {
SDArchiverLinkedList **files_list_out) {
if (output_buf_size) {
*output_buf_size = 0;
}
if (files_set_out) {
*files_set_out = simple_archiver_hash_map_init();
if (files_list_out) {
*files_list_out = simple_archiver_list_init();
}
size_t path_len_size_t = strlen(path) + 1;
if (path_len_size_t > 0xFFFFFFFF) {
@ -1202,8 +1191,11 @@ char *c_simple_http_path_to_generated(
}
html_buf[html_file_size] = 0;
html_buf_size = (size_t)html_file_size;
c_simple_http_internal_set_out_insert(files_set_out,
html_file_value->value);
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);
}
} else {
C_SIMPLE_HTTP_ConfigValue *stored_html_config_value =
simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5);
@ -1263,7 +1255,7 @@ char *c_simple_http_path_to_generated(
array_vars,
string_part_list,
wrapped_hash_map,
files_set_out) != 0) {
files_list_out) != 0) {
return NULL;
}
}

View file

@ -17,16 +17,13 @@
#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 <stddef.h>
// Third-party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
#include <SimpleArchiver/src/data_structures/hash_map.h>
// 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
@ -38,7 +35,7 @@ char *c_simple_http_path_to_generated(
const char *path,
const C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *output_buf_size,
SDArchiverHashMap **files_set_out);
SDArchiverLinkedList **files_list_out);
#endif

View file

@ -99,13 +99,9 @@ void test_internal_cleanup_delete_temporary_file(const char **filename) {
}
}
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) {
int test_internal_check_matching_string_in_list(void *value, void *ud) {
if (value && ud) {
if (strcmp(value, ud) == 0) {
return 1;
}
}
@ -282,18 +278,18 @@ int main(int argc, char **argv) {
size_t output_buf_size;
__attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *filenames_set = NULL;
__attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *filenames_list = NULL;
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *buf = c_simple_http_path_to_generated(
"/", &config, &output_buf_size, &filenames_set);
"/", &config, &output_buf_size, &filenames_list);
ASSERT_TRUE(buf != NULL);
ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0);
CHECK_TRUE(output_buf_size == 13);
CHECK_TRUE(filenames_set->count == 0);
CHECK_TRUE(filenames_list->count == 0);
simple_archiver_helper_cleanup_c_string(&buf);
simple_archiver_hash_map_free(&filenames_set);
simple_archiver_list_free(&filenames_list);
__attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
const char *test_http_template_filename2 =
@ -325,7 +321,7 @@ int main(int argc, char **argv) {
ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated(
"/", &config, &output_buf_size, &filenames_set);
"/", &config, &output_buf_size, &filenames_list);
ASSERT_TRUE(buf != NULL);
//printf("%s\n", buf);
ASSERT_TRUE(
@ -334,9 +330,9 @@ int main(int argc, char **argv) {
"<h1> Some text. </h1><br><h2> More text. </h2>")
== 0);
CHECK_TRUE(output_buf_size == 46);
CHECK_TRUE(filenames_set->count == 0);
CHECK_TRUE(filenames_list->count == 0);
simple_archiver_helper_cleanup_c_string(&buf);
simple_archiver_hash_map_free(&filenames_set);
simple_archiver_list_free(&filenames_list);
__attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
const char *test_http_template_filename3 =
@ -391,7 +387,7 @@ int main(int argc, char **argv) {
ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated(
"/", &config, &output_buf_size, &filenames_set);
"/", &config, &output_buf_size, &filenames_list);
ASSERT_TRUE(buf != NULL);
//printf("%s\n", buf);
ASSERT_TRUE(
@ -400,14 +396,14 @@ int main(int argc, char **argv) {
"<h1> testVar text. </h1><br><h2> testVar2 text. </h2>")
== 0);
CHECK_TRUE(output_buf_size == 53);
CHECK_TRUE(filenames_set->count == 1);
CHECK_TRUE(simple_archiver_hash_map_iter(
filenames_set,
CHECK_TRUE(filenames_list->count == 1);
CHECK_TRUE(simple_archiver_list_get(
filenames_list,
test_internal_check_matching_string_in_list,
(void*)test_http_template_html_filename)
!= 0);
!= NULL);
simple_archiver_helper_cleanup_c_string(&buf);
simple_archiver_hash_map_free(&filenames_set);
simple_archiver_list_free(&filenames_list);
__attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
const char *test_http_template_filename4 =
@ -482,7 +478,7 @@ int main(int argc, char **argv) {
ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated(
"/", &config, &output_buf_size, &filenames_set);
"/", &config, &output_buf_size, &filenames_list);
ASSERT_TRUE(buf != NULL);
//printf("%s\n", buf);
ASSERT_TRUE(
@ -491,17 +487,17 @@ int main(int argc, char **argv) {
"<h1> some test text in test var file. </h1>")
== 0);
CHECK_TRUE(output_buf_size == 43);
CHECK_TRUE(filenames_set->count == 2);
CHECK_TRUE(simple_archiver_hash_map_iter(
filenames_set,
CHECK_TRUE(filenames_list->count == 2);
CHECK_TRUE(simple_archiver_list_get(
filenames_list,
test_internal_check_matching_string_in_list,
(void*)test_http_template_html_filename2)
!= 0);
CHECK_TRUE(simple_archiver_hash_map_iter(
filenames_set,
!= NULL);
CHECK_TRUE(simple_archiver_list_get(
filenames_list,
test_internal_check_matching_string_in_list,
(void*)test_http_template_html_var_filename)
!= 0);
!= NULL);
simple_archiver_helper_cleanup_c_string(&buf);
}