From f3b2dd65f5d9fd4f3ef7a3ca94efd5b2882e358c Mon Sep 17 00:00:00 2001 From: couriersud Date: Sun, 6 Sep 2020 12:12:24 +0200 Subject: netlist: fix multi byte string issues. --- src/lib/netlist/plib/pdynlib.cpp | 6 +++--- src/lib/netlist/plib/pfmtlog.h | 11 ++++++++++ src/lib/netlist/plib/pstonum.h | 43 +++++++++++++++++++--------------------- src/lib/netlist/plib/pstream.h | 8 ++------ 4 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/lib/netlist/plib/pdynlib.cpp b/src/lib/netlist/plib/pdynlib.cpp index c33120049d5..f7a651c353b 100644 --- a/src/lib/netlist/plib/pdynlib.cpp +++ b/src/lib/netlist/plib/pdynlib.cpp @@ -22,7 +22,7 @@ dynlib::dynlib(const pstring &libname) #ifdef _WIN32 //fprintf(stderr, "win: loading <%s>\n", libname.c_str()); if (!libname.empty()) - m_lib = LoadLibrary(winapi_string(libname).c_str()); + m_lib = LoadLibrary(winapi_string(putf8string(libname)).c_str()); else m_lib = GetModuleHandle(nullptr); #elif defined(__EMSCRIPTEN__) @@ -48,7 +48,7 @@ dynlib::dynlib(const pstring &path, const pstring &libname) // printf("win: loading <%s>\n", libname.c_str()); #ifdef _WIN32 if (!libname.empty()) - m_lib = LoadLibrary(winapi_string(libname).c_str()); + m_lib = LoadLibrary(winapi_string(putf8string(libname)).c_str()); else m_lib = GetModuleHandle(nullptr); #elif defined(__EMSCRIPTEN__) @@ -83,7 +83,7 @@ dynlib::~dynlib() void *dynlib::getsym_p(const pstring &name) const noexcept { #ifdef _WIN32 - return (void *) GetProcAddress((HMODULE) m_lib, name.c_str()); + return (void *) GetProcAddress((HMODULE) m_lib, putf8string(name).c_str()); #else return dlsym(m_lib, putf8string(name).c_str()); #endif diff --git a/src/lib/netlist/plib/pfmtlog.h b/src/lib/netlist/plib/pfmtlog.h index 9f3ff382583..a7485f8bfe6 100644 --- a/src/lib/netlist/plib/pfmtlog.h +++ b/src/lib/netlist/plib/pfmtlog.h @@ -183,6 +183,17 @@ namespace plib { } }; + template<> + struct ptype_traits : ptype_traits_base + { + static char32_t fmt_spec() { return 's'; } + static inline void streamify(std::ostream &s, const char32_t *v) + { + const putf32string su32(v); + s << putf8string(su32).c_str(); + } + }; + template<> struct ptype_traits : ptype_traits_base { diff --git a/src/lib/netlist/plib/pstonum.h b/src/lib/netlist/plib/pstonum.h index b3e0df5504e..3be3f949b7a 100644 --- a/src/lib/netlist/plib/pstonum.h +++ b/src/lib/netlist/plib/pstonum.h @@ -27,11 +27,11 @@ namespace plib // ---------------------------------------------------------------------------------------- template - T pstonum_locale(const std::locale &loc, const S &arg, std::size_t *idx) + T pstonum_locale(const std::locale &loc, const S &arg, bool *err) { std::stringstream ss; ss.imbue(loc); - ss << arg; + ss << putf8string(arg); auto len(ss.tellp()); T x(constants::zero()); if (ss >> x) @@ -39,10 +39,10 @@ namespace plib auto pos(ss.tellg()); if (pos == decltype(pos)(-1)) pos = len; - *idx = narrow_cast(pos); + *err = (pos != len); } else - *idx = constants::zero(); + *err = true; return x; } @@ -53,10 +53,10 @@ namespace plib struct pstonum_helper::value && plib::is_signed::value>> { template - long long operator()(std::locale loc, const S &arg, std::size_t *idx) + long long operator()(std::locale loc, const S &arg, bool *err) { //return std::stoll(arg, idx); - return pstonum_locale(loc, arg, idx); + return pstonum_locale(loc, arg, err); } }; @@ -64,10 +64,10 @@ namespace plib struct pstonum_helper::value && !plib::is_signed::value>> { template - unsigned long long operator()(std::locale loc, const S &arg, std::size_t *idx) + unsigned long long operator()(std::locale loc, const S &arg, bool *err) { //return std::stoll(arg, idx); - return pstonum_locale(loc, arg, idx); + return pstonum_locale(loc, arg, err); } }; @@ -75,9 +75,9 @@ namespace plib struct pstonum_helper::value>> { template - long double operator()(std::locale loc, const S &arg, std::size_t *idx) + long double operator()(std::locale loc, const S &arg, bool *err) { - return pstonum_locale(loc, arg, idx); + return pstonum_locale(loc, arg, err); } }; @@ -87,9 +87,9 @@ namespace plib { // FIXME: use strtoflt128 from quadmath.h template - FLOAT128 operator()(std::locale loc, const S &arg, std::size_t *idx) + FLOAT128 operator()(std::locale loc, const S &arg, bool *err) { - return narrow_cast(pstonum_locale(loc, arg, idx)); + return narrow_cast(pstonum_locale(loc, arg, err)); } }; #endif @@ -97,22 +97,19 @@ namespace plib template T pstonum(const S &arg, const std::locale &loc = std::locale::classic()) noexcept(false) { - putf8string u8arg(arg); - decltype(u8arg.c_str()) cstr(u8arg.c_str()); - std::size_t idx(0); - auto ret = pstonum_helper()(loc, cstr, &idx); + bool err(false); + auto ret = pstonum_helper()(loc, arg, &err); + if (err) + throw pexception(pstring("Error converting string to number: ") + pstring(arg)); + using ret_type = decltype(ret); if (ret >= narrow_cast(plib::numeric_limits::lowest()) && ret <= narrow_cast(plib::numeric_limits::max())) { - if (cstr[idx] != 0) - throw pexception(pstring("Continuation after numeric value ends: ") + pstring(cstr)); + return narrow_cast(ret); } - else - { - throw pexception(pstring("Out of range: ") + pstring(cstr)); - } - return narrow_cast(ret); + + throw pexception(pstring("Out of range: ") + pstring(arg)); } template diff --git a/src/lib/netlist/plib/pstream.h b/src/lib/netlist/plib/pstream.h index a52b6194fad..e69671df90c 100644 --- a/src/lib/netlist/plib/pstream.h +++ b/src/lib/netlist/plib/pstream.h @@ -372,12 +372,10 @@ public: { } -#if 0 explicit ifstream(const std::string name, ios_base::openmode mode = ios_base::in) - : std::ifstream(filename_type(name).c_str(), mode) + : std::ifstream(filename_type(putf8string(name)).c_str(), mode) { } -#endif }; /// @@ -395,12 +393,10 @@ public: { } -#if 0 explicit ofstream(const std::string name, ios_base::openmode mode = ios_base::out | ios_base::trunc) - : std::ofstream(filename_type(name).c_str(), mode) + : std::ofstream(filename_type(putf8string(name)).c_str(), mode) { } -#endif }; -- cgit v1.2.3