2005-11-21 20:27:57 +08:00
|
|
|
#include "ts_locale.h"
|
|
|
|
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/pg_locale.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
|
|
|
|
|
|
|
|
2005-12-12 19:10:12 +08:00
|
|
|
#ifdef TS_USE_WIDE
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
size_t
|
2005-11-23 02:17:34 +08:00
|
|
|
wchar2char(char *to, const wchar_t *from, size_t len)
|
|
|
|
{
|
|
|
|
if (GetDatabaseEncoding() == PG_UTF8)
|
|
|
|
{
|
|
|
|
int r,
|
|
|
|
nbytes;
|
|
|
|
|
|
|
|
if (len == 0)
|
2005-11-21 20:27:57 +08:00
|
|
|
return 0;
|
|
|
|
|
2005-11-22 17:01:35 +08:00
|
|
|
/* in any case, *to should be allocated with enough space */
|
|
|
|
nbytes = WideCharToMultiByte(CP_UTF8, 0, from, len, NULL, 0, NULL, NULL);
|
2005-11-23 02:17:34 +08:00
|
|
|
if (nbytes == 0)
|
2005-11-22 17:01:35 +08:00
|
|
|
ereport(ERROR,
|
2005-11-23 02:17:34 +08:00
|
|
|
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
|
|
|
|
errmsg("UTF-16 to UTF-8 translation failed: %lu",
|
|
|
|
GetLastError())));
|
2005-11-22 17:01:35 +08:00
|
|
|
|
2005-11-21 20:27:57 +08:00
|
|
|
r = WideCharToMultiByte(CP_UTF8, 0, from, len, to, nbytes,
|
2005-11-23 02:17:34 +08:00
|
|
|
NULL, NULL);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (r == 0)
|
2005-11-21 20:27:57 +08:00
|
|
|
ereport(ERROR,
|
2005-11-23 02:17:34 +08:00
|
|
|
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
|
|
|
|
errmsg("UTF-16 to UTF-8 translation failed: %lu",
|
|
|
|
GetLastError())));
|
2005-11-21 20:27:57 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wcstombs(to, from, len);
|
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
size_t
|
|
|
|
char2wchar(wchar_t *to, const char *from, size_t len)
|
|
|
|
{
|
|
|
|
if (GetDatabaseEncoding() == PG_UTF8)
|
|
|
|
{
|
|
|
|
int r;
|
2005-11-21 20:27:57 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (len == 0)
|
2005-11-21 20:27:57 +08:00
|
|
|
return 0;
|
|
|
|
|
2005-11-22 17:01:35 +08:00
|
|
|
r = MultiByteToWideChar(CP_UTF8, 0, from, len, to, len);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (!r)
|
|
|
|
{
|
2005-11-21 20:27:57 +08:00
|
|
|
pg_verifymbstr(from, len, false);
|
|
|
|
ereport(ERROR,
|
2005-11-23 02:17:34 +08:00
|
|
|
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
|
|
|
|
errmsg("invalid multibyte character for locale"),
|
|
|
|
errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
|
2005-11-21 20:27:57 +08:00
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
Assert(r <= len);
|
2005-11-21 20:27:57 +08:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
2005-11-23 02:17:34 +08:00
|
|
|
|
2005-11-21 20:27:57 +08:00
|
|
|
return mbstowcs(to, from, len);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
#endif /* WIN32 */
|
2005-12-12 19:10:12 +08:00
|
|
|
|
|
|
|
int
|
2006-10-04 08:30:14 +08:00
|
|
|
_t_isalpha(const char *ptr)
|
|
|
|
{
|
|
|
|
wchar_t character;
|
2005-12-12 19:10:12 +08:00
|
|
|
|
|
|
|
char2wchar(&character, ptr, 1);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return iswalpha((wint_t) character);
|
2005-12-12 19:10:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-10-04 08:30:14 +08:00
|
|
|
_t_isprint(const char *ptr)
|
|
|
|
{
|
|
|
|
wchar_t character;
|
2005-12-12 19:10:12 +08:00
|
|
|
|
|
|
|
char2wchar(&character, ptr, 1);
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
return iswprint((wint_t) character);
|
2005-12-12 19:10:12 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
#endif /* TS_USE_WIDE */
|
2005-12-12 19:10:12 +08:00
|
|
|
|
|
|
|
char *
|
|
|
|
lowerstr(char *str)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
char *ptr = str;
|
2005-12-12 19:10:12 +08:00
|
|
|
|
|
|
|
#ifdef TS_USE_WIDE
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2005-12-12 19:10:12 +08:00
|
|
|
/*
|
|
|
|
* Use wide char code only when max encoding length > 1 and ctype != C.
|
|
|
|
* Some operating systems fail with multi-byte encodings and a C locale.
|
|
|
|
* Also, for a C locale there is no need to process as multibyte. From
|
|
|
|
* backend/utils/adt/oracle_compat.c Teodor
|
|
|
|
*/
|
2006-10-04 08:30:14 +08:00
|
|
|
if (pg_database_encoding_max_length() > 1 && !lc_ctype_is_c())
|
|
|
|
{
|
|
|
|
wchar_t *wstr,
|
|
|
|
*wptr;
|
|
|
|
int len = strlen(str);
|
|
|
|
|
|
|
|
wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len + 1));
|
|
|
|
char2wchar(wstr, str, len + 1);
|
|
|
|
while (*wptr)
|
|
|
|
{
|
|
|
|
*wptr = towlower((wint_t) *wptr);
|
|
|
|
wptr++;
|
|
|
|
}
|
|
|
|
wchar2char(str, wstr, len);
|
|
|
|
pfree(wstr);
|
|
|
|
}
|
|
|
|
else
|
2005-11-21 20:27:57 +08:00
|
|
|
#endif
|
2005-12-12 19:10:12 +08:00
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
*ptr = tolower(*(unsigned char *) ptr);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|