[C] Began options refactoring

This commit is contained in:
seleznevae 2018-02-25 11:39:41 +03:00
parent 1741a8a169
commit 570113aa29
7 changed files with 574 additions and 142 deletions

View File

@ -248,14 +248,31 @@ 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_get_default_options(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_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);
struct border_chars
{
char top_border_ch;
char separator_ch;
char bottom_border_ch;
char side_border_ch;
char out_intersect_ch;
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);
FORT_EXTERN int ft_set_default_option(uint32_t option, int value);
FORT_EXTERN int ft_set_table_option(FTABLE * FORT_RESTRICT table, uint32_t option, int value);
FORT_END_DECLS
#endif // LIBFORT_H

View File

@ -394,17 +394,17 @@ int ft_table_write_ln(FTABLE *FORT_RESTRICT table, size_t rows, size_t cols, con
int ft_set_default_options(const fort_table_options_t *options)
{
memcpy(&g_table_options, options, sizeof(fort_table_options_t));
return 0;
}
//int ft_set_default_options(const fort_table_options_t *options)
//{
// memcpy(&g_table_options, options, sizeof(fort_table_options_t));
// return 0;
//}
int ft_get_default_options(fort_table_options_t *options)
{
memcpy(options, &g_table_options, sizeof(fort_table_options_t));
return 0;
}
//int ft_get_default_options(fort_table_options_t *options)
//{
// memcpy(options, &g_table_options, sizeof(fort_table_options_t));
// return 0;
//}
int ft_set_table_options(FTABLE * FORT_RESTRICT table, const fort_table_options_t * FORT_RESTRICT options)
{
@ -598,3 +598,129 @@ int ft_add_separator(FTABLE *table)
return F_ERROR;
return F_SUCCESS;
}
int ft_set_default_option(uint32_t option, int value)
{
switch (option) {
case FT_OPT_TOP_PADDING:
g_table_options.cell_padding_top = value;
break;
case FT_OPT_BOTTOM_PADDING:
g_table_options.cell_padding_bottom = value;
break;
case FT_OPT_LEFT_PADDING:
g_table_options.cell_padding_left = value;
break;
case FT_OPT_RIGHT_PADDING:
g_table_options.cell_padding_right = value;
break;
case FT_OPT_EMPTY_STR_HEIGHT:
g_table_options.cell_empty_string_height = value;
break;
default:
// todo
exit(22);
}
return F_SUCCESS;
}
static void set_border_options_for_options(fort_table_options_t *options, struct border_chars *border_chs, struct border_chars *header_border_chs)
{
#define BOR_CHARS options->border_chars
#define H_BOR_CHARS options->header_border_chars
// 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;
// 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[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;
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[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';
}
#undef BOR_CHARS
#undef H_BOR_CHARS
}
int ft_set_default_borders(struct border_chars *border_chs, struct border_chars *header_border_chs)
{
set_border_options_for_options(&g_table_options, border_chs, header_border_chs);
return F_SUCCESS;
}
int ft_set_table_borders(FTABLE *table, struct border_chars *border_chs, struct border_chars *header_border_chs)
{
assert(table);
if (table->options == NULL) {
table->options = create_table_options();
if (table->options == NULL)
return F_MEMORY_ERROR;
}
set_border_options_for_options(table->options, border_chs, header_border_chs);
return F_SUCCESS;
}
int ft_set_table_option(FTABLE *table, uint32_t option, int value)
{
assert(table);
if (table->options == NULL) {
table->options = create_table_options();
if (table->options == NULL)
return F_MEMORY_ERROR;
}
switch (option) {
case FT_OPT_TOP_PADDING:
table->options->cell_padding_top = value;
break;
case FT_OPT_BOTTOM_PADDING:
table->options->cell_padding_bottom = value;
break;
case FT_OPT_LEFT_PADDING:
table->options->cell_padding_left = value;
break;
case FT_OPT_RIGHT_PADDING:
table->options->cell_padding_right = value;
break;
case FT_OPT_EMPTY_STR_HEIGHT:
table->options->cell_empty_string_height = value;
break;
default:
// todo
exit(22);
}
return F_SUCCESS;
}

View File

@ -21,6 +21,68 @@ fort_column_options_t create_column_options()
return result;
}
#define DEFAULT_CELL_OPTION {FT_ROW_UNSPEC, FT_COLUMN_UNSPEC, 0, 0, 0}
fort_cell_opt_container_t *create_cell_opt_container()
{
fort_cell_opt_container_t *ret = create_vector(sizeof(fort_cell_options_t), DEFAULT_VECTOR_CAPACITY);
return ret;
}
void destroy_cell_opt_container(fort_cell_opt_container_t *cont)
{
if (cont)
destroy_vector(cont);
}
const fort_cell_options_t* cget_cell_opt(const fort_cell_opt_container_t *cont, unsigned row, unsigned col)
{
assert(cont);
size_t sz = vector_size(cont);
for (size_t i = 0; i < sz; ++i) {
const fort_cell_options_t* opt = (const fort_cell_options_t*)vector_at_c(cont, i);
if (opt->cell_row == row && opt->cell_col == col)
return opt;
}
return NULL;
}
fort_cell_options_t* get_cell_opt_and_create_if_not_exists(fort_cell_opt_container_t *cont, unsigned row, unsigned col)
{
assert(cont);
size_t sz = vector_size(cont);
for (size_t i = 0; i < sz; ++i) {
fort_cell_options_t* opt = (fort_cell_options_t*)vector_at(cont, i);
if (opt->cell_row == row && opt->cell_col == col)
return opt;
}
const fort_cell_options_t opt = DEFAULT_CELL_OPTION;
if (IS_SUCCESS(vector_push(cont, &opt))) {
vector_at(cont, sz);
}
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, option);
if (OPTION_IS_SET(*opt, FT_OPT_MIN_WIDTH)) {
opt->col_min_width = value;
} else if (OPTION_IS_SET(*opt, FT_OPT_TEXT_ALIGN)) {
opt->align = value;
}
return F_SUCCESS;
}
/*****************************************************************************
* OPTIONS
* ***************************************************************************/
@ -83,6 +145,7 @@ fort_table_options_t g_table_options = {
},
NULL, /* col_options */
NULL, /* cell_options */
};
@ -93,6 +156,12 @@ fort_table_options_t* create_table_options()
return NULL;
}
memcpy(options, &g_table_options, sizeof(fort_table_options_t));
options->cell_options = create_cell_opt_container();
if (options->cell_options == NULL) {
destroy_table_options(options);
options = NULL;
}
return options;
}
@ -105,6 +174,15 @@ fort_table_options_t* copy_table_options(const fort_table_options_t *option)
return NULL;
memcpy(new_opt, option, sizeof(fort_table_options_t));
if (option->cell_options) {
destroy_cell_opt_container(new_opt->cell_options);
new_opt->cell_options = copy_vector(option->cell_options);
if (new_opt->cell_options == NULL) {
destroy_table_options(new_opt);
new_opt = NULL;
}
}
return new_opt;
}
@ -117,6 +195,9 @@ void destroy_table_options(fort_table_options_t* options)
if (options->col_options != NULL) {
destroy_vector(options->col_options);
}
if (options->cell_options != NULL) {
destroy_cell_opt_container(options->cell_options);
}
F_FREE(options);
}

