1
0
Fork 0
libfort/lib/fort.hpp

789 lines
22 KiB
C++
Raw Normal View History

2018-05-05 17:38:45 +02:00
/*
libfort
MIT License
Copyright (c) 2017 - 2018 Seleznev Anton
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* @file fort.hpp
* @brief Main header file describing libfort C++ API .
*
* This files contains C++ wrappers around libfort API that can
* be used in C++ code.
*/
2018-04-01 10:36:52 +02:00
#ifndef LIBFORT_HPP
#define LIBFORT_HPP
#include <string>
2018-04-01 12:27:02 +02:00
#include <stdexcept>
2018-04-01 10:36:52 +02:00
#include <sstream>
2018-11-21 18:50:56 +01:00
2018-04-01 12:27:02 +02:00
#include "fort.h"
2018-04-01 10:36:52 +02:00
namespace fort
{
2018-11-21 18:50:56 +01:00
enum class text_align {
left = FT_ALIGNED_LEFT,
center = FT_ALIGNED_CENTER,
right = FT_ALIGNED_RIGHT
2018-07-25 21:37:10 +02:00
};
2018-11-21 18:50:56 +01:00
enum class row_type {
common = FT_ROW_COMMON,
header = FT_ROW_HEADER
2018-07-25 21:37:10 +02:00
};
2018-11-21 18:50:56 +01:00
enum class color {
default_color = FT_COLOR_DEFAULT,
black = FT_COLOR_BLACK,
red = FT_COLOR_RED,
green = FT_COLOR_GREEN,
yellow = FT_COLOR_YELLOW,
blue = FT_COLOR_BLUE,
magenta = FT_COLOR_MAGENTA,
cyan = FT_COLOR_CYAN,
light_gray = FT_COLOR_LIGHT_GRAY,
dark_gray = FT_COLOR_DARK_GRAY,
light_red = FT_COLOR_LIGHT_RED,
light_green = FT_COLOR_LIGHT_GREEN,
light_yellow = FT_COLOR_LIGHT_YELLOW,
light_blue = FT_COLOR_LIGHT_BLUE,
light_magenta = FT_COLOR_LIGHT_MAGENTA,
light_cyan = FT_COLOR_LIGHT_CYAN,
light_whyte = FT_COLOR_LIGHT_WHYTE
2018-07-25 21:37:10 +02:00
};
2018-11-21 18:50:56 +01:00
enum class text_style {
default_style = FT_TSTYLE_DEFAULT,
bold = FT_TSTYLE_BOLD,
dim = FT_TSTYLE_DIM,
italic = FT_TSTYLE_ITALIC,
underlined = FT_TSTYLE_UNDERLINED,
blink = FT_TSTYLE_BLINK,
inverted = FT_TSTYLE_INVERTED,
hidden = FT_TSTYLE_HIDDEN
2018-07-25 21:37:10 +02:00
};
2018-11-21 18:50:56 +01:00
class table_manipulator {
2018-04-01 10:36:52 +02:00
public:
2018-11-21 18:50:56 +01:00
explicit table_manipulator(int i)
2018-04-01 10:36:52 +02:00
:value(i)
{
}
2018-11-21 18:50:56 +01:00
friend class table;
2018-04-01 10:36:52 +02:00
private:
int value;
};
2018-11-21 18:50:56 +01:00
const table_manipulator header(0);
const table_manipulator endr(1);
const table_manipulator separator(2);
template <typename table>
class property_setter {
public:
property_setter(std::size_t row_idx, std::size_t coll_idx, table &tbl)
:ps_row_idx_(row_idx), ps_coll_idx_(coll_idx), ps_table_(tbl) {}
/**
* Set min width for the specified cell of the table.
*
* @param value
* Value of the min width.
* @return
* - true: Success; cell property was changed.
* - false: In case of error
*/
bool set_cell_min_width(unsigned value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_MIN_WIDTH, value));
}
/**
* Set text alignment for the specified cell of the table.
*
* @param value
* Value of the text alignment.
* @return
* - true: Success; cell property was changed.
* - false: In case of error
*/
bool set_cell_text_align(enum fort::text_align value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_TEXT_ALIGN, static_cast<int>(value)));
}
/**
* Set top padding for the specified cell of the table.
*
* @param value
* Value of the top padding.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_top_padding(unsigned value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_TOP_PADDING, value));
}
/**
* Set bottom padding for the specified cell of the table.
*
* @param value
* Value of the bottom padding.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_bottom_padding(unsigned value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_BOTTOM_PADDING, value));
}
/**
* Set left padding for the specified cell of the table.
*
* @param value
* Value of the left padding.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_left_padding(unsigned value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_LEFT_PADDING, value));
}
/**
* Set right padding for the specified cell of the table.
*
* @param value
* Value of the left padding.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_right_padding(unsigned value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_RIGHT_PADDING, value));
}
/**
* Set row type for the specified cell of the table.
*
* @param value
* Value of the row type.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_row_type(enum fort::row_type value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_ROW_TYPE, static_cast<int>(value)));
}
/**
* Set empty string height for the specified cell of the table.
*
* @param value
* Value of the empty string height.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_empty_str_height(unsigned value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_EMPTY_STR_HEIGHT, value));
}
/**
* Set content foreground color for the specified cell of the table.
*
* @param value
* Color.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_content_fg_color(enum fort::color value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CONT_FG_COLOR, static_cast<int>(value)));
}
/**
* Set cell background color for the specified cell of the table.
*
* @param value
* Color.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_cell_bg_color(enum fort::color value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CELL_BG_COLOR, static_cast<int>(value)));
}
/**
* Set content background color for the specified cell of the table.
*
* @param value
* Color.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_content_bg_color(enum fort::color value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CONT_BG_COLOR, static_cast<int>(value)));
}
/**
* Set cell text style for the specified cell of the table.
*
* @param value
* Text style.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_cell_text_style(enum fort::text_style value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CELL_TEXT_STYLE, static_cast<int>(value)));
}
/**
* Set content text style for the specified cell of the table.
*
* @param value
* Text style.
* @return
* - true: Success; cell property was changed.
* - false: In case of error.
*/
bool set_cell_content_text_style(enum fort::text_style value)
{
return FT_IS_SUCCESS(ft_set_cell_prop(ps_table_.table_, ps_row_idx_, ps_coll_idx_, FT_CPROP_CONT_TEXT_STYLE, static_cast<int>(value)));
}
/**
* Set column span for the specified cell of the table.
*
* @param hor_span
* Column span.
* @return
* - true: Success; cell span was changed.
* - false: In case of error.
*/
bool set_cell_span(size_t hor_span)
{
return FT_IS_SUCCESS(ft_set_cell_span(ps_table_.table_, ps_row_idx_, ps_coll_idx_, hor_span));
}
protected:
std::size_t ps_row_idx_;
std::size_t ps_coll_idx_;
table &ps_table_;
};
2018-04-01 10:36:52 +02:00
2018-05-05 17:38:45 +02:00
/**
2018-07-22 21:52:55 +02:00
* Table - formatted table.
2018-05-05 17:38:45 +02:00
*
2018-07-22 21:52:55 +02:00
* Table class is a C++ wrapper around struct ft_table.
2018-05-05 17:38:45 +02:00
*/
2018-11-21 18:50:56 +01:00
class table: public property_setter<table> {
2018-04-01 10:36:52 +02:00
public:
2018-11-21 18:50:56 +01:00
table()
:property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(ft_create_table())
2018-04-01 10:36:52 +02:00
{
2018-11-21 18:50:56 +01:00
if (table_ == NULL)
2018-04-01 10:36:52 +02:00
throw std::runtime_error("Runtime error");
}
2018-11-21 18:50:56 +01:00
~table()
2018-04-01 10:36:52 +02:00
{
2018-11-21 18:50:56 +01:00
ft_destroy_table(table_);
2018-04-01 10:36:52 +02:00
}
2018-11-02 18:06:04 +01:00
/**
2018-11-02 22:16:20 +01:00
* Copy contstructor.
2018-11-02 18:06:04 +01:00
*/
2018-11-21 18:50:56 +01:00
table(const table& tbl)
:property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(NULL)
2018-11-02 22:16:20 +01:00
{
2018-11-21 18:50:56 +01:00
if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table_);
2018-11-02 22:16:20 +01:00
if (table_copy == NULL)
throw std::runtime_error("Runtime error");
2018-11-21 18:50:56 +01:00
stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
stream_ << tbl.stream_.str();
2018-11-02 22:16:20 +01:00
}
2018-11-21 18:50:56 +01:00
table_ = table_copy;
2018-11-02 22:16:20 +01:00
}
}
2018-11-02 18:06:04 +01:00
/**
* Move contstructor.
*/
2018-11-21 18:50:56 +01:00
table(table&& tbl)
:property_setter(FT_ANY_ROW, FT_ANY_COLUMN, *this), table_(tbl.table_)
2018-11-02 18:06:04 +01:00
{
2018-11-21 18:50:56 +01:00
if (tbl.stream_.tellp() >= 0) {
stream_ << tbl.stream_.str();
tbl.stream_.str(std::string());
2018-11-02 18:06:04 +01:00
}
2018-11-21 18:50:56 +01:00
tbl.table_ = 0;
2018-11-02 18:06:04 +01:00
}
/**
2018-11-02 22:16:20 +01:00
* Copy assignment operator.
2018-11-02 18:06:04 +01:00
*/
2018-11-21 18:50:56 +01:00
table& operator=(const table& tbl)
2018-11-02 22:16:20 +01:00
{
if (&tbl == this)
return *this;
2018-11-21 18:50:56 +01:00
if (tbl.table_) {
ft_table_t *table_copy = ft_copy_table(tbl.table_);
2018-11-02 22:16:20 +01:00
if (table_copy == NULL)
throw std::runtime_error("Runtime error");
2018-11-21 18:50:56 +01:00
stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
stream_ << tbl.stream_.str();
2018-11-02 22:16:20 +01:00
}
2018-11-21 18:50:56 +01:00
ft_destroy_table(table_);
table_ = table_copy;
2018-11-02 22:16:20 +01:00
}
return *this;
}
2018-11-02 18:06:04 +01:00
/**
* Move assignment operator.
*/
2018-11-21 18:50:56 +01:00
table& operator=(table&& tbl)
2018-11-02 18:06:04 +01:00
{
if (&tbl == this)
return *this;
2018-11-21 18:50:56 +01:00
if (tbl.table_) {
stream_.str(std::string());
if (tbl.stream_.tellp() >= 0) {
stream_ << tbl.stream_.str();
tbl.stream_.str(std::string());
2018-11-02 18:06:04 +01:00
}
2018-11-21 18:50:56 +01:00
ft_destroy_table(table_);
table_ = tbl.table_;
tbl.table_ = NULL;
2018-11-02 18:06:04 +01:00
}
return *this;
}
2018-07-22 21:52:55 +02:00
/**
* Convert table to string representation.
*
* @return
* - String representation of formatted table, on success.
* - In case of error std::runtime_error is thrown.
*/
2018-04-01 10:36:52 +02:00
std::string to_string() const
{
2018-11-21 18:50:56 +01:00
const char *str = ft_to_string(table_);
2018-04-01 10:36:52 +02:00
if (str == NULL)
throw std::runtime_error("Runtime error");
return str;
}
2018-07-22 21:52:55 +02:00
/**
* Convert table to string representation.
*
* Table object has ownership of the returned pointer. So there is no need to
* free it. To take ownership user should explicitly copy the returned
* string with strdup or similar functions.
*
* Returned pointer may be later invalidated by:
* - Calling destroying the table;
* - Other invocations of c_str or to_string.
*
* @return
* - The pointer to the string representation of formatted table, on success.
* - NULL on error.
*/
2018-04-01 10:36:52 +02:00
const char *c_str() const
{
2018-11-21 18:50:56 +01:00
return ft_to_string(table_);
2018-04-01 10:36:52 +02:00
}
2018-07-22 21:52:55 +02:00
/**
* Write provided object to the the table.
*
* To convert object to the string representation conversion for
* std::ostream is used.
*
* @param arg
* Obect that would be inserted in the current cell.
* @return
* - Reference to the current table.
*/
2018-04-01 10:36:52 +02:00
template <typename T>
2018-11-21 18:50:56 +01:00
table &operator<<(const T &arg)
2018-04-01 10:36:52 +02:00
{
2018-11-21 18:50:56 +01:00
stream_ << arg;
if (stream_.tellp() >= 0) {
ft_nwrite(table_, 1, stream_.str().c_str());
stream_.str(std::string());
2018-04-08 14:48:15 +02:00
}
2018-04-01 10:36:52 +02:00
return *this;
}
2018-11-21 18:50:56 +01:00
table &operator<<(const table_manipulator &arg)
2018-04-01 10:36:52 +02:00
{
if (arg.value == header.value)
2018-11-21 18:50:56 +01:00
ft_set_cell_prop(table_, FT_CUR_ROW, FT_ANY_ROW, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
2018-07-22 21:52:55 +02:00
else if (arg.value == endr.value)
2018-11-21 18:50:56 +01:00
ft_ln(table_);
2018-04-01 10:36:52 +02:00
else if (arg.value == separator.value)
2018-11-21 18:50:56 +01:00
ft_add_separator(table_);
2018-04-01 10:36:52 +02:00
return *this;
}
2018-04-08 14:48:15 +02:00
bool write(const char *str)
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_write(table_, str));
2018-04-08 14:48:15 +02:00
}
bool write_ln(const char *str)
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_write_ln(table_, str));
2018-04-08 14:48:15 +02:00
}
bool write(const std::string &str)
{
return write(str.c_str());
}
bool write_ln(const std::string &str)
{
return write_ln(str.c_str());
}
#ifdef __cpp_variadic_templates
2018-04-08 14:48:15 +02:00
template <typename T, typename ...Ts>
bool write(const T &arg, const Ts &...args)
{
return write(arg) && write(args...);
}
template <typename T, typename ...Ts>
bool write_ln(const T &arg, const Ts &...args)
{
return write(arg) && write_ln(args...);
}
#else /* __cpp_variadic_templates */
template <typename T_0, typename T_1>
bool write(const T_0 &arg_0, const T_1 &arg_1)
{
return write(arg_0) && write(arg_1);
}
template <typename T_0, typename T_1, typename T_2>
bool write(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2)
{
return write(arg_0) && write(arg_1, arg_2);
}
template <typename T_0, typename T_1, typename T_2, typename T_3>
bool write(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3)
{
return write(arg_0) && write(arg_1, arg_2, arg_3);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4>
bool write(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4)
{
return write(arg_0) && write(arg_1, arg_2, arg_3, arg_4);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4, typename T_5>
bool write(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4, const T_5 &arg_5)
{
return write(arg_0) && write(arg_1, arg_2, arg_3, arg_4, arg_5);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4, typename T_5, typename T_6>
bool write(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4, const T_5 &arg_5, const T_6 &arg_6)
{
return write(arg_0) && write(arg_1, arg_2, arg_3, arg_4, arg_5, arg_6);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4, typename T_5, typename T_6, typename T_7>
bool write(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4, const T_5 &arg_5, const T_6 &arg_6, const T_7 &arg_7)
{
return write(arg_0) && write(arg_1, arg_2, arg_3, arg_4, arg_5, arg_6, arg_7);
}
template <typename T_0, typename T_1>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1)
{
return write(arg_0) && write_ln(arg_1);
}
template <typename T_0, typename T_1, typename T_2>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2)
{
return write(arg_0) && write_ln(arg_1, arg_2);
}
template <typename T_0, typename T_1, typename T_2, typename T_3>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3)
{
return write(arg_0) && write_ln(arg_1, arg_2, arg_3);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4)
{
return write(arg_0) && write_ln(arg_1, arg_2, arg_3, arg_4);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4, typename T_5>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4, const T_5 &arg_5)
{
return write(arg_0) && write_ln(arg_1, arg_2, arg_3, arg_4, arg_5);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4, typename T_5, typename T_6>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4, const T_5 &arg_5, const T_6 &arg_6)
{
return write(arg_0) && write_ln(arg_1, arg_2, arg_3, arg_4, arg_5, arg_6);
}
template <typename T_0, typename T_1, typename T_2, typename T_3, typename T_4, typename T_5, typename T_6, typename T_7>
bool write_ln(const T_0 &arg_0, const T_1 &arg_1, const T_2 &arg_2, const T_3 &arg_3, const T_4 &arg_4, const T_5 &arg_5, const T_6 &arg_6, const T_7 &arg_7)
{
return write(arg_0) && write_ln(arg_1, arg_2, arg_3, arg_4, arg_5, arg_6, arg_7);
}
#endif /* __cpp_variadic_templates */
2018-04-01 10:36:52 +02:00
2018-08-09 21:15:05 +02:00
template <typename InputIt>
bool row_write(InputIt first, InputIt last)
{
while (first != last) {
*this << *first;
++first;
}
return true;
}
template <typename InputIt>
bool row_write_ln(InputIt first, InputIt last)
{
while (first != last) {
*this << *first;
++first;
}
2018-11-21 18:50:56 +01:00
ft_ln(table_);
2018-08-09 21:15:05 +02:00
return true;
}
2018-07-25 21:37:10 +02:00
/**
2018-11-21 18:50:56 +01:00
* Set border style for the table.
2018-07-25 21:37:10 +02:00
*
2018-11-21 18:50:56 +01:00
* @param style
* Pointer to border style.
2018-07-25 21:37:10 +02:00
* @return
2018-11-21 18:50:56 +01:00
* - True: Success; table border style was changed.
* - False: Error
2018-07-25 21:37:10 +02:00
*/
2018-11-21 18:50:56 +01:00
bool set_border_style(struct ft_border_style *style)
2018-07-25 21:37:10 +02:00
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_set_border_style(table_, style));
2018-07-25 21:37:10 +02:00
}
2018-07-29 08:59:40 +02:00
/**
2018-11-21 18:50:56 +01:00
* Set current cell position.
2018-07-29 08:59:40 +02:00
*
2018-11-21 18:50:56 +01:00
* Current cell - cell that will be edited with all modifiing functions.
2018-07-29 08:59:40 +02:00
*
2018-11-21 18:50:56 +01:00
* @param row_i
* New row number for the current cell.
* @param col_i
* New row number for the current cell.
2018-07-29 08:59:40 +02:00
*/
2018-11-21 18:50:56 +01:00
void set_cur_cell(size_t row_i, size_t col_i)
2018-07-29 08:59:40 +02:00
{
2018-11-21 18:50:56 +01:00
ft_set_cur_cell(table_, row_i, col_i);
2018-07-29 08:59:40 +02:00
}
2018-07-25 21:37:10 +02:00
2018-07-29 08:59:40 +02:00
/**
2018-11-21 18:50:56 +01:00
* Set table left margin.
2018-07-29 08:59:40 +02:00
*
* @param value
2018-11-21 18:50:56 +01:00
* Left margin.
2018-07-29 08:59:40 +02:00
* @return
2018-11-21 18:50:56 +01:00
* - true: Success; table property was changed.
* - false: In case of error.
2018-07-29 08:59:40 +02:00
*/
2018-11-21 18:50:56 +01:00
bool set_left_margin(unsigned value)
2018-07-29 08:59:40 +02:00
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_LEFT_MARGIN, value));
2018-07-29 08:59:40 +02:00
}
/**
2018-11-21 18:50:56 +01:00
* Set table top margin.
2018-07-29 08:59:40 +02:00
*
* @param value
2018-11-21 18:50:56 +01:00
* Top margin.
2018-07-29 08:59:40 +02:00
* @return
2018-11-21 18:50:56 +01:00
* - true: Success; table property was changed.
* - false: In case of error.
2018-07-29 08:59:40 +02:00
*/
2018-11-21 18:50:56 +01:00
bool set_top_margin(unsigned value)
2018-07-29 08:59:40 +02:00
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_TOP_MARGIN, value));
2018-07-29 08:59:40 +02:00
}
/**
2018-11-21 18:50:56 +01:00
* Set table right margin.
2018-07-29 08:59:40 +02:00
*
* @param value
2018-11-21 18:50:56 +01:00
* Right margin.
2018-07-29 08:59:40 +02:00
* @return
2018-11-21 18:50:56 +01:00
* - true: Success; table property was changed.
* - false: In case of error.
2018-07-29 08:59:40 +02:00
*/
2018-11-21 18:50:56 +01:00
bool set_right_margin(unsigned value)
2018-07-29 08:59:40 +02:00
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_RIGHT_MARGIN, value));
2018-07-29 08:59:40 +02:00
}
/**
2018-11-21 18:50:56 +01:00
* Set table bottom margin.
2018-07-29 08:59:40 +02:00
*
* @param value
2018-11-21 18:50:56 +01:00
* Bottom margin.
2018-07-29 08:59:40 +02:00
* @return
2018-11-21 18:50:56 +01:00
* - true: Success; table property was changed.
* - false: In case of error.
2018-07-29 08:59:40 +02:00
*/
2018-11-21 18:50:56 +01:00
bool set_bottom_margin(unsigned value)
2018-07-29 08:59:40 +02:00
{
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_set_tbl_prop(table_, FT_TPROP_BOTTOM_MARGIN, value));
2018-07-25 21:37:10 +02:00
}
2018-04-01 10:36:52 +02:00
private:
2018-11-21 18:50:56 +01:00
ft_table_t *table_;
mutable std::stringstream stream_;
friend class property_setter<table>;
2018-08-12 11:00:07 +02:00
public:
/* Iterators */
/* todo: implement chains like table[0][0] = table [0][1] = "somethings" */
2018-11-21 18:50:56 +01:00
class table_cell_iterator: public property_setter<table>
2018-08-12 11:00:07 +02:00
{
public:
2018-11-21 18:50:56 +01:00
table_cell_iterator(std::size_t row_idx, std::size_t coll_idx, table &tbl)
:property_setter(row_idx, coll_idx, tbl) {}
2018-08-12 11:00:07 +02:00
table_cell_iterator& operator=(const char *str)
{
2018-11-21 18:50:56 +01:00
ft_set_cur_cell(ps_table_.table_, ps_row_idx_, ps_coll_idx_);
ps_table_.write(str);
2018-08-12 11:00:07 +02:00
return *this;
}
};
2018-11-21 18:50:56 +01:00
class table_row_iterator: public property_setter<table>
2018-08-12 11:00:07 +02:00
{
public:
2018-11-21 18:50:56 +01:00
table_row_iterator(std::size_t row_idx, table &tbl)
:property_setter(row_idx, FT_ANY_COLUMN, tbl) {}
2018-08-12 11:00:07 +02:00
2018-09-01 19:10:38 +02:00
class table_cell_iterator
2018-08-12 11:00:07 +02:00
operator[](std::size_t coll_idx)
{
2018-11-21 18:50:56 +01:00
return table_cell_iterator(ps_row_idx_, coll_idx, ps_table_);
2018-08-12 11:00:07 +02:00
}
2018-11-21 18:50:56 +01:00
};
2018-08-12 11:00:07 +02:00
2018-11-21 18:50:56 +01:00
class table_column_iterator: public property_setter<table>
{
public:
table_column_iterator(std::size_t col_idx, table &tbl)
:property_setter(FT_ANY_ROW, col_idx, tbl) {}
2018-08-12 11:00:07 +02:00
};
2018-09-01 18:52:15 +02:00
class table_row_iterator
2018-08-12 11:00:07 +02:00
operator[](std::size_t row_idx)
{
return table_row_iterator(row_idx, *this);
}
2018-11-21 18:50:56 +01:00
class table_row_iterator
row(std::size_t row_idx)
{
return table_row_iterator(row_idx, *this);
}
class table_column_iterator
column(std::size_t col_idx)
{
return table_column_iterator(col_idx, *this);
}
2018-04-01 10:36:52 +02:00
};
2018-07-25 21:37:10 +02:00
2018-11-03 18:24:50 +01:00
/**
* Set default border style for all new formatted tables.
*
* @param style
* Pointer to border style.
* @return
* - True: Success; table border style was changed.
* - False: Error
*/
2018-11-21 18:50:56 +01:00
inline bool set_default_border_style(struct ft_border_style *style)
2018-11-03 18:24:50 +01:00
{
return FT_IS_SUCCESS(ft_set_default_border_style(style));
}
2018-07-25 21:37:10 +02:00
2018-04-01 10:36:52 +02:00
}
#endif // LIBFORT_HPP