-
Notifications
You must be signed in to change notification settings - Fork 64
Filling tables with data (C API)
At each moment of time a libfort table has a current cell - cell to which data will be written in the next write operation. Functions ft_set_cur_cell and ft_ln can be used to change current cell:
/* Set current cell to the cell with coordinates (row, col) */
void ft_set_cur_cell(ft_table_t *table, size_t row, size_t col);
/* Set current cell to the first cell of the next row(line) */
void ft_ln(ft_table_t *table);
There are a lot of functions that can be used to fill tables with data. All write functions are grouped in pairs (function, function_ln), where function writes data to a group of consecutive cells, funciton_ln does the same and moves current-cell pointer to the first cell of the next row(line).
ft_write, ft_write_ln macros can be used to write an arbitrary number of strings to the table cells.
ft_table_t *table = ft_create_table();
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "N", "Driver", "Time", "Avg Speed");
ft_write_ln(table, "1", "Ricciardo", "1:25.945", "222.128");
ft_write_ln(table, "2", "Hamilton", "1:26.373", "221.027");
ft_write_ln(table, "3", "Verstappen", "1:26.469", "220.782");
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 47.362 |
| 2 | Hamilton | 1:26.373 | 35.02 |
| 3 | Verstappen | 1:26.469 | 29.78 |
+---+------------+----------+-----------+
ft_printf, ft_printf_ln functions provide habitual printf-like interface.
int ft_printf(ft_table_t *table, const char *fmt, ...);
int ft_printf_ln(ft_table_t *table, const char *fmt, ...);
By default vertical line character '|' in the format string is treated as a cell separator.
ft_table_t *table = ft_create_table();
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
ft_write_ln(table, "N", "Driver", "Time", "Avg Speed");
ft_printf_ln(table, "%d|%s|%s|%7.3f", 1, "Ricciardo", "1:25.945", 222.128);
ft_printf_ln(table, "%d|%s|%s|%d.%d", 2, "Hamilton", "1:26.373", 221, 027);
ft_printf_ln(table, "%d|%s|%s|%s.%d", 3, "Verstappen", "1:26.469", "220", 782);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
ft_row_write, ft_row_write_ln functions provide means to copy string data from the string arrays to the table.
int ft_row_write(ft_table_t *table, size_t cols, const char *row_cells[]);
int ft_row_write_ln(ft_table_t *table, size_t cols, const char *row_cells[]);
ft_table_t *table = ft_create_table();
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
const char *header[] = {"N", "Driver", "Time", "Avg Speed"};
const char *line_1[] = {"1", "Ricciardo", "1:25.945", "222.128"};
const char *line_2[] = {"2", "Hamilton", "1:26.373", "221.027"};
const char *line_3[] = {"3", "Verstappen", "1:26.469", "220.782"};
ft_row_write_ln(table, 4, header);
ft_row_write_ln(table, 4, line_1);
ft_row_write_ln(table, 4, line_2);
ft_row_write_ln(table, 4, line_3);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
ft_table_write, ft_table_write_ln functions provide means to copy string data from the 2D array to the table.
int ft_table_write(ft_table_t *table, size_t rows, size_t cols, const char *table_cells[]);
int ft_table_write_ln(ft_table_t *table, size_t rows, size_t cols, const char *table_cells[]);
ft_table_t *table = ft_create_table();
ft_set_cell_option(table, 0, FT_ANY_COLUMN, FT_COPT_ROW_TYPE, FT_ROW_HEADER);
const char *data[4][4] = {
{"N", "Driver", "Time", "Avg Speed"},
{"1", "Ricciardo", "1:25.945", "222.128"},
{"2", "Hamilton", "1:26.373", "221.027"},
{"3", "Verstappen", "1:26.469", "220.782"}};
ft_table_write_ln(table, 4, 4, (const char **)data);
printf("%s\n", ft_to_string(table));
ft_destroy_table(table);
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
This operator (<<) applied to a libfort table is known as insertion operator. It inserts string to the table cell. Internally libfort converts argument to a string using std::stringstream. If you want to insert argument of some custom type in a table you should overload std::stringstream::operator<<.
fort::Table table;
table << fort::header
<< "N" << "Driver" << "Time" << "Avg Speed" << fort::endr
<< "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr
<< "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr
<< "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr;
std::cout << table.to_string() << std::endl;
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
This operator ([]) provides an ability to directly change content of a particular cell.
fort::Table table;
table << fort::header;
table[0][0] = "N"; table[0][1] = "Driver"; table[0][2] = "Time"; table[0][3] = "Avg Speed";
table[1][0] = "1"; table[1][1] = "Ricciardo"; table[1][2] = "1:25.945"; table[1][3] = "47.362";
table[2][0] = "2"; table[2][1] = "Hamilton"; table[2][2] = "1:26.373"; table[2][3] = "35.02";
table[3][0] = "3"; table[3][1] = "Verstappen"; table[3][2] = "1:26.469"; table[3][3] = "29.78";
std::cout << table.to_string() << std::endl;
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
write, write_ln write an arbitrary number of string arguments to the table cells.
fort::Table table;
table << fort::header;
table.write_ln("N", "Driver", "Time", "Avg Speed");
table.write_ln("1", "Ricciardo", "1:25.945", "47.362");
table.write_ln("2", "Hamilton", "1:26.373", "35.02");
table.write_ln("3", "Verstappen", "1:26.469", "29.78");
std::cout << table.to_string() << std::endl;
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+
row_write, row_write_ln write data from a container determined by a pair of iterators.
template <typename InputIt>
bool row_write(InputIt first, InputIt last);
template <typename InputIt>
bool row_write_ln(InputIt first, InputIt last);
fort::Table table;
table << fort::header;
std::vector<std::string> header = {"N", "Driver", "Time", "Avg Speed"};
std::list<std::string> line_1 = {"1", "Ricciardo", "1:25.945", "47.362"};
std::initializer_list<std::string> line_2 = {"2", "Hamilton", "1:26.373", "35.02"};
std::deque<std::string> line_3 = {"3", "Verstappen", "1:26.469", "29.78"};
table.row_write_ln(header.begin(), header.end());
table.row_write_ln(line_1.begin(), line_1.end());
table.row_write_ln(line_2.begin(), line_2.end());
table.row_write_ln(line_3.begin(), line_3.end());
std::cout << table.to_string() << std::endl;
Output:
+---+------------+----------+-----------+
| N | Driver | Time | Avg Speed |
+---+------------+----------+-----------+
| 1 | Ricciardo | 1:25.945 | 222.128 |
| 2 | Hamilton | 1:26.373 | 221.027 |
| 3 | Verstappen | 1:26.469 | 220.782 |
+---+------------+----------+-----------+