1
0
Fork 0

[C] Refactoring continue

This commit is contained in:
seleznevae 2018-02-25 19:25:00 +03:00
parent 570113aa29
commit 5eee2ec56c
5 changed files with 118 additions and 54 deletions

View File

@ -251,7 +251,7 @@ FORT_EXTERN const char* ft_to_string(const FTABLE *FORT_RESTRICT table);
//FORT_EXTERN int ft_set_default_options(const fort_table_options_t *options); //FORT_EXTERN int ft_set_default_options(const fort_table_options_t *options);
//FORT_EXTERN int ft_get_default_options(fort_table_options_t *options); //FORT_EXTERN int ft_get_default_options(fort_table_options_t *options);
FORT_EXTERN int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options); //FORT_EXTERN int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options);
FORT_EXTERN int ft_set_column_min_width(FTABLE * FORT_RESTRICT table, size_t column, size_t width); FORT_EXTERN int ft_set_column_min_width(FTABLE * FORT_RESTRICT table, size_t column, size_t width);
FORT_EXTERN int ft_set_column_alignment(FTABLE * FORT_RESTRICT table, size_t column, enum TextAlignment align); FORT_EXTERN int ft_set_column_alignment(FTABLE * FORT_RESTRICT table, size_t column, enum TextAlignment align);

View File

@ -406,37 +406,49 @@ int ft_table_write_ln(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, con
// return 0; // return 0;
//} //}
int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options) //int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options)
{ //{
assert(table); // assert(table);
if (options == NULL) { // if (options == NULL) {
destroy_table_options(table->options); // destroy_table_options(table->options);
table->options = NULL; // table->options = NULL;
return 0; // return 0;
} // }
fort_table_options_t *new_options = copy_table_options(options); // fort_table_options_t *new_options = copy_table_options(options);
if (new_options == NULL) {
return -1;
}
destroy_table_options(table->options);
table->options = new_options;
return 0;
// fort_table_options_t *new_options = F_CALLOC(sizeof(fort_table_options_t), 1);
// if (new_options == NULL) { // if (new_options == NULL) {
// return -1; // return -1;
// } // }
// memcpy(new_options, options, sizeof(fort_table_options_t)); // destroy_table_options(table->options);
// F_FREE(table->options);
// table->options = new_options; // table->options = new_options;
// return 0; // return 0;
}
//// fort_table_options_t *new_options = F_CALLOC(sizeof(fort_table_options_t), 1);
//// if (new_options == NULL) {
//// return -1;
//// }
//// memcpy(new_options, options, sizeof(fort_table_options_t));
//// F_FREE(table->options);
//// table->options = new_options;
//// return 0;
//}
//int ft_set_column_min_width(FTABLE *table, size_t column, size_t width)
//{
// if (table->options == NULL) {
// table->options = create_table_options();
// if (table->options == NULL)
// return F_MEMORY_ERROR;
// }
// int status = fort_options_set_column_min_width(table->options, column, width);
// return status;
//}
int ft_set_column_min_width(FTABLE *table, size_t column, size_t width) int ft_set_column_min_width(FTABLE *table, size_t column, size_t width)
{ {
@ -445,26 +457,44 @@ int ft_set_column_min_width(FTABLE *table, size_t column, size_t width)
if (table->options == NULL) if (table->options == NULL)
return F_MEMORY_ERROR; return F_MEMORY_ERROR;
} }
if (table->options->cell_options == NULL) {
int status = fort_options_set_column_min_width(table->options, column, width); table->options->cell_options = create_cell_opt_container();
return status; if (table->options->cell_options == NULL) {
return F_ERROR;
}
}
return set_cell_option(table->options->cell_options, FT_ANY_ROW, column, FT_OPT_MIN_WIDTH, width);
} }
int ft_set_column_alignment(FTABLE * FORT_RESTRICT table, size_t column, enum TextAlignment align) //int ft_set_column_alignment(FTABLE * FORT_RESTRICT table, size_t column, enum TextAlignment align)
//{
// if (table->options == NULL) {
// table->options = create_table_options();
// if (table->options == NULL)
// return F_MEMORY_ERROR;
// }
// int status = fort_options_set_column_alignment(table->options, column, align);
// return status;
//}
int ft_set_column_alignment(FTABLE *table, size_t column, enum TextAlignment align)
{ {
if (table->options == NULL) { if (table->options == NULL) {
table->options = create_table_options(); table->options = create_table_options();
if (table->options == NULL) if (table->options == NULL)
return F_MEMORY_ERROR; return F_MEMORY_ERROR;
} }
if (table->options->cell_options == NULL) {
int status = fort_options_set_column_alignment(table->options, column, align); table->options->cell_options = create_cell_opt_container();
return status; if (table->options->cell_options == NULL) {
return F_ERROR;
}
}
return set_cell_option(table->options->cell_options, FT_ANY_ROW, column, FT_OPT_TEXT_ALIGN, align);
} }
/***************************************************************************** /*****************************************************************************
* TABLE * TABLE
* ***************************************************************************/ * ***************************************************************************/

View File

