1
0
Fork 0

[C] Refactoring of cell paddings

This commit is contained in:
seleznevae 2018-02-27 20:41:57 +03:00
parent d12ea18353
commit 66578f379c
5 changed files with 84 additions and 126 deletions

View File

@ -5,8 +5,9 @@
int main()
{
FTABLE *table = ft_create_table();
ft_set_column_alignment(table, 0, CenterAligned);
ft_set_column_alignment(table, 1, LeftAligned);
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_OPT_TEXT_ALIGN, CenterAligned);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_OPT_TEXT_ALIGN, LeftAligned);
ft_hdr_printf_ln(table, "#|Planet|Avg. speed");
ft_printf_ln(table, "%d|%s|%5.2f km/s", 1, "Mercury", 47.362);
ft_printf_ln(table, "%d|%s|%5.2f km/s", 2, "Venus", 35.02);
@ -19,10 +20,10 @@ int main()
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_column_alignment(table, 0, CenterAligned);
ft_set_column_alignment(table, 1, LeftAligned);
ft_hdr_printf_ln(table, "Rank|Title|Year|Rating");
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_OPT_TEXT_ALIGN, CenterAligned);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_OPT_TEXT_ALIGN, LeftAligned);
ft_hdr_printf_ln(table, "Rank|Title|Year|Rating");
FT_NWRITE_LN(table, "1", "The Shawshank Redemption", "1994", "9.5");
FT_NWRITE_LN(table, "2", "12 Angry Men", "1957", "8.8");
FT_NWRITE_LN(table, "3", "2001: A Space Odyssey", "1968", "8.5");
@ -36,10 +37,10 @@ int main()
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_column_alignment(table, 0, LeftAligned);
ft_set_column_alignment(table, 1, CenterAligned);
ft_hdr_printf_ln(table, "Commodity|Farm price|Avg. spread");
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_OPT_TEXT_ALIGN, LeftAligned);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_OPT_TEXT_ALIGN, CenterAligned);
ft_hdr_printf_ln(table, "Commodity|Farm price|Avg. spread");
const char *row1[] = {"Potatoes", "$1.60", "200.94%"};
const char *row2[] = {"Carrots", "$0.32 ", "190.63%"};
ft_row_write_ln(table, 3, row1);
@ -52,10 +53,10 @@ int main()
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_column_alignment(table, 0, CenterAligned);
ft_set_column_alignment(table, 1, LeftAligned);
ft_hdr_printf_ln(table, "No.|Name|Avg. Mark");
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_OPT_TEXT_ALIGN, LeftAligned);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_OPT_TEXT_ALIGN, CenterAligned);
ft_hdr_printf_ln(table, "No.|Name|Avg. Mark");
const char *ctab[2][3] = {
{"1", "Joe Public", "3.14"},
{"2", "John Doe", "4.50"}
@ -69,10 +70,10 @@ int main()
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_column_alignment(table, 0, CenterAligned);
ft_set_column_alignment(table, 1, LeftAligned);
ft_hdr_printf_ln(table, "No.|Name|Avg. Mark");
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_OPT_TEXT_ALIGN, CenterAligned);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_OPT_TEXT_ALIGN, LeftAligned);
ft_hdr_printf_ln(table, "No.|Name|Avg. Mark");
const char **tab[2] = {
row1,
row2
@ -83,43 +84,4 @@ int main()
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
table = NULL;
// FTABLE *table = ft_create_table();
// ft_set_column_alignment(table, 2, LeftAligned);
// ft_hdr_printf_ln(table, "%d , %c|| %s|%f", 3, 'c', "234", 3.14);
// const char *row[] = {"AAA", " ADf qwer", "qwerwqer", "11111 23333", "qwe"};
// ft_row_write_ln(table, 5, row);
// ft_row_write(table, 5, row);
// ft_row_write(table, 5, row);
// ft_ln(table);
// const char *ctab[2][2] = {
// {"AAA", " ADf qwer"},
// {"AAA", " ADf 2222"}
// };
// ft_s_table_write_ln(table, 2, 2, ctab);
// const char **tab[2] = {
// row,
// row
// };
// ft_table_write(table, 2, 5, tab);
// ft_ln(table);
// ft_nwrite(table, 3, "123", "2345", "4567");
// ft_ln(table);
// FT_NWRITE_LN(table, "123", "132445\n123\n123", "Anton", "Petr", "Pavel");
// fprintf(stderr, "Table:\n");
// fprintf(stderr, "%s\n", ft_to_string(table));
// ft_destroy_table(table);
}

View File

@ -252,8 +252,6 @@ FORT_EXTERN const char* ft_to_string(const FTABLE *FORT_RESTRICT table);
//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_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);

View File

