[A] Added ft_set_default_printf_field_separator funciton

This commit is contained in:
seleznevae 2019-01-27 10:48:42 +03:00
parent 534cffa90d
commit d2d6249817
9 changed files with 125 additions and 15 deletions

View File

@ -2100,7 +2100,7 @@ INCLUDE_FILE_PATTERNS =
# recursively expanded use the := operator instead of the = operator. # recursively expanded use the := operator instead of the = operator.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. # This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
PREDEFINED = PREDEFINED = FT_GCC_COMPILER
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this # If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
# tag can be used to specify a list of macro names that should be expanded. The # tag can be used to specify a list of macro names that should be expanded. The

View File

@ -38,6 +38,7 @@ These pages contain the API documentation of **libfort** - simple library to cre
- Others - Others
- @link ft_set_memory_funcs ft_set_memory_funcs @endlink -- set memory allocation functions for the library - @link ft_set_memory_funcs ft_set_memory_funcs @endlink -- set memory allocation functions for the library
- @link ft_set_default_printf_field_separator ft_set_default_printf_field_separator @endlink -- Set field separator for ft_printf, ft_printf_ln
- Data structures and types - Data structures and types
- @link ft_table_t ft_table_t @endlink -- table handler - @link ft_table_t ft_table_t @endlink -- table handler

View File

