From 628b509991ba12dc73f59c976dbdc23cf2c55375 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Fri, 10 Jan 2020 21:22:41 +0300 Subject: [PATCH] [A] Add function `ft_row_count` --- ChangeLog.md | 1 + docs/index.md | 2 ++ lib/fort.c | 11 +++++++---- lib/fort.h | 10 ++++++++++ lib/fort.hpp | 12 ++++++++++++ src/fort.h | 10 ++++++++++ src/fort.hpp | 12 ++++++++++++ src/fort_impl.c | 11 +++++++---- tests/bb_tests/test_table_basic.c | 4 ++++ tests/bb_tests_cpp/test_table_basic.cpp | 2 ++ 10 files changed, 67 insertions(+), 8 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 81135bc..5351a78 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -5,6 +5,7 @@ - Add functions to check if table is empty to the API. - `ft_ln` returns status of operation. - Add new table property `adding_strategy` (2 strategies available - replace(default) and insert). +- Add function `ft_row_count` (`row_count` in C++ API) to get number of rows in the table. ## v0.3.2 diff --git a/docs/index.md b/docs/index.md index 55ba9c3..43fc3d9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -40,6 +40,7 @@ These pages contain the API documentation of **libfort** - simple library to cre - @link ft_set_memory_funcs ft_set_memory_funcs @endlink -- set memory allocation functions for the library - @link ft_set_default_printf_field_separator ft_set_default_printf_field_separator @endlink -- Set field separator for ft_printf, ft_printf_ln - @link ft_is_empty ft_is_empty @endlink -- check if table is empty + - @link ft_row_count ft_row_count @endlink -- get number of rows in the table - Data structures and types - @link ft_table_t ft_table_t @endlink -- table handler @@ -76,6 +77,7 @@ These pages contain the API documentation of **libfort** - simple library to cre - @link fort::table::row row @endlink -- get row - @link fort::table::column column @endlink -- get column - @link fort::table::is_empty is_empty @endlink -- check if table is empty + - @link fort::table::row_count row_count @endlink -- get number of rows in the table - @link fort::property_owner fort::property_owner @endlink -- base class for all objects (table, row, column, cell) for which user can specify properties - Modify appearance diff --git a/lib/fort.c b/lib/fort.c index a48c39d..86d147f 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -2797,10 +2797,13 @@ void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col) int ft_is_empty(const ft_table_t *table) { assert(table); - if (table->rows == NULL || (vector_size(table->rows) == 0)) - return 1; - else - return 0; + return ft_row_count(table) == 0; +} + +size_t ft_row_count(const ft_table_t *table) +{ + assert(table && table->rows); + return vector_size(table->rows); } static int ft_row_printf_impl_(ft_table_t *table, size_t row, const struct f_string_view *fmt, va_list *va) diff --git a/lib/fort.h b/lib/fort.h index 132086f..5e9d1d7 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -330,6 +330,16 @@ void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col); */ int ft_is_empty(const ft_table_t *table); +/** + * Get number of rows in the table. + * + * @param table + * Pointer to formatted table. + * @return + * Number of rows in the table. + */ +size_t ft_row_count(const ft_table_t *table); + #if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) /** diff --git a/lib/fort.hpp b/lib/fort.hpp index fe0ffb5..2591814 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -1069,6 +1069,18 @@ public: return ft_is_empty(table_); } + /** + * Get number of rows in the table. + * + * @return + * Number of rows in the table. + */ + std::size_t + row_count() const noexcept + { + return ft_row_count(table_); + } + /** * Get current cell. * diff --git a/src/fort.h b/src/fort.h index 132086f..5e9d1d7 100644 --- a/src/fort.h +++ b/src/fort.h @@ -330,6 +330,16 @@ void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col); */ int ft_is_empty(const ft_table_t *table); +/** + * Get number of rows in the table. + * + * @param table + * Pointer to formatted table. + * @return + * Number of rows in the table. + */ +size_t ft_row_count(const ft_table_t *table); + #if defined(FT_CLANG_COMPILER) || defined(FT_GCC_COMPILER) /** diff --git a/src/fort.hpp b/src/fort.hpp index fe0ffb5..2591814 100644 --- a/src/fort.hpp +++ b/src/fort.hpp @@ -1069,6 +1069,18 @@ public: return ft_is_empty(table_); } + /** + * Get number of rows in the table. + * + * @return + * Number of rows in the table. + */ + std::size_t + row_count() const noexcept + { + return ft_row_count(table_); + } + /** * Get current cell. * diff --git a/src/fort_impl.c b/src/fort_impl.c index 1c505ac..d52ecea 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -224,10 +224,13 @@ void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col) int ft_is_empty(const ft_table_t *table) { assert(table); - if (table->rows == NULL || (vector_size(table->rows) == 0)) - return 1; - else - return 0; + return ft_row_count(table) == 0; +} + +size_t ft_row_count(const ft_table_t *table) +{ + assert(table && table->rows); + return vector_size(table->rows); } static int ft_row_printf_impl_(ft_table_t *table, size_t row, const struct f_string_view *fmt, va_list *va) diff --git a/tests/bb_tests/test_table_basic.c b/tests/bb_tests/test_table_basic.c index 86001b0..3fecb53 100644 --- a/tests/bb_tests/test_table_basic.c +++ b/tests/bb_tests/test_table_basic.c @@ -198,6 +198,7 @@ void test_table_basic(void) table = ft_create_table(); assert_true(table != NULL); assert_true(ft_is_empty(table) == 1); + assert_true(ft_row_count(table) == 0); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); @@ -217,6 +218,7 @@ void test_table_basic(void) ft_set_cur_cell(table, 5, 6); assert_true(ft_is_empty(table) == 1); + assert_true(ft_row_count(table) == 0); ft_destroy_table(table); } @@ -226,6 +228,7 @@ void test_table_basic(void) assert_true(ft_is_empty(table) == 1); assert_true(ft_write_ln(table, "") == FT_SUCCESS); assert_true(ft_is_empty(table) == 0); + assert_true(ft_row_count(table) == 1); const char *table_str = ft_to_string(table); const char *table_str_etalon = @@ -247,6 +250,7 @@ void test_table_basic(void) assert_true(ft_write_ln(table, "3", "c", "234", "3.140000") == FT_SUCCESS); assert_true(ft_write_ln(table, "3", "c", "234", "3.140000") == FT_SUCCESS); assert_true(ft_is_empty(table) == 0); + assert_true(ft_row_count(table) == 3); const char *table_str = ft_to_string(table); assert_true(table_str != NULL); diff --git a/tests/bb_tests_cpp/test_table_basic.cpp b/tests/bb_tests_cpp/test_table_basic.cpp index 29e3b88..baf180e 100644 --- a/tests/bb_tests_cpp/test_table_basic.cpp +++ b/tests/bb_tests_cpp/test_table_basic.cpp @@ -38,6 +38,7 @@ void test_cpp_table_basic(void) std::string table_str_etalon = ""; assert_string_equal(table_str, table_str_etalon); assert_true(table.is_empty()); + assert_true(table.row_count() == 0); } WHEN("All columns are equal and not empty.") { @@ -66,6 +67,7 @@ void test_cpp_table_basic(void) "+---+---+-----+----------+\n"; assert_string_equal(table_str, table_str_etalon); assert_true(table.is_empty() == false); + assert_true(table.row_count() == 3); } WHEN("Checking basic constructors and assignmets.") {