View File

@ -2,6 +2,8 @@
#define OPTIONS_H
#include "fort_impl.h"
#include <stdint.h>
#include <limits.h>
enum TextAlignment
{
@ -26,10 +28,44 @@ fort_column_options_t create_column_options();
struct vector;
typedef struct vector vector_t;
#define FT_ANY_COLUMN (UINT_MAX)
#define FT_ANY_ROW (UINT_MAX)
#define FT_ROW_UNSPEC (UINT_MAX-1)
#define FT_COLUMN_UNSPEC (UINT_MAX-1)
#define FT_OPT_MIN_WIDTH ((uint32_t)(0x01U << (0)))
#define FT_OPT_TEXT_ALIGN ((uint32_t)(0x01U << (1)))
#define FT_OPT_TOP_PADDING ((uint32_t)(0x01U << (2)))
#define FT_OPT_BOTTOM_PADDING ((uint32_t)(0x01U << (3)))
#define FT_OPT_LEFT_PADDING ((uint32_t)(0x01U << (4)))
#define FT_OPT_RIGHT_PADDING ((uint32_t)(0x01U << (5)))
#define FT_OPT_EMPTY_STR_HEIGHT ((uint32_t)(0x01U << (6)))
#define OPTION_IS_SET(ft_opts, option) ((ft_opts).options & (option))
#define OPTION_SET(ft_opts, option) ((ft_opts).options |=(option))
#define OPTION_UNSET(ft_opts, option) ((ft_opts).options &= ~((uint32_t)option))
struct fort_cell_options
{
unsigned cell_row;
unsigned cell_col;
uint32_t options;
int col_min_width;
enum TextAlignment align;
};
typedef struct fort_cell_options fort_cell_options_t;
typedef vector_t fort_cell_opt_container_t;
fort_cell_opt_container_t *create_cell_opt_container();
void destroy_cell_opt_container(fort_cell_opt_container_t *cont);
const fort_cell_options_t* cget_cell_opt(const fort_cell_opt_container_t *cont, unsigned row, unsigned col);
fort_cell_options_t* get_cell_opt_and_create_if_not_exists(fort_cell_opt_container_t *cont, unsigned row, unsigned col);
fort_status_t set_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option, int value);
fort_status_t unset_cell_option(fort_cell_opt_container_t *cont, unsigned row, unsigned col, uint32_t option);
/*****************************************************************************
* TABLE BORDER
@ -99,6 +135,7 @@ struct fort_table_options
char separator_chars[SepratorItemPosSize];
vector_t *col_options;
fort_cell_opt_container_t * cell_options;
};
typedef struct fort_table_options fort_table_options_t;
typedef fort_table_options_t context_t;

View File

@ -58,6 +58,22 @@ void destroy_vector(vector_t* vector)
free(vector);
}
vector_t* copy_vector(vector_t *v)
{
if (v == NULL)
return NULL;
vector_t* new_vector = create_vector(v->m_item_size, v->m_capacity);
if (new_vector == NULL)
return NULL;
memcpy(new_vector->m_data, v->m_data, v->m_item_size * v->m_size);
new_vector->m_size = v->m_size ;
new_vector->m_item_size = v->m_item_size ;
return new_vector;
}
/* ----------- Nonmodifying functions --------------------------------- */
@ -131,6 +147,13 @@ void vector_clear(vector_t *vector)
vector->m_size = 0;
}
const void *vector_at_c(const vector_t *vector, size_t index)
{
if (index >= vector->m_size)
return NULL;
return vector->m_data + index * vector->m_item_size;
}
void *vector_at(vector_t *vector, size_t index)

