aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-24 02:05:30 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-24 02:05:30 +0200
commit98f28569b631d36a6f129a17ea7589b48884c1eb (patch)
tree45fdc18108edaf45fa424324591ab7a7aaa1db10
parent3a133c4063cbb81bee9f5bc55427be75d8e584e0 (diff)
downloadEpisodeBrowser-98f28569b631d36a6f129a17ea7589b48884c1eb.tar.gz
Fix PL_get_tchars.
See c6cd2f1.
-rw-r--r--c/main.cpp6
-rw-r--r--c/pl.cpp26
-rw-r--r--c/pl.h4
3 files changed, 20 insertions, 16 deletions
diff --git a/c/main.cpp b/c/main.cpp
index 6865e4c..d59fd1d 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -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) {
diff --git a/c/pl.cpp b/c/pl.cpp
index 46ed717..a1849db 100644
--- a/c/pl.cpp
+++ b/c/pl.cpp
@@ -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;
}
diff --git a/c/pl.h b/c/pl.h
index d1c01c4..4a44f72 100644
--- a/c/pl.h
+++ b/c/pl.h
@@ -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); }