Mercurial > hg > th-libs
diff th_util.c @ 96:3df6bec3c6b1
Add error handling stuff.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 22 May 2014 03:29:08 +0300 |
parents | 029b3dea31ff |
children | 1760e7f53359 |
line wrap: on
line diff
--- a/th_util.c Wed May 21 22:31:10 2014 +0300 +++ b/th_util.c Thu May 22 03:29:08 2014 +0300 @@ -7,6 +7,8 @@ */ #include "th_util.h" #include <stdio.h> +#include <errno.h> + /* Default settings */ @@ -122,6 +124,49 @@ } +/* Error handling + */ +int th_get_errno() +{ + return TH_SYSTEM_ERRORS + errno; +} + + +const char *th_error_str(int error) +{ + if (error >= TH_SYSTEM_ERRORS) + return strerror(error - TH_SYSTEM_ERRORS); + + switch (error) + { + case THERR_OK: return "No error"; + case THERR_FOPEN: return "File open error"; + case THERR_FREAD: return "Read error"; + case THERR_FWRITE: return "Write error"; + case THERR_FSEEK: return "Seek error"; + case THERR_NOT_FOUND: return "Resource not found"; + + case THERR_INVALID_DATA: return "Invalid data"; + case THERR_MALLOC: return "Memory allocation failure"; + case THERR_ALREADY_INIT: return "Already initialized"; + case THERR_INIT_FAIL: return "Initialization failed"; + case THERR_INVALID_ARGS: return "Invalid arguments"; + + case THERR_NULLPTR: return "NULL pointer"; + case THERR_NOT_SUPPORTED: return "Operation not supported"; + case THERR_OUT_OF_DATA: return "Out of data"; + case THERR_EXTRA_DATA: return "Extra data"; + case THERR_BOUNDS: return "Bounds check failed"; + + case THERR_TIMED_OUT: return "Operation timed out"; + + case THERR_AUTH_FAILED: return "Authentication failed"; + + default: return "Unknown error"; + } +} + + /* Memory handling routines */ void *th_malloc(size_t l)