1
0
Fork 0

[A] Added table options

This commit is contained in:
seleznevae 2018-01-03 10:40:54 +03:00
parent f528c48c78
commit 16043a89f4
5 changed files with 215 additions and 25 deletions

View File

@ -100,6 +100,22 @@ FORT_EXTERN char* ft_to_string(const FTABLE *FORT_RESTRICT table);
//FORT_EXTERN char* ft_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row); //FORT_EXTERN char* ft_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row);
//FORT_EXTERN ssize_t ft_n_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row, char *FORT_RESTRICT dst, size_t dst_len); //FORT_EXTERN ssize_t ft_n_row_to_string(const FTABLE *FORT_RESTRICT table, size_t row, char *FORT_RESTRICT dst, size_t dst_len);
struct fort_table_options
{
int cell_padding_top;
int cell_padding_bottom;
int cell_padding_left;
int cell_padding_right;
char hor_separator;
char ver_separator;
};
typedef struct fort_table_options fort_table_options_t;
FORT_EXTERN void ft_set_default_options(const fort_table_options_t *options);
FORT_EXTERN void ft_get_default_options(fort_table_options_t *options);
FORT_END_DECLS FORT_END_DECLS
#endif // LIBFORT_H #endif // LIBFORT_H

View File

@ -7,8 +7,8 @@
#include "wchar.h" #include "wchar.h"
#define FORT_COL_SEPARATOR '|' #define FORT_COL_SEPARATOR '|'
#define FORT_HOR_CELL_SEPARATOR '=' #define FORT_HOR_CELL_SEPARATOR (g_table_options.hor_separator)
#define FORT_VER_CELL_SEPARATOR '|' #define FORT_VER_CELL_SEPARATOR (g_table_options.ver_separator)
#define FORT_UNUSED __attribute__((unused)) #define FORT_UNUSED __attribute__((unused))
#define F_CALLOC calloc #define F_CALLOC calloc
@ -139,23 +139,23 @@ static void* vector_at(vector_t*, size_t index);
/***************************************************************************** /*****************************************************************************
* CELL * CELL
* ***************************************************************************/ * ***************************************************************************/
struct cell_options //struct cell_options
{ //{
int padding_top; // int padding_top;
int padding_bottom; // int padding_bottom;
int padding_left; // int padding_left;
int padding_right; // int padding_right;
}; //};
typedef struct cell_options cell_options_t; //typedef struct cell_options cell_options_t;
static cell_options_t g_cell_options = {1, 1, 1, 1}; static fort_table_options_t g_table_options = {1, 1, 1, 1, '=', '|'};
static void init_cell_options(cell_options_t *options) static void init_cell_options(fort_table_options_t *options)
{ {
assert(options); assert(options);
options->padding_top = g_cell_options.padding_top; options->cell_padding_top = g_table_options.cell_padding_top;
options->padding_bottom = g_cell_options.padding_bottom; options->cell_padding_bottom = g_table_options.cell_padding_bottom;
options->padding_left = g_cell_options.padding_left; options->cell_padding_left = g_table_options.cell_padding_left;
options->padding_right = g_cell_options.padding_right; options->cell_padding_right = g_table_options.cell_padding_right;
} }
struct fort_cell; struct fort_cell;
@ -163,7 +163,7 @@ typedef struct fort_cell fort_cell_t;
struct fort_cell struct fort_cell
{ {
string_buffer_t *str_buffer; string_buffer_t *str_buffer;
cell_options_t options; fort_table_options_t options;
}; };
static fort_cell_t * create_cell() static fort_cell_t * create_cell()
@ -191,7 +191,7 @@ static void destroy_cell(fort_cell_t *cell)
static int hint_width_cell(const fort_cell_t *cell) static int hint_width_cell(const fort_cell_t *cell)
{ {
assert(cell); assert(cell);
int result = cell->options.padding_left + cell->options.padding_right; int result = cell->options.cell_padding_left + cell->options.cell_padding_right;
if (cell->str_buffer && cell->str_buffer->str) { if (cell->str_buffer && cell->str_buffer->str) {
result += buffer_text_width(cell->str_buffer); result += buffer_text_width(cell->str_buffer);
} }
@ -201,7 +201,7 @@ static int hint_width_cell(const fort_cell_t *cell)
static int hint_height_cell(const fort_cell_t *cell) static int hint_height_cell(const fort_cell_t *cell)
{ {
assert(cell); assert(cell);
int result = cell->options.padding_top + cell->options.padding_bottom; int result = cell->options.cell_padding_top + cell->options.cell_padding_bottom;
if (cell->str_buffer && cell->str_buffer->str) { if (cell->str_buffer && cell->str_buffer->str) {
result += buffer_text_height(cell->str_buffer); result += buffer_text_height(cell->str_buffer);
} }
@ -236,19 +236,19 @@ static int cell_printf(fort_cell_t *cell, size_t row, char *buf, size_t buf_len)
return -1; return -1;
} }
if (row < cell->options.padding_top if (row < cell->options.cell_padding_top
|| row >= (cell->options.padding_top + buffer_text_height(cell->str_buffer))) { || row >= (cell->options.cell_padding_top + buffer_text_height(cell->str_buffer))) {
int k = snprint_n_chars(buf, buf_len, buf_len - 1, ' '); int k = snprint_n_chars(buf, buf_len, buf_len - 1, ' ');
return k; return k;
} else { } else {
int written = 0; int written = 0;
int left = cell->options.padding_left; int left = cell->options.cell_padding_left;
int right = cell->options.padding_right; int right = cell->options.cell_padding_right;
written += snprint_n_chars(buf + written, buf_len - written, left, ' '); written += snprint_n_chars(buf + written, buf_len - written, left, ' ');
if (cell->str_buffer) if (cell->str_buffer)
written += buffer_printf(cell->str_buffer, row - cell->options.padding_top, buf + written, buf_len - written - right); written += buffer_printf(cell->str_buffer, row - cell->options.cell_padding_top, buf + written, buf_len - written - right);
else else
written += snprint_n_chars(buf + written, buf_len - written, buf_len - written - right, ' '); written += snprint_n_chars(buf + written, buf_len - written, buf_len - written - right, ' ');
written += snprint_n_chars(buf + written, buf_len - written, right, ' '); written += snprint_n_chars(buf + written, buf_len - written, right, ' ');
@ -485,6 +485,15 @@ int ft_row_printf(FTABLE *FORT_RESTRICT table, size_t row, const char* FORT_REST
return result; return result;
} }
void ft_set_default_options(const fort_table_options_t *options)
{
memcpy(&g_table_options, options, sizeof(fort_table_options_t));
}
void ft_get_default_options(fort_table_options_t *options)
{
memcpy(options, &g_table_options, sizeof(fort_table_options_t));
}
/***************************************************************************** /*****************************************************************************

View File

@ -6,7 +6,8 @@ int main(void) {
cmocka_unit_test(test_vector_basic), cmocka_unit_test(test_vector_basic),
cmocka_unit_test(test_table_sizes), cmocka_unit_test(test_table_sizes),
cmocka_unit_test(test_table_geometry), cmocka_unit_test(test_table_geometry),
cmocka_unit_test(test_table_basic) cmocka_unit_test(test_table_basic),
cmocka_unit_test(test_table_options)
}; };
return cmocka_run_group_tests(tests, NULL, NULL); return cmocka_run_group_tests(tests, NULL, NULL);
} }

View File

@ -231,6 +231,169 @@ void test_table_basic(void **state)
free(table_str); free(table_str);
ft_destroy_table(table); ft_destroy_table(table);
} }
}
FTABLE *create_test_int_table()
{
FTABLE *table = NULL;
table = ft_create_table();
assert_true (table != NULL);
int n = ft_hdr_printf(table, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
n = ft_row_printf(table, 1, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
n = ft_row_printf(table, 2, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
return table;
}
void test_table_options(void **state)
{
(void)state;
FTABLE *table = NULL;
fort_table_options_t def_options;
ft_get_default_options(&def_options);
WHEN("All paddings = 1") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, 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);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===================\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"===================\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"===================\n"
"| | | | |\n"
"| 3 | 4 | 55 | 67 |\n"
"| | | | |\n"
"===================\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("Top and bottom padding = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, 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);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===================\n"
"| 3 | 4 | 55 | 67 |\n"
"===================\n"
"| 3 | 4 | 55 | 67 |\n"
"===================\n"
"| 3 | 4 | 55 | 67 |\n"
"===================\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("Left and right padding = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, 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);
table = create_test_int_table();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===========\n"
"| | | | |\n"
"|3|4|55|67|\n"
"| | | | |\n"
"===========\n"
"| | | | |\n"
"|3|4|55|67|\n"
"| | | | |\n"
"===========\n"
"| | | | |\n"
"|3|4|55|67|\n"
"| | | | |\n"
"===========\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
WHEN("All paddings = 0") {
fort_table_options_t table_options;
memcpy(&table_options, &def_options, 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();
char *table_str = ft_to_string(table);
assert_true( table_str != NULL );
const char *table_str_etalon =
"===========\n"
"|3|4|55|67|\n"
"===========\n"
"|3|4|55|67|\n"
"===========\n"
"|3|4|55|67|\n"
"===========\n";
// fprintf(stderr, "content:\n%s", table_str);
assert_true( strcmp(table_str, table_str_etalon) == 0);
free(table_str);
ft_destroy_table(table);
}
} }

View File

@ -15,6 +15,7 @@ void test_vector_basic(void **state);
void test_table_sizes(void **state); void test_table_sizes(void **state);
void test_table_geometry(void **state); void test_table_geometry(void **state);
void test_table_basic(void **state); void test_table_basic(void **state);
void test_table_options(void **state);
#endif // TESTS_H #endif // TESTS_H