[A] Add ft_strerror function

This commit is contained in:
seleznevae 2020-02-08 12:15:35 +03:00
parent bff48549de
commit 58a63f90f2
9 changed files with 137 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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");
}
}

View File

@ -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