rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit 816c64a5d927456be6ace2b39de50bf487289c65
parent 8f8381373ee976fc07c8dadf167392ac9bc70b4a
Author: vaplv <vaplv@free.fr>
Date:   Mon,  8 Oct 2018 12:26:17 +0200

Add and test the res_to_cstr function

Diffstat:
Msrc/cstr.h | 21+++++++++++++++++++++
Msrc/test_cstr.c | 13+++++++++++++
2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/cstr.h b/src/cstr.h @@ -131,6 +131,25 @@ cstr_to_uint(const char* str, unsigned* dst) return RES_OK; } +static INLINE const char* +res_to_cstr(const res_T res) +{ + const char* cstr = NULL; + switch(res) { + case RES_OK: cstr = "Success"; break; + case RES_BAD_ARG: cstr = "Invalid argument"; break; + case RES_MEM_ERR: cstr = "Could not allocate memory"; break; + case RES_IO_ERR: cstr = "Input/Ouput error"; break; + case RES_UNKNOWN_ERR: cstr = "Unknown error"; break; + case RES_BAD_OP: cstr = "Invalid operation"; break; + case RES_EOF: cstr = "Reached end of file"; break; + default: FATAL("Ureachable code.\n"); break; + } + return cstr; +} + +BEGIN_DECLS + /* Convert a string "A:B:C:D:E:F" in a list of { A, B, C, D, E, F }. ':' can be * any user defined character */ RSYS_API res_T @@ -157,5 +176,7 @@ cstr_to_list_uint size_t* length, const size_t max_length); +END_DECLS + #endif /* CSTR_H */ diff --git a/src/test_cstr.c b/src/test_cstr.c @@ -258,6 +258,18 @@ test_list_uint(void) CHK(ulist[3] == 3); } +static void +test_res_to_cstr(void) +{ + printf("%s\n", res_to_cstr(RES_OK)); + printf("%s\n", res_to_cstr(RES_BAD_ARG)); + printf("%s\n", res_to_cstr(RES_MEM_ERR)); + printf("%s\n", res_to_cstr(RES_IO_ERR)); + printf("%s\n", res_to_cstr(RES_UNKNOWN_ERR)); + printf("%s\n", res_to_cstr(RES_BAD_OP)); + printf("%s\n", res_to_cstr(RES_EOF)); +} + int main(int argc, char** argv) { @@ -271,5 +283,6 @@ main(int argc, char** argv) test_list_double(); test_list_float(); test_list_uint(); + test_res_to_cstr(); return 0; }