@ -59,7 +59,8 @@ SOFTWARE.
#endif /* FT_AMALGAMED_SORCE */ #endif /* FT_AMALGAMED_SORCE */
#define FORT_COL_SEPARATOR '|' #define FORT_DEFAULT_COL_SEPARATOR '|'
extern char g_col_separator;
#define FORT_COL_SEPARATOR_LENGTH 1 #define FORT_COL_SEPARATOR_LENGTH 1
@ -1484,6 +1485,11 @@ int ft_wprintf_ln(ft_table_t *table, const wchar_t *fmt, ...)
#endif #endif
void ft_set_default_printf_field_separator(char separator)
{
g_col_separator = separator;
}
static int ft_write_impl(ft_table_t *table, const char *cell_content) static int ft_write_impl(ft_table_t *table, const char *cell_content)
{ {
@ -2180,6 +2186,7 @@ int ft_set_cell_span(ft_table_t *table, size_t row, size_t col, size_t hor_span)
#endif #endif
char g_col_separator = FORT_DEFAULT_COL_SEPARATOR;
/***************************************************************************** /*****************************************************************************
* LIBFORT helpers * LIBFORT helpers
@ -2315,7 +2322,7 @@ size_t number_of_columns_in_format_string(const char *fmt)
size_t separator_counter = 0; size_t separator_counter = 0;
const char *pos = fmt; const char *pos = fmt;
while (1) { while (1) {
pos = strchr(pos, FORT_COL_SEPARATOR); pos = strchr(pos, g_col_separator);
if (pos == NULL) if (pos == NULL)
break; break;
@ -2333,7 +2340,7 @@ size_t number_of_columns_in_format_wstring(const wchar_t *fmt)
size_t separator_counter = 0; size_t separator_counter = 0;
const wchar_t *pos = fmt; const wchar_t *pos = fmt;
while (1) { while (1) {
pos = wcschr(pos, FORT_COL_SEPARATOR); pos = wcschr(pos, g_col_separator);
if (pos == NULL) if (pos == NULL)
break; break;
@ -4145,7 +4152,7 @@ fort_row_t *create_row_from_string(const char *str)
base_pos = str_copy; base_pos = str_copy;
number_of_separators = 0; number_of_separators = 0;
while (*pos) { while (*pos) {
pos = STRCHR(pos, FORT_COL_SEPARATOR); pos = STRCHR(pos, g_col_separator);
if (pos != NULL) { if (pos != NULL) {
*(pos) = zero_char; *(pos) = zero_char;
++pos; ++pos;
@ -4234,7 +4241,7 @@ fort_row_t *create_row_from_wstring(const wchar_t *str)
base_pos = str_copy; base_pos = str_copy;
number_of_separators = 0; number_of_separators = 0;
while (*pos) { while (*pos) {
pos = STRCHR(pos, FORT_COL_SEPARATOR); pos = STRCHR(pos, g_col_separator);
if (pos != NULL) { if (pos != NULL) {
*(pos) = zero_char; *(pos) = zero_char;
++pos; ++pos;

View File

@ -313,8 +313,9 @@ void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col);
* the data. The format string consists of ordinary characters (except % and |), * the data. The format string consists of ordinary characters (except % and |),
* which are copied unchanged into the output stream, and conversion * which are copied unchanged into the output stream, and conversion
* specifications. Conversion specifications are the same as for standard * specifications. Conversion specifications are the same as for standard
* printf function. Character '|' in the format string is treated as a cell * printf function. Character '|' (wich can be changed with
* separator. * {@link ft_set_default_printf_field_separator}) in the format string is treated as
* a cell separator.
* @param ... * @param ...
* Arguments specifying data to print. Similarly to standard printf-like * Arguments specifying data to print. Similarly to standard printf-like
* functions if any argument after default conversions is not the type * functions if any argument after default conversions is not the type
@ -339,8 +340,9 @@ int ft_printf(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBUTE_FORMA
* the data. The format string consists of ordinary characters (except % and |), * the data. The format string consists of ordinary characters (except % and |),
* which are copied unchanged into the output stream, and conversion * which are copied unchanged into the output stream, and conversion
* specifications. Conversion specifications are the same as for standard * specifications. Conversion specifications are the same as for standard
* printf function. Character '|' in the format string is treated as a cell * printf function. Character '|' (wich can be changed with
* separator. * {@link ft_set_default_printf_field_separator}) in the format string is treated as
* a cell separator.
* @param ... * @param ...
* Arguments specifying data to print. Similarly to standard printf-like * Arguments specifying data to print. Similarly to standard printf-like
* functions if any argument after default conversions is not the type * functions if any argument after default conversions is not the type
@ -373,6 +375,15 @@ int ft_printf_ln_impl(ft_table_t *table, const char *fmt, ...) FT_PRINTF_ATTRIBU
*/ */
#endif #endif
/**
* Set field separator for {@link ft_printf}, {@link ft_printf_ln}
* (default separator is '|').
*
* @param separator
* New separator.
*/
void ft_set_default_printf_field_separator(char separator);
/** /**
* Write strings to the table. * Write strings to the table.

View File

@ -324,6 +324,11 @@ int ft_wprintf_ln(ft_table_t *table, const wchar_t *fmt, ...)
#endif #endif
void ft_set_default_printf_field_separator(char separator)
{
g_col_separator = separator;
}
static int ft_write_impl(ft_table_t *table, const char *cell_content) static int ft_write_impl(ft_table_t *table, const char *cell_content)
{ {

View File

@ -4,6 +4,7 @@
#endif #endif
char g_col_separator = FORT_DEFAULT_COL_SEPARATOR;
/***************************************************************************** /*****************************************************************************
* LIBFORT helpers * LIBFORT helpers
@ -139,7 +140,7 @@ size_t number_of_columns_in_format_string(const char *fmt)
size_t separator_counter = 0; size_t separator_counter = 0;
const char *pos = fmt; const char *pos = fmt;
while (1) { while (1) {
pos = strchr(pos, FORT_COL_SEPARATOR); pos = strchr(pos, g_col_separator);
if (pos == NULL) if (pos == NULL)
break; break;
@ -157,7 +158,7 @@ size_t number_of_columns_in_format_wstring(const wchar_t *fmt)
size_t separator_counter = 0; size_t separator_counter = 0;
const wchar_t *pos = fmt; const wchar_t *pos = fmt;
while (1) { while (1) {
pos = wcschr(pos, FORT_COL_SEPARATOR); pos = wcschr(pos, g_col_separator);
if (pos == NULL) if (pos == NULL)
break; break;

View File

@ -22,7 +22,8 @@
#endif /* FT_AMALGAMED_SORCE */ #endif /* FT_AMALGAMED_SORCE */
#define FORT_COL_SEPARATOR '|' #define FORT_DEFAULT_COL_SEPARATOR '|'
extern char g_col_separator;
#define FORT_COL_SEPARATOR_LENGTH 1 #define FORT_COL_SEPARATOR_LENGTH 1

View File

@ -629,7 +629,7 @@ fort_row_t *create_row_from_string(const char *str)
base_pos = str_copy; base_pos = str_copy;
number_of_separators = 0; number_of_separators = 0;
while (*pos) { while (*pos) {
pos = STRCHR(pos, FORT_COL_SEPARATOR); pos = STRCHR(pos, g_col_separator);
if (pos != NULL) { if (pos != NULL) {
*(pos) = zero_char; *(pos) = zero_char;
++pos; ++pos;
@ -718,7 +718,7 @@ fort_row_t *create_row_from_wstring(const wchar_t *str)
base_pos = str_copy; base_pos = str_copy;
number_of_separators = 0; number_of_separators = 0;
while (*pos) { while (*pos) {
pos = STRCHR(pos, FORT_COL_SEPARATOR); pos = STRCHR(pos, g_col_separator);
if (pos != NULL) { if (pos != NULL) {
*(pos) = zero_char; *(pos) = zero_char;
++pos; ++pos;

View File

@ -865,6 +865,90 @@ void test_table_write(void)
} }
#endif #endif
SCENARIO("Test printf functions with custom separator") {
ft_set_default_printf_field_separator('$');
table = ft_create_table();
assert_true(table != NULL);
assert_true(set_test_props_for_table(table) == FT_SUCCESS);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
int n = ft_printf_ln(table, "%d$%c$%s$%f", 3, 'c', "234", 3.14);
assert_true(n == 4);
n = ft_printf(table, "%c$%s$%f$%d", 'c', "235", 3.15, 5);
assert_true(n == 4);
ft_ln(table);
n = ft_printf_ln(table, "%s$%f$%d$%c", "234", 3.14, 3, 'c');
assert_true(n == 4);
/* Replace old values */
ft_set_cur_cell(table, 1, 1);
n = ft_printf(table, "%s$%f$%d", "234", 3.14, 3);
assert_true(n == 3);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
const char *table_str_etalon =
"+-----+----------+----------+----------+\n"
"| | | | |\n"
"| 3 | c | 234 | 3.140000 |\n"
"| | | | |\n"
"+-----+----------+----------+----------+\n"
"| | | | |\n"
"| c | 234 | 3.140000 | 3 |\n"
"| | | | |\n"
"+-----+----------+----------+----------+\n"
"| | | | |\n"
"| 234 | 3.140000 | 3 | c |\n"
"| | | | |\n"
"+-----+----------+----------+----------+\n";
assert_str_equal(table_str, table_str_etalon);
ft_destroy_table(table);
ft_set_default_printf_field_separator('|');
}
#ifdef FT_HAVE_WCHAR
SCENARIO("Test printf functions(wide strings) with custom separator") {
ft_set_default_printf_field_separator('$');
table = ft_create_table();
assert_true(table != NULL);
assert_true(set_test_props_for_table(table) == FT_SUCCESS);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
int n = ft_wprintf_ln(table, L"%d$%c$%ls$%f", 3, 'c', L"234", 3.14);
assert_true(n == 4);
n = ft_wprintf(table, L"%c$%ls$%f$%d", 'c', L"235", 3.15, 5);
assert_true(n == 4);
ft_ln(table);
n = ft_wprintf_ln(table, L"%ls$%f$%d$%c", L"234", 3.14, 3, 'c');
assert_true(n == 4);
/* Replace old values */
ft_set_cur_cell(table, 1, 1);
n = ft_wprintf_ln(table, L"%ls$%f$%d", L"234", 3.14, 3);
assert_true(n == 3);
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 | c | 234 | 3.140000 |\n"
L"| | | | |\n"
L"+-----+----------+----------+----------+\n"
L"| | | | |\n"
L"| c | 234 | 3.140000 | 3 |\n"
L"| | | | |\n"
L"+-----+----------+----------+----------+\n"
L"| | | | |\n"
L"| 234 | 3.140000 | 3 | c |\n"
L"| | | | |\n"
L"+-----+----------+----------+----------+\n";
assert_wcs_equal(table_str, table_str_etalon);
ft_destroy_table(table);
ft_set_default_printf_field_separator('|');
}
#endif
} }