From: František Dvořák Date: Wed, 9 Oct 2013 12:05:46 +0000 (+0200) Subject: Add format strings versions of throw helper functions. X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=b1cefe26d7ec536dc5e837e9893b5cd70d88a48c;p=dmlite-plugins-vfs.git Add format strings versions of throw helper functions. --- diff --git a/src/Throw.cpp b/src/Throw.cpp index f7f0cd6..d51888b 100644 --- a/src/Throw.cpp +++ b/src/Throw.cpp @@ -2,6 +2,8 @@ /// @brief Maps the errno codes to DM_ERROR_* /// @author Alejandro Álvarez Ayllón #include +#include +#include #include #include "Vfs.h" @@ -13,12 +15,33 @@ void dmlite::vfsThrowHelper(const std::string &classname, const std::string& fun +void dmlite::vfsThrowHelper(const std::string &classname, const std::string& func, int code, const char *fmt, ...) throw (DmException) +{ + va_list ap; + char *s; + int ret; + std::string msg; + + va_start(ap, fmt); + ret = vasprintf(&s, fmt, ap); + va_end(ap); + + if (ret == -1) + throw new DmException(DMLITE_SYSERR(ENOMEM)); + + msg = std::string(s); + free(s); + dmlite::vfsThrowHelper(classname, func, code, msg); +} + + + int dmlite::vfsWrapCallHelper(const std::string &classname, const std::string &func, int r, const std::string& msg) throw (DmException) { if (r < 0) { if (msg.length() != 0) vfsThrowHelper(classname, func, errno, msg + ": " + strerror(errno)); else - vfsThrowHelper(classname, func, errno, strerror(errno)); + vfsThrowHelper(classname, func, errno, std::string(strerror(errno))); } return r; } @@ -30,7 +53,61 @@ void *dmlite::vfsWrapCallHelper(const std::string &classname, const std::string if (msg.length() != 0) vfsThrowHelper(classname, func, errno, msg + ": " + strerror(errno)); else - vfsThrowHelper(classname, func, errno, strerror(errno)); + vfsThrowHelper(classname, func, errno, std::string(strerror(errno))); } return p; } + + + +int dmlite::vfsWrapCallHelper(const std::string &classname, const std::string &func, int r, const char *fmt, ...) throw (DmException) +{ + va_list ap; + char *s; + int ret, origerrno; + std::string msg; + + origerrno = errno; + + va_start(ap, fmt); + ret = vasprintf(&s, fmt, ap); + va_end(ap); + + if (ret == -1) + throw new DmException(DMLITE_SYSERR(ENOMEM)); + + msg = std::string(s); + free(s); + + if (r < 0) + vfsThrowHelper(classname, func, origerrno, msg + ": " + strerror(origerrno)); + + return r; +} + + + +void *dmlite::vfsWrapCallHelper(const std::string &classname, const std::string &func, void *p, const char *fmt, ...) throw (DmException) +{ + va_list ap; + char *s; + int ret, origerrno; + std::string msg; + + origerrno = errno; + + va_start(ap, fmt); + ret = vasprintf(&s, fmt, ap); + va_end(ap); + + if (ret == -1) + throw new DmException(DMLITE_SYSERR(ENOMEM)); + + msg = std::string(s); + free(s); + + if (!p) + vfsThrowHelper(classname, func, origerrno, msg + ": " + strerror(origerrno)); + + return p; +} diff --git a/src/Vfs.h b/src/Vfs.h index 82bb060..93a3e32 100644 --- a/src/Vfs.h +++ b/src/Vfs.h @@ -10,12 +10,14 @@ #include #include -#define vfsThrowErrno(MSG) dmlite::vfsThrowHelper(getImplId(), __func__, DMLITE_SYSERR(errno), (MSG)) -#define vfsThrow(CODE, MSG) dmlite::vfsThrowHelper(getImplId(), __func__, (CODE), (MSG)) #ifdef __GNUC__ + #define vfsThrowErrno(MSG...) dmlite::vfsThrowHelper(getImplId(), __func__, DMLITE_SYSERR(errno) , ##MSG) + #define vfsThrow(CODE, MSG...) dmlite::vfsThrowHelper(getImplId(), __func__, (CODE) , ##MSG) #define wrapCall(RETVAL, MSG...) dmlite::vfsWrapCallHelper(getImplId(), __func__, (RETVAL) , ##MSG) #else + #define vfsThrowErrno(MSG, ...) dmlite::vfsThrowHelper(getImplId(), __func__, DMLITE_SYSERR(errno), (MSG) , ##__VA_ARGS__) + #define vfsThrow(CODE, MSG, ...) dmlite::vfsThrowHelper(getImplId(), __func__, (CODE), (MSG) , ##__VA_ARGS__) #define wrapCall(RETVAL, ...) dmlite::vfsWrapCallHelper(getImplId(), __func__, (RETVAL) , ##__VA_ARGS__) #endif @@ -55,6 +57,9 @@ namespace dmlite { int vfsWrapCallHelper(const std::string &classname, const std::string &func, int r, const std::string& msg = "") throw (DmException); void *vfsWrapCallHelper(const std::string &classname, const std::string &func, void *p, const std::string& msg = "") throw (DmException); + void vfsThrowHelper(const std::string &classname, const std::string& func, int code, const char *fmt, ...) throw (DmException); + int vfsWrapCallHelper(const std::string &classname, const std::string &func, int r, const char *fmt, ...) throw (DmException); + void *vfsWrapCallHelper(const std::string &classname, const std::string &func, void *p, const char *fmt, ...) throw (DmException); }; #endif // VFS_H