From 423ddb3ab80ddc9eeaae5d6e4a41bdbc6ebbe57f Mon Sep 17 00:00:00 2001 From: seleznevae Date: Sun, 21 Jan 2018 12:53:26 +0300 Subject: [PATCH] [R] Removed redundant code --- include/fort.h | 12 ++++++------ src/fort.c | 21 +++++++++++++-------- src/string_buffer.c | 24 ------------------------ tests/test_table.c | 38 ++++++++++++++++++-------------------- 4 files changed, 37 insertions(+), 58 deletions(-) diff --git a/include/fort.h b/include/fort.h index 84460a8..f4c851c 100644 --- a/include/fort.h +++ b/include/fort.h @@ -99,13 +99,13 @@ FORT_EXTERN void ft_destroy_table(FTABLE *FORT_RESTRICT table); FORT_EXTERN void ft_ln(FTABLE *FORT_RESTRICT table); -FORT_EXTERN int ft_printf(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_RESTRICT fmt, ...); -FORT_EXTERN int ft_printf_ln(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_RESTRICT fmt, ...); +FORT_EXTERN int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); +FORT_EXTERN int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); -#define FT_PRINTF(table, row, ...) \ - (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf(table, row, __VA_ARGS__)) -#define FT_PRINTF_LN(table, row, ...) \ - (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_ln(table, row, __VA_ARGS__)) +#define FT_PRINTF(table, ...) \ + (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf(table, __VA_ARGS__)) +#define FT_PRINTF_LN(table, ...) \ + (( 0 ? fprintf(stderr, __VA_ARGS__) : 1), ft_printf_ln(table, __VA_ARGS__)) FORT_EXTERN int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...); diff --git a/src/fort.c b/src/fort.c index be47a0e..60490bc 100644 --- a/src/fort.c +++ b/src/fort.c @@ -140,7 +140,7 @@ int ft_hdr_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, .. va_list va; va_start(va, fmt); - int result = ft_row_printf_impl(table, 0, fmt, &va); + int result = ft_row_printf_impl(table, table->cur_row, fmt, &va); va_end(va); if (result >= 0 && table->rows) { int sz = vector_size(table->rows); @@ -158,7 +158,7 @@ int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, va_list va; va_start(va, fmt); - int result = ft_row_printf_impl(table, 0, fmt, &va); + int result = ft_row_printf_impl(table, table->cur_row, fmt, &va); va_end(va); if (result >= 0 && table->rows) { int sz = vector_size(table->rows); @@ -172,20 +172,22 @@ int ft_hdr_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, return result; } -int ft_printf(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_RESTRICT fmt, ...) +int ft_printf(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) { + assert(table); va_list va; va_start(va, fmt); - int result = ft_row_printf_impl(table, row, fmt, &va); + int result = ft_row_printf_impl(table, table->cur_row, fmt, &va); va_end(va); return result; } -int ft_printf_ln(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_RESTRICT fmt, ...) +int ft_printf_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT fmt, ...) { + assert(table); va_list va; va_start(va, fmt); - int result = ft_row_printf_impl(table, row, fmt, &va); + int result = ft_row_printf_impl(table, table->cur_row, fmt, &va); if (result >= 0) { ft_ln(table); } @@ -196,6 +198,7 @@ int ft_printf_ln(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_RESTR int ft_write(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content) { + assert(table); string_buffer_t *str_buffer = get_cur_str_buffer_and_create_if_not_exists(table); if (str_buffer == NULL) return F_ERROR; @@ -209,6 +212,7 @@ int ft_write(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content int ft_write_ln(FTABLE *FORT_RESTRICT table, const char* FORT_RESTRICT cell_content) { + assert(table); int status = ft_write(table, cell_content); if (IS_SUCCESS(status)) { ft_ln(table); @@ -249,11 +253,12 @@ FORT_EXTERN int ft_nwrite_ln(FTABLE *FORT_RESTRICT table, size_t n, const char* for (size_t i = 0; i < n; ++i) { const char *cell = va_arg(va, const char*); status = ft_write(table, cell); - if (IS_ERROR(status)) + if (IS_ERROR(status)) { + va_end(va); return status; + } } va_end(va); - return status; ft_ln(table); return status; diff --git a/src/string_buffer.c b/src/string_buffer.c index dfdd33f..0ab89c5 100644 --- a/src/string_buffer.c +++ b/src/string_buffer.c @@ -188,27 +188,6 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, size_t table_colum return written; - -// const char *substr = str_n_substring(buffer->str, '\n', buffer_row); -// if (substr == NULL) -// return -1; -// const char *next_substr = str_n_substring(buffer->str, '\n', buffer_row + 1); -// size_t buf_row_len = 0; -// if (next_substr) { -// buf_row_len = next_substr - substr - 1; -// } else { -// buf_row_len = strlen(buffer->str) - (next_substr - buffer->str); -// } todo -// if (buf_row_len > content_width) -// return -1; - -// written += snprintf(buf + written, buf_len - written, "%*s", (int)buf_row_len, substr); -// if (written < 0) -// return written; -// written += snprint_n_chars(buf + written, buf_len - written, content_width - buf_row_len, ' '); -// if (written < 0) -// return written; - const char *beg = NULL; const char *end = NULL; str_n_substring(buffer->str, '\n', buffer_row, &beg, &end); @@ -225,9 +204,6 @@ int buffer_printf(string_buffer_t *buffer, size_t buffer_row, size_t table_colum if (written < 0) return written; -// written += snprintf(buf + written, buf_len - written, "%*s", (int)content_width, buffer->str); -// if (written < 0) -// return written; written += snprint_n_chars(buf + written, buf_len - written, right, ' '); return written; diff --git a/tests/test_table.c b/tests/test_table.c index 4dc5cb8..259d4bb 100644 --- a/tests/test_table.c +++ b/tests/test_table.c @@ -33,7 +33,7 @@ void test_table_sizes(void **state) } WHEN("Insert two cells in the next row") { - int n = FT_PRINTF_LN(table, 1, "%c|%c", 'c', 'd'); + int n = FT_PRINTF_LN(table, "%c|%c", 'c', 'd'); assert_true( n == 2 ); status = get_table_sizes(table, &rows, &cols); assert_true( IS_SUCCESS(status) ); @@ -42,7 +42,7 @@ void test_table_sizes(void **state) } WHEN("Insert five cells in the next row") { - int n = FT_PRINTF_LN(table, 2, "%d|%d|%d|%d|%d", 1, 2, 3, 4, 5); + int n = FT_PRINTF_LN(table, "%d|%d|%d|%d|%d", 1, 2, 3, 4, 5); assert_true( n == 5 ); status = get_table_sizes(table, &rows, &cols); assert_true( IS_SUCCESS(status) ); @@ -80,7 +80,7 @@ void test_table_geometry(void **state) } WHEN("Inserting 3 cells in the next row") { - int n = FT_PRINTF_LN(table, 1, "%c|%s|%c", 'c', "as", 'e'); + int n = FT_PRINTF_LN(table, "%c|%s|%c", 'c', "as", 'e'); assert_true( n == 3 ); status = table_geometry(table, &height, &width); assert_true( IS_SUCCESS(status) ); @@ -102,9 +102,9 @@ void test_table_basic(void **state) int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 1, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + n = FT_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 2, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + n = FT_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -137,9 +137,9 @@ void test_table_basic(void **state) int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 1, "%c|%s|%f|%d", 'c', "234", 3.14, 3); + n = FT_PRINTF_LN(table, "%c|%s|%f|%d", 'c', "234", 3.14, 3); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 2, "%s|%f|%d|%c", "234", 3.14, 3, 'c'); + n = FT_PRINTF_LN(table, "%s|%f|%d|%c", "234", 3.14, 3, 'c'); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -170,9 +170,9 @@ void test_table_basic(void **state) int n = FT_HDR_PRINTF_LN(table, "||%s|%f", "234", 3.14); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 1, "%c|%s|%f", 'c', "234", 3.14); + n = FT_PRINTF_LN(table, "%c|%s|%f", 'c', "234", 3.14); assert_true( n == 3 ); - n = FT_PRINTF_LN(table, 2, "%s|%f||", "234", 3.14); + n = FT_PRINTF_LN(table, "%s|%f||", "234", 3.14); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -203,9 +203,9 @@ void test_table_basic(void **state) int n = FT_HDR_PRINTF_LN(table, "|||"); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 1, "|||"); + n = FT_PRINTF_LN(table, "|||"); assert_true( n == 4 ); - n = FT_PRINTF_LN(table, 2, "|||"); + n = FT_PRINTF_LN(table, "|||"); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -403,7 +403,7 @@ void test_table_options(void **state) ft_set_default_options(&table_options); table = create_test_int_table(); - int n = ft_printf_ln(table, 3, "|||"); + int n = ft_printf_ln(table, "|||"); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -618,13 +618,11 @@ void test_table_options(void **state) WHEN("All columns are equal and not empty") { table = ft_create_table(); - int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + int n = FT_HDR_PRINTF_LN(table, "%d|%c|%s|%f", 4, 'c', "234", 3.14); assert_true( n == 4 ); -// n = FT_PRINTF_LN(table, 1, "%d|%c|%s|%f", 3, 'c', "234\n123", 3.14); -// assert_true( n == 4 ); -// FT_NWRITE_LN(table, "3", "c", "234", "3.140000"); - FT_NWRITE_LN(table, "3", "c", "234\n12", "3.140000"); - n = FT_PRINTF_LN(table, 2, "%d|%c|%s|%f", 3, 'c', "234", 3.14); + n = FT_NWRITE_LN(table, "5", "c", "234\n12", "3.140000"); + assert_true( n == F_SUCCESS ); + n = FT_PRINTF_LN(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14); assert_true( n == 4 ); const char *table_str = ft_to_string(table); @@ -632,11 +630,11 @@ void test_table_options(void **state) const char *table_str_etalon = "+---+---+-----+----------+\n" "| | | | |\n" - "| 3 | c | 234 | 3.140000 |\n" + "| 4 | c | 234 | 3.140000 |\n" "| | | | |\n" "+---+---+-----+----------+\n" "| | | | |\n" - "| 3 | c | 234 | 3.140000 |\n" + "| 5 | c | 234 | 3.140000 |\n" "| | | 12 | |\n" "| | | | |\n" "+---+---+-----+----------+\n"