Simplify some locale_t code

This commit is contained in:
Evan Miller 2021-08-22 12:20:13 -04:00
parent d1369407c5
commit d3eea5f38e
2 changed files with 24 additions and 15 deletions

View File

@ -65,6 +65,13 @@
// M$VC see http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc // M$VC see http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc
#define MDB_DEPRECATED(type, funcname) type __attribute__((deprecated)) funcname #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 { enum {
MDB_PAGE_DB = 0, MDB_PAGE_DB = 0,
MDB_PAGE_DATA, MDB_PAGE_DATA,
@ -311,10 +318,8 @@ typedef struct {
#if MDBTOOLS_H_HAVE_ICONV_H #if MDBTOOLS_H_HAVE_ICONV_H
iconv_t iconv_in; iconv_t iconv_in;
iconv_t iconv_out; iconv_t iconv_out;
#elif defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS)
_locale_t locale;
#else #else
locale_t locale; mdb_locale_t locale;
#endif #endif
} MdbHandle; } MdbHandle;

View File

@ -88,15 +88,7 @@ static size_t latin1_to_utf8_without_iconv(const char *in_ptr, size_t len_in, ch
return out - dest; 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) { static size_t unicode2ascii_locale(mdb_locale_t locale, 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;
}
size_t i; size_t i;
size_t count = 0; size_t count = 0;
size_t len_out = dlen - 1; 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'; w[len_in/2] = '\0';
#if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64) || defined(WINDOWS) #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) #elif defined(HAVE_WCSTOMBS_L)
count = wcstombs_l(dest, w, len_out, mdb->locale); count = wcstombs_l(dest, w, len_out, locale);
#else #else
locale_t oldlocale = uselocale(mdb->locale); locale_t oldlocale = uselocale(locale);
count = wcstombs(dest, w, len_out); count = wcstombs(dest, w, len_out);
uselocale(oldlocale); uselocale(oldlocale);
#endif #endif
@ -124,6 +116,18 @@ static size_t decompressed_to_utf8_without_iconv(MdbHandle *mdb, const char *in_
dest[count] = '\0'; dest[count] = '\0';
return count; 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 #endif
/* /*