diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index fdcdc3b..1fe1fe6 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -22,6 +22,25 @@ jobs: - name: Build Project run: cmake --build build --config Release + build-examples: + name: Build Examples + runs-on: ${{ matrix.os }}-latest + strategy: + fail-fast: false + matrix: + os: [ubuntu, windows] + steps: + - name: Checkout + uses: actions/checkout@v4.1.1 + + - name: Configure Project + uses: threeal/cmake-action@v1.3.0 + with: + options: BUILD_EXAMPLES=ON + + - name: Build Examples + run: cmake --build build --target examples + build-docs: name: Build Documentation runs-on: ubuntu-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e1e2a0..a517bf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ target_include_directories(errors PUBLIC include) # Check if this project is the main project if(NOT_SUBPROJECT) option(BUILD_DOCS "Enable documentations build" OFF) + option(BUILD_EXAMPLES "Enable examples build" OFF) # Statically analyze code by checking for warnings cpmaddpackage(gh:threeal/CheckWarning.cmake@2.0.1) @@ -70,6 +71,10 @@ if(NOT_SUBPROJECT) include(cmake/add_xml_docs.cmake) add_xml_docs(errors_docs include/errors/error.hpp) endif() + + if(BUILD_EXAMPLES) + add_subdirectory(examples) + endif() endif() add_subdirectory(components) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..113427a --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +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; +} diff --git a/examples/read_file.cpp b/examples/read_file.cpp new file mode 100644 index 0000000..d9b3351 --- /dev/null +++ b/examples/read_file.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +errors::Error read_file(const char* filepath) { + std::ifstream file(filepath); + if (!file.is_open()) { + return errors::format("failed to open `{}` ({})", filepath, static_cast(file.rdstate())); + } + + std::string line; + while (std::getline(file, line)) { + fmt::print("{}\n", line); + } + + return errors::nil(); +} + +int main(int argc, char **argv) { + if (argc < 2) { + fmt::print(stderr, "usage: {} \n", argv[0]); + return 1; + } + + const auto err = read_file(argv[1]); + if (err) { + fmt::print(stderr, "{}\n", err); + return 1; + } + + return 0; +}