Skip to content

Commit

Permalink
feat(example): add print_hex example
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Dec 27, 2023
1 parent d0d929b commit 7e1a80d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions examples/print_hex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <errors/error.hpp>
#include <iostream>

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] << " <number>" << std::endl;
return 1;
}

const auto err = print_hex(argv[1]);
if (err) {
std::cerr << err << std::endl;
return 1;
}

return 0;
}

0 comments on commit 7e1a80d

Please # to comment.