libfort/lib/fort.hpp

1285 lines
34 KiB
C++
Raw Permalink Normal View History

2018-05-05 17:38:45 +02:00
/*
libfort
MIT License
2020-02-02 12:26:05 +01:00
Copyright (c) 2017 - 2020 Seleznev Anton
2018-05-05 17:38:45 +02:00
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 <iomanip>
#include <sstream>
2018-04-01 10:36:52 +02:00
#include <string>
2018-04-01 12:27:02 +02:00
#include <stdexcept>
#include <type_traits>
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-12-01 08:39:25 +01:00
/**
* Alignment of cell content.
*/
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-12-01 08:39:25 +01:00
/**
* Type of table row. Determines appearance of row.
*/
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
};
/**
* Adding strategy.
*
* Determines what happens with old content if current cell is not empty after
* adding data to it. Default strategy is 'replace'.
*/
enum class add_strategy {
replace = FT_STRATEGY_REPLACE,
insert = FT_STRATEGY_INSERT
};
2018-12-01 08:39:25 +01:00
/**
* Colors.
*/
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-12-01 08:39:25 +01:00
/**
* Text styles.
*/
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
};
2019-09-28 10:02:34 +02:00
enum class table_type {
character,
#ifdef FT_HAVE_UTF8
utf8
#endif /* FT_HAVE_UTF8 */
};
2018-12-01 08:39:25 +01:00
/**
* Table manipulator.
*
* Table manipulators can be used to change current cell and change appearance
* of cells.
*/
2019-09-28 10:02:34 +02: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)
2019-09-28 10:02:34 +02:00
: value(i)
2018-04-01 10:36:52 +02:00
{
}
2019-09-28 10:02:34 +02:00
template <table_type TT>
2018-11-21 18:50:56 +01:00
friend class table;
2018-04-01 10:36:52 +02:00
private:
int value;
};
2018-12-01 08:39:25 +01:00
/**
* Table manipulator to set current row as a header.
*/
2018-11-21 18:50:56 +01:00
const table_manipulator header(0);
2018-12-01 08:39:25 +01:00
/**
* Table manipulator to move current cell to the first cell of the next row.
*/
2018-11-21 18:50:56 +01:00
const table_manipulator endr(1);
2018-12-01 08:39:25 +01:00
/**
* Table manipulator to add separator to the table.
*/
const table_manipulator separator(2);
2018-11-21 18:50:56 +01:00
// Utility functions for internal library usage.
namespace detail
{
template <typename T>
constexpr bool is_stream_manipulator_impl() noexcept
{
using Tdec = typename std::decay<T>::type;
// Manipulators for integral types
return std::is_same<Tdec, typename std::decay<decltype(std::dec)>::type>::value
|| std::is_same<Tdec, typename std::decay<decltype(std::hex)>::type>::value
|| std::is_same<Tdec, typename std::decay<decltype(std::oct)>::type>::value
// Floating-point manipulators
|| std::is_same<Tdec, typename std::decay<decltype(std::fixed)>::type>::value
|| std::is_same<Tdec, typename std::decay<decltype(std::scientific)>::type>::value
// Misc
|| std::is_same<Tdec, typename std::decay<decltype(std::setbase(0))>::type>::value
|| std::is_same<Tdec, typename std::decay<decltype(std::setfill('\0'))>::type>::value
|| std::is_same<Tdec, typename std::decay<decltype(std::setprecision(0))>::type>::value
|| std::is_same<Tdec, typename std::decay<decltype(std::setw(0))>::type>::value;
}
}
/**
* Utility function that is used internally by the library to check if argument
* passed to operator<< is a manipulator. In case default behaviour is not
* enough write custom specialization of this function.
*/
template <typename T>
constexpr bool is_stream_manipulator() noexcept
{
return detail::is_stream_manipulator_impl<T>();
}
2018-12-01 08:39:25 +01:00
/**
* Property owner.
*
* property_owner is a base class for all objects (table, row, column, cell) for
* which user can specify properties.
*/
2018-11-21 18:50:56 +01:00
template <typename table>
2019-09-28 10:02:34 +02:00
class property_owner
{
2018-11-21 18:50:56 +01:00
public:
2018-12-01 08:39:25 +01:00
property_owner(std::size_t row_idx, std::size_t coll_idx, table *tbl, bool def = false)
2019-09-28 10:02:34 +02:00
: ps_row_idx_(row_idx), ps_coll_idx_(coll_idx),
2018-11-23 19:02:08 +01:00
ps_table_(tbl), set_default_properties_(def) {}
2018-11-21 18:50:56 +01:00
/**
* 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)
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_MIN_WIDTH, value);
2018-11-21 18:50:56 +01:00
}
/**
* 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(fort::text_align value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_TEXT_ALIGN, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
/**
* 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)
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_TOP_PADDING, value);
2018-11-21 18:50:56 +01:00
}
/**
* 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)
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_BOTTOM_PADDING, value);
2018-11-21 18:50:56 +01:00
}
/**
* 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)
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_LEFT_PADDING, value);
2018-11-21 18:50:56 +01:00
}
/**
* 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)
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_RIGHT_PADDING, value);
2018-11-21 18:50:56 +01:00
}
/**
* 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(fort::row_type value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_ROW_TYPE, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
/**
* 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)
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_EMPTY_STR_HEIGHT, value);
2018-11-21 18:50:56 +01:00
}
/**
* 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(fort::color value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_CONT_FG_COLOR, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
/**
* 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_bg_color(fort::color value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_CELL_BG_COLOR, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
/**
* 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(fort::color value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_CONT_BG_COLOR, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
/**
* 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_text_style(fort::text_style value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_CELL_TEXT_STYLE, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
/**
* 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(fort::text_style value)
2018-11-21 18:50:56 +01:00
{
2018-11-23 19:02:08 +01:00
return set_property(FT_CPROP_CONT_TEXT_STYLE, static_cast<int>(value));
2018-11-21 18:50:56 +01:00
}
protected:
std::size_t ps_row_idx_;
std::size_t ps_coll_idx_;
2018-11-23 19:02:08 +01:00
table *ps_table_;
bool set_default_properties_;
bool set_property(uint32_t property, int value)
{
int status;
if (set_default_properties_) {
status = ft_set_default_cell_prop(property, value);
} else {
status = ft_set_cell_prop(ps_table_->table_, ps_row_idx_, ps_coll_idx_, property, value);
}
return FT_IS_SUCCESS(status);
}
2018-11-21 18:50:56 +01:00
};
2018-04-01 10:36:52 +02:00
2019-09-28 10:02:34 +02:00
2018-05-05 17:38:45 +02:00
/**
2018-12-01 08:39:25 +01:00
* Formatted table.
2018-05-05 17:38:45 +02:00
*
2019-09-28 10:02:34 +02:00
* Table template class is a C++ wrapper around struct {@link ft_table}.
* Template parameter is {@link table_type}. Useful instantiations of table
* template class are {@link char_table} and {@link utf8_table}.
2018-05-05 17:38:45 +02:00
*/
2019-09-28 10:02:34 +02:00
template <table_type TT = table_type::character>
class table: public property_owner<table<TT>>
{
/**
* Utility types.
*/
using table_t = table<TT>;
using property_owner_t = property_owner<table_t>;
2018-04-01 10:36:52 +02:00
public:
2018-12-01 08:39:25 +01:00
/**
* Default constructor.
*/
2018-11-21 18:50:56 +01:00
table()
2019-09-28 10:02:34 +02:00
: property_owner_t(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)
2020-02-23 09:54:59 +01:00
throw std::bad_alloc();
2018-04-01 10:36:52 +02:00
}
2018-12-01 08:39:25 +01:00
/**
* Destructor.
*/
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
*/
2019-09-28 10:02:34 +02:00
table(const table &tbl)
: property_owner_t(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)
2020-02-23 09:54:59 +01:00
throw std::runtime_error("Error during table copy");
2018-11-02 22:16:20 +01:00
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.
*/
2019-09-28 10:02:34 +02:00
table(table &&tbl)
: property_owner_t(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
*/
2019-09-28 10:02:34 +02: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)
2020-02-23 09:54:59 +01:00
throw std::runtime_error("Error during table copy");
2018-11-02 22:16:20 +01:00
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.
*/
2019-09-28 10:02:34 +02: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
{
2019-09-28 10:02:34 +02:00
const char *str = c_str();
2018-04-01 10:36:52 +02:00
if (str == NULL)
2020-02-23 09:54:59 +01:00
throw std::runtime_error("Error during table to string conversion");
2018-04-01 10:36:52 +02:00
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
{
2019-09-28 10:02:34 +02:00
#ifdef FT_HAVE_UTF8
return (TT == table_type::character)
? ft_to_string(table_)
: (const char *)ft_to_u8string(table_);
#else
2018-11-21 18:50:56 +01:00
return ft_to_string(table_);
2019-09-28 10:02:34 +02:00
#endif
2018-04-01 10:36:52 +02:00
}
2018-07-22 21:52:55 +02:00
/**
2018-12-22 09:35:55 +01:00
* Write provided object to the table.
2018-07-22 21:52:55 +02:00
*
* 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
{
constexpr bool is_manip = fort::is_stream_manipulator<typename std::decay<T>::type>();
2018-11-21 18:50:56 +01:00
stream_ << arg;
if (stream_.tellp() >= 0 && !is_manip) {
2019-09-28 10:02:34 +02:00
#ifdef FT_HAVE_UTF8
if (TT == table_type::character) {
ft_nwrite(table_, 1, stream_.str().c_str());
} else {
ft_u8nwrite(table_, 1, (const void *)stream_.str().c_str());
}
#else
2018-11-21 18:50:56 +01:00
ft_nwrite(table_, 1, stream_.str().c_str());
2019-09-28 10:02:34 +02:00
#endif
2018-11-21 18:50:56 +01:00
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-12-01 08:39:25 +01:00
/**
* Write string to the the table.
*
* Write specified string to the current cell.
*
* @param str
* String to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-08 14:48:15 +02:00
bool write(const char *str)
{
2019-09-28 10:02:34 +02:00
#ifdef FT_HAVE_UTF8
if (TT == table_type::character) {
return FT_IS_SUCCESS(ft_write(table_, str));
} else {
return FT_IS_SUCCESS(ft_u8write(table_, (const void *)str));
}
#else
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_write(table_, str));
2019-09-28 10:02:34 +02:00
#endif
2018-04-08 14:48:15 +02:00
}
2018-12-01 08:39:25 +01:00
/**
* Write string to the the table and go to the next line.
*
* Write specified string to the current cell and move current position to
* the first cell of the next line(row).
*
* @param str
* String to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-08 14:48:15 +02:00
bool write_ln(const char *str)
{
2019-09-28 10:02:34 +02:00
#ifdef FT_HAVE_UTF8
if (TT == table_type::character) {
return FT_IS_SUCCESS(ft_write_ln(table_, str));
} else {
return FT_IS_SUCCESS(ft_u8write_ln(table_, str));
}
#else
2018-11-21 18:50:56 +01:00
return FT_IS_SUCCESS(ft_write_ln(table_, str));
2019-09-28 10:02:34 +02:00
#endif
2018-04-08 14:48:15 +02:00
}
2018-12-01 08:39:25 +01:00
/**
* Write string to the the table.
*
* Write specified string to the current cell.
*
* @param str
* String to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-08 14:48:15 +02:00
bool write(const std::string &str)
{
return write(str.c_str());
}
2018-12-01 08:39:25 +01:00
/**
* Write string to the the table and go to the next line.
*
* Write specified string to the current cell and move current position to
* the first cell of the next line(row).
*
* @param str
* String to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-08 14:48:15 +02:00
bool write_ln(const std::string &str)
{
return write_ln(str.c_str());
}
#ifdef __cpp_variadic_templates
2018-12-01 08:39:25 +01:00
/**
* Write strings to the table.
*
* Write specified strings to the same number of consecutive cells in the
* current row.
*
* @param str
* String to write.
* @param strings
* Strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-08 14:48:15 +02:00
template <typename T, typename ...Ts>
2018-12-01 08:39:25 +01:00
bool write(const T &str, const Ts &...strings)
2018-04-08 14:48:15 +02:00
{
2018-12-01 08:39:25 +01:00
return write(str) && write(strings...);
2018-04-08 14:48:15 +02:00
}
2018-12-01 08:39:25 +01:00
/**
* Write strings to the table and go to the next line.
*
* Write specified strings to the same number of consecutive cells in the
* current row and move current position to the first cell of the next
* line(row).
*
* @param str
* String to write.
* @param strings
* Strings to write.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-04-08 14:48:15 +02:00
template <typename T, typename ...Ts>
2018-12-01 08:39:25 +01:00
bool write_ln(const T &str, const Ts &...strings)
2018-04-08 14:48:15 +02:00
{
2018-12-01 08:39:25 +01:00
return write(str) && write_ln(strings...);
2018-04-08 14:48:15 +02:00
}
#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-12-01 08:39:25 +01:00
/**
* Write elements from range to the table.
*
* Write objects from range to consecutive cells in the current row.
*
* @param first, last
* Range of elements.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-08-09 21:15:05 +02:00
template <typename InputIt>
2018-12-01 08:39:25 +01:00
bool range_write(InputIt first, InputIt last)
2018-08-09 21:15:05 +02:00
{
while (first != last) {
*this << *first;
++first;
}
return true;
}
2018-12-01 08:39:25 +01:00
/**
* Write elements from range to the table and go to the next line.
*
* Write objects from range to consecutive cells in the current row and
* move current position to the first cell of the next line(row).
*
* @param first, last
* Range of elements.
* @return
* - 0: Success; data were written
* - (<0): In case of error
*/
2018-08-09 21:15:05 +02:00
template <typename InputIt>
2018-12-01 08:39:25 +01:00
bool range_write_ln(InputIt first, InputIt last)
2018-08-09 21:15:05 +02:00
{
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
*/
2019-04-27 15:00:20 +02:00
bool set_border_style(const 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
}
/**
* Set table adding strategy.
*
* @param value
* Adding strategy.
* @return
* - true: Success; table property was changed.
* - false: In case of error.
*/
bool set_adding_strategy(fort::add_strategy value)
{
return FT_IS_SUCCESS(ft_set_tbl_prop(table_,
FT_TPROP_ADDING_STRATEGY,
static_cast<int>(value)));
}
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_;
2018-12-01 08:39:25 +01:00
friend class property_owner<table>;
2018-11-21 18:50:56 +01:00
2018-08-12 11:00:07 +02:00
public:
/* Iterators */
/* todo: implement chains like table[0][0] = table [0][1] = "somethings" */
2018-12-22 09:48:57 +01:00
/**
* Table cell.
*/
2019-09-28 10:02:34 +02:00
class table_cell: public property_owner_t
2018-08-12 11:00:07 +02:00
{
2019-09-28 10:02:34 +02:00
using property_owner_t::ps_coll_idx_;
using property_owner_t::ps_row_idx_;
using property_owner_t::ps_table_;
using property_owner_t::set_default_properties_;
2018-08-12 11:00:07 +02:00
public:
2018-12-22 09:06:10 +01:00
table_cell(std::size_t row_idx, std::size_t coll_idx, table &tbl)
2019-09-28 10:02:34 +02:00
: property_owner_t(row_idx, coll_idx, &tbl) {}
2018-08-12 11:00:07 +02:00
2019-09-28 10:02:34 +02:00
table_cell &operator=(const char *str)
2018-08-12 11:00:07 +02:00
{
2018-11-23 19:02:08 +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-12-01 08:39:25 +01:00
2019-09-28 10:02:34 +02:00
table_cell &operator=(const std::string &str)
2018-12-01 08:39:25 +01:00
{
return operator=(str.c_str());
}
2018-12-22 09:06:10 +01:00
/**
* 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)
{
if (set_default_properties_)
return false;
return FT_IS_SUCCESS(ft_set_cell_span(ps_table_->table_, ps_row_idx_, ps_coll_idx_, hor_span));
}
2018-08-12 11:00:07 +02:00
};
2018-12-22 09:48:57 +01:00
/**
* Table row.
*/
2019-09-28 10:02:34 +02:00
class table_row: public property_owner_t
2018-08-12 11:00:07 +02:00
{
2019-09-28 10:02:34 +02:00
using property_owner_t::ps_row_idx_;
using property_owner_t::ps_table_;
2018-08-12 11:00:07 +02:00
public:
2018-12-22 09:06:10 +01:00
table_row(std::size_t row_idx, table &tbl)
2019-09-28 10:02:34 +02:00
: property_owner_t(row_idx, FT_ANY_COLUMN, &tbl) {}
2018-08-12 11:00:07 +02:00
2018-12-22 09:06:10 +01:00
class table_cell
2019-09-28 10:02:34 +02:00
operator[](std::size_t coll_idx)
2018-08-12 11:00:07 +02:00
{
2018-12-22 09:06:10 +01:00
return table_cell(ps_row_idx_, coll_idx, *ps_table_);
2018-08-12 11:00:07 +02:00
}
void erase()
{
if (FT_IS_ERROR(ft_erase_range(ps_table_->table_,
property_owner_t::ps_row_idx_, 0,
2020-01-21 17:03:57 +01:00
property_owner_t::ps_row_idx_, FT_MAX_COL_INDEX))) {
throw std::runtime_error("Failed to erase row");
}
}
2018-11-21 18:50:56 +01:00
};
2018-08-12 11:00:07 +02:00
2018-12-22 09:48:57 +01:00
/**
* Table column.
*/
2019-09-28 10:02:34 +02:00
class table_column: public property_owner_t
2018-11-21 18:50:56 +01:00
{
using property_owner_t::ps_coll_idx_;
using property_owner_t::ps_table_;
2018-11-21 18:50:56 +01:00
public:
2018-12-22 09:06:10 +01:00
table_column(std::size_t col_idx, table &tbl)
2019-09-28 10:02:34 +02:00
: property_owner_t(FT_ANY_ROW, col_idx, &tbl) {}
void erase()
{
if (FT_IS_ERROR(ft_erase_range(ps_table_->table_,
0, ps_coll_idx_,
2020-01-21 17:03:57 +01:00
FT_MAX_ROW_INDEX, ps_coll_idx_))) {
throw std::runtime_error("Failed to erase column");
}
}
2018-11-23 19:02:08 +01:00
};
/**
2020-01-19 20:24:17 +01:00
* Range of cells.
*
* @note: at the moment function of propery owener will work only on the
* top left cell.
* @todo: Implement their work on the whole range.
*/
class cell_range: public property_owner_t
{
using property_owner_t::ps_coll_idx_;
using property_owner_t::ps_row_idx_;
using property_owner_t::ps_table_;
public:
2020-01-19 20:24:17 +01:00
cell_range(size_t top_left_row, size_t top_left_col,
size_t bottom_right_row, size_t bottom_right_col,
table &tbl)
: property_owner_t(top_left_row, top_left_col, &tbl),
bottom_right_row_(bottom_right_row),
bottom_right_col_(bottom_right_col)
{}
void erase()
{
if (FT_IS_ERROR(ft_erase_range(ps_table_->table_,
2020-01-19 20:24:17 +01:00
ps_row_idx_, ps_coll_idx_,
bottom_right_row_, bottom_right_col_))) {
throw std::runtime_error("Failed to erase column");
}
}
2020-01-19 20:24:17 +01:00
private:
std::size_t bottom_right_row_;
std::size_t bottom_right_col_;
};
2019-09-28 10:02:34 +02:00
class default_properties: public property_owner_t
2018-11-23 19:02:08 +01:00
{
public:
default_properties(table *tbl)
2019-09-28 10:02:34 +02:00
: property_owner_t(FT_ANY_ROW, FT_ANY_COLUMN, tbl, true) {}
2018-08-12 11:00:07 +02:00
};
2018-12-22 09:06:10 +01:00
class table_row
2019-09-28 10:02:34 +02:00
operator[](std::size_t row_idx)
2018-08-12 11:00:07 +02:00
{
2018-12-22 09:06:10 +01:00
return table_row(row_idx, *this);
2018-08-12 11:00:07 +02:00
}
2018-11-21 18:50:56 +01:00
2018-12-22 09:35:55 +01:00
/**
* Get cell.
*
* @param row_idx
* Row index.
* @param col_idx
* Column index.
* @return
* table_cell object.
*/
class table_cell
2019-09-28 10:02:34 +02:00
cell(std::size_t row_idx, std::size_t col_idx)
2018-12-22 09:35:55 +01:00
{
return (*this)[row_idx][col_idx];
}
/**
* Get column number of the current cell.
*
* @return
* Column number of the current cell.
*/
2019-08-26 13:26:41 +02:00
size_t
cur_col() const noexcept
2019-08-26 13:26:41 +02:00
{
return ft_cur_col(table_);
}
/**
* Get row number of the current cell.
*
* @return
* Row number of the current cell.
*/
2019-08-26 13:26:41 +02:00
size_t
cur_row() const noexcept
2019-08-26 13:26:41 +02:00
{
return ft_cur_row(table_);
}
/**
* Check if table is empty.
*
* @return
* true - table is empty
* false - some data has been inserted
*/
bool
is_empty() const noexcept
{
return ft_is_empty(table_);
}
2020-01-10 19:22:41 +01:00
/**
* Get number of rows in the table.
*
* @return
* Number of rows in the table.
*/
std::size_t
row_count() const noexcept
{
return ft_row_count(table_);
}
2020-11-30 18:09:09 +01:00
/**
* Get number of columns in the table.
*
* @return
* Number of columns in the table.
*/
std::size_t
col_count() const noexcept
{
return ft_col_count(table_);
}
/**
* Get current cell.
*
* @return
* Current cell.
*/
2019-08-26 13:26:41 +02:00
class table_cell
2019-09-28 10:02:34 +02:00
cur_cell()
2019-08-26 13:26:41 +02:00
{
return cell(cur_row(), cur_col());
}
2018-12-22 09:35:55 +01:00
/**
* Get row.
*
* @param row_idx
* Row index.
* @return
* table_row object.
*/
2018-12-22 09:06:10 +01:00
class table_row
2019-09-28 10:02:34 +02:00
row(std::size_t row_idx)
2018-11-21 18:50:56 +01:00
{
2018-12-22 09:06:10 +01:00
return table_row(row_idx, *this);
2018-11-21 18:50:56 +01:00
}
2018-12-22 09:35:55 +01:00
/**
* Get column.
*
* @param col_idx
* Column index.
* @return
* table_column object.
*/
2018-12-22 09:06:10 +01:00
class table_column
2019-09-28 10:02:34 +02:00
column(std::size_t col_idx)
2018-11-21 18:50:56 +01:00
{
2018-12-22 09:06:10 +01:00
return table_column(col_idx, *this);
2018-11-21 18:50:56 +01:00
}
2020-01-19 20:24:17 +01:00
/**
* Get range of cells.
*
* @param col_idx
* Column index.
* @return
* table_column object.
*/
class cell_range
range(std::size_t top_left_row, std::size_t top_left_col,
std::size_t bottom_right_row, std::size_t bottom_right_col)
{
return cell_range(top_left_row, top_left_col,
bottom_right_row, bottom_right_col,
*this);
}
2018-11-23 19:02:08 +01:00
static class default_properties
2019-09-28 10:02:34 +02:00
default_props()
2018-11-23 19:02:08 +01:00
{
return default_properties(NULL);
}
2018-04-01 10:36:52 +02:00
};
2019-09-28 10:02:34 +02:00
/**
* Formatted table containing common char content.
*
* Content of the table is treated as a string where each byte represesents a
* character. Should work for ascii characters. In case of usage of different
* international symbols it is recommended to use {@link utf8_table}.
*/
using char_table = table<table_type::character>;
#ifdef FT_HAVE_UTF8
/**
* Formatted table containing utf-8 content.
*/
using utf8_table = table<table_type::utf8>;
#endif
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
2019-09-28 10:02:34 +02:00
} // namespace fort
2018-04-01 10:36:52 +02:00
#endif // LIBFORT_HPP