diff --git a/src/lib/arch/XArch.cpp b/src/lib/arch/XArch.cpp deleted file mode 100644 index d805c9a8..00000000 --- a/src/lib/arch/XArch.cpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * synergy -- mouse and keyboard sharing utility - * Copyright (C) 2012 Bolton Software Ltd. - * Copyright (C) 2002 Chris Schoeneman - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file COPYING that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "arch/XArch.h" - -// -// XArch -// - -const char* -XArch::what() const _NOEXCEPT -{ - const char* what = std::runtime_error::what(); - try { - if (strlen(what) == 0 && m_eval != NULL) { - return m_eval->eval().c_str(); - } - } - catch (...) { - // ignore - } - return what; -} diff --git a/src/lib/arch/XArch.h b/src/lib/arch/XArch.h index fc0bdaae..043ef08b 100644 --- a/src/lib/arch/XArch.h +++ b/src/lib/arch/XArch.h @@ -55,26 +55,15 @@ string for that error code. */ class XArchEval { public: - XArchEval() { } - virtual ~XArchEval() { } - - virtual XArchEval* clone() const throw() = 0; - - virtual std::string eval() const throw() = 0; + virtual std::string eval() const = 0; }; //! Generic exception architecture dependent library class XArch : public std::runtime_error { public: - XArch(XArchEval* adoptedEvaluator) : std::runtime_error(""), m_eval(adoptedEvaluator) { } - XArch(const std::string& msg) : std::runtime_error(msg), m_eval(NULL) { } - XArch(const XArch& e) : std::runtime_error(e.what()), m_eval(e.m_eval != NULL ? e.m_eval->clone() : NULL) { } - ~XArch() _NOEXCEPT { delete m_eval; } - - virtual const char* what() const _NOEXCEPT; - -private: - XArchEval* m_eval; + XArch(XArchEval* adopted) : std::runtime_error(adopted->eval()) { delete adopted; } + XArch(const std::string& msg) : std::runtime_error(msg) { } + ~XArch() _NOEXCEPT { } }; // Macro to declare XArch derived types diff --git a/src/lib/arch/unix/XArchUnix.cpp b/src/lib/arch/unix/XArchUnix.cpp index 6bc94157..3cd9dffd 100644 --- a/src/lib/arch/unix/XArchUnix.cpp +++ b/src/lib/arch/unix/XArchUnix.cpp @@ -24,12 +24,6 @@ // XArchEvalUnix // -XArchEval* -XArchEvalUnix::clone() const throw() -{ - return new XArchEvalUnix(m_errno); -} - std::string XArchEvalUnix::eval() const throw() { diff --git a/src/lib/arch/unix/XArchUnix.h b/src/lib/arch/unix/XArchUnix.h index f040a629..1ff00280 100644 --- a/src/lib/arch/unix/XArchUnix.h +++ b/src/lib/arch/unix/XArchUnix.h @@ -23,13 +23,11 @@ //! Lazy error message string evaluation for unix class XArchEvalUnix : public XArchEval { public: - XArchEvalUnix(int err) : m_errno(err) { } + XArchEvalUnix(int error) : m_error(error) { } virtual ~XArchEvalUnix() { } - // XArchEval overrides - virtual XArchEval* clone() const throw(); - virtual std::string eval() const throw(); + virtual std::string eval() const; private: - int m_errno; + int m_error; }; diff --git a/src/lib/arch/win32/XArchWindows.cpp b/src/lib/arch/win32/XArchWindows.cpp index e37e1a5a..a00da98a 100644 --- a/src/lib/arch/win32/XArchWindows.cpp +++ b/src/lib/arch/win32/XArchWindows.cpp @@ -18,17 +18,12 @@ #include "arch/win32/XArchWindows.h" #include "arch/win32/ArchNetworkWinsock.h" +#include "base/String.h" // // XArchEvalWindows // -XArchEval* -XArchEvalWindows::clone() const throw() -{ - return new XArchEvalWindows(m_errno); -} - std::string XArchEvalWindows::eval() const throw() { @@ -37,13 +32,13 @@ XArchEvalWindows::eval() const throw() FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, 0, - m_errno, + m_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&cmsg, 0, NULL) == 0) { cmsg = NULL; - return "Unknown error"; + return string_format("Unknown error, code %d", m_error); } std::string smsg(cmsg); LocalFree(cmsg); @@ -55,12 +50,6 @@ XArchEvalWindows::eval() const throw() // XArchEvalWinsock // -XArchEval* -XArchEvalWinsock::clone() const throw() -{ - return new XArchEvalWinsock(m_errno); -} - std::string XArchEvalWinsock::eval() const throw() { @@ -123,7 +112,7 @@ XArchEvalWinsock::eval() const throw() }; for (unsigned int i = 0; s_netErrorCodes[i].m_code != 0; ++i) { - if (s_netErrorCodes[i].m_code == m_errno) { + if (s_netErrorCodes[i].m_code == m_error) { return s_netErrorCodes[i].m_msg; } } diff --git a/src/lib/arch/win32/XArchWindows.h b/src/lib/arch/win32/XArchWindows.h index ca92c15b..378754f1 100644 --- a/src/lib/arch/win32/XArchWindows.h +++ b/src/lib/arch/win32/XArchWindows.h @@ -26,28 +26,24 @@ //! Lazy error message string evaluation for windows class XArchEvalWindows : public XArchEval { public: - XArchEvalWindows() : m_errno(GetLastError()) { } - XArchEvalWindows(DWORD err) : m_errno(err) { } + XArchEvalWindows() : m_error(GetLastError()) { } + XArchEvalWindows(DWORD error) : m_error(error) { } virtual ~XArchEvalWindows() { } - // XArchEval overrides - virtual XArchEval* clone() const throw(); - virtual std::string eval() const throw(); + virtual std::string eval() const; private: - DWORD m_errno; + DWORD m_error; }; //! Lazy error message string evaluation for winsock class XArchEvalWinsock : public XArchEval { public: - XArchEvalWinsock(int err) : m_errno(err) { } + XArchEvalWinsock(int error) : m_error(error) { } virtual ~XArchEvalWinsock() { } - // XArchEval overrides - virtual XArchEval* clone() const throw(); - virtual std::string eval() const throw(); + virtual std::string eval() const; private: - int m_errno; + int m_error; }; diff --git a/src/lib/base/String.cpp b/src/lib/base/String.cpp index cea40b0d..8f23a2ae 100644 --- a/src/lib/base/String.cpp +++ b/src/lib/base/String.cpp @@ -17,6 +17,9 @@ #include "base/String.h" +#include +#include + void find_replace_all( CString& subject, @@ -29,3 +32,34 @@ find_replace_all( pos += replace.length(); } } + +CString +string_format(const CString format, ...) +{ + // reserve 2 times as much as the length of the format + size_t final, n = format.size() * 2; + + CString str; + std::unique_ptr formatted; + va_list ap; + + while (true) { + + // wrap the plain char array in unique_ptr + formatted.reset(new char[n]); + + strcpy(&formatted[0], format.c_str()); + va_start(ap, format); + final = vsnprintf(&formatted[0], n, format.c_str(), ap); + va_end(ap); + + if (final < 0 || final >= n) { + n += abs(static_cast(final - n + 1)); + } + else { + break; + } + } + + return CString(formatted.get()); +} diff --git a/src/lib/base/String.h b/src/lib/base/String.h index 670b3f48..99478125 100644 --- a/src/lib/base/String.h +++ b/src/lib/base/String.h @@ -25,3 +25,4 @@ typedef std::string CString; void find_replace_all(CString& subject, const CString& find, const CString& replace); +CString string_format(const CString format, ...);