[R] Refactoring of examples

This commit is contained in:
seleznevae 2019-09-15 11:18:51 +03:00
parent 385668f02c
commit 5ee379ae06
4 changed files with 180 additions and 187 deletions

View File

@ -107,7 +107,7 @@ if("${FORT_COMPILER}" STREQUAL "GNU" OR "${FORT_COMPILER}" STREQUAL "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_FLAGS}")
#target_link_libraries(${PROJECT_NAME}_example_cpp gcov) #target_link_libraries(${PROJECT_NAME}_ex_cpp gcov)
endif() endif()
endif() endif()
@ -157,6 +157,7 @@ set(FORT_HAVE_UTF8 "${FORT_ENABLE_UTF8}" CACHE STRING "fort option")
add_subdirectory(lib) add_subdirectory(lib)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(tests) add_subdirectory(tests)
add_subdirectory(example)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -172,24 +173,6 @@ file(GLOB_RECURSE FortHeaders
add_custom_target(headers SOURCES ${FortHeaders}) add_custom_target(headers SOURCES ${FortHeaders})
add_executable(${PROJECT_NAME}_example
example/main.c)
target_link_libraries(${PROJECT_NAME}_example
fort)
add_executable(${PROJECT_NAME}_example_cpp
example/main.cpp)
target_link_libraries(${PROJECT_NAME}_example_cpp
fort)
if(DEFINED FORT_LINK_LIBRARIES)
target_link_libraries(${PROJECT_NAME}_example_cpp "${FORT_LINK_LIBRARIES}")
target_link_libraries(${PROJECT_NAME}_example "${FORT_LINK_LIBRARIES}")
endif()
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Set preprocessor macros for all test builds # Set preprocessor macros for all test builds
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -269,8 +252,9 @@ endif()
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
enable_testing() enable_testing()
add_test(NAME ${PROJECT_NAME}_example COMMAND ${PROJECT_NAME}_example) add_test(NAME ${PROJECT_NAME}_ex COMMAND ${PROJECT_NAME}_ex)
add_test(NAME ${PROJECT_NAME}_example_cpp COMMAND ${PROJECT_NAME}_example_cpp) add_test(NAME ${PROJECT_NAME}_ex_print_styles COMMAND ${PROJECT_NAME}_ex_print_styles)
add_test(NAME ${PROJECT_NAME}_ex_cpp COMMAND ${PROJECT_NAME}_ex_cpp)
add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test) add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test)
add_test(NAME ${PROJECT_NAME}_test_cpp COMMAND ${PROJECT_NAME}_test_cpp) add_test(NAME ${PROJECT_NAME}_test_cpp COMMAND ${PROJECT_NAME}_test_cpp)
add_test(NAME ${PROJECT_NAME}_test_dev COMMAND ${PROJECT_NAME}_test_dev) add_test(NAME ${PROJECT_NAME}_test_dev COMMAND ${PROJECT_NAME}_test_dev)

21
example/CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
add_executable(${PROJECT_NAME}_ex
main.c)
target_link_libraries(${PROJECT_NAME}_ex
fort)
add_executable(${PROJECT_NAME}_ex_print_styles
print_styles.c)
target_link_libraries(${PROJECT_NAME}_ex_print_styles
fort)
add_executable(${PROJECT_NAME}_ex_cpp
main.cpp)
target_link_libraries(${PROJECT_NAME}_ex_cpp
fort)
if(DEFINED FORT_LINK_LIBRARIES)
target_link_libraries(${PROJECT_NAME}_ex "${FORT_LINK_LIBRARIES}")
target_link_libraries(${PROJECT_NAME}_ex_print_styles "${FORT_LINK_LIBRARIES}")
target_link_libraries(${PROJECT_NAME}_ex_cpp "${FORT_LINK_LIBRARIES}")
endif()

View File

