From 87275e93fa3fedcaeb1a8254ba20d7914f18c3e0 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 12 Mar 2018 22:28:55 +0300 Subject: [PATCH] [R] Refactoring of setting style --- example/main.c | 71 +++++++++++++++++++++++++++- include/fort.h | 23 +++++---- src/fort.c | 85 +++++++++++++++++++++++++++++++-- src/options.h | 7 +++ tests/test_table.c | 115 ++++++++++++++++++++++----------------------- 5 files changed, 226 insertions(+), 75 deletions(-) diff --git a/example/main.c b/example/main.c index 93bcc57..0879bad 100644 --- a/example/main.c +++ b/example/main.c @@ -110,6 +110,8 @@ int main() /*-------------------------------------------------------------*/ + + table = ft_create_table(); 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); @@ -126,9 +128,74 @@ int main() printf("%s\n", ft_to_string(table)); ft_destroy_table(table); - /*-------------------------------------------------------------*/ - table = NULL; + /*---------------- Different styles --------------------*/ + + ft_set_default_borders(FT_BASIC_STYLE); + + table = ft_create_table(); + 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_set_cell_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); + FT_NWRITE_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"); + FT_NWRITE_LN(table, "4", "Blade Runner", "1982", "8.1"); + + + printf("Table:\n"); + printf("%s\n", ft_to_string(table)); + ft_destroy_table(table); + + + /* ------------- */ + +// ft_set_default_borders(FT_SIMPLE_STYLE); + +// table = ft_create_table(); +// 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_set_cell_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); +// FT_NWRITE_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"); +// FT_NWRITE_LN(table, "4", "Blade Runner", "1982", "8.1"); + + +// printf("Table:\n"); +// printf("%s\n", ft_to_string(table)); +// ft_destroy_table(table); + +// table = NULL; + +// /* ------------- */ + +// ft_set_default_borders(FT_DOT_STYLE); + +// table = ft_create_table(); +// 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_set_cell_option(table, 0, FT_ANY_COLUMN, FT_OPT_ROW_TYPE, Header); +// FT_NWRITE_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"); +// FT_NWRITE_LN(table, "4", "Blade Runner", "1982", "8.1"); + + +// printf("Table:\n"); +// printf("%s\n", ft_to_string(table)); +// ft_destroy_table(table); + + table = NULL; return 0; } diff --git a/include/fort.h b/include/fort.h index 0ea8f62..fae2bc3 100644 --- a/include/fort.h +++ b/include/fort.h @@ -282,12 +282,7 @@ FORT_EXTERN const wchar_t* ft_to_wstring(const FTABLE *FORT_RESTRICT table); - - - - - -struct border_chars +struct ft_border_chars { char top_border_ch; char separator_ch; @@ -297,8 +292,20 @@ struct border_chars char in_intersect_ch; }; -FORT_EXTERN int ft_set_default_borders(struct border_chars *border_chs, struct border_chars *header_border_chs); -FORT_EXTERN int ft_set_table_borders(FTABLE * FORT_RESTRICT table, struct border_chars *border_chs, struct border_chars *header_border_chs); +struct ft_border_style +{ + struct ft_border_chars border_chs; + struct ft_border_chars header_border_chs; +}; + +/* List of built-in table border styles */ +extern struct ft_border_style * FT_BASIC_STYLE; +extern struct ft_border_style * FT_SIMPLE_STYLE; +extern struct ft_border_style * FT_DOT_STYLE; + + +FORT_EXTERN int ft_set_default_borders(struct ft_border_style *style); +FORT_EXTERN int ft_set_table_borders(FTABLE * FORT_RESTRICT table, struct ft_border_style *style); FORT_EXTERN int ft_set_default_cell_option(uint32_t option, int value); FORT_EXTERN int ft_set_cell_option(FTABLE * FORT_RESTRICT table, unsigned row, unsigned col, uint32_t option, int value); diff --git a/src/fort.c b/src/fort.c index 00c79f0..e7a4ba1 100644 --- a/src/fort.c +++ b/src/fort.c @@ -662,8 +662,80 @@ int ft_add_separator(FTABLE *table) -static void set_border_options_for_options(fort_table_options_t *options, struct border_chars *border_chs, struct border_chars *header_border_chs) + +/* ******************************************************************************* */ + +struct ft_border_style ft_basic_style = { + { + '-', /* top_border_ch */ + '\0', /* separator_ch */ + '-', /* bottom_border_ch */ + '|', /* side_border_ch */ + '+', /* out_intersect_ch */ + '\0', /* in_intersect_ch */ + }, + { + '-', /* top_border_ch */ + '-', /* separator_ch */ + '-', /* bottom_border_ch */ + '|', /* side_border_ch */ + '+', /* out_intersect_ch */ + '+', /* in_intersect_ch */ + } +}; + +//struct ft_border_style ft_simple_style = +//{ +// { +// ' ', /* top_border_ch */ +// '\0', /* separator_ch */ +// ' ', /* bottom_border_ch */ +// ' ', /* side_border_ch */ +// ' ', /* out_intersect_ch */ +// '\0', /* in_intersect_ch */ +// }, +// { +// ' ', /* top_border_ch */ +// '-', /* separator_ch */ +// ' ', /* bottom_border_ch */ +// ' ', /* side_border_ch */ +// ' ', /* out_intersect_ch */ +// ' ', /* in_intersect_ch */ +// } +//}; + +//struct ft_border_style ft_dot_style = +//{ +// { +// '.', /* top_border_ch */ +// '\0', /* separator_ch */ +// '.', /* bottom_border_ch */ +// ':', /* side_border_ch */ +// '.', /* out_intersect_ch */ +// '\0', /* in_intersect_ch */ +// }, +// { +// '.', /* top_border_ch */ +// '.', /* separator_ch */ +// '.', /* bottom_border_ch */ +// ':', /* side_border_ch */ +// '.', /* out_intersect_ch */ +// '.', /* in_intersect_ch */ +// } +//}; + +struct ft_border_style * FT_BASIC_STYLE = &ft_basic_style; +//struct ft_border_style * FT_SIMPLE_STYLE = &ft_simple_style; +//struct ft_border_style * FT_DOT_STYLE = &ft_dot_style; + + + +static void set_border_options_for_options(fort_table_options_t *options, struct ft_border_style *style) +{ + struct ft_border_chars *border_chs = &(style->border_chs); + struct ft_border_chars *header_border_chs = &(style->header_border_chs); + #define BOR_CHARS options->border_chars #define H_BOR_CHARS options->header_border_chars @@ -712,13 +784,14 @@ static void set_border_options_for_options(fort_table_options_t *options, struct #undef H_BOR_CHARS } -int ft_set_default_borders(struct border_chars *border_chs, struct border_chars *header_border_chs) + +int ft_set_default_borders(struct ft_border_style *style) { - set_border_options_for_options(&g_table_options, border_chs, header_border_chs); + set_border_options_for_options(&g_table_options, style); return F_SUCCESS; } -int ft_set_table_borders(FTABLE *table, struct border_chars *border_chs, struct border_chars *header_border_chs) +int ft_set_table_borders(FTABLE *table, struct ft_border_style *style) { assert(table); if (table->options == NULL) { @@ -726,7 +799,7 @@ int ft_set_table_borders(FTABLE *table, struct border_chars *border_chs, struct if (table->options == NULL) return F_MEMORY_ERROR; } - set_border_options_for_options(table->options, border_chs, header_border_chs); + set_border_options_for_options(table->options, style); return F_SUCCESS; } @@ -754,3 +827,5 @@ int ft_set_default_cell_option(uint32_t option, int value) { return set_default_cell_option(option, value); } + + diff --git a/src/options.h b/src/options.h index 9f0ffe8..95453d0 100644 --- a/src/options.h +++ b/src/options.h @@ -139,6 +139,13 @@ enum SeparatorItemPos }; +struct fort_border_style +{ + char border_chars[BorderItemPosSize]; + char header_border_chars[BorderItemPosSize]; + char separator_chars[SepratorItemPosSize]; +}; + struct fort_table_options { char border_chars[BorderItemPosSize]; diff --git a/tests/test_table.c b/tests/test_table.c index f9dfd44..ee88140 100644 --- a/tests/test_table.c +++ b/tests/test_table.c @@ -23,22 +23,21 @@ int set_test_options_for_table(FTABLE *table) assert_true( status == F_SUCCESS ); - struct border_chars border_chs; - border_chs.top_border_ch = '-'; - border_chs.separator_ch = '-'; - border_chs.bottom_border_ch = '-'; - border_chs.side_border_ch = '|'; - border_chs.out_intersect_ch = '+'; - border_chs.in_intersect_ch = '+'; + struct ft_border_style brdr_style; + brdr_style.border_chs.top_border_ch = '-'; + brdr_style.border_chs.separator_ch = '-'; + brdr_style.border_chs.bottom_border_ch = '-'; + brdr_style.border_chs.side_border_ch = '|'; + brdr_style.border_chs.out_intersect_ch = '+'; + brdr_style.border_chs.in_intersect_ch = '+'; - struct border_chars header_border_chs; - header_border_chs.top_border_ch = '-'; - header_border_chs.separator_ch = '-'; - header_border_chs.bottom_border_ch = '-'; - header_border_chs.side_border_ch = '|'; - header_border_chs.out_intersect_ch = '+'; - header_border_chs.in_intersect_ch = '+'; - return ft_set_table_borders(table, &border_chs, &header_border_chs); + brdr_style.header_border_chs.top_border_ch = '-'; + brdr_style.header_border_chs.separator_ch = '-'; + brdr_style.header_border_chs.bottom_border_ch = '-'; + brdr_style.header_border_chs.side_border_ch = '|'; + brdr_style.header_border_chs.out_intersect_ch = '+'; + brdr_style.header_border_chs.in_intersect_ch = '+'; + return ft_set_table_borders(table, &brdr_style); } int set_test_options_as_default() @@ -57,22 +56,21 @@ int set_test_options_as_default() assert_true( status == F_SUCCESS ); - struct border_chars border_chs; - border_chs.top_border_ch = '-'; - border_chs.separator_ch = '-'; - border_chs.bottom_border_ch = '-'; - border_chs.side_border_ch = '|'; - border_chs.out_intersect_ch = '+'; - border_chs.in_intersect_ch = '+'; + struct ft_border_style brdr_style; + brdr_style.border_chs.top_border_ch = '-'; + brdr_style.border_chs.separator_ch = '-'; + brdr_style.border_chs.bottom_border_ch = '-'; + brdr_style.border_chs.side_border_ch = '|'; + brdr_style.border_chs.out_intersect_ch = '+'; + brdr_style.border_chs.in_intersect_ch = '+'; - struct border_chars header_border_chs; - header_border_chs.top_border_ch = '-'; - header_border_chs.separator_ch = '-'; - header_border_chs.bottom_border_ch = '-'; - header_border_chs.side_border_ch = '|'; - header_border_chs.out_intersect_ch = '+'; - header_border_chs.in_intersect_ch = '+'; - return ft_set_default_borders(&border_chs, &header_border_chs); + brdr_style.header_border_chs.top_border_ch = '-'; + brdr_style.header_border_chs.separator_ch = '-'; + brdr_style.header_border_chs.bottom_border_ch = '-'; + brdr_style.header_border_chs.side_border_ch = '|'; + brdr_style.header_border_chs.out_intersect_ch = '+'; + brdr_style.header_border_chs.in_intersect_ch = '+'; + return ft_set_default_borders(&brdr_style); } void test_table_sizes(void **state) @@ -564,22 +562,21 @@ void test_table_options(void **state) WHEN("Changing cell separators") { - struct border_chars border_chs; - border_chs.top_border_ch = '|'; - border_chs.separator_ch = '|'; - border_chs.bottom_border_ch = '|'; - border_chs.side_border_ch = '='; - border_chs.out_intersect_ch = '+'; - border_chs.in_intersect_ch = '#'; + struct ft_border_style brdr_style; + brdr_style.border_chs.top_border_ch = '|'; + brdr_style.border_chs.separator_ch = '|'; + brdr_style.border_chs.bottom_border_ch = '|'; + brdr_style.border_chs.side_border_ch = '='; + brdr_style.border_chs.out_intersect_ch = '+'; + brdr_style.border_chs.in_intersect_ch = '#'; - struct border_chars header_border_chs; - header_border_chs.top_border_ch = '*'; - header_border_chs.separator_ch = '*'; - header_border_chs.bottom_border_ch = '*'; - header_border_chs.side_border_ch = 'v'; - header_border_chs.out_intersect_ch = '+'; - header_border_chs.in_intersect_ch = '#'; - ft_set_default_borders(&border_chs, &header_border_chs); + brdr_style.header_border_chs.top_border_ch = '*'; + brdr_style.header_border_chs.separator_ch = '*'; + brdr_style.header_border_chs.bottom_border_ch = '*'; + brdr_style.header_border_chs.side_border_ch = 'v'; + brdr_style.header_border_chs.out_intersect_ch = '+'; + brdr_style.header_border_chs.in_intersect_ch = '#'; + ft_set_default_borders(&brdr_style); @@ -608,23 +605,21 @@ void test_table_options(void **state) + brdr_style.border_chs.top_border_ch = '|'; + brdr_style.border_chs.separator_ch = '\0'; + brdr_style.border_chs.bottom_border_ch = '|'; + brdr_style.border_chs.side_border_ch = '='; + brdr_style.border_chs.out_intersect_ch = '+'; + brdr_style.border_chs.in_intersect_ch = '\0'; + brdr_style.header_border_chs.top_border_ch = '*'; + brdr_style.header_border_chs.separator_ch = '*'; + brdr_style.header_border_chs.bottom_border_ch = '*'; + brdr_style.header_border_chs.side_border_ch = 'v'; + brdr_style.header_border_chs.out_intersect_ch = '+'; + brdr_style.header_border_chs.in_intersect_ch = '#'; - border_chs.top_border_ch = '|'; - border_chs.separator_ch = '\0'; - border_chs.bottom_border_ch = '|'; - border_chs.side_border_ch = '='; - border_chs.out_intersect_ch = '+'; - border_chs.in_intersect_ch = '\0'; - - header_border_chs.top_border_ch = '*'; - header_border_chs.separator_ch = '*'; - header_border_chs.bottom_border_ch = '*'; - header_border_chs.side_border_ch = 'v'; - header_border_chs.out_intersect_ch = '+'; - header_border_chs.in_intersect_ch = '#'; - - ft_set_default_borders(&border_chs, &header_border_chs); + ft_set_default_borders(&brdr_style); ft_set_default_cell_option(FT_OPT_BOTTOM_PADDING, 0); ft_set_default_cell_option(FT_OPT_TOP_PADDING, 0);