From 9b8a8db9f6f52890289f338923cedb92210a6a3e Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 12 Mar 2018 23:02:59 +0300 Subject: [PATCH] [R] Refactoring of setting style --- example/main.c | 56 +++++++++---------- src/fort.c | 145 ++++++++++++++++--------------------------------- src/options.c | 89 +++++++++++++++++++++++------- src/options.h | 10 ++-- src/row.c | 32 +++++------ 5 files changed, 163 insertions(+), 169 deletions(-) diff --git a/example/main.c b/example/main.c index 0879bad..c013ea4 100644 --- a/example/main.c +++ b/example/main.c @@ -153,47 +153,45 @@ int main() /* ------------- */ -// ft_set_default_borders(FT_SIMPLE_STYLE); + 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); + 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_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"); + 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); + printf("Table:\n"); + printf("%s\n", ft_to_string(table)); + ft_destroy_table(table); -// table = NULL; + /* ------------- */ -// /* ------------- */ + ft_set_default_borders(FT_DOT_STYLE); -// 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); -// 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_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"); + 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); + printf("Table:\n"); + printf("%s\n", ft_to_string(table)); + ft_destroy_table(table); table = NULL; diff --git a/src/fort.c b/src/fort.c index e7a4ba1..7cbfd5e 100644 --- a/src/fort.c +++ b/src/fort.c @@ -665,120 +665,69 @@ int ft_add_separator(FTABLE *table) /* ******************************************************************************* */ -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; +struct ft_border_style * FT_BASIC_STYLE = (struct ft_border_style *)&FORT_BASIC_STYLE; +struct ft_border_style * FT_SIMPLE_STYLE = (struct ft_border_style *)&FORT_SIMPLE_STYLE; +struct ft_border_style * FT_DOT_STYLE = (struct ft_border_style *)&FORT_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); + if ((struct fort_border_style *)style == &FORT_BASIC_STYLE + || (struct fort_border_style *)style == &FORT_SIMPLE_STYLE + || (struct fort_border_style *)style == &FORT_DOT_STYLE) + { + memcpy(&(options->border_style), (struct fort_border_style *)style, sizeof(struct fort_border_style)); + return; + } -#define BOR_CHARS options->border_chars -#define H_BOR_CHARS options->header_border_chars + struct ft_border_chars *border_chs = &(style->border_chs); + struct ft_border_chars *header_border_chs = &(style->header_border_chs); - /* - BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = border_chs->top_border_ch; - BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = border_chs->separator_ch; - BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = border_chs->bottom_border_ch; - BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = border_chs->side_border_ch; +#define BOR_CHARS options->border_style.border_chars +#define H_BOR_CHARS options->border_style.header_border_chars - H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = header_border_chs->top_border_ch; - H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = header_border_chs->separator_ch; - H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = header_border_chs->bottom_border_ch; - H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = header_border_chs->side_border_ch; - */ + /* + BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = border_chs->top_border_ch; + BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = border_chs->separator_ch; + BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = border_chs->bottom_border_ch; + BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = border_chs->side_border_ch; - BOR_CHARS[TT_bip] = border_chs->top_border_ch; - BOR_CHARS[IH_bip] = border_chs->separator_ch; - BOR_CHARS[BB_bip] = border_chs->bottom_border_ch; - BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = border_chs->side_border_ch; + H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = header_border_chs->top_border_ch; + H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = header_border_chs->separator_ch; + H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = header_border_chs->bottom_border_ch; + H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = header_border_chs->side_border_ch; + */ - BOR_CHARS[TL_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = border_chs->out_intersect_ch; - BOR_CHARS[LH_bip] = BOR_CHARS[RH_bip] = border_chs->out_intersect_ch; - BOR_CHARS[BL_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = border_chs->out_intersect_ch; - BOR_CHARS[II_bip] = border_chs->in_intersect_ch; + BOR_CHARS[TT_bip] = border_chs->top_border_ch; + BOR_CHARS[IH_bip] = border_chs->separator_ch; + BOR_CHARS[BB_bip] = border_chs->bottom_border_ch; + BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = border_chs->side_border_ch; - if (border_chs->separator_ch == '\0' && border_chs->in_intersect_ch == '\0') { - BOR_CHARS[LH_bip] = BOR_CHARS[RH_bip] = '\0'; - } + BOR_CHARS[TL_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = border_chs->out_intersect_ch; + BOR_CHARS[LH_bip] = BOR_CHARS[RH_bip] = border_chs->out_intersect_ch; + BOR_CHARS[BL_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = border_chs->out_intersect_ch; + BOR_CHARS[II_bip] = border_chs->in_intersect_ch; + + if (border_chs->separator_ch == '\0' && border_chs->in_intersect_ch == '\0') { + BOR_CHARS[LH_bip] = BOR_CHARS[RH_bip] = '\0'; + } - H_BOR_CHARS[TT_bip] = header_border_chs->top_border_ch; - H_BOR_CHARS[IH_bip] = header_border_chs->separator_ch; - H_BOR_CHARS[BB_bip] = header_border_chs->bottom_border_ch; - H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = header_border_chs->side_border_ch; + H_BOR_CHARS[TT_bip] = header_border_chs->top_border_ch; + H_BOR_CHARS[IH_bip] = header_border_chs->separator_ch; + H_BOR_CHARS[BB_bip] = header_border_chs->bottom_border_ch; + H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = header_border_chs->side_border_ch; - H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = header_border_chs->out_intersect_ch; - H_BOR_CHARS[LH_bip] = H_BOR_CHARS[RH_bip] = header_border_chs->out_intersect_ch; - H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = header_border_chs->out_intersect_ch; - H_BOR_CHARS[II_bip] = header_border_chs->in_intersect_ch; + H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = header_border_chs->out_intersect_ch; + H_BOR_CHARS[LH_bip] = H_BOR_CHARS[RH_bip] = header_border_chs->out_intersect_ch; + H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = header_border_chs->out_intersect_ch; + H_BOR_CHARS[II_bip] = header_border_chs->in_intersect_ch; - if (header_border_chs->separator_ch == '\0' && header_border_chs->in_intersect_ch == '\0') { - H_BOR_CHARS[LH_bip] = H_BOR_CHARS[RH_bip] = '\0'; - } + if (header_border_chs->separator_ch == '\0' && header_border_chs->in_intersect_ch == '\0') { + H_BOR_CHARS[LH_bip] = H_BOR_CHARS[RH_bip] = '\0'; + } #undef BOR_CHARS #undef H_BOR_CHARS diff --git a/src/options.c b/src/options.c index 1dad252..2bc58fe 100644 --- a/src/options.c +++ b/src/options.c @@ -207,31 +207,78 @@ fort_status_t set_default_cell_option(uint32_t option, int value) * ***************************************************************************/ +#define BASIC_STYLE { \ + /* border_chars */ \ + { \ + '+', '-', '+', '+', \ + '|', '|', '|', \ + '\0', '\0', '\0', '\0', \ + '+', '-', '+', '+' \ + }, \ + /* header_border_chars */ \ + { \ + '+', '-', '+', '+', \ + '|', '|', '|', \ + '+', '-', '+', '+', \ + '+', '-', '+', '+' \ + }, \ + /* separator_chars */ \ + { \ + '+', '=', '+', '+', \ + }, \ +} + +#define SIMPLE_STYLE { \ + /* border_chars */ \ + { \ + ' ', ' ', ' ', ' ', \ + ' ', ' ', ' ', \ + '\0', '\0', '\0', '\0', \ + ' ', ' ', ' ', ' ' \ + }, \ + /* header_border_chars */ \ + { \ + ' ', ' ', ' ', ' ', \ + ' ', ' ', ' ', \ + ' ', '-', ' ', ' ', \ + ' ', ' ', ' ', ' ' \ + }, \ + /* separator_chars */ \ + { \ + ' ', '=', ' ', ' ', \ + }, \ +} + +#define DOT_STYLE { \ + /* border_chars */ \ + { \ + '.', '.', '.', '.', \ + ':', ':', ':', \ + '\0', '\0', '\0', '\0', \ + ':', '.', ':', ':' \ + }, \ + /* header_border_chars */ \ + { \ + '.', '.', '.', '.', \ + ':', ':', ':', \ + ':', '.', ':', ':', \ + ':', '.', ':', ':' \ + }, \ + /* separator_chars */ \ + { \ + '+', '=', '+', '+', \ + }, \ +} + +struct fort_border_style FORT_BASIC_STYLE = BASIC_STYLE; +struct fort_border_style FORT_SIMPLE_STYLE = SIMPLE_STYLE; +struct fort_border_style FORT_DOT_STYLE = DOT_STYLE; fort_table_options_t g_table_options = { - /* border_chars */ - { - '+', '-', '+', '+', - '|', '|', '|', - '\0', '\0', '\0', '\0', - '+', '-', '+', '+' - }, - - /* header_border_chars */ - { - '+', '-', '+', '+', - '|', '|', '|', - '+', '-', '+', '+', - '+', '-', '+', '+' - }, - - /* separator_chars */ - { - '+', '=', '+', '+', - }, - + /* border_style */ + BASIC_STYLE, NULL, /* cell_options */ }; diff --git a/src/options.h b/src/options.h index 95453d0..f36ff69 100644 --- a/src/options.h +++ b/src/options.h @@ -145,17 +145,17 @@ struct fort_border_style char header_border_chars[BorderItemPosSize]; char separator_chars[SepratorItemPosSize]; }; +extern struct fort_border_style FORT_BASIC_STYLE; +extern struct fort_border_style FORT_SIMPLE_STYLE; +extern struct fort_border_style FORT_DOT_STYLE; + struct fort_table_options { - char border_chars[BorderItemPosSize]; - char header_border_chars[BorderItemPosSize]; - char separator_chars[SepratorItemPosSize]; - + struct fort_border_style border_style; fort_cell_opt_container_t * cell_options; }; typedef struct fort_table_options fort_table_options_t; -/*typedef fort_table_options_t context_t;*/ extern fort_table_options_t g_table_options; diff --git a/src/row.c b/src/row.c index c6c4699..c203bab 100644 --- a/src/row.c +++ b/src/row.c @@ -156,16 +156,16 @@ int print_row_separator(char *buffer, size_t buffer_sz, typedef const char (*border_chars_point_t)[BorderItemPosSize]; const char (*border_chars)[BorderItemPosSize] = NULL; - border_chars = (border_chars_point_t)&context->table_options->border_chars; + border_chars = (border_chars_point_t)&context->table_options->border_style.border_chars; if (upper_row_type == Header || lower_row_type == Header) { - border_chars = (border_chars_point_t)&context->table_options->header_border_chars; + border_chars = (border_chars_point_t)&context->table_options->border_style.header_border_chars; } if (sep && sep->enabled) { - L = &(context->table_options->separator_chars[LH_sip]); - I = &(context->table_options->separator_chars[IH_sip]); - IV = &(context->table_options->separator_chars[II_sip]); - R = &(context->table_options->separator_chars[RH_sip]); + L = &(context->table_options->border_style.separator_chars[LH_sip]); + I = &(context->table_options->border_style.separator_chars[IH_sip]); + IV = &(context->table_options->border_style.separator_chars[II_sip]); + R = &(context->table_options->border_style.separator_chars[RH_sip]); } else { switch (separatorPos) { case TopSeparator: @@ -262,17 +262,17 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz, typedef const char (*border_chars_point_t)[BorderItemPosSize]; const char (*border_chars)[BorderItemPosSize] = NULL; - border_chars = (border_chars_point_t)&context->table_options->border_chars; + border_chars = (border_chars_point_t)&context->table_options->border_style.border_chars; if (upper_row_type == Header || lower_row_type == Header) { - border_chars = (border_chars_point_t)&context->table_options->header_border_chars; + border_chars = (border_chars_point_t)&context->table_options->border_style.header_border_chars; } if (sep && sep->enabled) { - L = &(context->table_options->separator_chars[LH_sip]); - I = &(context->table_options->separator_chars[IH_sip]); - IV = &(context->table_options->separator_chars[II_sip]); - R = &(context->table_options->separator_chars[RH_sip]); + L = &(context->table_options->border_style.separator_chars[LH_sip]); + I = &(context->table_options->border_style.separator_chars[IH_sip]); + IV = &(context->table_options->border_style.separator_chars[II_sip]); + R = &(context->table_options->border_style.separator_chars[RH_sip]); } else { switch (separatorPos) { case TopSeparator: @@ -475,8 +475,8 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col typedef const char (*border_chars_point_t)[BorderItemPosSize]; enum RowType row_type = get_cell_opt_value_hierarcial(context->table_options, context->row, FT_ANY_COLUMN, FT_OPT_ROW_TYPE); const char (*bord_chars)[BorderItemPosSize] = (row_type == Header) - ? (border_chars_point_t) (&context->table_options->header_border_chars) - : (border_chars_point_t) (&context->table_options->border_chars); + ? (border_chars_point_t) (&context->table_options->border_style.header_border_chars) + : (border_chars_point_t) (&context->table_options->border_style.border_chars); const char *L = &(*bord_chars)[LL_bip]; const char *IV = &(*bord_chars)[IV_bip]; const char *R = &(*bord_chars)[RR_bip]; @@ -534,8 +534,8 @@ int wsnprintf_row(const fort_row_t *row, wchar_t *buffer, size_t buf_sz, size_t typedef const char (*border_chars_point_t)[BorderItemPosSize]; enum RowType row_type = get_cell_opt_value_hierarcial(context->table_options, context->row, FT_ANY_COLUMN, FT_OPT_ROW_TYPE); const char (*bord_chars)[BorderItemPosSize] = (row_type) - ? (border_chars_point_t) (&context->table_options->header_border_chars) - : (border_chars_point_t) (&context->table_options->border_chars); + ? (border_chars_point_t) (&context->table_options->border_style.header_border_chars) + : (border_chars_point_t) (&context->table_options->border_style.border_chars); const char *L = &(*bord_chars)[LL_bip]; const char *IV = &(*bord_chars)[IV_bip]; const char *R = &(*bord_chars)[RR_bip];