[A] Added more examples

This commit is contained in:
seleznevae 2019-09-21 12:28:48 +03:00
parent 27c3558137
commit d7bd8e4a55
4 changed files with 137 additions and 155 deletions

View File

@ -1,9 +1,4 @@
add_executable(${PROJECT_NAME}_ex
main.c)
target_link_libraries(${PROJECT_NAME}_ex
fort)
add_executable(${PROJECT_NAME}_simple_table add_executable(${PROJECT_NAME}_simple_table
simple_table.c) simple_table.c)
target_link_libraries(${PROJECT_NAME}_simple_table target_link_libraries(${PROJECT_NAME}_simple_table
@ -14,6 +9,11 @@ add_executable(${PROJECT_NAME}_custom_table
target_link_libraries(${PROJECT_NAME}_custom_table target_link_libraries(${PROJECT_NAME}_custom_table
fort) fort)
add_executable(${PROJECT_NAME}_fill_table
fill_table.c)
target_link_libraries(${PROJECT_NAME}_fill_table
fort)
add_executable(${PROJECT_NAME}_custom_border_style add_executable(${PROJECT_NAME}_custom_border_style
custom_border_style.c) custom_border_style.c)
target_link_libraries(${PROJECT_NAME}_custom_border_style target_link_libraries(${PROJECT_NAME}_custom_border_style
@ -39,6 +39,11 @@ add_executable(${PROJECT_NAME}_complex_layout
target_link_libraries(${PROJECT_NAME}_complex_layout target_link_libraries(${PROJECT_NAME}_complex_layout
fort) fort)
add_executable(${PROJECT_NAME}_non_ascii_table
non_ascii_table.c)
target_link_libraries(${PROJECT_NAME}_non_ascii_table
fort)
@ -48,14 +53,15 @@ target_link_libraries(${PROJECT_NAME}_ex_cpp
fort) fort)
set(${PROJECT_NAME}_examples set(${PROJECT_NAME}_examples
${PROJECT_NAME}_ex
${PROJECT_NAME}_simple_table ${PROJECT_NAME}_simple_table
${PROJECT_NAME}_custom_table ${PROJECT_NAME}_custom_table
${PROJECT_NAME}_fill_table
${PROJECT_NAME}_custom_border_style ${PROJECT_NAME}_custom_border_style
${PROJECT_NAME}_print_styles ${PROJECT_NAME}_print_styles
${PROJECT_NAME}_math_table ${PROJECT_NAME}_math_table
${PROJECT_NAME}_beautiful_table ${PROJECT_NAME}_beautiful_table
${PROJECT_NAME}_complex_layout ${PROJECT_NAME}_complex_layout
${PROJECT_NAME}_non_ascii_table
${PROJECT_NAME}_ex_cpp ${PROJECT_NAME}_ex_cpp
PARENT_SCOPE) PARENT_SCOPE)

83
example/fill_table.c Normal file
View File

@ -0,0 +1,83 @@
#include <stdio.h>
#include "fort.h"
#include <wchar.h>
#include <locale.h>
#include <string.h>
void use_printf(void)
{
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_printf_ln(table, "N|Planet|Speed, km/s");
ft_printf_ln(table, "%d|%s|%5.2f", 1, "Mercury", 47.362);
ft_printf_ln(table, "%d|%s|%5.2f", 2, "Venus", 35.02);
ft_printf_ln(table, "%d|%s|%5.2f", 3, "Earth", 29.78);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
}
void use_write(void)
{
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "N", "Planet", "Speed, km/s");
ft_write_ln(table, "1", "Mercury", "47.362");
ft_write_ln(table, "2", "Venus", "35.02");
ft_write_ln(table, "3", "Earth", "29.78");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
}
void use_row_write(void)
{
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
const char *header[] = {"N", "Planet", "Speed, km/s"};
const char *row1[] = {"1", "Mercury", "47.362"};
const char *row2[] = {"2", "Venus", "35.02"};
const char *row3[] = {"3", "Earth", "29.78"};
ft_row_write_ln(table, 3, header);
ft_row_write_ln(table, 3, row1);
ft_row_write_ln(table, 3, row2);
ft_row_write_ln(table, 3, row3);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
}
void use_table_write(void)
{
ft_table_t *table = ft_create_table();
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
const char *ctab[4][3] = {
{"N", "Planet", "Speed, km/s"},
{"1", "Mercury", "47.362"},
{"2", "Venus", "35.02"},
{"3", "Earth", "29.78"}
};
ft_table_write_ln(table, 4, 3, (const char **)ctab);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
}
int main(void)
{
use_printf();
use_write();
use_row_write();
use_table_write();
return 0;
}

View File

@ -1,149 +0,0 @@
#include <stdio.h>
#include "fort.h"
#include <wchar.h>
#include <locale.h>
#include <string.h>
void fill_table_with_data_example(void)
{
ft_table_t *table = ft_create_table();
/* Set "header" type for the first row */
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "N", "Planet", "Speed, km/s", "Temperature, K");
/* Fill row with printf like function */
ft_printf_ln(table, "1|%s|%6.3f|%d", "Mercury", 47.362, 340);
/* Fill row explicitly with strings */
ft_write_ln(table, "2", "Venus", "35.02", "737");
/* Fill row with the array of strings */
const char *arr[4] = {"3", "Earth", "29.78", "288"};
ft_row_write_ln(table, 4, arr);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
}
int main(void)
{
fill_table_with_data_example();
int result = 0;
ft_table_t *table = NULL;
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_printf_ln(table, "%d|%s|%5.2f km/s", 1, "Mercury", 47.362);
ft_printf_ln(table, "%d|%s|%5.2f km/s", 1, "Mercury", 47.362);
ft_printf_ln(table, "%d|%s|%5.2f km/s", 2, "Venus", 35.02);
ft_printf_ln(table, "%d|%s|%5.2f km/s", 3, "Earth", 29.78);
printf("Table:\n");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
/*-------------------------------------------------------------*/
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", "2001: A Space Odyssey", "1968", "8.5");
ft_write_ln(table, "4", "Blade Runner", "1982", "8.1");
printf("Table:\n");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_printf_ln(table, "Commodity|Farm price|Avg. spread");
const char *row1[] = {"Potatoes", "$1.60", "200.94%"};
const char *row2[] = {"Carrots", "$0.32 ", "190.63%"};
ft_row_write_ln(table, 3, row1);
ft_row_write_ln(table, 3, row2);
printf("Table:\n");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
/*-------------------------------------------------------------*/
table = ft_create_table();
ft_set_cell_prop(table, FT_ANY_ROW, 0, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_LEFT);
ft_set_cell_prop(table, FT_ANY_ROW, 1, FT_CPROP_TEXT_ALIGN, FT_ALIGNED_CENTER);
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
ft_printf_ln(table, "No.|Name|Avg. Mark");
const char *ctab[2][3] = {
{"1", "Joe Public", "3.14"},
{"2", "John Doe", "4.50"}
};
ft_table_write_ln(table, 2, 3, (const char **)ctab);
printf("Table:\n");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
fflush(stdout);
/*-------------------------------------------------------------*/
#if defined(FT_HAVE_WCHAR) && !defined(FT_MICROSOFT_COMPILER)
setlocale(LC_CTYPE, "");
ft_set_default_border_style(FT_BASIC_STYLE);
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"Ранг", L"Название", L"Год", L"Рейтинг");
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"); */
/*FT_NWWRITE_LN(table, L"1", L"\x41f\x43e\x431\x435\x433 \x438\x437 \x428\x43e\x443\x448\x435\x43d\x43a\x430", L"1994", L"9.5");*/ /* Побег из Шоушенка */
/*FT_NWWRITE_LN(table, L"2", L"12 \x440\x430\x437\x433\x43d\x435\x432\x430\x43d\x43d\x44b\x445 \x43c\x443\x436\x447\x438\x43d", L"1957", L"8.8");*/ /* 12 разгневанных мужчин */
/*FT_NWWRITE_LN(table, L"3", L"\x41a\x43e\x441\x43c\x438\x447\x435\x441\x43a\x430\x44f \x43e\x434\x438\x441\x441\x435\x44f 2001 \x433\x43e\x434\x430", L"1968", L"8.5");*/ /* Космическая одиссея 2001 года */
/*FT_NWWRITE_LN(table, L"4", L"\x411\x435\x433\x443\x449\x438\x439 \x43f\x43e \x43b\x435\x437\x432\x438\x44e", L"1982", L"8.1");*/ /* Бегущий по лезвию */
const wchar_t* table_wstr = ft_to_wstring(table);
if (table_wstr) {
fwprintf(stderr, L"Table:\n%ls\n ", table_wstr);
} else {
fwprintf(stderr, L"Table conversion failed !!!\n ");
result += 1;
}
ft_destroy_table(table);
#endif
return result;
}

42
example/non_ascii_table.c Normal file
View File

@ -0,0 +1,42 @@
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "fort.h"
int main(void)
{
#if defined(FT_HAVE_WCHAR) && !defined(FT_MICROSOFT_COMPILER)
setlocale(LC_CTYPE, "");
ft_set_default_border_style(FT_BASIC_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"Ранг", L"Название", L"Год", L"Рейтинг");
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"); */
/*FT_NWWRITE_LN(table, L"1", L"\x41f\x43e\x431\x435\x433 \x438\x437 \x428\x43e\x443\x448\x435\x43d\x43a\x430", L"1994", L"9.5");*/ /* Побег из Шоушенка */
/*FT_NWWRITE_LN(table, L"2", L"12 \x440\x430\x437\x433\x43d\x435\x432\x430\x43d\x43d\x44b\x445 \x43c\x443\x436\x447\x438\x43d", L"1957", L"8.8");*/ /* 12 разгневанных мужчин */
/*FT_NWWRITE_LN(table, L"3", L"\x41a\x43e\x441\x43c\x438\x447\x435\x441\x43a\x430\x44f \x43e\x434\x438\x441\x441\x435\x44f 2001 \x433\x43e\x434\x430", L"1968", L"8.5");*/ /* Космическая одиссея 2001 года */
/*FT_NWWRITE_LN(table, L"4", L"\x411\x435\x433\x443\x449\x438\x439 \x43f\x43e \x43b\x435\x437\x432\x438\x44e", L"1982", L"8.1");*/ /* Бегущий по лезвию */
const wchar_t* table_wstr = ft_to_wstring(table);
fwprintf(stderr, L"%ls\n ", table_wstr);
ft_destroy_table(table);
#endif
return 0;
}