View File

@ -15,6 +15,7 @@ typedef struct vector vector_t;
extern vector_t* create_vector(size_t item_size, size_t capacity);
extern void destroy_vector(vector_t*);
extern vector_t* copy_vector(vector_t*);
extern size_t vector_size(const vector_t*);
extern size_t vector_capacity(const vector_t*);
@ -23,6 +24,7 @@ extern size_t vector_index_of(const vector_t*, const void *item);
extern int vector_push(vector_t*, const void *item);
extern int vector_erase(vector_t*, size_t index);
extern void vector_clear(vector_t*);
extern const void *vector_at_c(const vector_t *vector, size_t index);
extern void* vector_at(vector_t*, size_t index);

View File

@ -10,44 +10,105 @@
#include "vector.h"
fort_table_options_t test_table_opts = {
1, /* cell_padding_top */
1, /* cell_padding_bottom */
1, /* cell_padding_left */
1, /* cell_padding_right */
1, /* cell_empty_string_height */
//fort_table_options_t test_table_opts = {
// 1, /* cell_padding_top */
// 1, /* cell_padding_bottom */
// 1, /* cell_padding_left */
// 1, /* cell_padding_right */
// 1, /* cell_empty_string_height */
/* border_chars */
// /* border_chars */
// {
// '+', '-', '+', '+',
// '|', '|', '|',
// '+', '-', '+', '+',
// '+', '-', '+', '+'
// },
// /* header_border_chars */
// {
// '+', '-', '+', '+',
// '|', '|', '|',
// '+', '-', '+', '+',
// '+', '-', '+', '+'
// },
// /* separator_chars */
// {
// '+', '=', '+', '+',
// },
// NULL, /* col_options */
//};
int set_test_options_for_table(FTABLE *table)
{
'+', '-', '+', '+',
'|', '|', '|',
'+', '-', '+', '+',
'+', '-', '+', '+'
},
assert(table);
int status = F_SUCCESS;
status |= ft_set_table_option(table, FT_OPT_BOTTOM_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_TOP_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_LEFT_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_RIGHT_PADDING, 1);
status |= ft_set_table_option(table, FT_OPT_EMPTY_STR_HEIGHT, 1);
assert_true( status == F_SUCCESS );
/* header_border_chars */
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 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);
}
int set_test_options_as_default()
{
'+', '-', '+', '+',
'|', '|', '|',
'+', '-', '+', '+',
'+', '-', '+', '+'
},
int status = F_SUCCESS;
status |= ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
status |= ft_set_default_option(FT_OPT_TOP_PADDING, 1);
status |= ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
status |= ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
status |= ft_set_default_option(FT_OPT_EMPTY_STR_HEIGHT, 1);
assert_true( status == F_SUCCESS );
/* separator_chars */
{
'+', '=', '+', '+',
},
NULL, /* col_options */
};
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 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);
}
void test_table_sizes(void **state)
{
(void)state;
FTABLE *table = ft_create_table();
assert_true( table != NULL );
ft_set_table_options(table, &test_table_opts);
// ft_set_table_options(table, &test_table_opts);
assert_true( set_test_options_for_table(table) == F_SUCCESS);
size_t rows = 0;
size_t cols = 0;
@ -96,7 +157,8 @@ void test_table_geometry(void **state)
(void)state;
FTABLE *table = ft_create_table();
assert_true( table != NULL );
ft_set_table_options(table, &test_table_opts);
// ft_set_table_options(table, &test_table_opts);
assert_true( set_test_options_for_table(table) == F_SUCCESS);
size_t height = 0;
size_t width = 0;
@ -139,7 +201,8 @@ void test_table_basic(void **state)
WHEN("All columns are equal and not empty") {
table = ft_create_table();
assert_true( table != NULL );
ft_set_table_options(table, &test_table_opts);
// ft_set_table_options(table, &test_table_opts);
assert_true( set_test_options_for_table(table) == F_SUCCESS);
int n = ft_hdr_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true( n == 4 );
@ -176,7 +239,8 @@ void test_table_basic(void **state)
WHEN("All columns are not equal and not empty") {
table = ft_create_table();
assert_true( table != NULL );
ft_set_table_options(table, &test_table_opts);
// ft_set_table_options(table, &test_table_opts);
assert_true( set_test_options_for_table(table) == F_SUCCESS);
int n = ft_hdr_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true( n == 4 );
@ -211,7 +275,8 @@ void test_table_basic(void **state)
WHEN("All columns are not equal and some cells are empty") {
table = ft_create_table();
assert_true( table != NULL );
ft_set_table_options(table, &test_table_opts);
// ft_set_table_options(table, &test_table_opts);
assert_true( set_test_options_for_table(table) == F_SUCCESS);
int n = ft_hdr_printf_ln(table, "||%s|%f", "234", 3.14);
assert_true( n == 4 );
@ -246,7 +311,8 @@ void test_table_basic(void **state)
WHEN("All cells are empty") {
table = ft_create_table();
assert_true( table != NULL );
ft_set_table_options(table, &test_table_opts);
// ft_set_table_options(table, &test_table_opts);
assert_true( set_test_options_for_table(table) == F_SUCCESS);
int n = ft_hdr_printf_ln(table, "|||");
assert_true( n == 4 );
@ -288,8 +354,10 @@ FTABLE *create_test_int_table(int set_test_opts)
table = ft_create_table();
assert_true( table != NULL );
if (set_test_opts)
ft_set_table_options(table, &test_table_opts);
if (set_test_opts) {
assert_true( set_test_options_for_table(table) == F_SUCCESS);
}
// ft_set_table_options(table, &test_table_opts);
assert_true (table != NULL);
@ -319,13 +387,19 @@ void test_table_options(void **state)
WHEN("All paddings = 1") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// ft_set_default_options(&table_options);
set_test_options_as_default();
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
table = create_test_int_table(0);
@ -387,13 +461,17 @@ void test_table_options(void **state)
}
WHEN("Top and bottom padding = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 0;
table_options.cell_padding_top = 0;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
table = create_test_int_table(0);
@ -415,13 +493,17 @@ void test_table_options(void **state)
}
WHEN("Left and right padding = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 0;
table_options.cell_padding_right = 0;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_option(FT_OPT_LEFT_PADDING, 0);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 0);
table = create_test_int_table(0);
@ -449,13 +531,17 @@ void test_table_options(void **state)
}
WHEN("All paddings = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 0;
table_options.cell_padding_top = 0;
table_options.cell_padding_left = 0;
table_options.cell_padding_right = 0;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_option(FT_OPT_LEFT_PADDING, 0);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 0);
table = create_test_int_table(0);
@ -477,14 +563,19 @@ void test_table_options(void **state)
}
WHEN("Empty string has 0 heigt") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
table_options.cell_empty_string_height = 0;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// table_options.cell_empty_string_height = 0;
// ft_set_default_options(&table_options);
ft_set_default_option(FT_OPT_BOTTOM_PADDING, 1);
ft_set_default_option(FT_OPT_TOP_PADDING, 1);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_option(FT_OPT_EMPTY_STR_HEIGHT, 0);
table = create_test_int_table(0);
int n = ft_printf_ln(table, "|||");
@ -517,45 +608,63 @@ void test_table_options(void **state)
}
WHEN("Changing cell separators") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
#define BOR_CHARS table_options.border_chars
#define H_BOR_CHARS table_options.header_border_chars
//#define BOR_CHARS table_options.border_chars
//#define H_BOR_CHARS table_options.header_border_chars
BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = '|';
BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = '|';
BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = '|';
BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = '=';
// BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = '|';
// BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = '|';
// BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = '|';
// BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = '=';
H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = '*';
H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = '*';
H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = '*';
H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = 'v';
// H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = '*';
// H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = '*';
// H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = '*';
// H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = 'v';
//#undef BOR_CHARS
//#undef H_BOR_CHARS
// ft_set_default_options(&table_options);
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 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);
#undef BOR_CHARS
#undef H_BOR_CHARS
ft_set_default_options(&table_options);
table = create_test_int_table(0);
const char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"*******************\n"
"+***+***+****+****+\n"
"v v v v v\n"
"v 3 v 4 v 55 v 67 v\n"
"v v v v v\n"
"*******************\n"
"+***#***#****#****+\n"
"= = = = =\n"
"= 3 = 4 = 55 = 67 =\n"
"= = = = =\n"
"|||||||||||||||||||\n"
"+|||#|||#||||#||||+\n"
"= = = = =\n"
"= 3 = 4 = 55 = 67 =\n"
"= = = = =\n"
"|||||||||||||||||||\n";
"+|||+|||+||||+||||+\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
@ -564,40 +673,63 @@ void test_table_options(void **state)
#define BOR_CHARS table_options.border_chars
#define H_BOR_CHARS table_options.header_border_chars
//#define BOR_CHARS table_options.border_chars
//#define H_BOR_CHARS table_options.header_border_chars
BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = '|';
BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = '\0';
BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = '|';
BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = '=';
// BOR_CHARS[TL_bip] = BOR_CHARS[TT_bip] = BOR_CHARS[TV_bip] = BOR_CHARS[TR_bip] = '|';
// BOR_CHARS[LH_bip] = BOR_CHARS[IH_bip] = BOR_CHARS[II_bip] = BOR_CHARS[RH_bip] = '\0';
// BOR_CHARS[BL_bip] = BOR_CHARS[BB_bip] = BOR_CHARS[BV_bip] = BOR_CHARS[BR_bip] = '|';
// BOR_CHARS[LL_bip] = BOR_CHARS[IV_bip] = BOR_CHARS[RR_bip] = '=';
H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = '*';
H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = '*';
H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = '*';
H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = 'v';
// H_BOR_CHARS[TL_bip] = H_BOR_CHARS[TT_bip] = H_BOR_CHARS[TV_bip] = H_BOR_CHARS[TR_bip] = '*';
// H_BOR_CHARS[LH_bip] = H_BOR_CHARS[IH_bip] = H_BOR_CHARS[II_bip] = H_BOR_CHARS[RH_bip] = '*';
// H_BOR_CHARS[BL_bip] = H_BOR_CHARS[BB_bip] = H_BOR_CHARS[BV_bip] = H_BOR_CHARS[BR_bip] = '*';
// H_BOR_CHARS[LL_bip] = H_BOR_CHARS[IV_bip] = H_BOR_CHARS[RR_bip] = 'v';
#undef BOR_CHARS
#undef H_BOR_CHARS
//#undef BOR_CHARS
//#undef H_BOR_CHARS
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// table_options.cell_empty_string_height = 0;
// ft_set_default_options(&table_options);
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_option(FT_OPT_BOTTOM_PADDING, 0);
ft_set_default_option(FT_OPT_TOP_PADDING, 0);
ft_set_default_option(FT_OPT_LEFT_PADDING, 1);
ft_set_default_option(FT_OPT_RIGHT_PADDING, 1);
ft_set_default_option(FT_OPT_EMPTY_STR_HEIGHT, 0);
table_options.cell_padding_bottom = 0;
table_options.cell_padding_top = 0;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
table_options.cell_empty_string_height = 0;
ft_set_default_options(&table_options);
table = create_test_int_table(0);
table_str = ft_to_string(table);
assert_true( table_str != NULL );
table_str_etalon =
"*******************\n"
"+***+***+****+****+\n"
"v 3 v 4 v 55 v 67 v\n"
"*******************\n"
"+***#***#****#****+\n"
"= 3 = 4 = 55 = 67 =\n"
"= 3 = 4 = 55 = 67 =\n"
"|||||||||||||||||||\n";
"+|||+|||+||||+||||+\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
@ -606,15 +738,21 @@ void test_table_options(void **state)
}
WHEN("Setting options for a particular table") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 0;
table_options.cell_padding_top = 0;
table_options.cell_padding_left = 0;
table_options.cell_padding_right = 0;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 0;
// table_options.cell_padding_top = 0;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_default_options(&table_options);
table = create_test_int_table(0);
set_test_options_for_table(table);
ft_set_table_option(table, FT_OPT_BOTTOM_PADDING, 0);
ft_set_table_option(table, FT_OPT_TOP_PADDING, 0);
ft_set_table_option(table, FT_OPT_LEFT_PADDING, 0);
ft_set_table_option(table, FT_OPT_RIGHT_PADDING, 0);
const char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
@ -630,11 +768,18 @@ void test_table_options(void **state)
assert_true( strcmp(table_str, table_str_etalon) == 0);
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 0;
table_options.cell_padding_right = 0;
ft_set_table_options(table, &table_options);
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 0;
// table_options.cell_padding_right = 0;
// ft_set_table_options(table, &table_options);
ft_set_table_option(table, FT_OPT_BOTTOM_PADDING, 1);
ft_set_table_option(table, FT_OPT_TOP_PADDING, 1);
ft_set_table_option(table, FT_OPT_LEFT_PADDING, 0);
ft_set_table_option(table, FT_OPT_RIGHT_PADDING, 0);
ft_set_table_option(table, FT_OPT_EMPTY_STR_HEIGHT, 0);
table_str = ft_to_string(table);
assert_true( table_str != NULL );
table_str_etalon =
@ -660,14 +805,15 @@ void test_table_options(void **state)
WHEN("Set table width and column alignment") {
fort_table_options_t table_options;
memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
table_options.cell_padding_bottom = 1;
table_options.cell_padding_top = 1;
table_options.cell_padding_left = 1;
table_options.cell_padding_right = 1;
ft_set_default_options(&table_options);
// fort_table_options_t table_options;
// memcpy(&table_options, &test_table_opts, sizeof(fort_table_options_t));
// table_options.cell_padding_bottom = 1;
// table_options.cell_padding_top = 1;
// table_options.cell_padding_left = 1;
// table_options.cell_padding_right = 1;
// ft_set_default_options(&table_options);
set_test_options_as_default();
table = create_test_int_table(0);
ft_set_column_min_width(table, 1, 7);