From db9f741875ad1fd3c7c3e5b932420081862159bc Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sat, 22 Dec 2018 02:47:07 +0100 Subject: wip --- src/common/error.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'src/common/error.c') diff --git a/src/common/error.c b/src/common/error.c index a3a08e7..ef6edff 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -6,9 +6,19 @@ * See the file LICENSE. */ +#ifdef HAVE_CONFIG_H +#include +#endif /* HAVE_CONFIG_H */ + +#ifdef HAVE_WINDOWS_H +#include +#endif /* HAVE_WINDOWS_H */ + +#include #include #include #include +#include #include "error.h" #include "utils.h" @@ -23,19 +33,73 @@ bc_error_new(bc_error_type_t type, const char *msg) } +bc_error_t* +bc_error_new_vprintf(bc_error_type_t type, const char *format, va_list ap) +{ + char *tmp = bc_strdup_vprintf(format, ap); + bc_error_t *rv = bc_error_new(type, tmp); + free(tmp); + return rv; +} + + bc_error_t* bc_error_new_printf(bc_error_type_t type, const char *format, ...) { va_list ap; va_start(ap, format); - char *tmp = bc_strdup_vprintf(format, ap); + bc_error_t *rv = bc_error_new_vprintf(type, format, ap); va_end(ap); - bc_error_t *rv = bc_error_new(type, tmp); + return rv; +} + + +bc_error_t* +bc_error_new_errno_vprintf(bc_error_type_t type, int errno_, const char *format, + va_list ap) +{ + char *tmp = bc_strdup_vprintf(format, ap); +#if defined(WIN32) || defined(_WIN32) + LPTSTR buf = "bola"; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, errno_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&buf, + 0, NULL); + bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, buf); + LocalFree(buf); +#else + bc_error_t *rv = bc_error_new_printf(type, "%s: %s", tmp, strerror(errno_)); +#endif free(tmp); return rv; } +bc_error_t* +bc_error_new_errno_printf(bc_error_type_t type, int errno_, const char *format, ...) +{ + va_list ap; + va_start(ap, format); + bc_error_t *rv = bc_error_new_errno_vprintf(type, errno_, format, ap); + va_end(ap); + return rv; +} + + +bc_error_t* +bc_error_new_default_errno_printf(bc_error_type_t type, const char *format, ...) +{ + va_list ap; + va_start(ap, format); +#if defined(WIN32) || defined(_WIN32) + bc_error_t *rv = bc_error_new_errno_vprintf(type, GetLastError(), format, ap); +#else + bc_error_t *rv = bc_error_new_errno_vprintf(type, errno, format, ap); +#endif + va_end(ap); + return rv; +} + + bc_error_t* bc_error_parser(bc_error_type_t type, const char *src, size_t src_len, size_t current, const char *format, ...) -- cgit v1.2.3-18-g5258