[A] Added left and right table margins
This commit is contained in:
parent
52c100bdf9
commit
49d36179bd
55
src/row.c
55
src/row.c
@ -127,7 +127,7 @@ int print_row_separator(char *buffer, size_t buffer_sz,
|
|||||||
typedef char char_type;
|
typedef char char_type;
|
||||||
char new_line_char = '\n';
|
char new_line_char = '\n';
|
||||||
int (*snprint_n_chars_)(char *, size_t , size_t , char) = snprint_n_chars;
|
int (*snprint_n_chars_)(char *, size_t , size_t , char) = snprint_n_chars;
|
||||||
|
char space_char = ' ';
|
||||||
|
|
||||||
assert(buffer);
|
assert(buffer);
|
||||||
assert(context);
|
assert(context);
|
||||||
@ -196,6 +196,10 @@ int print_row_separator(char *buffer, size_t buffer_sz,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
size_t i = 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) {
|
for (i = 0; i < cols; ++i) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, (char_type)*L));
|
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));
|
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));
|
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, new_line_char));
|
||||||
|
|
||||||
return dev;
|
return dev;
|
||||||
@ -234,6 +241,7 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz,
|
|||||||
typedef wchar_t char_type;
|
typedef wchar_t char_type;
|
||||||
char new_line_char = L'\n';
|
char new_line_char = L'\n';
|
||||||
int (*snprint_n_chars_)(wchar_t*, size_t , size_t , wchar_t) = wsnprint_n_chars;
|
int (*snprint_n_chars_)(wchar_t*, size_t , size_t , wchar_t) = wsnprint_n_chars;
|
||||||
|
wchar_t space_char = L' ';
|
||||||
|
|
||||||
|
|
||||||
assert(buffer);
|
assert(buffer);
|
||||||
@ -303,6 +311,10 @@ int wprint_row_separator(wchar_t *buffer, size_t buffer_sz,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
size_t i = 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) {
|
for (i = 0; i < cols; ++i) {
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, (char_type)*L));
|
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));
|
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));
|
CHECK_RESULT_AND_MOVE_DEV(snprint_n_chars_(buffer + dev, buffer_sz - dev, 1, new_line_char));
|
||||||
|
|
||||||
return dev;
|
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,
|
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)
|
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;
|
typedef char char_type;
|
||||||
char space_char = ' ';
|
char space_char = ' ';
|
||||||
char new_line_char = '\n';
|
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 dev = 0;
|
||||||
|
int k = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (i = 0; i < row_height; ++i) {
|
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);
|
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, (char_type)*L);
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
for (j = 0; j < col_width_arr_sz; ++j) {
|
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);
|
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);
|
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, new_line_char);
|
||||||
}
|
}
|
||||||
return dev;
|
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,
|
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)
|
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;
|
typedef wchar_t char_type;
|
||||||
char space_char = L' ';
|
char space_char = L' ';
|
||||||
char new_line_char = L'\n';
|
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 dev = 0;
|
||||||
|
int k = 0;
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (i = 0; i < row_height; ++i) {
|
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);
|
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, (char_type)*L);
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
for (j = 0; j < col_width_arr_sz; ++j) {
|
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);
|
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);
|
dev += snprint_n_chars_(buffer + dev, buf_sz - dev, 1, new_line_char);
|
||||||
}
|
}
|
||||||
return dev;
|
return dev;
|
||||||
|
|
||||||
|
clear:
|
||||||
|
return -1;
|
||||||
|
#undef CHECK_RESULT_AND_MOVE_DEV
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,27 +39,27 @@ void test_table_tbl_options(void)
|
|||||||
/* Now set table options */
|
/* Now set table options */
|
||||||
ft_set_tbl_option(table, FT_TOPT_TOP_MARGIN, 3);
|
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_BOTTOM_MARGIN, 4);
|
||||||
// ft_set_tbl_option(table, FT_TOPT_LEFT_MARGIN, 1);
|
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_RIGHT_MARGIN, 2);
|
||||||
table_str = ft_to_string(table);
|
table_str = ft_to_string(table);
|
||||||
assert_true( table_str != NULL );
|
assert_true( table_str != NULL );
|
||||||
table_str_etalon =
|
table_str_etalon =
|
||||||
" \n"
|
" \n"
|
||||||
" \n"
|
" \n"
|
||||||
" \n"
|
" \n"
|
||||||
"+---+---+----+----+\n"
|
" +---+---+----+----+ \n"
|
||||||
"| | | | |\n"
|
" | | | | | \n"
|
||||||
"| 3 | 4 | 55 | 67 |\n"
|
" | 3 | 4 | 55 | 67 | \n"
|
||||||
"| | | | |\n"
|
" | | | | | \n"
|
||||||
"+---+---+----+----+\n"
|
" +---+---+----+----+ \n"
|
||||||
"| | | | |\n"
|
" | | | | | \n"
|
||||||
"| 3 | 4 | 55 | 67 |\n"
|
" | 3 | 4 | 55 | 67 | \n"
|
||||||
"| | | | |\n"
|
" | | | | | \n"
|
||||||
"+---+---+----+----+\n"
|
" +---+---+----+----+ \n"
|
||||||
"| | | | |\n"
|
" | | | | | \n"
|
||||||
"| 3 | 4 | 55 | 67 |\n"
|
" | 3 | 4 | 55 | 67 | \n"
|
||||||
"| | | | |\n"
|
" | | | | | \n"
|
||||||
"+---+---+----+----+\n"
|
" +---+---+----+----+ \n"
|
||||||
" \n"
|
" \n"
|
||||||
" \n"
|
" \n"
|
||||||
" \n"
|
" \n"
|
||||||
@ -68,6 +68,63 @@ void test_table_tbl_options(void)
|
|||||||
|
|
||||||
ft_destroy_table(table);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,14 +80,13 @@ FTABLE *create_test_int_table(int set_test_opts)
|
|||||||
if (set_test_opts) {
|
if (set_test_opts) {
|
||||||
assert_true( set_test_options_for_table(table) == FT_SUCCESS);
|
assert_true( set_test_options_for_table(table) == FT_SUCCESS);
|
||||||
}
|
}
|
||||||
// ft_set_table_options(table, &test_table_opts);
|
|
||||||
|
|
||||||
assert_true (table != NULL);
|
assert_true (table != NULL);
|
||||||
|
|
||||||
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, Header);
|
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_printf_ln(table, "%d|%d|%d|%d", 3, 4, 55, 67);
|
||||||
|
int n = FT_NWRITE_LN(table, "3", "4", "55", "67");
|
||||||
assert_true( n == 4 );
|
assert(n == FT_SUCCESS);
|
||||||
|
|
||||||
assert(ft_write(table, "3") == FT_SUCCESS);
|
assert(ft_write(table, "3") == FT_SUCCESS);
|
||||||
assert(ft_write(table, "4") == FT_SUCCESS);
|
assert(ft_write(table, "4") == FT_SUCCESS);
|
||||||
@ -101,3 +100,33 @@ FTABLE *create_test_int_table(int set_test_opts)
|
|||||||
|
|
||||||
return table;
|
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;
|
||||||
|
}
|
||||||
|
@ -60,6 +60,8 @@ struct test_case
|
|||||||
int set_test_options_for_table(FTABLE *table);
|
int set_test_options_for_table(FTABLE *table);
|
||||||
int set_test_options_as_default();
|
int set_test_options_as_default();
|
||||||
FTABLE *create_test_int_table(int set_test_opts);
|
FTABLE *create_test_int_table(int set_test_opts);
|
||||||
|
FTABLE *create_test_int_wtable(int set_test_opts);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user