From 2859666708e80ba5fa19d33648d5142708c56c0f Mon Sep 17 00:00:00 2001 From: Alexander Shaduri Date: Wed, 3 Mar 2021 18:15:05 +0400 Subject: [PATCH] Make all examples use main_tools.h exception wrapper. --- .../examples/example_smartctl_executor.cpp | 48 ++-- .../examples/example_smartctl_parser.cpp | 2 +- src/applib/examples/example_spawn.cpp | 89 +++--- .../examples/example_storage_detector.cpp | 2 + src/hz/examples/example_format_unit.cpp | 53 ++-- src/hz/examples/example_string_algo.cpp | 111 ++++---- src/hz/examples/example_string_num.cpp | 253 +++++++++--------- src/libdebug/examples/example_libdebug.cpp | 22 +- src/rconfig/examples/example_rconfig.cpp | 52 ++-- 9 files changed, 331 insertions(+), 301 deletions(-) diff --git a/src/applib/examples/example_smartctl_executor.cpp b/src/applib/examples/example_smartctl_executor.cpp index 0e0a413..14f2e6a 100644 --- a/src/applib/examples/example_smartctl_executor.cpp +++ b/src/applib/examples/example_smartctl_executor.cpp @@ -9,47 +9,51 @@ Copyright: /// \weakgroup applib_examples /// @{ +// #include "applib/smartctl_executor_gui.h" +#include "applib/smartctl_executor.h" + #include "local_glibmm.h" #include #include // to avoid throw() macro errors. #include -// #include "applib/smartctl_executor_gui.h" -#include "applib/smartctl_executor.h" +#include "hz/main_tools.h" /// Main function of the test int main(int argc, char** argv) { - Gtk::Main m(argc, argv); + return hz::main_exception_wrapper([&argc, &argv]() + { + Gtk::Main m(argc, argv); - // NOTE: Don't use long options (e.g. --info). Use short ones (e.g. -i), - // because long options may be unsupported on some platforms. + // NOTE: Don't use long options (e.g. --info). Use short ones (e.g. -i), + // because long options may be unsupported on some platforms. -// SmartctlExecutor ex("smartctl", "-i /dev/sda"); -// SmartctlExecutorGui ex("ls", "-l --color=no -R /dev"); -// SmartctlExecutorGui ex("lsa", "-1 --color=no /sys/block"); -// SmartctlExecutorGui ex("../../../0test_binary.sh", ""); - SmartctlExecutor ex("../../../0test_binary.sh", ""); + // SmartctlExecutor ex("smartctl", "-i /dev/sda"); + // SmartctlExecutorGui ex("ls", "-l --color=no -R /dev"); + // SmartctlExecutorGui ex("lsa", "-1 --color=no /sys/block"); + // SmartctlExecutorGui ex("../../../0test_binary.sh", ""); + SmartctlExecutor ex("../../../0test_binary.sh", ""); - ex.execute(); + ex.execute(); - std::string out_str = ex.get_stdout_str(); -// std::cout << "OUT:\n" << out_str << "\n\n"; - std::cerr << "OUT SIZE: " << out_str.size() << "\n"; + std::string out_str = ex.get_stdout_str(); + // std::cout << "OUT:\n" << out_str << "\n\n"; + std::cerr << "OUT SIZE: " << out_str.size() << "\n"; - std::cerr << "STDERR:\n" << ex.get_stderr_str() << "\n"; + std::cerr << "STDERR:\n" << ex.get_stderr_str() << "\n"; - std::cerr << "ERROR MSG:\n"; - std::cerr << ex.get_error_msg(); + std::cerr << "ERROR MSG:\n"; + std::cerr << ex.get_error_msg(); - // execute it second time - ex.execute(); + // execute it second time + ex.execute(); -// m.run(); - - return 0; + // m.run(); + return EXIT_SUCCESS; + }); } diff --git a/src/applib/examples/example_smartctl_parser.cpp b/src/applib/examples/example_smartctl_parser.cpp index a3df5d2..1d007a6 100644 --- a/src/applib/examples/example_smartctl_parser.cpp +++ b/src/applib/examples/example_smartctl_parser.cpp @@ -26,7 +26,7 @@ Copyright: /// Main function of the test -int main(int argc, char** argv) +int main(int argc, char* argv[]) { if (argc < 2) { std::cout << "Usage: " << argv[0] << " \n"; diff --git a/src/applib/examples/example_spawn.cpp b/src/applib/examples/example_spawn.cpp index 64ed1ee..aaf6781 100644 --- a/src/applib/examples/example_spawn.cpp +++ b/src/applib/examples/example_spawn.cpp @@ -12,69 +12,66 @@ Copyright: #include #include +#include "hz/main_tools.h" + /// Main function of the test -int main(int argc, char** argv) +int main() { - GPid pid; - int fd_stdout = 0, fd_stderr = 0; + return hz::main_exception_wrapper([]() + { + GPid pid = {}; + int fd_stdout = 0, fd_stderr = 0; + std::string cmd = "iexplore"; + // std::vector child_argv = Glib::shell_parse_argv(cmd); - std::string cmd = "iexplore"; -// std::vector child_argv = Glib::shell_parse_argv(cmd); + gchar* curr_dir = g_get_current_dir(); - gchar* curr_dir = g_get_current_dir(); + int argcp = 0; // number of args + gchar** argvp = nullptr; // args vector + GError* shell_error = nullptr; + g_shell_parse_argv(cmd.c_str(), &argcp, &argvp, &shell_error); + GError* spawn_error = nullptr; - int argcp = 0; // number of args - gchar** argvp = 0; // args vector - GError* shell_error = 0; - g_shell_parse_argv(cmd.c_str(), &argcp, &argvp, &shell_error); + g_spawn_async_with_pipes(curr_dir, argvp, nullptr, + GSpawnFlags(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), + nullptr, nullptr, // child setup function + &pid, nullptr, &fd_stdout, &fd_stderr, &spawn_error); + g_free(curr_dir); - GError* spawn_error = 0; + g_strfreev(argvp); - g_spawn_async_with_pipes(curr_dir, argvp, nullptr, - GSpawnFlags(G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD), - nullptr, nullptr, // child setup function - &pid, 0, &fd_stdout, &fd_stderr, &spawn_error); + #ifdef _WIN32 + GIOChannel* channel_stdout = g_io_channel_win32_new_fd(fd_stdout); + GIOChannel* channel_stderr = g_io_channel_win32_new_fd(fd_stderr); + #else + GIOChannel* channel_stdout = g_io_channel_unix_new(fd_stdout); + GIOChannel* channel_stderr = g_io_channel_unix_new(fd_stderr); + #endif - g_free(curr_dir); + // blocking writes if the pipe is full helps for small-pipe systems (see man 7 pipe). + int channel_flags = ~G_IO_FLAG_NONBLOCK; - g_strfreev(argvp); + if (channel_stdout) { + g_io_channel_set_encoding(channel_stdout, nullptr, nullptr); // binary IO + g_io_channel_set_flags(channel_stdout, GIOFlags(g_io_channel_get_flags(channel_stdout) & channel_flags), nullptr); + g_io_channel_set_buffer_size(channel_stdout, 10000); + } + if (channel_stderr) { + g_io_channel_set_encoding(channel_stderr, nullptr, nullptr); // binary IO + g_io_channel_set_flags(channel_stderr, GIOFlags(g_io_channel_get_flags(channel_stderr) & channel_flags), nullptr); + g_io_channel_set_buffer_size(channel_stderr, 10000); + } + g_main_loop_run(g_main_loop_new(nullptr, FALSE)); -#ifdef _WIN32 - GIOChannel* channel_stdout = g_io_channel_win32_new_fd(fd_stdout); - GIOChannel* channel_stderr = g_io_channel_win32_new_fd(fd_stderr); -#else - GIOChannel* channel_stdout = g_io_channel_unix_new(fd_stdout); - GIOChannel* channel_stderr = g_io_channel_unix_new(fd_stderr); -#endif - - // blocking writes if the pipe is full helps for small-pipe systems (see man 7 pipe). - int channel_flags = ~G_IO_FLAG_NONBLOCK; - - if (channel_stdout) { - g_io_channel_set_encoding(channel_stdout, nullptr, 0); // binary IO - g_io_channel_set_flags(channel_stdout, GIOFlags(g_io_channel_get_flags(channel_stdout) & channel_flags), 0); - g_io_channel_set_buffer_size(channel_stdout, 10000); - } - - if (channel_stderr) { - g_io_channel_set_encoding(channel_stderr, nullptr, 0); // binary IO - g_io_channel_set_flags(channel_stderr, GIOFlags(g_io_channel_get_flags(channel_stderr) & channel_flags), 0); - g_io_channel_set_buffer_size(channel_stderr, 10000); - } - - - - g_main_loop_run(g_main_loop_new(nullptr, false)); - - - return 0; + return EXIT_SUCCESS; + }); } diff --git a/src/applib/examples/example_storage_detector.cpp b/src/applib/examples/example_storage_detector.cpp index 2f0bc71..e29da45 100644 --- a/src/applib/examples/example_storage_detector.cpp +++ b/src/applib/examples/example_storage_detector.cpp @@ -13,6 +13,7 @@ Copyright: #include "applib/storage_device.h" #include "applib/storage_detector.h" + #include "hz/main_tools.h" #include "gsc_settings.h" // in src directory @@ -45,6 +46,7 @@ int main() " (" << StorageDevice::get_type_storable_name(drive->get_detected_type()) << ")\n"; } } + return EXIT_SUCCESS; }); } diff --git a/src/hz/examples/example_format_unit.cpp b/src/hz/examples/example_format_unit.cpp index f0c8fc9..238ee24 100644 --- a/src/hz/examples/example_format_unit.cpp +++ b/src/hz/examples/example_format_unit.cpp @@ -23,41 +23,46 @@ Copyright: #include #include +#include "hz/main_tools.h" + /// Main function for the test int main() { - std::cerr << hz::format_size(3 * 1024 * 1024) << "\n"; // 3 MiB - std::cerr << hz::format_size(4LL * 1000 * 1000, true, true) << "\n"; // 4 Mbit - std::cerr << hz::format_size(100LL * 1000 * 1000 * 1000) << "\n"; // 100 GB in GiB (aka how hard disk manufactures screw you) - std::cerr << hz::format_size(100LL * 1024 * 1024, true) << "\n"; // 100 MiB in decimal GB + return hz::main_exception_wrapper([]() + { + std::cerr << hz::format_size(3 * 1024 * 1024) << "\n"; // 3 MiB + std::cerr << hz::format_size(4LL * 1000 * 1000, true, true) << "\n"; // 4 Mbit + std::cerr << hz::format_size(100LL * 1000 * 1000 * 1000) << "\n"; // 100 GB in GiB (aka how hard disk manufactures screw you) + std::cerr << hz::format_size(100LL * 1024 * 1024, true) << "\n"; // 100 MiB in decimal GB - std::cerr << hz::format_size(5) << "\n"; // 5 bytes - std::cerr << hz::format_size(6, true, true) << "\n"; // 6 bits - std::cerr << hz::format_size(uint64_t(2.5 * 1024)) << "\n"; // 2.5 KiB - std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024)) << "\n"; // 2.5 MiB - std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024)) << "\n"; // 2.5 GiB - std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024 * 1024)) << "\n"; // 2.5 TiB - std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024 * 1024 * 1024)) << "\n"; // 2.5 PiB - std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)) << "\n"; // 2.5 EiB + std::cerr << hz::format_size(5) << "\n"; // 5 bytes + std::cerr << hz::format_size(6, true, true) << "\n"; // 6 bits + std::cerr << hz::format_size(uint64_t(2.5 * 1024)) << "\n"; // 2.5 KiB + std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024)) << "\n"; // 2.5 MiB + std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024)) << "\n"; // 2.5 GiB + std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024 * 1024)) << "\n"; // 2.5 TiB + std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024 * 1024 * 1024)) << "\n"; // 2.5 PiB + std::cerr << hz::format_size(uint64_t(2.5 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024)) << "\n"; // 2.5 EiB - std::cerr << hz::format_size(uint64_t(1000204886016ULL), true) << "\n"; // common size of 1 TiB hdd in decimal + std::cerr << hz::format_size(uint64_t(1000204886016ULL), true) << "\n"; // common size of 1 TiB hdd in decimal - using namespace std::literals; - using days = std::chrono::duration, std::chrono::hours::period>>; + using namespace std::literals; + using days = std::chrono::duration, std::chrono::hours::period>>; - std::cerr << hz::format_time_length(5s) << "\n"; // 5 sec - std::cerr << hz::format_time_length(5min + 30s) << "\n"; // 5.5 min - std::cerr << hz::format_time_length(130min) << "\n"; // 130 min - std::cerr << hz::format_time_length(5h + 30min) << "\n"; // 5.5 hours - std::cerr << hz::format_time_length(24h + 20min) << "\n"; // 24 hours, 20 minutes - std::cerr << hz::format_time_length(130h + 30min) << "\n"; // 130.5 hours - std::cerr << hz::format_time_length(days(5) + 15h + 30min) << "\n"; // 5 days, 15 hours, 30 minutes - std::cerr << hz::format_time_length(days(20) - 8h) << "\n"; // 19 days, 16 hours + std::cerr << hz::format_time_length(5s) << "\n"; // 5 sec + std::cerr << hz::format_time_length(5min + 30s) << "\n"; // 5.5 min + std::cerr << hz::format_time_length(130min) << "\n"; // 130 min + std::cerr << hz::format_time_length(5h + 30min) << "\n"; // 5.5 hours + std::cerr << hz::format_time_length(24h + 20min) << "\n"; // 24 hours, 20 minutes + std::cerr << hz::format_time_length(130h + 30min) << "\n"; // 130.5 hours + std::cerr << hz::format_time_length(days(5) + 15h + 30min) << "\n"; // 5 days, 15 hours, 30 minutes + std::cerr << hz::format_time_length(days(20) - 8h) << "\n"; // 19 days, 16 hours - return 0; + return EXIT_SUCCESS; + }); } diff --git a/src/hz/examples/example_string_algo.cpp b/src/hz/examples/example_string_algo.cpp index 578c1d4..5b5e3cb 100644 --- a/src/hz/examples/example_string_algo.cpp +++ b/src/hz/examples/example_string_algo.cpp @@ -23,89 +23,94 @@ Copyright: #include #include +#include "hz/main_tools.h" + /// Main function for the test int main() { - using namespace hz; - - + return hz::main_exception_wrapper([]() { - std::string s = "/aa/bbb/ccccc//dsada//"; + using namespace hz; - std::vector v; + { + std::string s = "/aa/bbb/ccccc//dsada//"; - string_split(s, '/', v, false); + std::vector v; - for (std::size_t i = 0; i < v.size(); i++) - std::cerr << (v[i] == "" ? "[empty]" : v[i]) << "\n"; - } + string_split(s, '/', v, false); + + for (const auto& str : v) { + std::cerr << (str.empty() ? "[empty]" : str) << "\n"; + } + } - { - std::string s = "//aa////bbb/ccccc//dsada////"; + { + std::string s = "//aa////bbb/ccccc//dsada////"; - std::vector v; + std::vector v; - string_split(s, "//", v, false); + string_split(s, "//", v, false); - for (std::size_t i = 0; i < v.size(); i++) - std::cerr << (v[i] == "" ? "[empty]" : v[i]) << "\n"; - } + for (const auto& str : v) { + std::cerr << (str.empty() ? "[empty]" : str) << "\n"; + } + } - { - std::string s = " a b bb c d "; - std::cerr << string_remove_adjacent_duplicates_copy(s, ' ') << "\n"; - std::cerr << string_remove_adjacent_duplicates_copy(s, ' ', 2) << "\n"; - } + { + std::string s = " a b bb c d "; + std::cerr << string_remove_adjacent_duplicates_copy(s, ' ') << "\n"; + std::cerr << string_remove_adjacent_duplicates_copy(s, ' ', 2) << "\n"; + } - { - std::string s = "/a/b/c/dd//e/"; - string_replace(s, '/', ':'); - std::cerr << s << "\n"; - } + { + std::string s = "/a/b/c/dd//e/"; + string_replace(s, '/', ':'); + std::cerr << s << "\n"; + } - { - std::string s = "112/2123412"; - string_replace(s, "12", "AB"); - std::cerr << s << "\n"; - } + { + std::string s = "112/2123412"; + string_replace(s, "12", "AB"); + std::cerr << s << "\n"; + } - { - std::vector from, to; + { + std::vector from, to; - from.push_back("12"); - from.push_back("abc"); + from.emplace_back("12"); + from.emplace_back("abc"); - to.push_back("345"); - to.push_back("de"); + to.emplace_back("345"); + to.emplace_back("de"); - std::string s = "12345678abcdefg abc ab"; - std::cerr << s << "\n"; - string_replace_array(s, from, to); - std::cerr << s << "\n"; - } + std::string s = "12345678abcdefg abc ab"; + std::cerr << s << "\n"; + string_replace_array(s, from, to); + std::cerr << s << "\n"; + } - { - std::vector from, to; + { + std::vector from, to; - from.push_back("12"); - from.push_back("abc"); + from.emplace_back("12"); + from.emplace_back("abc"); - std::string s = "12345678abcdefg abc ab"; - std::cerr << s << "\n"; - string_replace_array(s, from, ":"); - std::cerr << s << "\n"; - } + std::string s = "12345678abcdefg abc ab"; + std::cerr << s << "\n"; + string_replace_array(s, from, ":"); + std::cerr << s << "\n"; + } - - return 0; + return EXIT_SUCCESS; + }); } diff --git a/src/hz/examples/example_string_num.cpp b/src/hz/examples/example_string_num.cpp index d07f24e..5994f36 100644 --- a/src/hz/examples/example_string_num.cpp +++ b/src/hz/examples/example_string_num.cpp @@ -24,165 +24,166 @@ Copyright: #include #include +#include "hz/main_tools.h" + /// Main function for the test int main() { - using namespace hz; - - -// int i = 0; -// double d = 0; - -// std::cerr << (string_is_numeric_nolocale("-1", i) ? "true" : "false"); -// std::cerr << ", parsed: i=" << i << "\n"; - - + return hz::main_exception_wrapper([]() { - int16_t int16_; - std::cerr << (string_is_numeric_nolocale("32768", int16_) ? "true" : "false"); // overflow, false - std::cerr << "\n"; + using namespace hz; - uint16_t uint16_; - std::cerr << (string_is_numeric_nolocale("65535", uint16_) ? "true" : "false"); // true - std::cerr << "\n"; + // int i = 0; + // double d = 0; - int32_t int32_; - std::cerr << (string_is_numeric_nolocale(" 1.33", int32_) ? "true" : "false"); // strict, false - std::cerr << "\n"; - - int32_t int32_2; - std::cerr << (string_is_numeric_nolocale("1.33", int32_2) ? "true" : "false"); // strict, false - std::cerr << "\n"; - - uint32_t uint32_; - std::cerr << (string_is_numeric_nolocale("-1", uint32_) ? "true" : "false"); // underflow, false - std::cerr << "\n"; - - int64_t int64_; - std::cerr << (string_is_numeric_nolocale("-1", int64_) ? "true" : "false"); // true - std::cerr << "\n"; - - uint64_t uint64_; - std::cerr << (string_is_numeric_nolocale("1", uint64_) ? "true" : "false"); // true - std::cerr << "\n"; - - int int_; - std::cerr << (string_is_numeric_nolocale("-1", int_) ? "true" : "false"); // true - std::cerr << "\n"; - - unsigned int uint_; - std::cerr << (string_is_numeric_nolocale("-1", uint_) ? "true" : "false"); // underflow, false - std::cerr << "\n"; - - long long_int_; - std::cerr << (string_is_numeric_nolocale("-1", long_int_) ? "true" : "false"); // true - std::cerr << "\n"; - - unsigned long long_uint_; - std::cerr << (string_is_numeric_nolocale("-1", long_uint_) ? "true" : "false"); // underflow, false - std::cerr << "\n"; - - char char_; - std::cerr << (string_is_numeric_nolocale("4", char_) ? "true" : "false"); // true - std::cerr << "\n"; - - unsigned char uchar_; - std::cerr << (string_is_numeric_nolocale("315", uchar_) ? "true" : "false"); // overflow, false - std::cerr << "\n"; - - signed char schar_2; - std::cerr << (string_is_numeric_nolocale("128", schar_2) ? "true" : "false"); // overflow, false - std::cerr << "\n"; - - signed char schar_3; - std::cerr << (string_is_numeric_nolocale("-128", schar_3) ? "true" : "false"); // true - std::cerr << "\n"; - - unsigned char uchar_2; - std::cerr << (string_is_numeric_nolocale("128", uchar_2) ? "true" : "false"); // true - std::cerr << "\n"; - - bool b; - std::cerr << (string_is_numeric_nolocale("true", b) ? "true" : "false"); // true - std::cerr << "\n"; - - } + // std::cerr << (string_is_numeric_nolocale("-1", i) ? "true" : "false"); + // std::cerr << ", parsed: i=" << i << "\n"; -// std::cerr << (string_is_numeric_nolocale("-1.3", i) ? "true" : "false"); -// std::cerr << ", parsed: i=" << i << "\n"; + { + int16_t int16_ = 0; + std::cerr << (string_is_numeric_nolocale("32768", int16_) ? "true" : "false"); // overflow, false + std::cerr << "\n"; + + uint16_t uint16_ = 0; + std::cerr << (string_is_numeric_nolocale("65535", uint16_) ? "true" : "false"); // true + std::cerr << "\n"; + + int32_t int32_ = 0; + std::cerr << (string_is_numeric_nolocale(" 1.33", int32_) ? "true" : "false"); // strict, false + std::cerr << "\n"; + + int32_t int32_2 = 0; + std::cerr << (string_is_numeric_nolocale("1.33", int32_2) ? "true" : "false"); // strict, false + std::cerr << "\n"; + + uint32_t uint32_ = 0; + std::cerr << (string_is_numeric_nolocale("-1", uint32_) ? "true" : "false"); // underflow, false + std::cerr << "\n"; + + int64_t int64_ = 0; + std::cerr << (string_is_numeric_nolocale("-1", int64_) ? "true" : "false"); // true + std::cerr << "\n"; + + uint64_t uint64_ = 0; + std::cerr << (string_is_numeric_nolocale("1", uint64_) ? "true" : "false"); // true + std::cerr << "\n"; + + int int_ = 0; + std::cerr << (string_is_numeric_nolocale("-1", int_) ? "true" : "false"); // true + std::cerr << "\n"; + + unsigned int uint_ = 0; + std::cerr << (string_is_numeric_nolocale("-1", uint_) ? "true" : "false"); // underflow, false + std::cerr << "\n"; + + long long_int_ = 0; + std::cerr << (string_is_numeric_nolocale("-1", long_int_) ? "true" : "false"); // true + std::cerr << "\n"; + + unsigned long long_uint_ = 0; + std::cerr << (string_is_numeric_nolocale("-1", long_uint_) ? "true" : "false"); // underflow, false + std::cerr << "\n"; + + char char_ = 0; + std::cerr << (string_is_numeric_nolocale("4", char_) ? "true" : "false"); // true + std::cerr << "\n"; + + unsigned char uchar_ = 0; + std::cerr << (string_is_numeric_nolocale("315", uchar_) ? "true" : "false"); // overflow, false + std::cerr << "\n"; + + signed char schar_2 = 0; + std::cerr << (string_is_numeric_nolocale("128", schar_2) ? "true" : "false"); // overflow, false + std::cerr << "\n"; + + signed char schar_3 = 0; + std::cerr << (string_is_numeric_nolocale("-128", schar_3) ? "true" : "false"); // true + std::cerr << "\n"; + + unsigned char uchar_2 = 0; + std::cerr << (string_is_numeric_nolocale("128", uchar_2) ? "true" : "false"); // true + std::cerr << "\n"; + + bool b = false; + std::cerr << (string_is_numeric_nolocale("true", b) ? "true" : "false"); // true + std::cerr << "\n"; + + } - { - double d = 0; + // std::cerr << (string_is_numeric_nolocale("-1.3", i) ? "true" : "false"); + // std::cerr << ", parsed: i=" << i << "\n"; - std::cerr << (string_is_numeric_nolocale("-1.3", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; - std::cerr << (string_is_numeric_nolocale(" inf", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; + { + double d = 0; - std::cerr << (string_is_numeric_nolocale("infinity", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; + std::cerr << (string_is_numeric_nolocale("-1.3", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; - std::cerr << (string_is_numeric_nolocale("NAn", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; + std::cerr << (string_is_numeric_nolocale(" inf", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; - std::cerr << (string_is_numeric_nolocale("3.e+4", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; + std::cerr << (string_is_numeric_nolocale("infinity", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; - std::cerr << (string_is_numeric_nolocale("123 ", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; + std::cerr << (string_is_numeric_nolocale("NAn", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; - std::cerr << (string_is_numeric_nolocale("e+3", d) ? "true" : "false"); - std::cerr << ", parsed: d=" << d << "\n"; - } + std::cerr << (string_is_numeric_nolocale("3.e+4", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; + + std::cerr << (string_is_numeric_nolocale("123 ", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; + + std::cerr << (string_is_numeric_nolocale("e+3", d) ? "true" : "false"); + std::cerr << ", parsed: d=" << d << "\n"; + } - { + { - // Note: suffixes are case-insensitive. - // Long int and long double are differentiated by '.' in constant. + // Note: suffixes are case-insensitive. + // Long int and long double are differentiated by '.' in constant. - std::cerr << number_to_string_nolocale(true) << "\n"; // bool - std::cerr << number_to_string_nolocale('a') << "\n"; // char - std::cerr << number_to_string_nolocale(1.L) << "\n"; // long double - std::cerr << number_to_string_nolocale(2L) << "\n"; // long int - std::cerr << number_to_string_nolocale(6) << "\n"; // int + std::cerr << number_to_string_nolocale(true) << "\n"; // bool + std::cerr << number_to_string_nolocale('a') << "\n"; // char + std::cerr << number_to_string_nolocale(1.L) << "\n"; // long double + std::cerr << number_to_string_nolocale(2L) << "\n"; // long int + std::cerr << number_to_string_nolocale(6) << "\n"; // int - std::cerr << number_to_string_nolocale(3LL) << "\n"; // long long int - std::cerr << number_to_string_nolocale(4ULL) << "\n"; // unsigned long long int + std::cerr << number_to_string_nolocale(3LL) << "\n"; // long long int + std::cerr << number_to_string_nolocale(4ULL) << "\n"; // unsigned long long int - std::cerr << number_to_string_nolocale(5.f) << "\n"; // float - std::cerr << number_to_string_nolocale(1.33) << "\n"; // double - std::cerr << number_to_string_nolocale(2.123456789123456789123456789L) << "\n"; + std::cerr << number_to_string_nolocale(5.f) << "\n"; // float + std::cerr << number_to_string_nolocale(1.33) << "\n"; // double + std::cerr << number_to_string_nolocale(2.123456789123456789123456789L) << "\n"; - std::cerr << number_to_string_nolocale(std::numeric_limits::quiet_NaN()) << "\n"; - std::cerr << number_to_string_nolocale(std::numeric_limits::signaling_NaN()) << "\n"; - std::cerr << number_to_string_nolocale(std::numeric_limits::infinity()) << "\n"; + std::cerr << number_to_string_nolocale(std::numeric_limits::quiet_NaN()) << "\n"; + std::cerr << number_to_string_nolocale(std::numeric_limits::signaling_NaN()) << "\n"; + std::cerr << number_to_string_nolocale(std::numeric_limits::infinity()) << "\n"; - std::cerr << number_to_string_nolocale(uint16_t(0x1133), 16) << "\n"; - std::cerr << number_to_string_nolocale(uint32_t(0x00001234), 16) << "\n"; - std::cerr << number_to_string_nolocale(uint64_t(0x00001234), 16) << "\n"; - std::cerr << number_to_string_nolocale(uint16_t(0), 16) << "\n"; + std::cerr << number_to_string_nolocale(uint16_t(0x1133), 16) << "\n"; + std::cerr << number_to_string_nolocale(uint32_t(0x00001234), 16) << "\n"; + std::cerr << number_to_string_nolocale(uint64_t(0x00001234), 16) << "\n"; + std::cerr << number_to_string_nolocale(uint16_t(0), 16) << "\n"; - std::cerr << number_to_string_nolocale(uint16_t(0xffff), 8) << "\n"; - std::cerr << number_to_string_nolocale(uint16_t(0), 8) << "\n"; + std::cerr << number_to_string_nolocale(uint16_t(0xffff), 8) << "\n"; + std::cerr << number_to_string_nolocale(uint16_t(0), 8) << "\n"; -// std::cerr << number_to_string_nolocale("abc", 8) << "\n"; + // std::cerr << number_to_string_nolocale("abc", 8) << "\n"; -// std::cerr << number_to_string_nolocale(uint16_t(0xffff), 2) << "\n"; // won't work -// std::cerr << number_to_string_nolocale(uint16_t(0), 2) << "\n"; + // std::cerr << number_to_string_nolocale(uint16_t(0xffff), 2) << "\n"; // won't work + // std::cerr << number_to_string_nolocale(uint16_t(0), 2) << "\n"; - } + } - - - - return 0; + return EXIT_SUCCESS; + }); } diff --git a/src/libdebug/examples/example_libdebug.cpp b/src/libdebug/examples/example_libdebug.cpp index 1944846..2ee5dcc 100644 --- a/src/libdebug/examples/example_libdebug.cpp +++ b/src/libdebug/examples/example_libdebug.cpp @@ -15,6 +15,9 @@ Copyright: #include +#include "hz/main_tools.h" + + /// libdebug namespace for tests namespace libdebug_example { @@ -62,8 +65,8 @@ namespace { -/// Main function for the test -int main() +/// A separate function is needed to avoid clang-tidy complaining about capturing __function__ from lambda. +int main_impl() { debug_register_domain("dom"); @@ -182,12 +185,21 @@ int main() TestClassB().func2(TestClassC(), 0); -// debug_out_warn("default", DBG_FUNC_MSG << "Doing something.\n"); -// debug_out_warn("default", DBG_FUNC_MSG << "Doing something.\n"); - + // debug_out_warn("default", DBG_FUNC_MSG << "Doing something.\n"); + // debug_out_warn("default", DBG_FUNC_MSG << "Doing something.\n"); return EXIT_SUCCESS; +} + + +/// Main function for the test +int main() +{ + return hz::main_exception_wrapper([]() + { + return main_impl(); + }); } diff --git a/src/rconfig/examples/example_rconfig.cpp b/src/rconfig/examples/example_rconfig.cpp index 9a317a3..201189b 100644 --- a/src/rconfig/examples/example_rconfig.cpp +++ b/src/rconfig/examples/example_rconfig.cpp @@ -21,6 +21,7 @@ Copyright: #if defined ENABLE_GLIB && ENABLE_GLIB #include "rconfig/autosave.h" #endif +#include "hz/main_tools.h" #include #include @@ -31,38 +32,41 @@ Copyright: /// Main function for the test int main() { - using namespace std::literals; + return hz::main_exception_wrapper([]() + { + using namespace std::literals; - rconfig::load_from_file("test.config"); + rconfig::load_from_file("test.config"); - // populate /default: - rconfig::set_default_data("app/use_stuff", true); // bool - rconfig::set_default_data("app/some_string1", std::string("some_string1_data")); - rconfig::set_default_data("app/some_string2", "some_string2_data"); // this will store it as std::string - rconfig::set_default_data("app/int_value", 5); // stored as int64_t - rconfig::set_default_data("app/int64_value", int64_t(5)); // explicitly - rconfig::set_default_data("app/double_value", 6.7); // double + // populate /default: + rconfig::set_default_data("app/use_stuff", true); // bool + rconfig::set_default_data("app/some_string1", std::string("some_string1_data")); + rconfig::set_default_data("app/some_string2", "some_string2_data"); // this will store it as std::string + rconfig::set_default_data("app/int_value", 5); // stored as int64_t + rconfig::set_default_data("app/int64_value", int64_t(5)); // explicitly + rconfig::set_default_data("app/double_value", 6.7); // double - rconfig::set_data("app/int_var", 11); // override default. + rconfig::set_data("app/int_var", 11); // override default. - int int_var = rconfig::get_data("app/int_value"); - std::cerr << "app/int_value: " << int_var << "\n"; + int int_var = rconfig::get_data("app/int_value"); + std::cerr << "app/int_value: " << int_var << "\n"; - std::cerr << "app/some_string2: " << rconfig::get_data("app/some_string2") << "\n"; + std::cerr << "app/some_string2: " << rconfig::get_data("app/some_string2") << "\n"; - rconfig::dump_config(); - rconfig::save_to_file("test.config"); + rconfig::dump_config(); + rconfig::save_to_file("test.config"); -#if defined ENABLE_GLIB && ENABLE_GLIB - rconfig::autosave_set_config_file("test2.config"); - rconfig::autosave_start(2s); // every 2 seconds - while(true) { - // without this the timeout function won't be called. - g_main_context_iteration(nullptr, false); - } -#endif + #if defined ENABLE_GLIB && ENABLE_GLIB + rconfig::autosave_set_config_file("test2.config"); + rconfig::autosave_start(2s); // every 2 seconds + while(true) { + // without this the timeout function won't be called. + g_main_context_iteration(nullptr, FALSE); + } + #endif - return 0; + return EXIT_SUCCESS; + }); }