From 06f182507ea72b700c9786ed3ed01b2f81eb5c7b Mon Sep 17 00:00:00 2001 From: Ricardo Martins Date: Mon, 16 Apr 2012 05:35:24 +0100 Subject: [PATCH] WIP --- CMakeLists.txt | 5 ++++- src/rpl/dll.c | 17 ++++++++++++++++- src/rpl/dll.h | 3 ++- src/rpl/error.c | 37 +++++++++++++++++++++++++++++++++---- src/rpl/error.h | 11 +++++++++++ src/rpl/rpl.h | 1 + 6 files changed, 67 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db6e496..39827d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ check_symbol_exists(RPL_OS_LINUX "rpl/platform.h" RPL_OS_LINUX) check_symbol_exists(RPL_OS_WINDOWS "rpl/platform.h" RPL_OS_WINDOWS) if (RPL_OS_LINUX) - set(RPL_LIBS pthread rt) + set(RPL_LIBS pthread rt dl) elseif(RPL_OS_WINDOWS) set(RPL_LIBS wsock32 ws2_32) endif() @@ -25,6 +25,9 @@ target_link_libraries(test_platform rpl ${RPL_LIBS}) add_executable(test_error test/test_error.c) target_link_libraries(test_error rpl ${RPL_LIBS}) +add_executable(test_dll test/test_dll.c) +target_link_libraries(test_dll rpl ${RPL_LIBS}) + add_executable(test_udp_socket test/test_udp_socket.c) target_link_libraries(test_udp_socket rpl ${RPL_LIBS}) diff --git a/src/rpl/dll.c b/src/rpl/dll.c index ddedc83..0e151df 100644 --- a/src/rpl/dll.c +++ b/src/rpl/dll.c @@ -23,6 +23,7 @@ /* RPL headers. */ #include +#include #include /* Platform headers. */ @@ -58,16 +59,28 @@ rpl_dll_free(rpl_dll_t* dll) *dll = NULL; } -void +rpl_bool_t rpl_dll_open(rpl_dll_t dll, const char* file) { rpl_dll_close(dll); #if defined(RPL_OS_WINDOWS) dll->handle = LoadLibrary(file); + if (dll->handle != NULL) + return RPL_TRUE; + + rpl_error_set(GetLastError()); + #elif defined(RPL_OS_UNIX) dll->handle = dlopen(file, RTLD_NOW); + if (dll->handle != NULL) + return RPL_TRUE; + + rpl_error_set(RPL_ERROR_NO_CODE); + rpl_error_set_message(dlerror()); #endif + + return RPL_FALSE; } void @@ -104,5 +117,7 @@ rpl_dll_get(rpl_dll_t dll, const char* symbol_name) symbol = dlsym(dll->handle, symbol_name); #endif + + return symbol; } diff --git a/src/rpl/dll.h b/src/rpl/dll.h index 9eedfba..c3d7e6f 100644 --- a/src/rpl/dll.h +++ b/src/rpl/dll.h @@ -26,6 +26,7 @@ /* RPL headers. */ #include +#include /** Dynamic-link Library object. */ typedef struct rpl_dll* rpl_dll_t; @@ -36,7 +37,7 @@ rpl_dll_new(void); void rpl_dll_free(rpl_dll_t* dll); -void +rpl_bool_t rpl_dll_open(rpl_dll_t dll, const char* file); void diff --git a/src/rpl/error.c b/src/rpl/error.c index bf31ea0..2ce4db2 100644 --- a/src/rpl/error.c +++ b/src/rpl/error.c @@ -91,6 +91,20 @@ rpl_error_set(int error) e->code = error; } +void +rpl_error_clear(void) +{ + struct rpl_error* e = rpl_tls_key_get_value(rpl_error_key); + e->code = 0; +} + +void +rpl_error_set_message(const char* message) +{ + struct rpl_error* e = rpl_tls_key_get_value(rpl_error_key); + strncpy(e->message, message, MAX_MESSAGE_SIZE); +} + const char* rpl_error_translate(int error) { @@ -108,14 +122,23 @@ rpl_error_translate(int error) struct rpl_error* e = rpl_tls_key_get_value(rpl_error_key); #if defined(RPL_OS_UNIX) + if (e->code == RPL_ERROR_NO_CODE) + return e->message; + if (strerror_r(error, e->message, MAX_MESSAGE_SIZE) == 0) return e->message; #elif defined(RPL_OS_WINDOWS) - WORD lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT); - DWORD size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, "%0", - error, lang, e->message, MAX_MESSAGE_SIZE, - NULL); + WORD lang = 0; + DWORD size = 0; + + if (e->code == RPL_ERROR_NO_CODE) + return e->message; + + lang = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT); + size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, "%0", + error, lang, e->message, MAX_MESSAGE_SIZE, + NULL); if (size > 0) { for (--size; size >= 0; --size) @@ -132,3 +155,9 @@ rpl_error_translate(int error) return "unable to translate error"; } +const char* +rpl_error_translate_last(void) +{ + struct rpl_error* e = rpl_tls_key_get_value(rpl_error_key); + return rpl_error_translate(e->code); +} diff --git a/src/rpl/error.h b/src/rpl/error.h index 24f802b..4534ae4 100644 --- a/src/rpl/error.h +++ b/src/rpl/error.h @@ -25,6 +25,8 @@ extern rpl_tls_key_t rpl_error_key; +#define RPL_ERROR_NO_CODE 2147483647 + void rpl_error_init(void); @@ -43,7 +45,16 @@ rpl_error_get(void); void rpl_error_set(int error); +void +rpl_error_clear(void); + +void +rpl_error_set_message(const char* message); + const char* rpl_error_translate(int error); +const char* +rpl_error_translate_last(void); + #endif diff --git a/src/rpl/rpl.h b/src/rpl/rpl.h index 403b746..837d75e 100644 --- a/src/rpl/rpl.h +++ b/src/rpl/rpl.h @@ -25,6 +25,7 @@ #include #include #include +#include /** * @defgroup INIT Initialization