[A] Added left and right table margins

This commit is contained in:
seleznevae 2018-03-25 11:32:10 +03:00
parent 52c100bdf9
commit 49d36179bd
4 changed files with 168 additions and 27 deletions

View File

@ -127,7 +127,7 @@ int print_row_separator(char *buffer, size_t buffer_sz,
typedef char char_type;
char new_line_char = '\n';
int (*snprint_n_chars_)(char *, size_t , size_t , char) = snprint_n_chars;
char space_char = ' ';
assert(buffer);
assert(context);
@ -196,6 +196,10 @@ int print_row_separator(char *buffer, size_t buffer_sz,
return 0;
size_t i = 0;
/* Print left margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, context->table_options->entire_table_options.left_margin, space_char));
for (i = 0; i < cols; ++i) {
if (i == 0) {
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, (char_type)*L));
@ -206,6 +210,9 @@ int print_row_separator(char *buffer, size_t buffer_sz,
}
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, (char_type)*R));
/* Print right margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, context->table_options->entire_table_options.right_margin, space_char));
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, new_line_char));
return dev;
@ -234,6 +241,7 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz,
typedef wchar_t char_type;
char new_line_char = L'\n';
int (*snprint_n_chars_)(wchar_t*, size_t , size_t , wchar_t) = wsnprint_n_chars;
wchar_t space_char = L' ';
assert(buffer);
@ -303,6 +311,10 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz,
return 0;
size_t i = 0;
/* Print left margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, context->table_options->entire_table_options.left_margin, space_char));
for (i = 0; i < cols; ++i) {
if (i == 0) {
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, (char_type)*L));
@ -313,6 +325,9 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz,
}
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, (char_type)*R));
/* Print right margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, context->table_options->entire_table_options.right_margin, space_char));
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, new_line_char));
return dev;
@ -457,6 +472,13 @@ clear:
int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col_width_arr, size_t col_width_arr_sz,
size_t row_height, const context_t *context)
{
#define CHECK_RESULT_AND_MOVE_DEV(statement) \
k = statement; \
if (k < 0) {\
goto clear; \
} \
dev += k;
typedef char char_type;
char space_char = ' ';
char new_line_char = '\n';
@ -488,8 +510,12 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
int dev = 0;
int k = 0;
size_t i = 0;
for (i = 0; i < row_height; ++i) {
/* Print left margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buf_sz - dev, context->table_options->entire_table_options.left_margin, space_char));
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, (char_type)*L);
size_t j = 0;
for (j = 0; j < col_width_arr_sz; ++j) {
@ -506,9 +532,17 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, (char_type)*IV);
}
}
/* Print right margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buf_sz - dev, context->table_options->entire_table_options.right_margin, space_char));
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, new_line_char);
}
return dev;
clear:
return -1;
#undef CHECK_RESULT_AND_MOVE_DEV
}
@ -516,6 +550,13 @@ int snprintf_row(const fort_row_t *row, char *buffer, size_t buf_sz, size_t *col
int wsnprintf_row(const fort_row_t *row, wchar_t *buffer, size_t buf_sz, size_t *col_width_arr, size_t col_width_arr_sz,
size_t row_height, const context_t *context)
{
#define CHECK_RESULT_AND_MOVE_DEV(statement) \
k = statement; \
if (k < 0) {\
goto clear; \
} \
dev += k;
typedef wchar_t char_type;
char space_char = L' ';
char new_line_char = L'\n';
@ -547,8 +588,12 @@ int wsnprintf_row(const fort_row_t *row, wchar_t *buffer, size_t buf_sz, size_t
int dev = 0;
int k = 0;
size_t i = 0;
for (i = 0; i < row_height; ++i) {
/* Print left margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buf_sz - dev, context->table_options->entire_table_options.left_margin, space_char));
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, (char_type)*L);
size_t j = 0;
for (j = 0; j < col_width_arr_sz; ++j) {
@ -565,8 +610,16 @@ int wsnprintf_row(const fort_row_t *row, wchar_t *buffer, size_t buf_sz, size_t
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, (char_type)*IV);
}
}
/* Print right margin */
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buf_sz - dev, context->table_options->entire_table_options.right_margin, space_char));
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, new_line_char);
}
return dev;
clear:
return -1;
#undef CHECK_RESULT_AND_MOVE_DEV
}

View File

