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)
|
|
|
|
{
|
2007-01-15 23:16:11 +08:00
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (GetDatabaseEncoding() == PG_UTF8)
|
|
|
|
{
|
2006-11-20 22:03:30 +08:00
|
|
|
int r;
|
2005-11-23 02:17:34 +08:00
|
|
|
|
2006-11-20 22:03:30 +08:00
|
|
|
r = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, len,
|
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())));
|
2006-11-20 22:03:30 +08:00
|
|
|
Assert(r <= len);
|
|
|
|
|
2005-11-21 20:27:57 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wcstombs(to, from, len);
|
|
|
|
}
|
2007-01-15 23:16:11 +08:00
|
|
|
#endif /* WIN32 */
|
2005-11-21 20:27:57 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
size_t
|
|
|
|
char2wchar(wchar_t *to, const char *from, size_t len)
|
|
|
|
{
|
2007-01-15 23:16:11 +08:00
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
2005-11-23 02:17:34 +08:00
|
|
|
if (GetDatabaseEncoding() == PG_UTF8)
|
|
|
|
{
|
|
|
|
int r;
|
2005-11-21 20:27:57 +08:00
|
|
|
|
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)
|
|
|
|
{
|
2006-11-20 22:03:30 +08:00
|
|
|
pg_verifymbstr(from, strlen(from), false);
|
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("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;
|
|
|
|
}
|
2007-01-15 23:16:11 +08:00
|
|
|
else
|
|
|
|
#endif /* WIN32 */
|
2005-11-23 02:17:34 +08:00
|
|
|
|
2009-03-02 23:13:17 +08:00
|
|
|
Assert( !lc_ctype_is_c() );
|
2005-11-21 20:27:57 +08:00
|
|
|
return mbstowcs(to, from, len);
|
|
|
|
}
|
2005-12-12 19:10:12 +08:00
|
|
|
|
|
|
|
int
|
2006-10-04 08:30:14 +08:00
|
|
|
_t_isalpha(const char *ptr)
|
|
|
|
{
|
2007-01-15 23:16:11 +08:00
|
|
|
wchar_t character[2];
|
|
|
|
|
|
|
|
if (lc_ctype_is_c())
|
|
|
|
return isalpha(TOUCHAR(ptr));
|
2005-12-12 19:10:12 +08:00
|
|
|
|
2007-01-15 23:16:11 +08:00
|
|
|
char2wchar(character, ptr, 1);
|
2005-12-12 19:10:12 +08:00
|
|
|
|
2007-01-15 23:16:11 +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)
|
|
|
|
{
|
2007-01-15 23:16:11 +08:00
|
|
|
wchar_t character[2];
|
|
|
|
|
|
|
|
if (lc_ctype_is_c())
|
|
|
|
return isprint(TOUCHAR(ptr));
|
2005-12-12 19:10:12 +08:00
|
|
|
|
2007-01-15 23:16:11 +08:00
|
|
|
char2wchar(character, ptr, 1);
|
2005-12-12 19:10:12 +08:00
|
|
|
|
2007-01-15 23:16:11 +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;
|
2006-11-20 22:03:30 +08:00
|
|
|
char *out;
|
|
|
|
int len = strlen(str);
|
|
|
|
|
|
|
|
if ( len == 0 )
|
|
|
|
return pstrdup("");
|
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;
|
2006-11-20 22:03:30 +08:00
|
|
|
int wlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
*alloc number of wchar_t for worst case, len contains
|
|
|
|
* number of bytes <= number of characters and
|
|
|
|
* alloc 1 wchar_t for 0, because wchar2char(wcstombs in really)
|
|
|
|
* wants zero-terminated string
|
|
|
|
*/
|
|
|
|
wptr = wstr = (wchar_t *) palloc(sizeof(wchar_t) * (len+1));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* str SHOULD be cstring, so wlen contains number
|
|
|
|
* of converted character
|
|
|
|
*/
|
|
|
|
wlen = char2wchar(wstr, str, len);
|
|
|
|
if ( wlen < 0 )
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
|
2007-01-15 23:16:11 +08:00
|
|
|
errmsg("translation failed from server encoding to wchar_t")));
|
2006-11-20 22:03:30 +08:00
|
|
|
|
|
|
|
Assert(wlen<=len);
|
|
|
|
wstr[wlen] = 0;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
while (*wptr)
|
|
|
|
{
|
|
|
|
*wptr = towlower((wint_t) *wptr);
|
|
|
|
wptr++;
|
|
|
|
}
|
2006-11-20 22:03:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Alloc result string for worst case + '\0'
|
|
|
|
*/
|
|
|
|
len = sizeof(char)*pg_database_encoding_max_length()*(wlen+1);
|
|
|
|
out = (char*)palloc(len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wlen now is number of bytes which is always >= number of characters
|
|
|
|
*/
|
|
|
|
wlen = wchar2char(out, wstr, len);
|
2006-10-04 08:30:14 +08:00
|
|
|
pfree(wstr);
|
2006-11-20 22:03:30 +08:00
|
|
|
|
|
|
|
if ( wlen < 0 )
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
|
2007-01-15 23:16:11 +08:00
|
|
|
errmsg("translation failed from wchar_t to server encoding %d", errno)));
|
2006-11-20 22:03:30 +08:00
|
|
|
Assert(wlen<=len);
|
|
|
|
out[wlen]='\0';
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
2005-11-21 20:27:57 +08:00
|
|
|
#endif
|
2006-11-20 22:03:30 +08:00
|
|
|
{
|
|
|
|
char *outptr;
|
|
|
|
|
|
|
|
outptr = out = (char*)palloc( sizeof(char) * (len+1) );
|
2005-12-12 19:10:12 +08:00
|
|
|
while (*ptr)
|
|
|
|
{
|
2006-11-20 22:03:30 +08:00
|
|
|
*outptr++ = tolower(*(unsigned char *) ptr);
|
2005-12-12 19:10:12 +08:00
|
|
|
ptr++;
|
|
|
|
}
|
2006-11-20 22:03:30 +08:00
|
|
|
*outptr = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2005-12-12 19:10:12 +08:00
|
|
|
}
|