diff --git a/ChangeLog.md b/ChangeLog.md index 992046d..ff6725c 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -7,6 +7,7 @@ - `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. +- Add function `ft_strerror` to get string descriptions of error codes. ### Bug fixes diff --git a/docs/index.md b/docs/index.md index b78f258..bdf7942 100644 --- a/docs/index.md +++ b/docs/index.md @@ -44,6 +44,7 @@ These pages contain the API documentation of **libfort** - simple library to cre - @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 + - @link ft_strerror ft_strerror @endlink -- get string describing the error code - Data structures and types - @link ft_table_t ft_table_t @endlink -- table handler diff --git a/lib/fort.c b/lib/fort.c index 654bc17..25052c7 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -3614,6 +3614,25 @@ void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *pt set_memory_funcs(f_malloc, f_free); } +const char *ft_strerror(int error_code) +{ + switch (error_code) { + case FT_MEMORY_ERROR: + return "Libfort error (out of memory)"; + case FT_ERROR: + return "Libfort error (general error)"; + case FT_EINVAL: + return "Libfort error (invalid argument)"; + case FT_INTERN_ERROR: + return "Libfort error (internal logic error)"; + default: + if (error_code < 0) + return "Libfort unknown error"; + else + return "Libfort success"; + } +} + int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span) { assert(table); diff --git a/lib/fort.h b/lib/fort.h index 713fe0f..94a873e 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -73,10 +73,33 @@ SOFTWARE. /***************************************************************************** * RETURN CODES *****************************************************************************/ + +/** + * Operation successfully ended. + */ #define FT_SUCCESS 0 + +/** + * Memory allocation failed. + */ #define FT_MEMORY_ERROR -1 + +/** + * General error. + */ #define FT_ERROR -2 + +/** + * Invalid argument. + */ #define FT_EINVAL -3 + +/** + * Libfort internal logic error. + */ +#define FT_INTERN_ERROR -4 + + #define FT_IS_SUCCESS(arg) ((arg) >= 0) #define FT_IS_ERROR(arg) ((arg) < 0) @@ -937,6 +960,16 @@ int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span) void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr)); +/** + * Return string describing the `error_code`. + * + * @param error_code + * Error code returned by the library. + * @return + * String describing the error. + */ +const char *ft_strerror(int error_code); + #ifdef FT_HAVE_WCHAR diff --git a/src/fort.h b/src/fort.h index 713fe0f..94a873e 100644 --- a/src/fort.h +++ b/src/fort.h @@ -73,10 +73,33 @@ SOFTWARE. /***************************************************************************** * RETURN CODES *****************************************************************************/ + +/** + * Operation successfully ended. + */ #define FT_SUCCESS 0 + +/** + * Memory allocation failed. + */ #define FT_MEMORY_ERROR -1 + +/** + * General error. + */ #define FT_ERROR -2 + +/** + * Invalid argument. + */ #define FT_EINVAL -3 + +/** + * Libfort internal logic error. + */ +#define FT_INTERN_ERROR -4 + + #define FT_IS_SUCCESS(arg) ((arg) >= 0) #define FT_IS_ERROR(arg) ((arg) < 0) @@ -937,6 +960,16 @@ int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span) void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *ptr)); +/** + * Return string describing the `error_code`. + * + * @param error_code + * Error code returned by the library. + * @return + * String describing the error. + */ +const char *ft_strerror(int error_code); + #ifdef FT_HAVE_WCHAR diff --git a/src/fort_impl.c b/src/fort_impl.c index 61e3d0b..5e16f3f 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -1018,6 +1018,25 @@ void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free)(void *pt set_memory_funcs(f_malloc, f_free); } +const char *ft_strerror(int error_code) +{ + switch (error_code) { + case FT_MEMORY_ERROR: + return "Libfort error (out of memory)"; + case FT_ERROR: + return "Libfort error (general error)"; + case FT_EINVAL: + return "Libfort error (invalid argument)"; + case FT_INTERN_ERROR: + return "Libfort error (internal logic error)"; + default: + if (error_code < 0) + return "Libfort unknown error"; + else + return "Libfort success"; + } +} + int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span) { assert(table); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 132a946..e88eb85 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,6 +12,7 @@ add_executable(${PROJECT_NAME}_test_dev bb_tests/test_table_border_style.c bb_tests/test_table_properties.c bb_tests/test_memory_errors.c + bb_tests/test_error_codes.c tests.c test_utils.c) target_link_libraries(${PROJECT_NAME}_test_dev @@ -30,6 +31,7 @@ add_executable(${PROJECT_NAME}_test bb_tests/test_table_border_style.c bb_tests/test_table_properties.c bb_tests/test_memory_errors.c + bb_tests/test_error_codes.c tests.c test_utils.c) target_link_libraries(${PROJECT_NAME}_test diff --git a/tests/bb_tests/test_error_codes.c b/tests/bb_tests/test_error_codes.c new file mode 100644 index 0000000..bb9aa3e --- /dev/null +++ b/tests/bb_tests/test_error_codes.c @@ -0,0 +1,27 @@ +#include "tests.h" +#include "fort.h" + + +void test_error_codes(void) +{ + // Nonnegative code is success + { + assert_str_equal(ft_strerror(0), "Libfort success"); + assert_str_equal(ft_strerror(1), "Libfort success"); + assert_str_equal(ft_strerror(2), "Libfort success"); + assert_str_equal(ft_strerror(42), "Libfort success"); + assert_str_equal(ft_strerror(INT_MAX), "Libfort success"); + } + + // Error codes + { + assert_str_equal(ft_strerror(FT_MEMORY_ERROR), "Libfort error (out of memory)"); + assert_str_equal(ft_strerror(FT_ERROR), "Libfort error (general error)"); + assert_str_equal(ft_strerror(FT_EINVAL), "Libfort error (invalid argument)"); + assert_str_equal(ft_strerror(FT_INTERN_ERROR), "Libfort error (internal logic error)"); + + assert_str_equal(ft_strerror(-42), "Libfort unknown error"); + assert_str_equal(ft_strerror(-666), "Libfort unknown error"); + assert_str_equal(ft_strerror(-INT_MAX), "Libfort unknown error"); + } +} diff --git a/tests/main_test.c b/tests/main_test.c index 6ea2a1a..263ba88 100644 --- a/tests/main_test.c +++ b/tests/main_test.c @@ -24,6 +24,7 @@ void test_table_cell_properties(void); void test_table_text_styles(void); void test_table_tbl_properties(void); void test_memory_errors(void); +void test_error_codes(void); #ifdef FT_HAVE_UTF8 void test_utf8_table(void); #endif @@ -60,6 +61,7 @@ struct test_case bb_test_suite [] = { {"test_table_tbl_properties", test_table_tbl_properties}, {"test_table_text_styles", test_table_text_styles}, {"test_memory_errors", test_memory_errors}, + {"test_error_codes", test_error_codes}, }; #ifdef FORT_WB_TESTING_ENABLED