From b192014ebed8d71fe675fc81d047eccc14e59a64 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss <113760121+Amllx@users.noreply.github.com> Date: Tue, 25 Apr 2023 17:09:32 +0200 Subject: Create str_checksum.cc new example to illustrate usage of stdx::opt. --- examples/str_checksum.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/str_checksum.cc (limited to 'examples') diff --git a/examples/str_checksum.cc b/examples/str_checksum.cc new file mode 100644 index 0000000..05cdfd8 --- /dev/null +++ b/examples/str_checksum.cc @@ -0,0 +1,43 @@ +/* + string checksum example + written by Amlal El Mahrouss. + licensed under GPL-2 license + */ + +#include + +extern "C" { +# include +} + +static const char do_hash(const char* in) +{ + int hash = 0; + + for (long index = 0; index < strlen(in); ++index) + { + hash += in[index]; + } + + return hash; +} + +static auto do_some(const char* recv_data, const char* check_data) +{ + const int hash_to_check = do_hash(check_data); /* here we assume this should match opt_hash */ + const int opt_hash = do_hash(recv_data); /* we assume that the hash is correct */ + + auto opt = stdx::opt(stdx::eval_eq(hash_to_check, opt_hash)); /* do the compute */ + return opt; +} + +/* finally test it */ +int main(int argc, char** argv) +{ + // ... let's assume we fetch data from network... + + auto opt = do_some("Michigan", "Ohio"); + opt.expect("Checksum failed, Michigan isn't Ohio!"); + + return 0; +} -- cgit v1.2.3