@ -22,41 +22,6 @@ static ft_table_t *create_basic_table(void)
return table; return table;
} }
#if defined(FT_HAVE_WCHAR)
static ft_table_t *create_basic_wtable(const struct ft_border_style *style)
{
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_wwrite_ln(table, L"Rank", L"Title", L"Year", L"Rating");
ft_wwrite_ln(table, L"1", L"The Shawshank Redemption", L"1994", L"9.5");
ft_wwrite_ln(table, L"2", L"12 Angry Men", L"1957", L"8.8");
ft_wwrite_ln(table, L"3", L"It's a Wonderful Life", L"1946", L"8.6");
ft_add_separator(table);
ft_wwrite_ln(table, L"4", L"2001: A Space Odyssey", L"1968", L"8.5");
ft_wwrite_ln(table, L"5", L"Blade Runner", L"1982", L"8.1");
ft_set_border_style(table, style);
return table;
}
#endif
void print_char_str(const char *str)
{
printf("Char_repr:\n");
while (*str) {
if (*str == '\n')
printf("\n");
printf("%x", (unsigned)*str);
str++;
}
}
void base_example(void) void base_example(void)
{ {
ft_table_t *table = ft_create_table(); ft_table_t *table = ft_create_table();
@ -295,69 +260,6 @@ void colorfull_table(void)
} }
void print_different_border_styles()
{
#if defined(FT_HAVE_WCHAR)
ft_table_t *table = ft_create_table();
ft_set_border_style(table, FT_EMPTY_STYLE);
ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_TOP_PADDING, 3);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_LEFT_PADDING, 10);
const struct ft_border_style *styles[] = {
FT_BASIC_STYLE,
FT_BASIC2_STYLE,
FT_SIMPLE_STYLE,
FT_PLAIN_STYLE,
FT_DOT_STYLE,
FT_EMPTY_STYLE,
FT_EMPTY2_STYLE,
FT_SOLID_STYLE,
FT_SOLID_ROUND_STYLE,
FT_NICE_STYLE,
FT_DOUBLE_STYLE,
FT_DOUBLE2_STYLE,
FT_BOLD_STYLE,
FT_BOLD2_STYLE,
FT_FRAME_STYLE
};
const wchar_t *style_names[] = {
L"FT_BASIC_STYLE",
L"FT_BASIC2_STYLE",
L"FT_SIMPLE_STYLE",
L"FT_PLAIN_STYLE",
L"FT_DOT_STYLE",
L"FT_EMPTY_STYLE",
L"FT_EMPTY2_STYLE",
L"FT_SOLID_STYLE",
L"FT_SOLID_ROUND_STYLE",
L"FT_NICE_STYLE",
L"FT_DOUBLE_STYLE",
L"FT_DOUBLE2_STYLE",
L"FT_BOLD_STYLE",
L"FT_BOLD2_STYLE",
L"FT_FRAME_STYLE"
};
for (size_t i = 0; i < sizeof(styles)/sizeof(styles[0]); i += 2) {
ft_table_t *table_tmp_1 = create_basic_wtable(styles[i]);
ft_wprintf(table, L" %ls \n\n%ls", style_names[i], ft_to_wstring(table_tmp_1));
ft_destroy_table(table_tmp_1);
if (i + 1 < sizeof(styles)/sizeof(styles[0])) {
ft_table_t *table_tmp_2 = create_basic_wtable(styles[i + 1]);
ft_wprintf_ln(table, L" %ls \n\n%ls", style_names[i + 1], ft_to_wstring(table_tmp_2));
ft_destroy_table(table_tmp_2);
}
}
fwprintf(stderr, L"Table:\n%ls\n ", ft_to_wstring(table));
ft_destroy_table(table);
#endif
}
int main(void) int main(void)
{ {
base_example(); base_example();
@ -440,73 +342,6 @@ int main(void)
printf("%s\n", ft_to_string(table)); printf("%s\n", ft_to_string(table));
ft_destroy_table(table); ft_destroy_table(table);
/*---------------- Different styles --------------------*/
ft_set_default_border_style(FT_BASIC_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_SIMPLE_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_PLAIN_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_DOT_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_EMPTY_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_EMPTY2_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_SOLID_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_SOLID_ROUND_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_DOUBLE_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_DOUBLE2_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_BOLD_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_BOLD2_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
ft_set_default_border_style(FT_FRAME_STYLE);
table = create_basic_table();
printf("Table:\n%s\n", ft_to_string(table));
ft_destroy_table(table);
/* Debug */ /* Debug */
ft_set_default_border_style(FT_SOLID_STYLE); ft_set_default_border_style(FT_SOLID_STYLE);
table = create_basic_table(); table = create_basic_table();
@ -555,7 +390,6 @@ int main(void)
ft_destroy_table(table); ft_destroy_table(table);
print_different_border_styles();
#endif #endif
return result; return result;

154
example/print_styles.c Normal file
View File

@ -0,0 +1,154 @@
#include <stdio.h>
#include "fort.h"
#include <wchar.h>
#include <locale.h>
#include <string.h>
void print_table_with_style(const struct ft_border_style *style, const char *name)
{
/* Just create a table with some content */
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "Rank", "Title", "Year", "Rating");
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_write_ln(table, "4", "2001: A Space Odyssey", "1968", "8.5");
ft_write_ln(table, "5", "Blade Runner", "1982", "8.1");
/* Setup border style */
ft_set_border_style(table, style);
/* Print table */
printf("%s style:\n\n%s\n\n", name, ft_to_string(table));
ft_destroy_table(table);
}
void print_table_with_different_styles(void)
{
#define PRINT_TABLE_WITH_STYLE(style) \
print_table_with_style(style, #style)
PRINT_TABLE_WITH_STYLE(FT_BASIC_STYLE);
PRINT_TABLE_WITH_STYLE(FT_BASIC2_STYLE);
PRINT_TABLE_WITH_STYLE(FT_SIMPLE_STYLE);
PRINT_TABLE_WITH_STYLE(FT_PLAIN_STYLE);
PRINT_TABLE_WITH_STYLE(FT_DOT_STYLE);
PRINT_TABLE_WITH_STYLE(FT_EMPTY_STYLE);
PRINT_TABLE_WITH_STYLE(FT_EMPTY2_STYLE);
PRINT_TABLE_WITH_STYLE(FT_SOLID_STYLE);
PRINT_TABLE_WITH_STYLE(FT_SOLID_ROUND_STYLE);
PRINT_TABLE_WITH_STYLE(FT_NICE_STYLE);
PRINT_TABLE_WITH_STYLE(FT_DOUBLE_STYLE);
PRINT_TABLE_WITH_STYLE(FT_DOUBLE2_STYLE);
PRINT_TABLE_WITH_STYLE(FT_BOLD_STYLE);
PRINT_TABLE_WITH_STYLE(FT_BOLD2_STYLE);
PRINT_TABLE_WITH_STYLE(FT_FRAME_STYLE);
#undef PRINT_TABLE_WITH_STYLE
fflush(stdout);
}
#if defined(FT_HAVE_WCHAR)
static ft_table_t *create_basic_wtable(const struct ft_border_style *style)
{
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_wwrite_ln(table, L"Rank", L"Title", L"Year", L"Rating");
ft_wwrite_ln(table, L"1", L"The Shawshank Redemption", L"1994", L"9.5");
ft_wwrite_ln(table, L"2", L"12 Angry Men", L"1957", L"8.8");
ft_wwrite_ln(table, L"3", L"It's a Wonderful Life", L"1946", L"8.6");
ft_add_separator(table);
ft_wwrite_ln(table, L"4", L"2001: A Space Odyssey", L"1968", L"8.5");
ft_wwrite_ln(table, L"5", L"Blade Runner", L"1982", L"8.1");
ft_set_border_style(table, style);
return table;
}
#endif
void print_table_with_different_styles_in_tbl_format(void)
{
#if defined(FT_HAVE_WCHAR) && !defined(FT_MICROSOFT_COMPILER)
setlocale(LC_CTYPE, "");
ft_table_t *table = ft_create_table();
ft_set_border_style(table, FT_EMPTY_STYLE);
ft_set_cell_prop(table, FT_ANY_ROW, FT_ANY_COLUMN, FT_CPROP_TOP_PADDING, 3);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_LEFT_PADDING, 10);
const struct ft_border_style *styles[] = {
FT_BASIC_STYLE,
FT_BASIC2_STYLE,
FT_SIMPLE_STYLE,
FT_PLAIN_STYLE,
FT_DOT_STYLE,
FT_EMPTY_STYLE,
FT_EMPTY2_STYLE,
FT_SOLID_STYLE,
FT_SOLID_ROUND_STYLE,
FT_NICE_STYLE,
FT_DOUBLE_STYLE,
FT_DOUBLE2_STYLE,
FT_BOLD_STYLE,
FT_BOLD2_STYLE,
FT_FRAME_STYLE
};
const wchar_t *style_names[] = {
L"FT_BASIC_STYLE",
L"FT_BASIC2_STYLE",
L"FT_SIMPLE_STYLE",
L"FT_PLAIN_STYLE",
L"FT_DOT_STYLE",
L"FT_EMPTY_STYLE",
L"FT_EMPTY2_STYLE",
L"FT_SOLID_STYLE",
L"FT_SOLID_ROUND_STYLE",
L"FT_NICE_STYLE",
L"FT_DOUBLE_STYLE",
L"FT_DOUBLE2_STYLE",
L"FT_BOLD_STYLE",
L"FT_BOLD2_STYLE",
L"FT_FRAME_STYLE"
};
#define PRINT_TABLE_WITH_STYLE(style, style_name) \
do { \
ft_table_t *table_tmp = create_basic_wtable(style); \
ft_wprintf(table, L" %ls \n\n%ls", style_name, ft_to_wstring(table_tmp)); \
ft_destroy_table(table_tmp); \
} while (0)
for (size_t i = 0; i < sizeof(styles)/sizeof(styles[0]); ++i) {
PRINT_TABLE_WITH_STYLE(styles[i], style_names[i]);
if (i % 2)
ft_ln(table);
}
fwprintf(stderr, L"Table:\n%ls\n ", ft_to_wstring(table));
ft_destroy_table(table);
#undef PRINT_TABLE_WITH_STYLE
#endif
}
int main(void)
{
print_table_with_different_styles();
print_table_with_different_styles_in_tbl_format();
return 0;
}