diff --git a/example/main.cpp b/example/main.cpp index f3ed53e..b66f67c 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -8,14 +8,14 @@ int main() fort::Table table; // Fill table with data table << fort::header - << "Rank" << "Title" << "Year" << "Rating" << fort::endl - << "1" << "The Shawshank Redemption" << "1994" << "9.5" << fort::endl - << "2" << "12 Angry Men" << "1957" << "8.8" << fort::endl - << "3" << "It's a Wonderful Life" << "1946" << "8.6" << fort::endl + << "Rank" << "Title" << "Year" << "Rating" << fort::endr + << "1" << "The Shawshank Redemption" << "1994" << "9.5" << fort::endr + << "2" << "12 Angry Men" << "1957" << "8.8" << fort::endr + << "3" << "It's a Wonderful Life" << "1946" << "8.6" << fort::endr << fort::separator - << "4" << "2001: A Space Odyssey" << "1968" << "8.5" << fort::endl - << "5" << "Blade Runner" << "1982" << "8.1" << fort::endl - << fort::endl; + << "4" << "2001: A Space Odyssey" << "1968" << "8.5" << fort::endr + << "5" << "Blade Runner" << "1982" << "8.1" << fort::endr + << fort::endr; std::cout << table.to_string() << std::endl; } @@ -30,6 +30,8 @@ int main() table.write_ln("3", "It's a Wonderful Life", "1946", "8.6"); table.write_ln("4", "2001: A Space Odyssey", "1968", "8.5"); table.write_ln("5", "Blade Runner", "1982", "8.1"); + + table.set_border_style(FT_SOLID_STYLE); std::cout << table.to_string(); } return 0; diff --git a/lib/fort.c b/lib/fort.c index 42dedab..4687f80 100644 --- a/lib/fort.c +++ b/lib/fort.c @@ -2350,7 +2350,7 @@ struct ft_border_style *FT_FRAME_STYLE = (struct ft_border_style *) &FORT_FRAME -static void set_border_options_for_options(fort_table_options_t *options, struct ft_border_style *style) +static void set_border_options_for_options(fort_table_options_t *options, const struct ft_border_style *style) { if ((struct fort_border_style *)style == &FORT_BASIC_STYLE || (struct fort_border_style *)style == &FORT_BASIC2_STYLE @@ -2369,8 +2369,8 @@ static void set_border_options_for_options(fort_table_options_t *options, struct return; } - struct ft_border_chars *border_chs = &(style->border_chs); - struct ft_border_chars *header_border_chs = &(style->header_border_chs); + const struct ft_border_chars *border_chs = &(style->border_chs); + const struct ft_border_chars *header_border_chs = &(style->header_border_chs); #define BOR_CHARS options->border_style.border_chars #define H_BOR_CHARS options->border_style.header_border_chars @@ -2433,13 +2433,13 @@ static void set_border_options_for_options(fort_table_options_t *options, struct } -int ft_set_default_border_style(struct ft_border_style *style) +int ft_set_default_border_style(const struct ft_border_style *style) { set_border_options_for_options(&g_table_options, style); return FT_SUCCESS; } -int ft_set_border_style(ft_table_t *table, struct ft_border_style *style) +int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style) { assert(table); if (table->options == NULL) { diff --git a/lib/fort.h b/lib/fort.h index 5fc7e17..c05c0f1 100644 --- a/lib/fort.h +++ b/lib/fort.h @@ -563,7 +563,7 @@ int ft_add_separator(ft_table_t *table); * Formatted table. * @return * - The pointer to the string representation of formatted table, on success. - * - NULL on error with ft_errno set appropriately. + * - NULL on error. */ const char *ft_to_string(const ft_table_t *table); @@ -631,7 +631,7 @@ extern struct ft_border_style *FT_FRAME_STYLE; * - 0: Success; default border style was changed. * - (<0): In case of error */ -int ft_set_default_border_style(struct ft_border_style *style); +int ft_set_default_border_style(const struct ft_border_style *style); /** * Set border style for the table. @@ -644,7 +644,7 @@ int ft_set_default_border_style(struct ft_border_style *style); * - 0: Success; table border style was changed. * - (<0): In case of error */ -int ft_set_border_style(ft_table_t *table, struct ft_border_style *style); +int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style); diff --git a/lib/fort.hpp b/lib/fort.hpp index 8d2110c..2798e4f 100644 --- a/lib/fort.hpp +++ b/lib/fort.hpp @@ -54,13 +54,13 @@ private: }; const TableManipulator header(0); -const TableManipulator endl(1); +const TableManipulator endr(1); const TableManipulator separator(2); /** - * Table - here is a short description. + * Table - formatted table. * - * Here is detailed description. + * Table class is a C++ wrapper around struct ft_table. */ class Table { public: @@ -76,6 +76,13 @@ public: ft_destroy_table(table); } + /** + * Convert table to string representation. + * + * @return + * - String representation of formatted table, on success. + * - In case of error std::runtime_error is thrown. + */ std::string to_string() const { const char *str = ft_to_string(table); @@ -84,11 +91,37 @@ public: return str; } + /** + * 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. + */ const char *c_str() const { return ft_to_string(table); } + /** + * 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. + */ template Table &operator<<(const T &arg) { @@ -104,7 +137,7 @@ public: { if (arg.value == header.value) ft_set_cell_option(table, FT_CUR_ROW, FT_ANY_ROW, FT_COPT_ROW_TYPE, FT_ROW_HEADER); - else if (arg.value == endl.value) + else if (arg.value == endr.value) ft_ln(table); else if (arg.value == separator.value) ft_add_separator(table); @@ -235,6 +268,21 @@ public: #endif /* __cpp_variadic_templates */ + /** + * Set border style for the table. + * + * @param style + * Pointer to border style. + * @return + * - True: Success; table border style was changed. + * - False: Error + */ + bool set_border_style(struct ft_border_style *style) + { + return FT_IS_SUCCESS(ft_set_border_style(table, style)); + } + + private: ft_table_t *table; std::stringstream stream; diff --git a/src/fort_impl.c b/src/fort_impl.c index 2eec347..7fad7d5 100644 --- a/src/fort_impl.c +++ b/src/fort_impl.c @@ -809,7 +809,7 @@ struct ft_border_style *FT_FRAME_STYLE = (struct ft_border_style *) &FORT_FRAME -static void set_border_options_for_options(fort_table_options_t *options, struct ft_border_style *style) +static void set_border_options_for_options(fort_table_options_t *options, const struct ft_border_style *style) { if ((struct fort_border_style *)style == &FORT_BASIC_STYLE || (struct fort_border_style *)style == &FORT_BASIC2_STYLE @@ -828,8 +828,8 @@ static void set_border_options_for_options(fort_table_options_t *options, struct return; } - struct ft_border_chars *border_chs = &(style->border_chs); - struct ft_border_chars *header_border_chs = &(style->header_border_chs); + const struct ft_border_chars *border_chs = &(style->border_chs); + const struct ft_border_chars *header_border_chs = &(style->header_border_chs); #define BOR_CHARS options->border_style.border_chars #define H_BOR_CHARS options->border_style.header_border_chars @@ -892,13 +892,13 @@ static void set_border_options_for_options(fort_table_options_t *options, struct } -int ft_set_default_border_style(struct ft_border_style *style) +int ft_set_default_border_style(const struct ft_border_style *style) { set_border_options_for_options(&g_table_options, style); return FT_SUCCESS; } -int ft_set_border_style(ft_table_t *table, struct ft_border_style *style) +int ft_set_border_style(ft_table_t *table, const struct ft_border_style *style) { assert(table); if (table->options == NULL) {