@ -58,9 +58,11 @@ fort_cell_options_t* get_cell_opt_and_create_if_not_exists(fort_cell_opt_contain
if (opt->cell_row == row && opt->cell_col == col) if (opt->cell_row == row && opt->cell_col == col)
return opt; return opt;
} }
const fort_cell_options_t opt = DEFAULT_CELL_OPTION; fort_cell_options_t opt = DEFAULT_CELL_OPTION;
opt.cell_row = row;
opt.cell_col = col;
if (IS_SUCCESS(vector_push(cont, &opt))) { if (IS_SUCCESS(vector_push(cont, &opt))) {
vector_at(cont, sz); return (fort_cell_options_t*)vector_at(cont, sz);
} }
return NULL; return NULL;
@ -72,10 +74,10 @@ fort_status_t set_cell_option(fort_cell_opt_container_t *cont, unsigned row, uns
if (opt == NULL) if (opt == NULL)
return F_ERROR; return F_ERROR;
OPTION_SET(*opt, option); OPTION_SET(opt->options, option);
if (OPTION_IS_SET(*opt, FT_OPT_MIN_WIDTH)) { if (OPTION_IS_SET(option, FT_OPT_MIN_WIDTH)) {
opt->col_min_width = value; opt->col_min_width = value;
} else if (OPTION_IS_SET(*opt, FT_OPT_TEXT_ALIGN)) { } else if (OPTION_IS_SET(option, FT_OPT_TEXT_ALIGN)) {
opt->align = value; opt->align = value;
} }
@ -231,28 +233,58 @@ fort_status_t fort_options_set_column_alignment(fort_table_options_t *options, s
FORT_OPTIONS_SET_COLUMN_OPTION(options, column, align, al); FORT_OPTIONS_SET_COLUMN_OPTION(options, column, align, al);
} }
//int fort_options_column_width(const fort_table_options_t *options, size_t column)
//{
// assert(options);
// if (options->col_options == NULL)
// return -1;
// if (vector_size(options->col_options) <= column)
// return -1;
// return ((fort_column_options_t*)vector_at(options->col_options, column))->col_min_width;
//}
int fort_options_column_width(const fort_table_options_t *options, size_t column) int fort_options_column_width(const fort_table_options_t *options, size_t column)
{ {
assert(options); assert(options);
if (options->col_options == NULL) if (options->cell_options == NULL)
return -1; return -1;
if (vector_size(options->col_options) <= column) const fort_cell_options_t* col_opt = cget_cell_opt(options->cell_options, FT_ANY_ROW, column);
if (col_opt == NULL || ((col_opt->options & FT_OPT_MIN_WIDTH) == 0))
return -1; return -1;
else {
return ((fort_column_options_t*)vector_at(options->col_options, column))->col_min_width; return col_opt->col_min_width;
}
} }
//int fort_options_column_alignment(const fort_table_options_t *options, size_t column)
//{
// assert(options);
// enum TextAlignment align = g_column_options.align;
// if (options->col_options == NULL)
// return align;
// if (vector_size(options->col_options) <= column)
// return align;
// return ((fort_column_options_t*)vector_at(options->col_options, column))->align;
//}
int fort_options_column_alignment(const fort_table_options_t *options, size_t column) int fort_options_column_alignment(const fort_table_options_t *options, size_t column)
{ {
assert(options); assert(options);
enum TextAlignment defaultAlign = g_column_options.align;
enum TextAlignment align = g_column_options.align; if (options->cell_options == NULL)
if (options->col_options == NULL) return defaultAlign;
return align;
if (vector_size(options->col_options) <= column) const fort_cell_options_t* col_opt = cget_cell_opt(options->cell_options, FT_ANY_ROW, column);
return align; if (col_opt == NULL || ((col_opt->options & FT_OPT_TEXT_ALIGN) == 0))
return defaultAlign;
return ((fort_column_options_t*)vector_at(options->col_options, column))->align; else {
return col_opt->align;
}
} }

View File

@ -42,9 +42,9 @@ typedef struct vector vector_t;
#define FT_OPT_RIGHT_PADDING ((uint32_t)(0x01U << (5))) #define FT_OPT_RIGHT_PADDING ((uint32_t)(0x01U << (5)))
#define FT_OPT_EMPTY_STR_HEIGHT ((uint32_t)(0x01U << (6))) #define FT_OPT_EMPTY_STR_HEIGHT ((uint32_t)(0x01U << (6)))
#define OPTION_IS_SET(ft_opts, option) ((ft_opts).options & (option)) #define OPTION_IS_SET(ft_opts, option) ((ft_opts) & (option))
#define OPTION_SET(ft_opts, option) ((ft_opts).options |=(option)) #define OPTION_SET(ft_opts, option) ((ft_opts) |=(option))
#define OPTION_UNSET(ft_opts, option) ((ft_opts).options &= ~((uint32_t)option)) #define OPTION_UNSET(ft_opts, option) ((ft_opts) &= ~((uint32_t)option))
struct fort_cell_options struct fort_cell_options
{ {

View File

@ -816,10 +816,12 @@ void test_table_options(void **state)
set_test_options_as_default(); set_test_options_as_default();
table = create_test_int_table(0); table = create_test_int_table(0);
ft_set_column_min_width(table, 1, 7); int status = F_SUCCESS;
ft_set_column_alignment(table, 1, LeftAligned); status |= ft_set_column_min_width(table, 1, 7);
ft_set_column_min_width(table, 2, 8); status |= ft_set_column_alignment(table, 1, LeftAligned);
ft_set_column_alignment(table, 2, CenterAligned); status |= ft_set_column_min_width(table, 2, 8);
status |= ft_set_column_alignment(table, 2, CenterAligned);
assert_true( status == F_SUCCESS);
const char *table_str = ft_to_string(table); const char *table_str = ft_to_string(table);