diff options
-rw-r--r-- | c/main.cpp | 6 | ||||
-rw-r--r-- | c/pl.cpp | 26 | ||||
-rw-r--r-- | c/pl.h | 4 |
3 files changed, 20 insertions, 16 deletions
@@ -64,9 +64,9 @@ void OnTerminate() noexcept try { std::rethrow_exception(std::current_exception()); } catch (const term_t& t) { - TCHAR* tsz; - if (PL_get_tchars(t, &tsz, CVT_WRITE)) - TerminateMsg<TCHAR>(TEXT("a Prolog exception"), tsz); + std::basic_string<TCHAR> tstr; + if (PlString(t, &tstr)) + TerminateMsg<TCHAR>(TEXT("a Prolog exception"), tstr.c_str()); else TerminateMsg<char>("a Prolog exception", NULL); } catch (const Win32Error& e) { @@ -78,16 +78,20 @@ int Query::NextSolution() } /* Convert Prolog term to normal or wide characters. */ -int PL_get_tchars(const term_t t, TCHAR** const pTsz, const int iFlags) +template <> +int PlString<char>(const term_t t, std::string* const pStr, const int iFlags) { -#ifdef UNICODE - size_t len; - if (!PL_get_wchars(t, &len, pTsz, iFlags)) - return 0; - return len; -#else - if (!PL_get_chars(t, pTsz, iFlags)) - return 0; - return -1; -#endif + char* sz; + int r = PL_get_chars(t, &sz, iFlags); + if (r) *pStr = sz; + return r; +} + +template <> +int PlString<wchar_t>(const term_t t, std::wstring* const pWstr, const int iFlags) +{ + char* sz; + int r = PL_get_chars(t, &sz, iFlags); + if (r) *pWstr = BstrFromSz<wchar_t>(sz); + return r; } @@ -7,8 +7,6 @@ #include "common.h" -int PL_get_tchars(term_t t, TCHAR** pTsz, int iFlags); - struct Frame { Frame(); @@ -39,6 +37,8 @@ private: qid_t m_q; }; +template <typename T> int PlString(term_t t, std::basic_string<T>* pBstr, int iFlags = CVT_WRITE); + /* Polymorphic aliases for PL_put_*, PL_get_*. */ inline int PlPut(term_t t, int x) { return PL_put_integer(t, x); } inline int PlPut(term_t t, long x) { return PL_put_integer(t, x); } |