generated from threeal/cpp-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): add
print_hex
example
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |