diff --git a/example/main.cpp b/example/main.cpp index 9978853..c4237c4 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -4,18 +4,33 @@ int main() { - fort::FTable table; + { + fort::FTable 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 + << fort::separator + << "4" << "2001: A Space Odyssey" << "1968" << "8.5" << fort::endl + << "5" << "Blade Runner" << "1982" << "8.1" << fort::endl + << fort::endl; + std::cout << table.to_string() << std::endl; + } - // 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 - << fort::separator - << "4" << "2001: A Space Odyssey" << "1968" << "8.5" << fort::endl - << "5" << "Blade Runner" << "1982" << "8.1" << fort::endl; - std::cout << table.to_string(); + { + fort::FTable table; + // Fill table with data + table << fort::header; + table.write_ln("Rank", "Title", "Year", "Rating"); + table.write_ln("1", "The Shawshank Redemption", "1994", "9.5"); + table.write_ln("2", "12 Angry Men", "1957", "8.8"); + 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"); + std::cout << table.to_string(); + } return 0; } diff --git a/include/fort.h b/include/fort.h index 0bc589f..ff1b663 100644 --- a/include/fort.h +++ b/include/fort.h @@ -107,6 +107,17 @@ SOFTWARE. #endif +/***************************************************************************** + * RETURN CODES + * ***************************************************************************/ +typedef int fort_status_t; +#define FT_SUCCESS 0 +#define FT_MEMORY_ERROR -1 +#define FT_ERROR -2 +#define FT_EINVAL -3 +#define IS_SUCCESS(arg) ((arg) >= 0) +#define IS_ERROR(arg) ((arg) < 0) + /* * Wchar support diff --git a/include/fort.hpp b/include/fort.hpp index 539afe6..7ce757f 100644 --- a/include/fort.hpp +++ b/include/fort.hpp @@ -54,9 +54,11 @@ public: template FTable &operator<<(const T &arg) { - std::stringstream stream; stream << arg; - ft_nwrite(table, 1, stream.str().c_str()); + if (stream.tellp()) { + ft_nwrite(table, 1, stream.str().c_str()); + stream = std::stringstream{}; + } return *this; } @@ -71,10 +73,44 @@ public: return *this; } + bool write(const char *str) + { + return IS_SUCCESS(ft_write(table, str)); + } + + bool write_ln(const char *str) + { + return IS_SUCCESS(ft_write_ln(table, str)); + } + + bool write(const std::string &str) + { + return write(str.c_str()); + } + + bool write_ln(const std::string &str) + { + return write_ln(str.c_str()); + } + + template + bool write(const T &arg, const Ts &...args) + { + return write(arg) && write(args...); + } + + template + bool write_ln(const T &arg, const Ts &...args) + { + if (sizeof...(args) == 0) + return write_ln(arg); + return write(arg) && write_ln(args...); + } private: FTABLE *table; + std::stringstream stream; }; } diff --git a/src/fort_impl.h b/src/fort_impl.h index aa122e3..95f06ed 100644 --- a/src/fort_impl.h +++ b/src/fort_impl.h @@ -57,16 +57,6 @@ enum F_BOOL #define SYS_LOG_ERROR(...) -/***************************************************************************** - * RETURN CODES - * ***************************************************************************/ -typedef int fort_status_t; -#define FT_SUCCESS 0 -#define FT_MEMORY_ERROR -1 -#define FT_ERROR -2 -#define FT_EINVAL -3 -#define IS_SUCCESS(arg) ((arg) >= 0) -#define IS_ERROR(arg) ((arg) < 0) /***************************************************************************** * DEFAULT_SIZES