diff options
Diffstat (limited to 'src/errno_status.c')
-rw-r--r-- | src/errno_status.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/errno_status.c b/src/errno_status.c new file mode 100644 index 0000000..1830234 --- /dev/null +++ b/src/errno_status.c @@ -0,0 +1,48 @@ +// Copyright 2014-2022 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#include "errno_status.h" + +#include "zix/common.h" + +#include <errno.h> +#include <stddef.h> + +ZixStatus +zix_errno_status_if(const int r) +{ + return r ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS; +} + +ZixStatus +zix_errno_status(const int e) +{ + typedef struct { + int code; + ZixStatus status; + } Mapping; + + static const Mapping map[] = { + {0, ZIX_STATUS_SUCCESS}, + {EACCES, ZIX_STATUS_BAD_PERMS}, + {EAGAIN, ZIX_STATUS_UNAVAILABLE}, + {EEXIST, ZIX_STATUS_EXISTS}, + {EINVAL, ZIX_STATUS_BAD_ARG}, + {ENOENT, ZIX_STATUS_NOT_FOUND}, + {ENOMEM, ZIX_STATUS_NO_MEM}, + {ENOSYS, ZIX_STATUS_NOT_SUPPORTED}, + {EPERM, ZIX_STATUS_BAD_PERMS}, + {ETIMEDOUT, ZIX_STATUS_TIMEOUT}, + {0, ZIX_STATUS_ERROR}, // Fallback mapping + }; + + static const size_t n_mappings = sizeof(map) / sizeof(Mapping); + + // Find the index of the matching mapping (or leave it at the fallback entry) + size_t m = 0; + while (m < n_mappings && map[m].code != e) { + ++m; + } + + return map[m].status; +} |