From 1b42320163d73a6fb7715c58d65a676fe3fd03db Mon Sep 17 00:00:00 2001 From: seleznevae Date: Sat, 8 Feb 2020 13:04:08 +0300 Subject: [PATCH] [C] Changed codes of errors --- ChangeLog.md | 1 + lib/fort.c | 46 +++++++++++++++---------------- lib/fort.h | 16 +++++------ src/cell.c | 2 +- src/fort.h | 16 +++++------ src/fort_impl.c | 18 ++++++------ src/properties.c | 2 +- src/row.c | 10 +++---- src/table.c | 6 ++-- src/vector.c | 8 +++--- tests/bb_tests/test_error_codes.c | 3 +- 11 files changed, 65 insertions(+), 63 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index ff6725c..1df9830 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -8,6 +8,7 @@ - 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. +- Change error code names and values. ### Bug fixes diff --git a/lib/fort.c b/lib/fort.c index 25052c7..9fa07ba 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -2580,7 +2580,7 @@ f_status fill_cell_from_buffer(f_cell_t *cell, const f_string_buffer_t *buffer) #endif /* FT_HAVE_UTF8 */ default: assert(0); - return FT_ERROR; + return FT_GEN_ERROR; } } @@ -2761,7 +2761,7 @@ static int split_cur_row(ft_table_t *table, f_row_t **tail_of_cur_row) f_row_t *tail = split_row(row, table->cur_col); if (!tail) { tail_of_cur_row = NULL; - return FT_ERROR; + return FT_GEN_ERROR; } *tail_of_cur_row = tail; @@ -2776,12 +2776,12 @@ int ft_ln(ft_table_t *table) case FT_STRATEGY_INSERT: { f_row_t *new_row = NULL; if (FT_IS_ERROR(split_cur_row(table, &new_row))) { - return FT_ERROR; + return FT_GEN_ERROR; } if (new_row) { if (FT_IS_ERROR(vector_insert(table->rows, &new_row, table->cur_row + 1))) { destroy_row(new_row); - return FT_ERROR; + return FT_GEN_ERROR; } } break; @@ -3034,7 +3034,7 @@ static int ft_write_impl_(ft_table_t *table, const f_string_view_t *cell_content assert(table); f_string_buffer_t *buf = get_cur_str_buffer_and_create_if_not_exists(table); if (buf == NULL) - return FT_ERROR; + return FT_GEN_ERROR; int status = FT_SUCCESS; switch (cell_content->type) { @@ -3052,7 +3052,7 @@ static int ft_write_impl_(ft_table_t *table, const f_string_view_t *cell_content break; #endif default: - status = FT_ERROR; + status = FT_GEN_ERROR; } if (FT_IS_SUCCESS(status)) { table->cur_col++; @@ -3436,7 +3436,7 @@ int ft_add_separator(ft_table_t *table) (*sep_p)->enabled = F_TRUE; if (*sep_p == NULL) - return FT_ERROR; + return FT_GEN_ERROR; return FT_SUCCESS; } @@ -3574,7 +3574,7 @@ int ft_set_cell_prop(ft_table_t *table, size_t row, size_t col, uint32_t propert if (table->properties->cell_properties == NULL) { table->properties->cell_properties = create_cell_prop_container(); if (table->properties->cell_properties == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } } @@ -3619,7 +3619,7 @@ const char *ft_strerror(int error_code) switch (error_code) { case FT_MEMORY_ERROR: return "Libfort error (out of memory)"; - case FT_ERROR: + case FT_GEN_ERROR: return "Libfort error (general error)"; case FT_EINVAL: return "Libfort error (invalid argument)"; @@ -3646,7 +3646,7 @@ int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span) f_row_t *row_p = get_row_and_create_if_not_exists(table, row); if (row_p == NULL) - return FT_ERROR; + return FT_GEN_ERROR; return row_set_cell_span(row_p, col, hor_span); } @@ -4702,7 +4702,7 @@ f_status set_cell_property(f_cell_prop_container_t *cont, size_t row, size_t col { f_cell_props_t *opt = get_cell_prop_and_create_if_not_exists(cont, row, col); if (opt == NULL) - return FT_ERROR; + return FT_GEN_ERROR; return set_cell_property_impl(opt, property, value); /* @@ -5498,7 +5498,7 @@ f_status insert_row(f_row_t *cur_row, f_row_t *ins_row, size_t pos) while (vector_size(cur_row->cells) < pos) { f_cell_t *new_cell = create_cell(); if (!new_cell) - return FT_ERROR; + return FT_GEN_ERROR; vector_push(cur_row->cells, &new_cell); } @@ -5511,7 +5511,7 @@ f_status insert_row(f_row_t *cur_row, f_row_t *ins_row, size_t pos) while (i--) { vector_erase(cur_row->cells, pos); } - return FT_ERROR; + return FT_GEN_ERROR; } } /* Clear cells so that it will be safe to destroy this row */ @@ -5573,7 +5573,7 @@ f_status row_set_cell_span(f_row_t *row, size_t cell_column, size_t hor_span) f_cell_t *main_cell = get_cell_and_create_if_not_exists(row, cell_column); if (main_cell == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } set_cell_type(main_cell, GROUP_MASTER_CELL); --hor_span; @@ -5582,7 +5582,7 @@ f_status row_set_cell_span(f_row_t *row, size_t cell_column, size_t hor_span) while (hor_span) { f_cell_t *slave_cell = get_cell_and_create_if_not_exists(row, cell_column); if (slave_cell == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } set_cell_type(slave_cell, GROUP_SLAVE_CELL); --hor_span; @@ -5601,7 +5601,7 @@ int print_row_separator_impl(f_conv_context_t *cntx, { assert(cntx); - int status = FT_ERROR; + int status = FT_GEN_ERROR; const f_context_t *context = cntx->cntx; @@ -7030,7 +7030,7 @@ f_status table_rows_and_cols_geometry(const ft_table_t *table, enum f_geometry_type geom) { if (table == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } size_t max_invis_codepoints = 0; @@ -7045,7 +7045,7 @@ f_status table_rows_and_cols_geometry(const ft_table_t *table, if (col_width_arr == NULL || row_height_arr == NULL) { F_FREE(col_width_arr); F_FREE(row_height_arr); - return FT_ERROR; + return FT_GEN_ERROR; } int combined_cells_found = 0; @@ -7163,7 +7163,7 @@ FT_INTERNAL f_status table_geometry(const ft_table_t *table, size_t *height, size_t *width) { if (table == NULL) - return FT_ERROR; + return FT_GEN_ERROR; *height = 0; *width = 0; @@ -7302,7 +7302,7 @@ int vector_push(f_vector_t *vector, const void *item) if (vector->m_size == vector->m_capacity) { if (vector_reallocate_(vector, vector->m_capacity * 2) == -1) - return FT_ERROR; + return FT_GEN_ERROR; vector->m_capacity = vector->m_capacity * 2; } @@ -7322,7 +7322,7 @@ int vector_insert(f_vector_t *vector, const void *item, size_t pos) size_t needed_capacity = MAX(pos + 1, vector->m_size + 1); if (vector->m_capacity < needed_capacity) { if (vector_reallocate_(vector, needed_capacity) == -1) - return FT_ERROR; + return FT_GEN_ERROR; vector->m_capacity = needed_capacity; } size_t offset = pos * vector->m_item_size; @@ -7402,7 +7402,7 @@ f_status vector_swap(f_vector_t *cur_vec, f_vector_t *mv_vec, size_t pos) size_t min_targ_size = pos + mv_sz; if (vector_capacity(cur_vec) < min_targ_size) { if (vector_reallocate_(cur_vec, min_targ_size) == -1) - return FT_ERROR; + return FT_GEN_ERROR; cur_vec->m_capacity = min_targ_size; } @@ -7451,7 +7451,7 @@ int vector_erase(f_vector_t *vector, size_t index) assert(vector); if (vector->m_size == 0 || index >= vector->m_size) - return FT_ERROR; + return FT_GEN_ERROR; memmove((char *)vector->m_data + vector->m_item_size * index, (char *)vector->m_data + vector->m_item_size * (index + 1), diff --git a/lib/fort.h b/lib/fort.h index 94a873e..825359e 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -82,22 +82,22 @@ SOFTWARE. /** * Memory allocation failed. */ -#define FT_MEMORY_ERROR -1 - -/** - * General error. - */ -#define FT_ERROR -2 +#define FT_MEMORY_ERROR -1 /** * Invalid argument. */ -#define FT_EINVAL -3 +#define FT_EINVAL -2 /** * Libfort internal logic error. */ -#define FT_INTERN_ERROR -4 +#define FT_INTERN_ERROR -3 + +/** + * General error. + */ +#define FT_GEN_ERROR -4 #define FT_IS_SUCCESS(arg) ((arg) >= 0) diff --git a/src/cell.c b/src/cell.c index cab0ab1..a808237 100644 --- a/src/cell.c +++ b/src/cell.c @@ -289,7 +289,7 @@ f_status fill_cell_from_buffer(f_cell_t *cell, const f_string_buffer_t *buffer) #endif /* FT_HAVE_UTF8 */ default: assert(0); - return FT_ERROR; + return FT_GEN_ERROR; } } diff --git a/src/fort.h b/src/fort.h index 94a873e..825359e 100644 --- a/src/fort.h +++ b/src/fort.h @@ -82,22 +82,22 @@ SOFTWARE. /** * Memory allocation failed. */ -#define FT_MEMORY_ERROR -1 - -/** - * General error. - */ -#define FT_ERROR -2 +#define FT_MEMORY_ERROR -1 /** * Invalid argument. */ -#define FT_EINVAL -3 +#define FT_EINVAL -2 /** * Libfort internal logic error. */ -#define FT_INTERN_ERROR -4 +#define FT_INTERN_ERROR -3 + +/** + * General error. + */ +#define FT_GEN_ERROR -4 #define FT_IS_SUCCESS(arg) ((arg) >= 0) diff --git a/src/fort_impl.c b/src/fort_impl.c index 5e16f3f..4e70631 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -165,7 +165,7 @@ static int split_cur_row(ft_table_t *table, f_row_t **tail_of_cur_row) f_row_t *tail = split_row(row, table->cur_col); if (!tail) { tail_of_cur_row = NULL; - return FT_ERROR; + return FT_GEN_ERROR; } *tail_of_cur_row = tail; @@ -180,12 +180,12 @@ int ft_ln(ft_table_t *table) case FT_STRATEGY_INSERT: { f_row_t *new_row = NULL; if (FT_IS_ERROR(split_cur_row(table, &new_row))) { - return FT_ERROR; + return FT_GEN_ERROR; } if (new_row) { if (FT_IS_ERROR(vector_insert(table->rows, &new_row, table->cur_row + 1))) { destroy_row(new_row); - return FT_ERROR; + return FT_GEN_ERROR; } } break; @@ -438,7 +438,7 @@ static int ft_write_impl_(ft_table_t *table, const f_string_view_t *cell_content assert(table); f_string_buffer_t *buf = get_cur_str_buffer_and_create_if_not_exists(table); if (buf == NULL) - return FT_ERROR; + return FT_GEN_ERROR; int status = FT_SUCCESS; switch (cell_content->type) { @@ -456,7 +456,7 @@ static int ft_write_impl_(ft_table_t *table, const f_string_view_t *cell_content break; #endif default: - status = FT_ERROR; + status = FT_GEN_ERROR; } if (FT_IS_SUCCESS(status)) { table->cur_col++; @@ -840,7 +840,7 @@ int ft_add_separator(ft_table_t *table) (*sep_p)->enabled = F_TRUE; if (*sep_p == NULL) - return FT_ERROR; + return FT_GEN_ERROR; return FT_SUCCESS; } @@ -978,7 +978,7 @@ int ft_set_cell_prop(ft_table_t *table, size_t row, size_t col, uint32_t propert if (table->properties->cell_properties == NULL) { table->properties->cell_properties = create_cell_prop_container(); if (table->properties->cell_properties == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } } @@ -1023,7 +1023,7 @@ const char *ft_strerror(int error_code) switch (error_code) { case FT_MEMORY_ERROR: return "Libfort error (out of memory)"; - case FT_ERROR: + case FT_GEN_ERROR: return "Libfort error (general error)"; case FT_EINVAL: return "Libfort error (invalid argument)"; @@ -1050,7 +1050,7 @@ int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span) f_row_t *row_p = get_row_and_create_if_not_exists(table, row); if (row_p == NULL) - return FT_ERROR; + return FT_GEN_ERROR; return row_set_cell_span(row_p, col, hor_span); } diff --git a/src/properties.c b/src/properties.c index 0541191..67ad827 100644 --- a/src/properties.c +++ b/src/properties.c @@ -462,7 +462,7 @@ f_status set_cell_property(f_cell_prop_container_t *cont, size_t row, size_t col { f_cell_props_t *opt = get_cell_prop_and_create_if_not_exists(cont, row, col); if (opt == NULL) - return FT_ERROR; + return FT_GEN_ERROR; return set_cell_property_impl(opt, property, value); /* diff --git a/src/row.c b/src/row.c index 937f90e..efae3f6 100644 --- a/src/row.c +++ b/src/row.c @@ -226,7 +226,7 @@ f_status insert_row(f_row_t *cur_row, f_row_t *ins_row, size_t pos) while (vector_size(cur_row->cells) < pos) { f_cell_t *new_cell = create_cell(); if (!new_cell) - return FT_ERROR; + return FT_GEN_ERROR; vector_push(cur_row->cells, &new_cell); } @@ -239,7 +239,7 @@ f_status insert_row(f_row_t *cur_row, f_row_t *ins_row, size_t pos) while (i--) { vector_erase(cur_row->cells, pos); } - return FT_ERROR; + return FT_GEN_ERROR; } } /* Clear cells so that it will be safe to destroy this row */ @@ -301,7 +301,7 @@ f_status row_set_cell_span(f_row_t *row, size_t cell_column, size_t hor_span) f_cell_t *main_cell = get_cell_and_create_if_not_exists(row, cell_column); if (main_cell == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } set_cell_type(main_cell, GROUP_MASTER_CELL); --hor_span; @@ -310,7 +310,7 @@ f_status row_set_cell_span(f_row_t *row, size_t cell_column, size_t hor_span) while (hor_span) { f_cell_t *slave_cell = get_cell_and_create_if_not_exists(row, cell_column); if (slave_cell == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } set_cell_type(slave_cell, GROUP_SLAVE_CELL); --hor_span; @@ -329,7 +329,7 @@ int print_row_separator_impl(f_conv_context_t *cntx, { assert(cntx); - int status = FT_ERROR; + int status = FT_GEN_ERROR; const f_context_t *context = cntx->cntx; diff --git a/src/table.c b/src/table.c index 01613c9..ad8febc 100644 --- a/src/table.c +++ b/src/table.c @@ -140,7 +140,7 @@ f_status table_rows_and_cols_geometry(const ft_table_t *table, enum f_geometry_type geom) { if (table == NULL) { - return FT_ERROR; + return FT_GEN_ERROR; } size_t max_invis_codepoints = 0; @@ -155,7 +155,7 @@ f_status table_rows_and_cols_geometry(const ft_table_t *table, if (col_width_arr == NULL || row_height_arr == NULL) { F_FREE(col_width_arr); F_FREE(row_height_arr); - return FT_ERROR; + return FT_GEN_ERROR; } int combined_cells_found = 0; @@ -273,7 +273,7 @@ FT_INTERNAL f_status table_geometry(const ft_table_t *table, size_t *height, size_t *width) { if (table == NULL) - return FT_ERROR; + return FT_GEN_ERROR; *height = 0; *width = 0; diff --git a/src/vector.c b/src/vector.c index 250c62f..1112f4d 100644 --- a/src/vector.c +++ b/src/vector.c @@ -79,7 +79,7 @@ int vector_push(f_vector_t *vector, const void *item) if (vector->m_size == vector->m_capacity) { if (vector_reallocate_(vector, vector->m_capacity * 2) == -1) - return FT_ERROR; + return FT_GEN_ERROR; vector->m_capacity = vector->m_capacity * 2; } @@ -99,7 +99,7 @@ int vector_insert(f_vector_t *vector, const void *item, size_t pos) size_t needed_capacity = MAX(pos + 1, vector->m_size + 1); if (vector->m_capacity < needed_capacity) { if (vector_reallocate_(vector, needed_capacity) == -1) - return FT_ERROR; + return FT_GEN_ERROR; vector->m_capacity = needed_capacity; } size_t offset = pos * vector->m_item_size; @@ -179,7 +179,7 @@ f_status vector_swap(f_vector_t *cur_vec, f_vector_t *mv_vec, size_t pos) size_t min_targ_size = pos + mv_sz; if (vector_capacity(cur_vec) < min_targ_size) { if (vector_reallocate_(cur_vec, min_targ_size) == -1) - return FT_ERROR; + return FT_GEN_ERROR; cur_vec->m_capacity = min_targ_size; } @@ -228,7 +228,7 @@ int vector_erase(f_vector_t *vector, size_t index) assert(vector); if (vector->m_size == 0 || index >= vector->m_size) - return FT_ERROR; + return FT_GEN_ERROR; memmove((char *)vector->m_data + vector->m_item_size * index, (char *)vector->m_data + vector->m_item_size * (index + 1), diff --git a/tests/bb_tests/test_error_codes.c b/tests/bb_tests/test_error_codes.c index bb9aa3e..811c987 100644 --- a/tests/bb_tests/test_error_codes.c +++ b/tests/bb_tests/test_error_codes.c @@ -6,6 +6,7 @@ void test_error_codes(void) { // Nonnegative code is success { + assert_str_equal(ft_strerror(FT_SUCCESS), "Libfort 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"); @@ -16,9 +17,9 @@ void test_error_codes(void) // 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(FT_GEN_ERROR), "Libfort error (general error)"); assert_str_equal(ft_strerror(-42), "Libfort unknown error"); assert_str_equal(ft_strerror(-666), "Libfort unknown error");