From d3eea5f38e69021adef4ab789efb283d51a6c86f Mon Sep 17 00:00:00 2001 From: Evan Miller Date: Sun, 22 Aug 2021 12:20:13 -0400 Subject: [PATCH] Simplify some locale_t code --- include/mdbtools.h.in | 11 ++++++++--- src/libmdb/iconv.c | 28 ++++++++++++++++------------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/include/mdbtools.h.in b/include/mdbtools.h.in index 9f7f518..c4b6fd8 100644 --- a/include/mdbtools.h.in +++ b/include/mdbtools.h.in @@ -65,6 +65,13 @@ // M$VC see http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc #define MDB_DEPRECATED(type, funcname) type __attribute__((deprecated)) funcname +#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS) +typedef _locale_t mdb_locale_t; +#else +typedef locale_t mdb_locale_t; +#endif + + enum { MDB_PAGE_DB = 0, MDB_PAGE_DATA, @@ -311,10 +318,8 @@ typedef struct { #if MDBTOOLS_H_HAVE_ICONV_H iconv_t iconv_in; iconv_t iconv_out; -#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS) - _locale_t locale; #else - locale_t locale; + mdb_locale_t locale; #endif } MdbHandle; diff --git a/src/libmdb/iconv.c b/src/libmdb/iconv.c index f8f2071..9302eb6 100644 --- a/src/libmdb/iconv.c +++ b/src/libmdb/iconv.c @@ -88,15 +88,7 @@ static size_t latin1_to_utf8_without_iconv(const char *in_ptr, size_t len_in, ch return out - dest; } -static size_t decompressed_to_utf8_without_iconv(MdbHandle *mdb, const char *in_ptr, size_t len_in, char *dest, size_t dlen) { - if (IS_JET3(mdb)) { - if (mdb->f->code_page == 1252) { - return latin1_to_utf8_without_iconv(in_ptr, len_in, dest, dlen); - } - int count = 0; - snprintf(dest, dlen, "%.*s%n", (int)len_in, in_ptr, &count); - return count; - } +static size_t unicode2ascii_locale(mdb_locale_t locale, const char *in_ptr, size_t len_in, char *dest, size_t dlen) { size_t i; size_t count = 0; size_t len_out = dlen - 1; @@ -109,11 +101,11 @@ static size_t decompressed_to_utf8_without_iconv(MdbHandle *mdb, const char *in_ w[len_in/2] = '\0'; #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS) - count = _wcstombs_l(dest, w, len_out, mdb->locale); + count = _wcstombs_l(dest, w, len_out, locale); #elif defined(HAVE_WCSTOMBS_L) - count = wcstombs_l(dest, w, len_out, mdb->locale); + count = wcstombs_l(dest, w, len_out, locale); #else - locale_t oldlocale = uselocale(mdb->locale); + locale_t oldlocale = uselocale(locale); count = wcstombs(dest, w, len_out); uselocale(oldlocale); #endif @@ -124,6 +116,18 @@ static size_t decompressed_to_utf8_without_iconv(MdbHandle *mdb, const char *in_ dest[count] = '\0'; return count; } + +static size_t decompressed_to_utf8_without_iconv(MdbHandle *mdb, const char *in_ptr, size_t len_in, char *dest, size_t dlen) { + if (IS_JET3(mdb)) { + if (mdb->f->code_page == 1252) { + return latin1_to_utf8_without_iconv(in_ptr, len_in, dest, dlen); + } + int count = 0; + snprintf(dest, dlen, "%.*s%n", (int)len_in, in_ptr, &count); + return count; + } + return unicode2ascii_locale(mdb->locale, in_ptr, len_in, dest, dlen); +} #endif /*