@ -39,27 +39,27 @@ void test_table_tbl_options(void)
/* Now set table options */
ft_set_tbl_option(table, FT_TOPT_TOP_MARGIN, 3);
ft_set_tbl_option(table, FT_TOPT_BOTTOM_MARGIN, 4);
// ft_set_tbl_option(table, FT_TOPT_LEFT_MARGIN, 1);
// ft_set_tbl_option(table, FT_TOPT_RIGHT_MARGIN, 2);
ft_set_tbl_option(table, FT_TOPT_LEFT_MARGIN, 1);
ft_set_tbl_option(table, FT_TOPT_RIGHT_MARGIN, 2);
table_str = ft_to_string(table);
assert_true( table_str != NULL );
table_str_etalon =
" \n"
" \n"
" \n"
"+---+---+----+----+\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"
" +---+---+----+----+ \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"
" \n"
" \n"
" \n"
@ -68,6 +68,63 @@ void test_table_tbl_options(void)
ft_destroy_table(table);
}
WHEN("Test setting entire table options") {
set_test_options_as_default();
table = create_test_int_wtable(0);
const wchar_t *table_str = ft_to_wstring(table);
assert_true( table_str != NULL );
const wchar_t *table_str_etalon =
L"+---+---+----+----+\n"
L"| | | | |\n"
L"| 3 | 4 | 55 | 67 |\n"
L"| | | | |\n"
L"+---+---+----+----+\n"
L"| | | | |\n"
L"| 3 | 4 | 55 | 67 |\n"
L"| | | | |\n"
L"+---+---+----+----+\n"
L"| | | | |\n"
L"| 3 | 4 | 55 | 67 |\n"
L"| | | | |\n"
L"+---+---+----+----+\n";
assert_wcs_equal(table_str, table_str_etalon);
/* Now set table options */
ft_set_tbl_option(table, FT_TOPT_TOP_MARGIN, 3);
ft_set_tbl_option(table, FT_TOPT_BOTTOM_MARGIN, 4);
ft_set_tbl_option(table, FT_TOPT_LEFT_MARGIN, 1);
ft_set_tbl_option(table, FT_TOPT_RIGHT_MARGIN, 2);
table_str = ft_to_wstring(table);
assert_true( table_str != NULL );
table_str_etalon =
L" \n"
L" \n"
L" \n"
L" +---+---+----+----+ \n"
L" | | | | | \n"
L" | 3 | 4 | 55 | 67 | \n"
L" | | | | | \n"
L" +---+---+----+----+ \n"
L" | | | | | \n"
L" | 3 | 4 | 55 | 67 | \n"
L" | | | | | \n"
L" +---+---+----+----+ \n"
L" | | | | | \n"
L" | 3 | 4 | 55 | 67 | \n"
L" | | | | | \n"
L" +---+---+----+----+ \n"
L" \n"
L" \n"
L" \n"
L" \n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
}
}

View File

@ -80,14 +80,13 @@ FTABLE *create_test_int_table(int set_test_opts)
if (set_test_opts) {
assert_true( set_test_options_for_table(table) == FT_SUCCESS);
}
// ft_set_table_options(table, &test_table_opts);
assert_true (table != NULL);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, Header);
int n = ft_printf_ln(table, "%d|%d|%d|%d", 3, 4, 55, 67);
assert_true( n == 4 );
// int n = ft_printf_ln(table, "%d|%d|%d|%d", 3, 4, 55, 67);
int n = FT_NWRITE_LN(table, "3", "4", "55", "67");
assert(n == FT_SUCCESS);
assert(ft_write(table, "3") == FT_SUCCESS);
assert(ft_write(table, "4") == FT_SUCCESS);
@ -101,3 +100,33 @@ FTABLE *create_test_int_table(int set_test_opts)
return table;
}
FTABLE *create_test_int_wtable(int set_test_opts)
{
FTABLE *table = NULL;
table = ft_create_table();
assert_true( table != NULL );
if (set_test_opts) {
assert_true( set_test_options_for_table(table) == FT_SUCCESS);
}
assert_true (table != NULL);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, Header);
// int n = ft_printf_ln(table, "%d|%d|%d|%d", 3, 4, 55, 67);
int n = FT_NWWRITE_LN(table, L"3", L"4", L"55", L"67");
assert(n == FT_SUCCESS);
assert(ft_wwrite(table, L"3") == FT_SUCCESS);
assert(ft_wwrite(table, L"4") == FT_SUCCESS);
assert(ft_wwrite(table, L"55") == FT_SUCCESS);
assert(ft_wwrite_ln(table, L"67") == FT_SUCCESS);
assert(ft_wwrite(table, L"3") == FT_SUCCESS);
assert(ft_wwrite(table, L"4") == FT_SUCCESS);
assert(ft_wwrite(table, L"55") == FT_SUCCESS);
assert(ft_wwrite_ln(table, L"67") == FT_SUCCESS);
return table;
}

View File

@ -60,6 +60,8 @@ struct test_case
int set_test_options_for_table(FTABLE *table);
int set_test_options_as_default();
FTABLE *create_test_int_table(int set_test_opts);
FTABLE *create_test_int_wtable(int set_test_opts);