diff --git a/Source/DrangPlatform/Include/drang/error.h b/Source/DrangPlatform/Include/drang/error.h new file mode 100644 index 0000000..2ee9f79 --- /dev/null +++ b/Source/DrangPlatform/Include/drang/error.h @@ -0,0 +1,83 @@ +#pragma once +#include "platform.h" + +DRANG_BEGIN_DECLS + +/** + * @brief Returns a human-readable string for the given DRANG error code. + * @param err Error code. + * @return String description of the error code. + */ +DRANG_API const char *drang_error_str(int err); + +#define DRANG_EOK (0) +#define DRANG_EINVAL (-1) +#define DRANG_ENOMEM (-2) +#define DRANG_EIO (-3) +#define DRANG_NOTSUP (-4) +#define DRANG_EAGAIN (-5) +#define DRANG_ENOENT (-6) + +#define DRANG_BEGIN_TRY() \ + int _res = DRANG_EOK; \ + { +#define DRANG_CATCH(name) \ + } \ + _drang_cleanup: \ + if (_res != DRANG_EOK) { \ + int name = _res; + +#define DRANG_END_TRY() \ + } \ + return _res; + +#define DRANG_END_TRY_IGNORE() \ + } \ + _drang_cleanup: \ + return _res; + +#define DRANG_TRY(expr) \ + do { \ + _res = (expr); \ + if (_res != DRANG_EOK) { \ + goto _drang_cleanup; \ + } \ + } while (0) + +#define DRANG_CHECK(expr, err) \ + do { \ + if (!(expr)) { \ + _res = (err); \ + goto _drang_cleanup; \ + } \ + } while (0) + +#define DRANG_FAIL(err) \ + _res = (err); \ + goto _drang_cleanup + +#define DRANG_RETURN_IN(_Out_Ptr_, _Value_) \ + _res = DRANG_EOK; \ + (*(_Out_Ptr_) = (_Value_)); \ + goto _drang_cleanup +#define DRANG_RETURN() \ + _res = DRANG_EOK; \ + goto _drang_cleanup + +#define DRANG_FAIL_IF_NULL(ptr, err) \ + do { \ + if ((ptr) == NULL) { \ + _res = (err); \ + goto _drang_cleanup; \ + } \ + } while (0) + +#define DRANG_FAIL_IF_NULL_OOM(ptr) \ + do { \ + if ((ptr) == NULL) { \ + _res = DRANG_ENOMEM; \ + goto _drang_cleanup; \ + } \ + } while (0) + +DRANG_END_DECLS diff --git a/Source/DrangPlatform/Source/error.c b/Source/DrangPlatform/Source/error.c new file mode 100644 index 0000000..6baec93 --- /dev/null +++ b/Source/DrangPlatform/Source/error.c @@ -0,0 +1,15 @@ +#include + +const char *drang_error_str(const int err) +{ + switch (err) { + case DRANG_EOK: return "No error"; + case DRANG_EINVAL: return "Invalid argument"; + case DRANG_ENOMEM: return "Out of memory"; + case DRANG_EIO: return "I/O error"; + case DRANG_NOTSUP: return "Operation not supported"; + case DRANG_EAGAIN: return "Resource temporarily unavailable"; + case DRANG_ENOENT: return "No such file or directory"; + default: return "Unknown error"; + } +}