@ -438,62 +438,6 @@ int ft_table_write_ln(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, con
//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)
{
if (table->options == NULL) {
table->options = create_table_options();
if (table->options == NULL)
return F_MEMORY_ERROR;
}
if (table->options->cell_options == NULL) {
table->options->cell_options = create_cell_opt_container();
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)
//{
// 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) {
table->options = create_table_options();
if (table->options == NULL)
return F_MEMORY_ERROR;
}
if (table->options->cell_options == NULL) {
table->options->cell_options = create_cell_opt_container();
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

View File

@ -17,7 +17,12 @@ struct fort_cell_options g_default_cell_option =
| FT_OPT_LEFT_PADDING | FT_OPT_RIGHT_PADDING | FT_OPT_EMPTY_STR_HEIGHT ,
0, /* col_min_width */
RightAligned /* align */
RightAligned, /* align */
0, /* cell_padding_top */
0, /* cell_padding_bottom */
1, /* cell_padding_left */
1, /* cell_padding_right */
1, /* cell_empty_string_height */
};
static int get_option_value_if_exists_otherwise_default(const struct fort_cell_options *cell_opts, uint32_t option)
@ -31,6 +36,16 @@ static int get_option_value_if_exists_otherwise_default(const struct fort_cell_o
return cell_opts->col_min_width;
case FT_OPT_TEXT_ALIGN:
return cell_opts->align;
case FT_OPT_TOP_PADDING:
return cell_opts->cell_padding_top;
case FT_OPT_BOTTOM_PADDING:
return cell_opts->cell_padding_bottom;
case FT_OPT_LEFT_PADDING:
return cell_opts->cell_padding_left;
case FT_OPT_RIGHT_PADDING:
return cell_opts->cell_padding_right;
case FT_OPT_EMPTY_STR_HEIGHT:
return cell_opts->cell_empty_string_height;
default:
// todo: implement later
exit(333);
@ -99,21 +114,7 @@ fort_cell_options_t* get_cell_opt_and_create_if_not_exists(fort_cell_opt_contain
return NULL;
}
fort_status_t set_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option, int value)
{
fort_cell_options_t* opt = get_cell_opt_and_create_if_not_exists(cont, row, col);
if (opt == NULL)
return F_ERROR;
OPTION_SET(opt->options, option);
if (OPTION_IS_SET(option, FT_OPT_MIN_WIDTH)) {
opt->col_min_width = value;
} else if (OPTION_IS_SET(option, FT_OPT_TEXT_ALIGN)) {
opt->align = value;
}
return F_SUCCESS;
}
int get_cell_opt_value_hierarcial(const fort_table_options_t *options, size_t row, size_t column, uint32_t option)
@ -144,6 +145,53 @@ int get_cell_opt_value_hierarcial(const fort_table_options_t *options, size_t ro
}
static fort_status_t set_cell_option_impl(fort_cell_options_t *opt, uint32_t option, int value)
{
assert(opt);
OPTION_SET(opt->options, option);
if (OPTION_IS_SET(option, FT_OPT_MIN_WIDTH)) {
opt->col_min_width = value;
} else if (OPTION_IS_SET(option, FT_OPT_TEXT_ALIGN)) {
opt->align = value;
} else if (OPTION_IS_SET(option, FT_OPT_TOP_PADDING)) {
opt->cell_padding_top = value;
} else if (OPTION_IS_SET(option, FT_OPT_BOTTOM_PADDING)) {
opt->cell_padding_bottom = value;
} else if (OPTION_IS_SET(option, FT_OPT_LEFT_PADDING)) {
opt->cell_padding_left = value;
} else if (OPTION_IS_SET(option, FT_OPT_RIGHT_PADDING)) {
opt->cell_padding_right = value;
} else if (OPTION_IS_SET(option, FT_OPT_EMPTY_STR_HEIGHT)) {
opt->cell_empty_string_height = value;
}
return F_SUCCESS;
}
fort_status_t set_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option, int value)
{
fort_cell_options_t* opt = get_cell_opt_and_create_if_not_exists(cont, row, col);
if (opt == NULL)
return F_ERROR;
return set_cell_option_impl(opt, option, value);
// OPTION_SET(opt->options, option);
// if (OPTION_IS_SET(option, FT_OPT_MIN_WIDTH)) {
// opt->col_min_width = value;
// } else if (OPTION_IS_SET(option, FT_OPT_TEXT_ALIGN)) {
// opt->align = value;
// }
// return F_SUCCESS;
}
fort_status_t set_default_cell_option(uint32_t option, int value)
{
return set_cell_option_impl(&g_default_cell_option, option, value);
}
/*****************************************************************************
* OPTIONS
* ***************************************************************************/

View File

@ -53,6 +53,11 @@ struct fort_cell_options
uint32_t options;
int col_min_width;
enum TextAlignment align;
int cell_padding_top;
int cell_padding_bottom;
int cell_padding_left;
int cell_padding_right;
int cell_empty_string_height;
};
typedef struct fort_cell_options fort_cell_options_t;
@ -69,6 +74,7 @@ fort_status_t unset_cell_option(fort_cell_opt_container_t *cont, unsigned row, u
int get_cell_opt_value_hierarcial(const fort_table_options_t *options, size_t row, size_t column, uint32_t option);
fort_status_t set_default_cell_option(uint32_t option, int value);
/*****************************************************************************
* TABLE BORDER