1
0
Fork 0

[C] Refactored write functions

This commit is contained in:
seleznevae 2018-04-24 20:41:14 +03:00
parent 78c1aef44c
commit f8e8793565
9 changed files with 211 additions and 192 deletions

View File

@ -10,14 +10,14 @@ static FTABLE *create_basic_table(void)
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_COPT_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
FT_NWRITE_LN(table, "Rank", "Title", "Year", "Rating");
ft_write_ln(table, "Rank", "Title", "Year", "Rating");
FT_NWRITE_LN(table, "1", "The Shawshank Redemption", "1994", "9.5");
FT_NWRITE_LN(table, "2", "12 Angry Men", "1957", "8.8");
FT_NWRITE_LN(table, "3", "It's a Wonderful Life", "1946", "8.6");
ft_write_ln(table, "1", "The Shawshank Redemption", "1994", "9.5");
ft_write_ln(table, "2", "12 Angry Men", "1957", "8.8");
ft_write_ln(table, "3", "It's a Wonderful Life", "1946", "8.6");
ft_add_separator(table);
FT_NWRITE_LN(table, "4", "2001: A Space Odyssey", "1968", "8.5");
FT_NWRITE_LN(table, "5", "Blade Runner", "1982", "8.1");
ft_write_ln(table, "4", "2001: A Space Odyssey", "1968", "8.5");
ft_write_ln(table, "5", "Blade Runner", "1982", "8.1");
return table;
}
@ -51,10 +51,10 @@ int main(void)
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
ft_printf_ln(table, "Rank|Title|Year|Rating");
FT_NWRITE_LN(table, "1", "The Shawshank Redemption", "1994", "9.5");
FT_NWRITE_LN(table, "2", "12 Angry Men", "1957", "8.8");
FT_NWRITE_LN(table, "3", "2001: A Space Odyssey", "1968", "8.5");
FT_NWRITE_LN(table, "4", "Blade Runner", "1982", "8.1");
ft_write_ln(table, "1", "The Shawshank Redemption", "1994", "9.5");
ft_write_ln(table, "2", "12 Angry Men", "1957", "8.8");
ft_write_ln(table, "3", "2001: A Space Odyssey", "1968", "8.5");
ft_write_ln(table, "4", "Blade Runner", "1982", "8.1");
printf("Table:\n");
@ -80,7 +80,6 @@ int main(void)
ft_destroy_table(table);
/*-------------------------------------------------------------*/
#if !defined(__cplusplus) && !defined(FT_MICROSOFT_COMPILER)
table = ft_create_table();
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_COPT_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_COPT_TEXT_ALIGN, FT_ALIGNED_CENTER);
@ -91,33 +90,12 @@ int main(void)
{"1", "Joe Public", "3.14"},
{"2", "John Doe", "4.50"}
};
ft_s_table_write_ln(table, 2, 3, ctab);
ft_table_write_ln(table, 2, 3, (const char **)ctab);
printf("Table:\n");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_cell_option(table, FT_ANY_ROW, 0, FT_COPT_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_COPT_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
ft_printf_ln(table, "No.|Name|Avg. Mark");
const char **tab[2] = {
row1,
row2
};
ft_table_write_ln(table, 2, 3, tab);
printf("Table:\n");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
#endif
/*---------------- Different styles --------------------*/
ft_set_default_border_style(FT_BASIC_STYLE);
@ -154,12 +132,12 @@ int main(void)
ft_set_cell_option(table, FT_ANY_ROW, 1, FT_COPT_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
FT_NWWRITE_LN(table, L"Ранг", L"Название", L"Год", L"Рейтинг");
ft_wwrite_ln(table, L"Ранг", L"Название", L"Год", L"Рейтинг");
FT_NWWRITE_LN(table, L"1", L"Побег из Шоушенка", L"1994", L"9.5");
FT_NWWRITE_LN(table, L"2", L"12 разгневанных мужчин", L"1957", L"8.8");
FT_NWWRITE_LN(table, L"3", L"Космическая одиссея 2001 года", L"1968", L"8.5");
FT_NWWRITE_LN(table, L"4", L"Бегущий по лезвию", L"1982", L"8.1");
ft_wwrite_ln(table, L"1", L"Побег из Шоушенка", L"1994", L"9.5");
ft_wwrite_ln(table, L"2", L"12 разгневанных мужчин", L"1957", L"8.8");
ft_wwrite_ln(table, L"3", L"Космическая одиссея 2001 года", L"1968", L"8.5");
ft_wwrite_ln(table, L"4", L"Бегущий по лезвию", L"1982", L"8.1");
/* Ранг | Название | Год | Рейтинг */
/*FT_NWWRITE_LN(table, L"\x420\x430\x43d\x433", L"\x41d\x430\x437\x432\x430\x43d\x438\x435", L"\x413\x43e\x434", L"\x420\x435\x439\x442\x438\x43d\x433"); */

View File

@ -5,7 +5,7 @@
int main()
{
{
fort::FTable table;
fort::Table table;
// Fill table with data
table << fort::header
<< "Rank" << "Title" << "Year" << "Rating" << fort::endl
@ -21,7 +21,7 @@ int main()
{
fort::FTable table;
fort::Table table;
// Fill table with data
table << fort::header;
table.write_ln("Rank", "Title", "Year", "Rating");

View File

@ -347,12 +347,9 @@ FT_EXTERN int ft_printf_ln_impl(FTABLE *table, const char *fmt, ...) FT_PRINTF_A
FT_EXTERN int ft_write(FTABLE *table, const char *cell_content);
FT_EXTERN int ft_write_ln(FTABLE *table, const char *cell_content);
#define FT_NWRITE(table, ...)\
#define ft_write(table, ...)\
(0 ? CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite(table, PP_NARG(__VA_ARGS__), __VA_ARGS__))
#define FT_NWRITE_LN(table, ...)\
#define ft_write_ln(table, ...)\
(0 ? CHECK_IF_ARGS_ARE_STRINGS(__VA_ARGS__) : ft_nwrite_ln(table, PP_NARG(__VA_ARGS__), __VA_ARGS__))
FT_EXTERN int ft_nwrite(FTABLE *table, size_t n, const char *cell_content, ...);
FT_EXTERN int ft_nwrite_ln(FTABLE *table, size_t n, const char *cell_content, ...);
@ -365,13 +362,9 @@ FT_EXTERN int ft_nwrite_ln(FTABLE *table, size_t n, const char *cell_content, ..
FT_EXTERN int ft_row_write(FTABLE *table, size_t cols, const char *row_cells[]);
FT_EXTERN int ft_row_write_ln(FTABLE *table, size_t cols, const char *row_cells[]);
#if !defined(__cplusplus) && !defined(FT_MICROSOFT_COMPILER)
FT_EXTERN int ft_s_table_write(FTABLE *table, size_t rows, size_t cols, const char *table_cells[rows][cols]);
FT_EXTERN int ft_s_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char *table_cells[rows][cols]);
FT_EXTERN int ft_table_write(FTABLE *table, size_t rows, size_t cols, const char *table_cells[]);
FT_EXTERN int ft_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char *table_cells[]);
FT_EXTERN int ft_table_write(FTABLE *table, size_t rows, size_t cols, const char * * table_cells[rows]);
FT_EXTERN int ft_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char * * table_cells[rows]);
#endif
@ -600,12 +593,10 @@ FT_EXTERN void ft_set_memory_funcs(void *(*f_malloc)(size_t size), void (*f_free
#ifdef FT_HAVE_WCHAR
FT_EXTERN int ft_wwrite(FTABLE *table, const wchar_t *cell_content);
FT_EXTERN int ft_wwrite_ln(FTABLE *table, const wchar_t *cell_content);
#define FT_NWWRITE(table, ...)\
#define ft_wwrite(table, ...)\
(0 ? CHECK_IF_ARGS_ARE_WSTRINGS(__VA_ARGS__) : ft_nwwrite(table, PP_NARG(__VA_ARGS__), __VA_ARGS__))
#define FT_NWWRITE_LN(table, ...)\
#define ft_wwrite_ln(table, ...)\
(0 ? CHECK_IF_ARGS_ARE_WSTRINGS(__VA_ARGS__) : ft_nwwrite_ln(table, PP_NARG(__VA_ARGS__), __VA_ARGS__))
FT_EXTERN int ft_nwwrite(FTABLE *table, size_t n, const wchar_t *cell_content, ...);
FT_EXTERN int ft_nwwrite_ln(FTABLE *table, size_t n, const wchar_t *cell_content, ...);
@ -613,6 +604,9 @@ FT_EXTERN int ft_nwwrite_ln(FTABLE *table, size_t n, const wchar_t *cell_content
FT_EXTERN int ft_row_wwrite(FTABLE *table, size_t cols, const wchar_t *row_cells[]);
FT_EXTERN int ft_row_wwrite_ln(FTABLE *table, size_t cols, const wchar_t *row_cells[]);
FT_EXTERN int ft_table_wwrite(FTABLE *table, size_t rows, size_t cols, const wchar_t *table_cells[]);
FT_EXTERN int ft_table_wwrite_ln(FTABLE *table, size_t rows, size_t cols, const wchar_t *table_cells[]);
FT_EXTERN const wchar_t *ft_to_wstring(const FTABLE *table);
#endif

View File

@ -9,31 +9,31 @@
namespace fort
{
class FTableManipulator {
class TableManipulator {
public:
explicit FTableManipulator(int i)
explicit TableManipulator(int i)
:value(i)
{
}
friend class FTable;
friend class Table;
private:
int value;
};
const FTableManipulator header(0);
const FTableManipulator endl(1);
const FTableManipulator separator(2);
const TableManipulator header(0);
const TableManipulator endl(1);
const TableManipulator separator(2);
class FTable {
class Table {
public:
FTable()
Table()
:table(ft_create_table())
{
if (table == NULL)
throw std::runtime_error("Runtime error");
}
~FTable()
~Table()
{
ft_destroy_table(table);
}
@ -52,7 +52,7 @@ public:
}
template <typename T>
FTable &operator<<(const T &arg)
Table &operator<<(const T &arg)
{
stream << arg;
if (stream.tellp()) {
@ -62,7 +62,7 @@ public:
return *this;
}
FTable &operator<<(const FTableManipulator &arg)
Table &operator<<(const TableManipulator &arg)
{
if (arg.value == header.value)
ft_set_cell_option(table, FT_CUR_ROW, FT_ANY_ROW, FT_COPT_ROW_TYPE, FT_ROW_HEADER);

View File

@ -209,7 +209,7 @@ int FT_PRINTF_LN(FTABLE *table, const char *fmt, ...)
#undef FT_HDR_PRINTF_LN
int ft_write(FTABLE *table, const char *cell_content)
static int ft_write_impl(FTABLE *table, const char *cell_content)
{
assert(table);
string_buffer_t *str_buffer = get_cur_str_buffer_and_create_if_not_exists(table);
@ -223,19 +223,10 @@ int ft_write(FTABLE *table, const char *cell_content)
return status;
}
int ft_write_ln(FTABLE *table, const char *cell_content)
{
assert(table);
int status = ft_write(table, cell_content);
if (IS_SUCCESS(status)) {
ft_ln(table);
}
return status;
}
#ifdef FT_HAVE_WCHAR
int ft_wwrite(FTABLE *table, const wchar_t *cell_content)
static int ft_wwrite_impl(FTABLE *table, const wchar_t *cell_content)
{
assert(table);
string_buffer_t *str_buffer = get_cur_str_buffer_and_create_if_not_exists(table);
@ -249,15 +240,6 @@ int ft_wwrite(FTABLE *table, const wchar_t *cell_content)
return status;
}
int ft_wwrite_ln(FTABLE *table, const wchar_t *cell_content)
{
assert(table);
int status = ft_wwrite(table, cell_content);
if (IS_SUCCESS(status)) {
ft_ln(table);
}
return status;
}
#endif
@ -265,7 +247,7 @@ int ft_nwrite(FTABLE *table, size_t n, const char *cell_content, ...)
{
size_t i = 0;
assert(table);
int status = ft_write(table, cell_content);
int status = ft_write_impl(table, cell_content);
if (IS_ERROR(status))
return status;
@ -274,7 +256,7 @@ int ft_nwrite(FTABLE *table, size_t n, const char *cell_content, ...)
--n;
for (i = 0; i < n; ++i) {
const char *cell = va_arg(va, const char *);
status = ft_write(table, cell);
status = ft_write_impl(table, cell);
if (IS_ERROR(status))
return status;
}
@ -286,7 +268,7 @@ int ft_nwrite_ln(FTABLE *table, size_t n, const char *cell_content, ...)
{
size_t i = 0;
assert(table);
int status = ft_write(table, cell_content);
int status = ft_write_impl(table, cell_content);
if (IS_ERROR(status))
return status;
@ -295,7 +277,7 @@ int ft_nwrite_ln(FTABLE *table, size_t n, const char *cell_content, ...)
--n;
for (i = 0; i < n; ++i) {
const char *cell = va_arg(va, const char *);
status = ft_write(table, cell);
status = ft_write_impl(table, cell);
if (IS_ERROR(status)) {
va_end(va);
return status;
@ -313,7 +295,7 @@ int ft_nwwrite(FTABLE *table, size_t n, const wchar_t *cell_content, ...)
{
size_t i = 0;
assert(table);
int status = ft_wwrite(table, cell_content);
int status = ft_wwrite_impl(table, cell_content);
if (IS_ERROR(status))
return status;
@ -322,7 +304,7 @@ int ft_nwwrite(FTABLE *table, size_t n, const wchar_t *cell_content, ...)
--n;
for (i = 0; i < n; ++i) {
const wchar_t *cell = va_arg(va, const wchar_t *);
status = ft_wwrite(table, cell);
status = ft_wwrite_impl(table, cell);
if (IS_ERROR(status))
return status;
}
@ -334,7 +316,7 @@ int ft_nwwrite_ln(FTABLE *table, size_t n, const wchar_t *cell_content, ...)
{
size_t i = 0;
assert(table);
int status = ft_wwrite(table, cell_content);
int status = ft_wwrite_impl(table, cell_content);
if (IS_ERROR(status))
return status;
@ -343,7 +325,7 @@ int ft_nwwrite_ln(FTABLE *table, size_t n, const wchar_t *cell_content, ...)
--n;
for (i = 0; i < n; ++i) {
const wchar_t *cell = va_arg(va, const wchar_t *);
status = ft_wwrite(table, cell);
status = ft_wwrite_impl(table, cell);
if (IS_ERROR(status)) {
va_end(va);
return status;
@ -362,7 +344,7 @@ int ft_row_write(FTABLE *table, size_t cols, const char *cells[])
size_t i = 0;
assert(table);
for (i = 0; i < cols; ++i) {
int status = ft_write(table, cells[i]);
int status = ft_write_impl(table, cells[i]);
if (IS_ERROR(status)) {
/* todo: maybe current pos in case of error should be equal to the one before function call? */
return status;
@ -387,7 +369,7 @@ int ft_row_wwrite(FTABLE *table, size_t cols, const wchar_t *cells[])
size_t i = 0;
assert(table);
for (i = 0; i < cols; ++i) {
int status = ft_wwrite(table, cells[i]);
int status = ft_wwrite_impl(table, cells[i]);
if (IS_ERROR(status)) {
/* todo: maybe current pos in case of error should be equal to the one before function call? */
return status;
@ -408,14 +390,13 @@ int ft_row_wwrite_ln(FTABLE *table, size_t cols, const wchar_t *cells[])
#endif
#if !defined(__cplusplus) && !defined(FT_MICROSOFT_COMPILER)
int ft_s_table_write(FTABLE *table, size_t rows, size_t cols, const char *table_cells[rows][cols])
int ft_table_write(FTABLE *table, size_t rows, size_t cols, const char *table_cells[])
{
size_t i = 0;
assert(table);
for (i = 0; i < rows; ++i) {
int status = ft_row_write(table, cols, table_cells[i]);
int status = ft_row_write(table, cols, (const char **)&table_cells[i * cols]);
if (IS_ERROR(status)) {
/* todo: maybe current pos in case of error should be equal to the one before function call? */
return status;
@ -426,34 +407,7 @@ int ft_s_table_write(FTABLE *table, size_t rows, size_t cols, const char *table_
return FT_SUCCESS;
}
int ft_s_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char *table_cells[rows][cols])
{
assert(table);
int status = ft_s_table_write(table, rows, cols, table_cells);
if (IS_SUCCESS(status)) {
ft_ln(table);
}
return status;
}
int ft_table_write(FTABLE *table, size_t rows, size_t cols, const char **table_cells[rows])
{
size_t i = 0;
assert(table);
for (i = 0; i < rows; ++i) {
int status = ft_row_write(table, cols, table_cells[i]);
if (IS_ERROR(status)) {
/* todo: maybe current pos in case of error should be equal to the one before function call? */
return status;
}
if (i != rows - 1)
ft_ln(table);
}
return FT_SUCCESS;
}
int ft_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char **table_cells[rows])
int ft_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char *table_cells[])
{
assert(table);
int status = ft_table_write(table, rows, cols, table_cells);
@ -462,6 +416,34 @@ int ft_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char **tabl
}
return status;
}
#ifdef FT_HAVE_WCHAR
int ft_table_wwrite(FTABLE *table, size_t rows, size_t cols, const wchar_t *table_cells[])
{
size_t i = 0;
assert(table);
for (i = 0; i < rows; ++i) {
int status = ft_row_wwrite(table, cols, (const wchar_t **)&table_cells[i * cols]);
if (IS_ERROR(status)) {
/* todo: maybe current pos in case of error should be equal to the one before function call? */
return status;
}
if (i != rows - 1)
ft_ln(table);
}
return FT_SUCCESS;
}
int ft_table_wwrite_ln(FTABLE *table, size_t rows, size_t cols, const wchar_t *table_cells[])
{
assert(table);
int status = ft_table_wwrite(table, rows, cols, table_cells);
if (IS_SUCCESS(status)) {
ft_ln(table);
}
return status;
}
#endif
@ -469,8 +451,6 @@ int ft_table_write_ln(FTABLE *table, size_t rows, size_t cols, const char **tabl
/*****************************************************************************
* TABLE
* ***************************************************************************/

View File

@ -40,15 +40,15 @@ static int create_simple_table_and_show(void)
result = 3;
goto exit;
}
if (FT_NWRITE_LN(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
if (ft_write_ln(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
result = 4;
goto exit;
}
if (FT_NWRITE_LN(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
if (ft_write_ln(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
result = 5;
goto exit;
}
if (FT_NWRITE_LN(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
if (ft_write_ln(table, "3", "c", "234", "3.140000") != FT_SUCCESS) {
result = 6;
goto exit;
}

View File

@ -11,9 +11,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
@ -42,9 +42,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
@ -75,9 +75,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWRITE_LN(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "c", "234", "3.140000", "3") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "234", "3.140000", "3", "c") == FT_SUCCESS);
assert_true(ft_write_ln(table, "3", "c", "234", "3.140000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "c", "234", "3.140000", "3") == FT_SUCCESS);
assert_true(ft_write_ln(table, "234", "3.140000", "3", "c") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
@ -106,9 +106,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWWRITE_LN(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"c", L"234", L"3.140000", L"3") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"234", L"3.140000", L"3", L"c") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"3", L"c", L"234", L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"c", L"234", L"3.140000", L"3") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"234", L"3.140000", L"3", L"c") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
@ -137,9 +137,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWRITE_LN(table, "", "", "234", "3.140000") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "c", "234", "3.140000", "") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "234", "3.140000", "", "") == FT_SUCCESS);
assert_true(ft_write_ln(table, "", "", "234", "3.140000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "c", "234", "3.140000", "") == FT_SUCCESS);
assert_true(ft_write_ln(table, "234", "3.140000", "", "") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
@ -168,9 +168,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWWRITE_LN(table, L"", L"", L"234", L"3.140000") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"c", L"234", L"3.140000", L"") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"234", L"3.140000", L"", L"") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"", L"", L"234", L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"c", L"234", L"3.140000", L"") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"234", L"3.140000", L"", L"") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
@ -199,9 +199,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWRITE_LN(table, "", "", "", "") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "", "", "", "") == FT_SUCCESS);
assert_true(FT_NWRITE_LN(table, "", "", "", "") == FT_SUCCESS);
assert_true(ft_write_ln(table, "", "", "", "") == FT_SUCCESS);
assert_true(ft_write_ln(table, "", "", "", "") == FT_SUCCESS);
assert_true(ft_write_ln(table, "", "", "", "") == FT_SUCCESS);
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
@ -230,9 +230,9 @@ void test_table_basic(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWWRITE_LN(table, L"", L"", L"", L"") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"", L"", L"", L"") == FT_SUCCESS);
assert_true(FT_NWWRITE_LN(table, L"", L"", L"", L"") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"", L"", L"", L"") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"", L"", L"", L"") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"", L"", L"", L"") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
@ -269,9 +269,9 @@ void test_wcs_table_boundaries(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(FT_NWWRITE_LN(table, L"3", L"12345\x8888\x8888", L"c") == FT_SUCCESS); /* \x8888,\x8888 - occupy 2 columns each */
assert_true(FT_NWWRITE_LN(table, L"c", L"12345678\x500", L"c") == FT_SUCCESS); /* \x500 - occupies 1 column */
assert_true(FT_NWWRITE_LN(table, L"234", L"123456789", L"c") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"3", L"12345\x8888\x8888", L"c") == FT_SUCCESS); /* \x8888,\x8888 - occupy 2 columns each */
assert_true(ft_wwrite_ln(table, L"c", L"12345678\x500", L"c") == FT_SUCCESS); /* \x500 - occupies 1 column */
assert_true(ft_wwrite_ln(table, L"234", L"123456789", L"c") == FT_SUCCESS);
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
@ -306,26 +306,26 @@ void test_table_write(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(ft_write(table, "3") == FT_SUCCESS);
assert_true(ft_write(table, "c") == FT_SUCCESS);
assert_true(ft_write(table, "234") == FT_SUCCESS);
assert_true(ft_write(table, "3.140000") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_write(table, "3")));
assert_true(IS_SUCCESS(ft_write(table, "c")));
assert_true(IS_SUCCESS(ft_write(table, "234")));
assert_true(IS_SUCCESS(ft_write(table, "3.140000")));
ft_ln(table);
assert_true(ft_write(table, "c") == FT_SUCCESS);
assert_true(ft_write(table, "235") == FT_SUCCESS);
assert_true(ft_write(table, "3.150000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "5") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_write(table, "c")));
assert_true(IS_SUCCESS(ft_write(table, "235")));
assert_true(IS_SUCCESS(ft_write(table, "3.150000")));
assert_true(IS_SUCCESS(ft_write_ln(table, "5")));
assert_true(ft_write(table, "234") == FT_SUCCESS);
assert_true(ft_write(table, "3.140000") == FT_SUCCESS);
assert_true(ft_write(table, "3") == FT_SUCCESS);
assert_true(ft_write_ln(table, "c") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_write(table, "234")));
assert_true(IS_SUCCESS(ft_write(table, "3.140000")));
assert_true(IS_SUCCESS(ft_write(table, "3")));
assert_true(IS_SUCCESS(ft_write_ln(table, "c")));
/* Replace old values */
ft_set_cur_cell(table, 1, 1);
assert_true(ft_write(table, "234") == FT_SUCCESS);
assert_true(ft_write(table, "3.140000") == FT_SUCCESS);
assert_true(ft_write_ln(table, "3") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_write(table, "234")));
assert_true(IS_SUCCESS(ft_write(table, "3.140000")));
assert_true(IS_SUCCESS(ft_write_ln(table, "3")));
const char *table_str = ft_to_string(table);
assert_true(table_str != NULL);
@ -354,26 +354,26 @@ void test_table_write(void)
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
assert_true(ft_wwrite(table, L"3") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"c") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"234") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"3.140000") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_wwrite(table, L"3")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"c")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"234")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"3.140000")));
ft_ln(table);
assert_true(ft_wwrite(table, L"c") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"235") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"3.150000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"5") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_wwrite(table, L"c")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"235")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"3.150000")));
assert_true(IS_SUCCESS(ft_wwrite_ln(table, L"5")));
assert_true(ft_wwrite(table, L"234") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"3") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"c") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_wwrite(table, L"234")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"3.140000")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"3")));
assert_true(IS_SUCCESS(ft_wwrite_ln(table, L"c")));
/* Replace old values */
ft_set_cur_cell(table, 1, 1);
assert_true(ft_wwrite(table, L"234") == FT_SUCCESS);
assert_true(ft_wwrite(table, L"3.140000") == FT_SUCCESS);
assert_true(ft_wwrite_ln(table, L"3") == FT_SUCCESS);
assert_true(IS_SUCCESS(ft_wwrite(table, L"234")));
assert_true(IS_SUCCESS(ft_wwrite(table, L"3.140000")));
assert_true(IS_SUCCESS(ft_wwrite_ln(table, L"3")));
const wchar_t *table_str = ft_to_wstring(table);
assert_true(table_str != NULL);
@ -551,6 +551,73 @@ void test_table_write(void)
}
#endif
SCENARIO("Test table_write functions") {
table = ft_create_table();
assert_true(table != NULL);
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
const char *table_cont[3][4] = {
{"3", "c", "234", "3.140000"},
{"c", "234", "3.140000", "3"},
{"234", "3.140000", "3", "c"}
};
assert_true(ft_table_write_ln(table, 3, 4, (const char **)table_cont) == FT_SUCCESS);
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);
}
#ifdef FT_HAVE_WCHAR
SCENARIO("Test table_write functions(wide strings)") {
table = ft_create_table();
assert_true(table != NULL);
assert_true(set_test_options_for_table(table) == FT_SUCCESS);
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
const wchar_t *table_cont[3][4] = {
{L"3", L"c", L"234", L"3.140000"},
{L"c", L"234", L"3.140000", L"3"},
{L"234", L"3.140000", L"3", L"c"}
};
assert_true(ft_table_wwrite_ln(table, 3, 4, (const wchar_t **)table_cont) == FT_SUCCESS);
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);
}
#endif
SCENARIO("Test printf functions") {
table = ft_create_table();

View File

@ -409,7 +409,7 @@ void test_table_cell_options(void)
int n = ft_printf_ln(table, "%d|%c|%s|%f", 4, 'c', "234", 3.14);
assert_true(n == 4);
n = FT_NWRITE_LN(table, "5", "c", "234\n12", "3.140000");
n = ft_write_ln(table, "5", "c", "234\n12", "3.140000");
assert_true(n == FT_SUCCESS);
n = ft_printf_ln(table, "%d|%c|%s|%f", 3, 'c', "234", 3.14);
assert_true(n == 4);

View File

@ -85,7 +85,7 @@ FTABLE *create_test_int_table(int set_test_opts)
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
// int n = ft_printf_ln(table, "%d|%d|%d|%d", 3, 4, 55, 67);
int n = FT_NWRITE_LN(table, "3", "4", "55", "67");
int n = ft_write_ln(table, "3", "4", "55", "67");
assert(n == FT_SUCCESS);
assert(ft_write(table, "3") == FT_SUCCESS);
@ -116,7 +116,7 @@ FTABLE *create_test_int_wtable(int set_test_opts)
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_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");
int n = ft_wwrite_ln(table, L"3", L"4", L"55", L"67");
assert(n == FT_SUCCESS);
assert(ft_wwrite(table, L"3") == FT_SUCCESS);