Skip to content

Commit 73b2dfd

Browse files
authored
docs: add readme in examples (#137)
* docs(readme): add `examples/README.md` * docs: add comments in the example codes
1 parent c2591d9 commit 73b2dfd

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

examples/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Examples
2+
3+
This project includes the following two examples:
4+
5+
- [`print_hex.cpp`](./print_hex.cpp):
6+
Prints the hexadecimal representation of the given number.
7+
This code returns an error if the provided input is not a number.
8+
This example demonstrates the basic flow of error handling and how to return an error object from a function.
9+
10+
- [`read_file.cpp`](./read_file.cpp):
11+
Reads a file from the given path and prints its content.
12+
This code returns an error if it fails to open the file.
13+
This example illustrates the same error handling flow but with examples of how to create an error object with a formatted message.

examples/print_hex.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include <errors/error.hpp>
22
#include <iostream>
33

4+
// Prints the hexadecimal representation of the given number string.
45
errors::Error print_hex(const char* number_str) {
56
int number = std::atoi(number_str);
6-
if (number == 0) {
7+
if (number_str[0] != '0' && number == 0) {
8+
// The given string is not a number, return an error.
79
return errors::make("is not a number");
810
}
911

12+
// Print the number and return nil.
1013
std::cout << std::hex << number << std::endl;
1114
return errors::nil();
1215
}
@@ -17,6 +20,7 @@ int main(int argc, char **argv) {
1720
return 1;
1821
}
1922

23+
// Call the function and handle the error.
2024
const auto err = print_hex(argv[1]);
2125
if (err) {
2226
std::cerr << err << std::endl;

examples/read_file.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
#include <errors/format.hpp>
33
#include <fstream>
44

5+
// Reads the file from the given path and prints the contents.
56
errors::Error read_file(const char* filepath) {
67
std::ifstream file(filepath);
78
if (!file.is_open()) {
8-
return errors::format("failed to open `{}` ({})", filepath, static_cast<int>(file.rdstate()));
9+
// Unable to open the file, return an error.
10+
return errors::format("failed to open '{}' ({})", filepath, static_cast<int>(file.rdstate()));
911
}
1012

13+
// Print the file contents and return nil.
1114
std::string line;
1215
while (std::getline(file, line)) {
1316
fmt::print("{}\n", line);
1417
}
15-
1618
return errors::nil();
1719
}
1820

@@ -22,6 +24,7 @@ int main(int argc, char **argv) {
2224
return 1;
2325
}
2426

27+
// Call the function and handle the error.
2528
const auto err = read_file(argv[1]);
2629
if (err) {
2730
fmt::print(stderr, "{}\n", err);

0 commit comments

Comments
 (0)