From 7e1a80d79f0bd278f380eeef9429415f37436190 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 27 Dec 2023 13:35:52 +0700 Subject: [PATCH] feat(example): add `print_hex` example --- examples/CMakeLists.txt | 4 ++++ examples/print_hex.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 examples/print_hex.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 54634d2..113427a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,9 @@ add_custom_target(examples) +add_executable(example_print_hex print_hex.cpp) +target_link_libraries(example_print_hex PRIVATE errors) +add_dependencies(examples example_print_hex) + add_executable(example_read_file read_file.cpp) target_link_libraries(example_read_file PRIVATE errors errors_format) add_dependencies(examples example_read_file) diff --git a/examples/print_hex.cpp b/examples/print_hex.cpp new file mode 100644 index 0000000..0564c1a --- /dev/null +++ b/examples/print_hex.cpp @@ -0,0 +1,27 @@ +#include +#include + +errors::Error print_hex(const char* number_str) { + int number = std::atoi(number_str); + if (number == 0) { + return errors::make("is not a number"); + } + + std::cout << std::hex << number << std::endl; + return errors::nil(); +} + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "usage: " << argv[0] << " " << std::endl; + return 1; + } + + const auto err = print_hex(argv[1]); + if (err) { + std::cerr << err << std::endl; + return 1; + } + + return 0; +}