# HG changeset patch # User Matti Hamalainen # Date 1400718548 -10800 # Node ID 3df6bec3c6b1e23aa281feeb6aeb3d66dd5bdfc7 # Parent d86ad68f326fbdd5fd1d4a09e5e889273a12e9ed Add error handling stuff. diff -r d86ad68f326f -r 3df6bec3c6b1 th_util.c --- 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 +#include + /* 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) diff -r d86ad68f326f -r 3df6bec3c6b1 th_util.h --- a/th_util.h Wed May 21 22:31:10 2014 +0300 +++ b/th_util.h Thu May 22 03:29:08 2014 +0300 @@ -58,6 +58,42 @@ # endif #endif + +/* Error codes + */ +enum { + // General error codes + THERR_OK = 0, + THERR_PROGRESS, // Status OK, but operation in progress + + THERR_INTERNAL, + + THERR_FOPEN, + THERR_FREAD, + THERR_FWRITE, + THERR_FSEEK, + THERR_NOT_FOUND, // Resource/data not found + + THERR_INVALID_DATA, // Some data was invalid + THERR_MALLOC, // Memory allocation failure + THERR_ALREADY_INIT, // Resource has already been initialized + THERR_INIT_FAIL, // General initialization failure + THERR_INVALID_ARGS, + + THERR_NULLPTR, // NULL pointer specified in critical argument + THERR_NOT_SUPPORTED,// Operation not supported + THERR_OUT_OF_DATA, + THERR_EXTRA_DATA, + THERR_BOUNDS, + THERR_TIMED_OUT, + + // Network errors + THERR_AUTH_FAILED, +}; + +#define TH_SYSTEM_ERRORS 100000 + + /* Global variables */ extern int th_verbosityLevel; @@ -73,6 +109,9 @@ char *author, char *license); void th_print_banner(FILE *outFile, const char *binName, const char *usage); +int th_get_errno(); +const char *th_error_str(int error); + void THERR(const char *, ...); void THMSG(int, const char *, ...); void THPRINT(int, const char *, ...);