From e92647418635deedd6bfaf8853d0b980898c8aca Mon Sep 17 00:00:00 2001 From: flyingsl0ths Date: Wed, 15 Jun 2022 15:32:17 -0500 Subject: [PATCH] Resolves #3 --- example/forest-example.cpp | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/example/forest-example.cpp b/example/forest-example.cpp index d9ee70d..daa6a91 100644 --- a/example/forest-example.cpp +++ b/example/forest-example.cpp @@ -1,12 +1,65 @@ #include +#include +#include #include +#include constexpr auto foo = forest::literal<64>("hello world"); +void with_temp_file(std::function const fn) { + constexpr auto tf_name = "temp.txt"; + + auto temp_file = std::fopen(tf_name, "w+"); + + if (temp_file == nullptr) { return; } + + fn(temp_file); + + std::fclose(temp_file); + temp_file = nullptr; + std::remove(tf_name); +} + int main() { std::cout << foo << '\n'; + + std::cout << forest::literal<71>("I copy study code from stackoverflow.com") << '\n'; + + // TERMINAL SUPPORT MAY VARY + std::cout << forest::literal<26>("Hey listen!") << '\n'; + + std::cout << forest::literal<21>("Loading...") << '\n'; + // + + std::cout << forest::literal<70>("GO Team!") << '\n'; + auto str = std::string("forest v"); str += forest::version_v; str += "\n"; forest::print(str); + + // Uncomment to clear everything written so far + // std::cout << forest::literal<24>("Goodbye..") << '\n'; + + std::string ft{}; + forest::format_to(std::back_inserter(ft), "Hello from the string!"); + std::cout << ft << '\n'; + + with_temp_file([](std::FILE* const tf) { + forest::print_to(tf, "Hello from the file!"); + + // Required when printing to FILE* that is not stdout/stderr + // As forest::print_to leaves the file pointer at the end of the file + // which is the end of the written text + // Can also be a call to std::fseek(tf, 0, SEEK_SET) + std::rewind(tf); + + constexpr int BUFFER_SIZE{256}; + char buffer[BUFFER_SIZE]{}; + while (std::fgets(buffer, BUFFER_SIZE, tf) != nullptr) { std::cout << buffer << '\n'; } + }); + + forest::print_to(stdout, "Hello from stdout\n"); + + forest::print_to(stderr, "Hello from stderr!\n"); }