From 18f1cc97b736a0d67280b65f323fc1a98bd7cfb3 Mon Sep 17 00:00:00 2001 From: Andreas Huggel Date: Mon, 25 Jan 2010 15:00:54 +0000 Subject: [PATCH] More W/Error changes to better support w/what(). --- src/error.cpp | 79 +++++++++++++++++++++++++++++++++++++-------------- src/error.hpp | 72 ++++++++++++++++------------------------------ 2 files changed, 81 insertions(+), 70 deletions(-) diff --git a/src/error.cpp b/src/error.cpp index 69146088..4017df39 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -115,34 +115,69 @@ namespace Exiv2 { { } - //! Specialization for Error template<> - const char* BasicError::what() const throw() + void BasicError::setMsg() { - return msg_.c_str(); + std::string msg = _(errMsg(code_)); + std::string::size_type pos; + pos = msg.find("%0"); + if (pos != std::string::npos) { + msg.replace(pos, 2, toString(code_)); + } + if (count_ > 0) { + pos = msg.find("%1"); + if (pos != std::string::npos) { + msg.replace(pos, 2, arg1_); + } + } + if (count_ > 1) { + pos = msg.find("%2"); + if (pos != std::string::npos) { + msg.replace(pos, 2, arg2_); + } + } + if (count_ > 2) { + pos = msg.find("%3"); + if (pos != std::string::npos) { + msg.replace(pos, 2, arg3_); + } + } + msg_ = msg; +#ifdef EXV_UNICODE_PATH + wmsg_ = s2ws(msg); +#endif } - //! Specialization for WError template<> - const char* BasicError::what() const throw() + void BasicError::setMsg() { - // Todo: Return something more useful - return 0; - } - - //! Specialization for Error - template<> - const wchar_t* BasicError::wwhat() const throw() - { - // Todo: Return something more useful - return 0; - } - - //! Specialization for Error - template<> - const wchar_t* BasicError::wwhat() const throw() - { - return msg_.c_str(); + std::string s = _(errMsg(code_)); + std::wstring wmsg(s.begin(), s.end()); + std::wstring::size_type pos; + pos = wmsg.find(L"%0"); + if (pos != std::wstring::npos) { + wmsg.replace(pos, 2, toBasicString(code_)); + } + if (count_ > 0) { + pos = wmsg.find(L"%1"); + if (pos != std::wstring::npos) { + wmsg.replace(pos, 2, arg1_); + } + } + if (count_ > 1) { + pos = wmsg.find(L"%2"); + if (pos != std::wstring::npos) { + wmsg.replace(pos, 2, arg2_); + } + } + if (count_ > 2) { + pos = wmsg.find(L"%3"); + if (pos != std::wstring::npos) { + wmsg.replace(pos, 2, arg3_); + } + } + wmsg_ = wmsg; +// msg_ = ws2s(wmsg); } const char* errMsg(int code) diff --git a/src/error.hpp b/src/error.hpp index 8cf6a8bd..ccd992a0 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -120,18 +120,18 @@ namespace Exiv2 { @brief Return the error message as a C-string. The pointer returned by what() is valid only as long as the BasicError object exists. */ - EXIV2API virtual const char* what() const throw(); + EXV_DLLLOCAL virtual const char* what() const throw(); /*! @brief Return the error message as a wchar_t-string. The pointer returned by wwhat() is valid only as long as the BasicError object exists. */ - EXIV2API virtual const wchar_t* wwhat() const throw(); + EXV_DLLLOCAL virtual const wchar_t* wwhat() const throw(); //@} private: //! @name Manipulators //@{ - EXV_DLLLOCAL void setMsg(); + EXIV2API void setMsg(); //@} // DATA @@ -140,7 +140,8 @@ namespace Exiv2 { std::basic_string arg1_; //!< First argument std::basic_string arg2_; //!< Second argument std::basic_string arg3_; //!< Third argument - std::basic_string msg_; //!< Complete error message + std::string msg_; //!< Complete error message + std::wstring wmsg_; //!< Complete error message as a wide string }; // class BasicError @@ -160,17 +161,6 @@ namespace Exiv2 { setMsg(); } - template - BasicError::~BasicError() throw() - { - } - - template - int BasicError::code() const throw() - { - return code_; - } - template template BasicError::BasicError(int code, const A& arg1) : code_(code), count_(1), arg1_(toBasicString(arg1)) @@ -198,40 +188,26 @@ namespace Exiv2 { } template - void BasicError::setMsg() + BasicError::~BasicError() throw() + { + } + + template + int BasicError::code() const throw() + { + return code_; + } + + template + const char* BasicError::what() const throw() + { + return msg_.c_str(); + } + + template + const wchar_t* BasicError::wwhat() const throw() { - std::string s(exvGettext(errMsg(code_))); - msg_.assign(s.begin(), s.end()); - std::string ph("%0"); - std::basic_string tph(ph.begin(), ph.end()); - size_t pos = msg_.find(tph); - if (pos != std::basic_string::npos) { - msg_.replace(pos, 2, toBasicString(code_)); - } - if (count_ > 0) { - ph = "%1"; - tph.assign(ph.begin(), ph.end()); - pos = msg_.find(tph); - if (pos != std::basic_string::npos) { - msg_.replace(pos, 2, arg1_); - } - } - if (count_ > 1) { - ph = "%2"; - tph.assign(ph.begin(), ph.end()); - pos = msg_.find(tph); - if (pos != std::basic_string::npos) { - msg_.replace(pos, 2, arg2_); - } - } - if (count_ > 2) { - ph = "%3"; - tph.assign(ph.begin(), ph.end()); - pos = msg_.find(tph); - if (pos != std::basic_string::npos) { - msg_.replace(pos, 2, arg3_); - } - } + return wmsg_.c_str(); } #ifdef _MSC_VER