diff --git a/src/fort.c b/src/fort.c index a021afa..4b822b8 100644 --- a/src/fort.c +++ b/src/fort.c @@ -715,12 +715,14 @@ static fort_row_t* create_row_from_string(const char *str) char *pos = str_copy; char *base_pos = str_copy; + int number_of_separators = 0; while (*pos) { pos = strchr(pos, FORT_COL_SEPARATOR); if (pos != NULL) { *(pos) = '\0'; ++pos; - } + number_of_separators++; + } fort_cell_t *cell = create_cell(); if (cell == NULL) @@ -743,6 +745,25 @@ static fort_row_t* create_row_from_string(const char *str) base_pos = pos; } + /* special case if in format string last cell is empty */ + while (vector_size(row->cells) < (number_of_separators + 1)) { + fort_cell_t *cell = create_cell(); + if (cell == NULL) + goto clear; + + int status = fill_buffer_from_string(cell->str_buffer, ""); + if (IS_ERROR(status)) { + destroy_cell(cell); + goto clear; + } + + status = vector_push(row->cells, &cell); + if (IS_ERROR(status)) { + destroy_cell(cell); + goto clear; + } + } + F_FREE(str_copy); return row; diff --git a/tests/test_table.c b/tests/test_table.c index 72d3d6f..7dd45ef 100644 --- a/tests/test_table.c +++ b/tests/test_table.c @@ -170,7 +170,7 @@ void test_table_basic(void **state) assert_true( n == 4 ); n = ft_row_printf(table, 1, "%c|%s|%f", 'c', "234", 3.14); assert_true( n == 3 ); - n = ft_row_printf(table, 2, "%s|%f||%c", "234", 3.14, 'c'); + n = ft_row_printf(table, 2, "%s|%f||", "234", 3.14); assert_true( n == 4 ); char *table_str = ft_to_string(table); @@ -186,10 +186,45 @@ void test_table_basic(void **state) "| | | | |\n" "========================================\n" "| | | | |\n" - "| 234 | 3.140000 | | c |\n" + "| 234 | 3.140000 | | |\n" "| | | | |\n" "========================================\n"; +// fprintf(stderr, "content:\n%s", table_str); + assert_true( strcmp(table_str, table_str_etalon) == 0); + + free(table_str); + ft_destroy_table(table); + } + + WHEN("All cells are empty") { + table = ft_create_table(); + + int n = ft_hdr_printf(table, "|||"); + assert_true( n == 4 ); + n = ft_row_printf(table, 1, "|||"); + assert_true( n == 4 ); + n = ft_row_printf(table, 2, "|||"); + assert_true( n == 4 ); + + char *table_str = ft_to_string(table); + assert_true( table_str != NULL ); + const char *table_str_etalon = + "=============\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "=============\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "=============\n" + "| | | | |\n" + "| | | | |\n" + "| | | | |\n" + "=============\n"; + + // fprintf(stderr, "content:\n%s", table_str); assert_true( strcmp(table_str, table_str_etalon) == 0);