diff options
author | David Robillard <d@drobilla.net> | 2022-08-18 16:44:41 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-08-18 16:44:41 -0400 |
commit | b9d58f74ea1d072e7a2d1c862dc7a1e0fe5fccb0 (patch) | |
tree | 9b01f7f6e96af405457cba746ca85dd21d38a3e5 | |
parent | 9ac3f4e009fd0d393d2c41449b121498e5b7ad54 (diff) | |
download | zix-b9d58f74ea1d072e7a2d1c862dc7a1e0fe5fccb0.tar.gz zix-b9d58f74ea1d072e7a2d1c862dc7a1e0fe5fccb0.tar.bz2 zix-b9d58f74ea1d072e7a2d1c862dc7a1e0fe5fccb0.zip |
Factor out converting errno codes to ZixStatus
-rw-r--r-- | include/zix/common.h | 5 | ||||
-rw-r--r-- | include/zix/thread.h | 12 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | src/status.c | 31 | ||||
-rw-r--r-- | test/test_status.c (renamed from test/test_strerror.c) | 33 |
5 files changed, 69 insertions, 14 deletions
diff --git a/include/zix/common.h b/include/zix/common.h index 5919fa9..c0944b5 100644 --- a/include/zix/common.h +++ b/include/zix/common.h @@ -33,6 +33,11 @@ ZIX_CONST_API const char* zix_strerror(ZixStatus status); +/// Return an errno value converted to a status code +ZIX_CONST_API +ZixStatus +zix_errno_status(int e); + /// Function for comparing two elements typedef int (*ZixComparator)(const void* a, const void* b, diff --git a/include/zix/thread.h b/include/zix/thread.h index 74bb19b..c80add0 100644 --- a/include/zix/thread.h +++ b/include/zix/thread.h @@ -9,7 +9,6 @@ #ifdef _WIN32 # include <windows.h> #else -# include <errno.h> # include <pthread.h> #endif @@ -85,16 +84,7 @@ zix_thread_create(ZixThread* thread, const int ret = pthread_create(thread, NULL, function, arg); pthread_attr_destroy(&attr); - switch (ret) { - case EAGAIN: - return ZIX_STATUS_NO_MEM; - case EINVAL: - return ZIX_STATUS_BAD_ARG; - case EPERM: - return ZIX_STATUS_BAD_PERMS; - } - - return ret ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; + return zix_errno_status(ret); } static inline ZixStatus diff --git a/meson.build b/meson.build index 45f8b88..2a31887 100644 --- a/meson.build +++ b/meson.build @@ -177,7 +177,7 @@ sequential_tests = [ 'test_btree', 'test_digest', 'test_hash', - 'test_strerror', + 'test_status', 'test_tree', ] diff --git a/src/status.c b/src/status.c index b932ff4..2ca3fe8 100644 --- a/src/status.c +++ b/src/status.c @@ -3,6 +3,8 @@ #include "zix/common.h" +#include <errno.h> + const char* zix_strerror(const ZixStatus status) { @@ -26,3 +28,32 @@ zix_strerror(const ZixStatus status) } return "Unknown error"; } + +ZixStatus +zix_errno_status(const int e) +{ + switch (e) { + case 0: + return ZIX_STATUS_SUCCESS; +#ifdef EAGAIN + case EAGAIN: + return ZIX_STATUS_NO_MEM; +#endif +#ifdef EEXIST + case EEXIST: + return ZIX_STATUS_EXISTS; +#endif +#ifdef EINVAL + case EINVAL: + return ZIX_STATUS_BAD_ARG; +#endif +#ifdef EPERM + case EPERM: + return ZIX_STATUS_BAD_PERMS; +#endif + default: + break; + } + + return ZIX_STATUS_ERROR; +} diff --git a/test/test_strerror.c b/test/test_status.c index 2f4c090..eb11bc1 100644 --- a/test/test_strerror.c +++ b/test/test_status.c @@ -6,11 +6,33 @@ #include "zix/common.h" #include <assert.h> +#include <errno.h> +#include <limits.h> #include <stdio.h> #include <string.h> -int -main(void) +static void +test_errno_status(void) +{ + assert(zix_errno_status(0) == ZIX_STATUS_SUCCESS); + assert(zix_errno_status(INT_MAX) == ZIX_STATUS_ERROR); + +#ifdef EAGAIN + assert(zix_errno_status(EAGAIN) == ZIX_STATUS_NO_MEM); +#endif +#ifdef EEXIST + assert(zix_errno_status(EEXIST) == ZIX_STATUS_EXISTS); +#endif +#ifdef EINVAL + assert(zix_errno_status(EINVAL) == ZIX_STATUS_BAD_ARG); +#endif +#ifdef EPERM + assert(zix_errno_status(EPERM) == ZIX_STATUS_BAD_PERMS); +#endif +} + +static void +test_strerror(void) { const char* msg = zix_strerror(ZIX_STATUS_SUCCESS); assert(!strcmp(msg, "Success")); @@ -27,5 +49,12 @@ main(void) assert(!strcmp(msg, "Unknown error")); printf("Success\n"); +} + +int +main(void) +{ + test_errno_status(); + test_strerror(); return 0; } |