00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include "postgres.h"
00053
00054 #include <locale.h>
00055 #include <time.h>
00056
00057 #include "access/htup_details.h"
00058 #include "catalog/pg_collation.h"
00059 #include "catalog/pg_control.h"
00060 #include "mb/pg_wchar.h"
00061 #include "utils/hsearch.h"
00062 #include "utils/memutils.h"
00063 #include "utils/pg_locale.h"
00064 #include "utils/syscache.h"
00065
00066 #ifdef WIN32
00067
00068
00069
00070
00071
00072 #undef StrNCpy
00073 #include <shlwapi.h>
00074 #ifdef StrNCpy
00075 #undef STrNCpy
00076 #endif
00077 #endif
00078
00079 #define MAX_L10N_DATA 80
00080
00081
00082
00083 char *locale_messages;
00084 char *locale_monetary;
00085 char *locale_numeric;
00086 char *locale_time;
00087
00088
00089 char *localized_abbrev_days[7];
00090 char *localized_full_days[7];
00091 char *localized_abbrev_months[12];
00092 char *localized_full_months[12];
00093
00094
00095 static bool CurrentLocaleConvValid = false;
00096 static bool CurrentLCTimeValid = false;
00097
00098
00099
00100 #define LC_ENV_BUFSIZE (NAMEDATALEN + 20)
00101
00102 static char lc_collate_envbuf[LC_ENV_BUFSIZE];
00103 static char lc_ctype_envbuf[LC_ENV_BUFSIZE];
00104
00105 #ifdef LC_MESSAGES
00106 static char lc_messages_envbuf[LC_ENV_BUFSIZE];
00107 #endif
00108 static char lc_monetary_envbuf[LC_ENV_BUFSIZE];
00109 static char lc_numeric_envbuf[LC_ENV_BUFSIZE];
00110 static char lc_time_envbuf[LC_ENV_BUFSIZE];
00111
00112
00113
00114 typedef struct
00115 {
00116 Oid collid;
00117 bool collate_is_c;
00118 bool ctype_is_c;
00119 bool flags_valid;
00120 pg_locale_t locale;
00121 } collation_cache_entry;
00122
00123 static HTAB *collation_cache = NULL;
00124
00125
00126 #if defined(WIN32) && defined(LC_MESSAGES)
00127 static char *IsoLocaleName(const char *);
00128 #endif
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 char *
00144 pg_perm_setlocale(int category, const char *locale)
00145 {
00146 char *result;
00147 const char *envvar;
00148 char *envbuf;
00149
00150 #ifndef WIN32
00151 result = setlocale(category, locale);
00152 #else
00153
00154
00155
00156
00157
00158
00159
00160 #ifdef LC_MESSAGES
00161 if (category == LC_MESSAGES)
00162 {
00163 result = (char *) locale;
00164 if (locale == NULL || locale[0] == '\0')
00165 return result;
00166 }
00167 else
00168 #endif
00169 result = setlocale(category, locale);
00170 #endif
00171
00172 if (result == NULL)
00173 return result;
00174
00175 switch (category)
00176 {
00177 case LC_COLLATE:
00178 envvar = "LC_COLLATE";
00179 envbuf = lc_collate_envbuf;
00180 break;
00181 case LC_CTYPE:
00182 envvar = "LC_CTYPE";
00183 envbuf = lc_ctype_envbuf;
00184 break;
00185 #ifdef LC_MESSAGES
00186 case LC_MESSAGES:
00187 envvar = "LC_MESSAGES";
00188 envbuf = lc_messages_envbuf;
00189 #ifdef WIN32
00190 result = IsoLocaleName(locale);
00191 if (result == NULL)
00192 result = (char *) locale;
00193 #endif
00194 break;
00195 #endif
00196 case LC_MONETARY:
00197 envvar = "LC_MONETARY";
00198 envbuf = lc_monetary_envbuf;
00199 break;
00200 case LC_NUMERIC:
00201 envvar = "LC_NUMERIC";
00202 envbuf = lc_numeric_envbuf;
00203 break;
00204 case LC_TIME:
00205 envvar = "LC_TIME";
00206 envbuf = lc_time_envbuf;
00207 break;
00208 default:
00209 elog(FATAL, "unrecognized LC category: %d", category);
00210 envvar = NULL;
00211 envbuf = NULL;
00212 return NULL;
00213 }
00214
00215 snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);
00216
00217 if (putenv(envbuf))
00218 return NULL;
00219
00220 return result;
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 bool
00235 check_locale(int category, const char *locale, char **canonname)
00236 {
00237 char *save;
00238 char *res;
00239
00240 if (canonname)
00241 *canonname = NULL;
00242
00243 save = setlocale(category, NULL);
00244 if (!save)
00245 return false;
00246
00247
00248 save = pstrdup(save);
00249
00250
00251 res = setlocale(category, locale);
00252
00253
00254 if (res && canonname)
00255 *canonname = pstrdup(res);
00256
00257
00258 if (!setlocale(category, save))
00259 elog(WARNING, "failed to restore old locale \"%s\"", save);
00260 pfree(save);
00261
00262 return (res != NULL);
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 bool
00278 check_locale_monetary(char **newval, void **extra, GucSource source)
00279 {
00280 return check_locale(LC_MONETARY, *newval, NULL);
00281 }
00282
00283 void
00284 assign_locale_monetary(const char *newval, void *extra)
00285 {
00286 CurrentLocaleConvValid = false;
00287 }
00288
00289 bool
00290 check_locale_numeric(char **newval, void **extra, GucSource source)
00291 {
00292 return check_locale(LC_NUMERIC, *newval, NULL);
00293 }
00294
00295 void
00296 assign_locale_numeric(const char *newval, void *extra)
00297 {
00298 CurrentLocaleConvValid = false;
00299 }
00300
00301 bool
00302 check_locale_time(char **newval, void **extra, GucSource source)
00303 {
00304 return check_locale(LC_TIME, *newval, NULL);
00305 }
00306
00307 void
00308 assign_locale_time(const char *newval, void *extra)
00309 {
00310 CurrentLCTimeValid = false;
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 bool
00324 check_locale_messages(char **newval, void **extra, GucSource source)
00325 {
00326 if (**newval == '\0')
00327 {
00328 if (source == PGC_S_DEFAULT)
00329 return true;
00330 else
00331 return false;
00332 }
00333
00334
00335
00336
00337
00338
00339 #if defined(LC_MESSAGES) && !defined(WIN32)
00340 return check_locale(LC_MESSAGES, *newval, NULL);
00341 #else
00342 return true;
00343 #endif
00344 }
00345
00346 void
00347 assign_locale_messages(const char *newval, void *extra)
00348 {
00349
00350
00351
00352
00353 #ifdef LC_MESSAGES
00354 (void) pg_perm_setlocale(LC_MESSAGES, newval);
00355 #endif
00356 }
00357
00358
00359
00360
00361
00362
00363 static void
00364 free_struct_lconv(struct lconv * s)
00365 {
00366 if (s == NULL)
00367 return;
00368
00369 if (s->currency_symbol)
00370 free(s->currency_symbol);
00371 if (s->decimal_point)
00372 free(s->decimal_point);
00373 if (s->grouping)
00374 free(s->grouping);
00375 if (s->thousands_sep)
00376 free(s->thousands_sep);
00377 if (s->int_curr_symbol)
00378 free(s->int_curr_symbol);
00379 if (s->mon_decimal_point)
00380 free(s->mon_decimal_point);
00381 if (s->mon_grouping)
00382 free(s->mon_grouping);
00383 if (s->mon_thousands_sep)
00384 free(s->mon_thousands_sep);
00385 if (s->negative_sign)
00386 free(s->negative_sign);
00387 if (s->positive_sign)
00388 free(s->positive_sign);
00389 }
00390
00391
00392
00393
00394
00395
00396 static char *
00397 db_encoding_strdup(int encoding, const char *str)
00398 {
00399 char *pstr;
00400 char *mstr;
00401
00402
00403 pstr = (char *) pg_do_encoding_conversion(
00404 (unsigned char *) str, strlen(str),
00405 encoding, GetDatabaseEncoding());
00406 mstr = strdup(pstr);
00407 if (pstr != str)
00408 pfree(pstr);
00409
00410 return mstr;
00411 }
00412
00413
00414
00415
00416
00417
00418 struct lconv *
00419 PGLC_localeconv(void)
00420 {
00421 static struct lconv CurrentLocaleConv;
00422 struct lconv *extlconv;
00423 char *save_lc_monetary;
00424 char *save_lc_numeric;
00425 char *decimal_point;
00426 char *grouping;
00427 char *thousands_sep;
00428 int encoding;
00429
00430 #ifdef WIN32
00431 char *save_lc_ctype;
00432 #endif
00433
00434
00435 if (CurrentLocaleConvValid)
00436 return &CurrentLocaleConv;
00437
00438 free_struct_lconv(&CurrentLocaleConv);
00439
00440
00441 save_lc_monetary = setlocale(LC_MONETARY, NULL);
00442 if (save_lc_monetary)
00443 save_lc_monetary = pstrdup(save_lc_monetary);
00444
00445 save_lc_numeric = setlocale(LC_NUMERIC, NULL);
00446 if (save_lc_numeric)
00447 save_lc_numeric = pstrdup(save_lc_numeric);
00448
00449 #ifdef WIN32
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 save_lc_ctype = setlocale(LC_CTYPE, NULL);
00474 if (save_lc_ctype)
00475 save_lc_ctype = pstrdup(save_lc_ctype);
00476
00477
00478 setlocale(LC_CTYPE, locale_numeric);
00479 #endif
00480
00481
00482 setlocale(LC_NUMERIC, locale_numeric);
00483 extlconv = localeconv();
00484 encoding = pg_get_encoding_from_locale(locale_numeric, true);
00485
00486 decimal_point = db_encoding_strdup(encoding, extlconv->decimal_point);
00487 thousands_sep = db_encoding_strdup(encoding, extlconv->thousands_sep);
00488 grouping = strdup(extlconv->grouping);
00489
00490 #ifdef WIN32
00491
00492 setlocale(LC_CTYPE, locale_monetary);
00493 #endif
00494
00495
00496 setlocale(LC_MONETARY, locale_monetary);
00497 extlconv = localeconv();
00498 encoding = pg_get_encoding_from_locale(locale_monetary, true);
00499
00500
00501
00502
00503
00504 CurrentLocaleConv = *extlconv;
00505 CurrentLocaleConv.decimal_point = decimal_point;
00506 CurrentLocaleConv.grouping = grouping;
00507 CurrentLocaleConv.thousands_sep = thousands_sep;
00508 CurrentLocaleConv.int_curr_symbol = db_encoding_strdup(encoding, extlconv->int_curr_symbol);
00509 CurrentLocaleConv.currency_symbol = db_encoding_strdup(encoding, extlconv->currency_symbol);
00510 CurrentLocaleConv.mon_decimal_point = db_encoding_strdup(encoding, extlconv->mon_decimal_point);
00511 CurrentLocaleConv.mon_grouping = strdup(extlconv->mon_grouping);
00512 CurrentLocaleConv.mon_thousands_sep = db_encoding_strdup(encoding, extlconv->mon_thousands_sep);
00513 CurrentLocaleConv.negative_sign = db_encoding_strdup(encoding, extlconv->negative_sign);
00514 CurrentLocaleConv.positive_sign = db_encoding_strdup(encoding, extlconv->positive_sign);
00515
00516
00517 if (save_lc_monetary)
00518 {
00519 if (!setlocale(LC_MONETARY, save_lc_monetary))
00520 elog(WARNING, "failed to restore old locale");
00521 pfree(save_lc_monetary);
00522 }
00523
00524 if (save_lc_numeric)
00525 {
00526 if (!setlocale(LC_NUMERIC, save_lc_numeric))
00527 elog(WARNING, "failed to restore old locale");
00528 pfree(save_lc_numeric);
00529 }
00530
00531 #ifdef WIN32
00532
00533 if (save_lc_ctype)
00534 {
00535 if (!setlocale(LC_CTYPE, save_lc_ctype))
00536 elog(WARNING, "failed to restore old locale");
00537 pfree(save_lc_ctype);
00538 }
00539 #endif
00540
00541 CurrentLocaleConvValid = true;
00542 return &CurrentLocaleConv;
00543 }
00544
00545 #ifdef WIN32
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 static size_t
00562 strftime_win32(char *dst, size_t dstlen, const wchar_t *format, const struct tm * tm)
00563 {
00564 size_t len;
00565 wchar_t wbuf[MAX_L10N_DATA];
00566 int encoding;
00567
00568 encoding = GetDatabaseEncoding();
00569
00570 len = wcsftime(wbuf, MAX_L10N_DATA, format, tm);
00571 if (len == 0)
00572
00573
00574
00575
00576
00577 return 0;
00578
00579 len = WideCharToMultiByte(CP_UTF8, 0, wbuf, len, dst, dstlen, NULL, NULL);
00580 if (len == 0)
00581 elog(ERROR,
00582 "could not convert string to UTF-8: error code %lu", GetLastError());
00583
00584 dst[len] = '\0';
00585 if (encoding != PG_UTF8)
00586 {
00587 char *convstr =
00588 (char *) pg_do_encoding_conversion((unsigned char *) dst,
00589 len, PG_UTF8, encoding);
00590
00591 if (dst != convstr)
00592 {
00593 strlcpy(dst, convstr, dstlen);
00594 len = strlen(dst);
00595 }
00596 }
00597
00598 return len;
00599 }
00600
00601
00602 #define strftime(a,b,c,d) strftime_win32(a,b,L##c,d)
00603 #endif
00604
00605
00606
00607
00608
00609 void
00610 cache_locale_time(void)
00611 {
00612 char *save_lc_time;
00613 time_t timenow;
00614 struct tm *timeinfo;
00615 char buf[MAX_L10N_DATA];
00616 char *ptr;
00617 int i;
00618
00619 #ifdef WIN32
00620 char *save_lc_ctype;
00621 #endif
00622
00623
00624 if (CurrentLCTimeValid)
00625 return;
00626
00627 elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);
00628
00629
00630 save_lc_time = setlocale(LC_TIME, NULL);
00631 if (save_lc_time)
00632 save_lc_time = pstrdup(save_lc_time);
00633
00634 #ifdef WIN32
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646 save_lc_ctype = setlocale(LC_CTYPE, NULL);
00647 if (save_lc_ctype)
00648 save_lc_ctype = pstrdup(save_lc_ctype);
00649
00650
00651 setlocale(LC_CTYPE, locale_time);
00652 #endif
00653
00654 setlocale(LC_TIME, locale_time);
00655
00656 timenow = time(NULL);
00657 timeinfo = localtime(&timenow);
00658
00659
00660 for (i = 0; i < 7; i++)
00661 {
00662 timeinfo->tm_wday = i;
00663 strftime(buf, MAX_L10N_DATA, "%a", timeinfo);
00664 ptr = MemoryContextStrdup(TopMemoryContext, buf);
00665 if (localized_abbrev_days[i])
00666 pfree(localized_abbrev_days[i]);
00667 localized_abbrev_days[i] = ptr;
00668
00669 strftime(buf, MAX_L10N_DATA, "%A", timeinfo);
00670 ptr = MemoryContextStrdup(TopMemoryContext, buf);
00671 if (localized_full_days[i])
00672 pfree(localized_full_days[i]);
00673 localized_full_days[i] = ptr;
00674 }
00675
00676
00677 for (i = 0; i < 12; i++)
00678 {
00679 timeinfo->tm_mon = i;
00680 timeinfo->tm_mday = 1;
00681 strftime(buf, MAX_L10N_DATA, "%b", timeinfo);
00682 ptr = MemoryContextStrdup(TopMemoryContext, buf);
00683 if (localized_abbrev_months[i])
00684 pfree(localized_abbrev_months[i]);
00685 localized_abbrev_months[i] = ptr;
00686
00687 strftime(buf, MAX_L10N_DATA, "%B", timeinfo);
00688 ptr = MemoryContextStrdup(TopMemoryContext, buf);
00689 if (localized_full_months[i])
00690 pfree(localized_full_months[i]);
00691 localized_full_months[i] = ptr;
00692 }
00693
00694
00695 if (save_lc_time)
00696 {
00697 if (!setlocale(LC_TIME, save_lc_time))
00698 elog(WARNING, "failed to restore old locale");
00699 pfree(save_lc_time);
00700 }
00701
00702 #ifdef WIN32
00703
00704 if (save_lc_ctype)
00705 {
00706 if (!setlocale(LC_CTYPE, save_lc_ctype))
00707 elog(WARNING, "failed to restore old locale");
00708 pfree(save_lc_ctype);
00709 }
00710 #endif
00711
00712 CurrentLCTimeValid = true;
00713 }
00714
00715
00716 #if defined(WIN32) && defined(LC_MESSAGES)
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 static char *
00755 IsoLocaleName(const char *winlocname)
00756 {
00757 #if (_MSC_VER >= 1400)
00758 static char iso_lc_messages[32];
00759 _locale_t loct = NULL;
00760
00761 if (pg_strcasecmp("c", winlocname) == 0 ||
00762 pg_strcasecmp("posix", winlocname) == 0)
00763 {
00764 strcpy(iso_lc_messages, "C");
00765 return iso_lc_messages;
00766 }
00767
00768 loct = _create_locale(LC_CTYPE, winlocname);
00769 if (loct != NULL)
00770 {
00771 #if (_MSC_VER >= 1700)
00772 size_t rc;
00773 char *hyphen;
00774
00775
00776 rc = wchar2char(iso_lc_messages, loct->locinfo->locale_name[LC_CTYPE],
00777 sizeof(iso_lc_messages), NULL);
00778 _free_locale(loct);
00779 if (rc == -1 || rc == sizeof(iso_lc_messages))
00780 return NULL;
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795 hyphen = strchr(iso_lc_messages, '-');
00796 if (hyphen)
00797 *hyphen = '_';
00798 #else
00799 char isolang[32],
00800 isocrty[32];
00801 LCID lcid;
00802
00803 lcid = loct->locinfo->lc_handle[LC_CTYPE];
00804 if (lcid == 0)
00805 lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
00806 _free_locale(loct);
00807
00808 if (!GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, isolang, sizeof(isolang)))
00809 return NULL;
00810 if (!GetLocaleInfoA(lcid, LOCALE_SISO3166CTRYNAME, isocrty, sizeof(isocrty)))
00811 return NULL;
00812 snprintf(iso_lc_messages, sizeof(iso_lc_messages) - 1, "%s_%s", isolang, isocrty);
00813 #endif
00814 return iso_lc_messages;
00815 }
00816 return NULL;
00817 #else
00818 return NULL;
00819 #endif
00820 }
00821 #endif
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848 static collation_cache_entry *
00849 lookup_collation_cache(Oid collation, bool set_flags)
00850 {
00851 collation_cache_entry *cache_entry;
00852 bool found;
00853
00854 Assert(OidIsValid(collation));
00855 Assert(collation != DEFAULT_COLLATION_OID);
00856
00857 if (collation_cache == NULL)
00858 {
00859
00860 HASHCTL ctl;
00861
00862 memset(&ctl, 0, sizeof(ctl));
00863 ctl.keysize = sizeof(Oid);
00864 ctl.entrysize = sizeof(collation_cache_entry);
00865 ctl.hash = oid_hash;
00866 collation_cache = hash_create("Collation cache", 100, &ctl,
00867 HASH_ELEM | HASH_FUNCTION);
00868 }
00869
00870 cache_entry = hash_search(collation_cache, &collation, HASH_ENTER, &found);
00871 if (!found)
00872 {
00873
00874
00875
00876
00877 cache_entry->flags_valid = false;
00878 cache_entry->locale = 0;
00879 }
00880
00881 if (set_flags && !cache_entry->flags_valid)
00882 {
00883
00884 HeapTuple tp;
00885 Form_pg_collation collform;
00886 const char *collcollate;
00887 const char *collctype;
00888
00889 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collation));
00890 if (!HeapTupleIsValid(tp))
00891 elog(ERROR, "cache lookup failed for collation %u", collation);
00892 collform = (Form_pg_collation) GETSTRUCT(tp);
00893
00894 collcollate = NameStr(collform->collcollate);
00895 collctype = NameStr(collform->collctype);
00896
00897 cache_entry->collate_is_c = ((strcmp(collcollate, "C") == 0) ||
00898 (strcmp(collcollate, "POSIX") == 0));
00899 cache_entry->ctype_is_c = ((strcmp(collctype, "C") == 0) ||
00900 (strcmp(collctype, "POSIX") == 0));
00901
00902 cache_entry->flags_valid = true;
00903
00904 ReleaseSysCache(tp);
00905 }
00906
00907 return cache_entry;
00908 }
00909
00910
00911
00912
00913
00914 bool
00915 lc_collate_is_c(Oid collation)
00916 {
00917
00918
00919
00920
00921 if (!OidIsValid(collation))
00922 return false;
00923
00924
00925
00926
00927
00928 if (collation == DEFAULT_COLLATION_OID)
00929 {
00930 static int result = -1;
00931 char *localeptr;
00932
00933 if (result >= 0)
00934 return (bool) result;
00935 localeptr = setlocale(LC_COLLATE, NULL);
00936 if (!localeptr)
00937 elog(ERROR, "invalid LC_COLLATE setting");
00938
00939 if (strcmp(localeptr, "C") == 0)
00940 result = true;
00941 else if (strcmp(localeptr, "POSIX") == 0)
00942 result = true;
00943 else
00944 result = false;
00945 return (bool) result;
00946 }
00947
00948
00949
00950
00951 if (collation == C_COLLATION_OID ||
00952 collation == POSIX_COLLATION_OID)
00953 return true;
00954
00955
00956
00957
00958 return (lookup_collation_cache(collation, true))->collate_is_c;
00959 }
00960
00961
00962
00963
00964 bool
00965 lc_ctype_is_c(Oid collation)
00966 {
00967
00968
00969
00970
00971 if (!OidIsValid(collation))
00972 return false;
00973
00974
00975
00976
00977
00978 if (collation == DEFAULT_COLLATION_OID)
00979 {
00980 static int result = -1;
00981 char *localeptr;
00982
00983 if (result >= 0)
00984 return (bool) result;
00985 localeptr = setlocale(LC_CTYPE, NULL);
00986 if (!localeptr)
00987 elog(ERROR, "invalid LC_CTYPE setting");
00988
00989 if (strcmp(localeptr, "C") == 0)
00990 result = true;
00991 else if (strcmp(localeptr, "POSIX") == 0)
00992 result = true;
00993 else
00994 result = false;
00995 return (bool) result;
00996 }
00997
00998
00999
01000
01001 if (collation == C_COLLATION_OID ||
01002 collation == POSIX_COLLATION_OID)
01003 return true;
01004
01005
01006
01007
01008 return (lookup_collation_cache(collation, true))->ctype_is_c;
01009 }
01010
01011
01012
01013 #ifdef HAVE_LOCALE_T
01014 static void
01015 report_newlocale_failure(const char *localename)
01016 {
01017
01018 int save_errno = errno;
01019
01020
01021
01022
01023
01024 ereport(ERROR,
01025 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
01026 errmsg("could not create locale \"%s\": %m",
01027 localename),
01028 (save_errno == ENOENT ?
01029 errdetail("The operating system could not find any locale data for the locale name \"%s\".",
01030 localename) : 0)));
01031 }
01032 #endif
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051 pg_locale_t
01052 pg_newlocale_from_collation(Oid collid)
01053 {
01054 collation_cache_entry *cache_entry;
01055
01056
01057 Assert(OidIsValid(collid));
01058
01059
01060 if (collid == DEFAULT_COLLATION_OID)
01061 return (pg_locale_t) 0;
01062
01063 cache_entry = lookup_collation_cache(collid, false);
01064
01065 if (cache_entry->locale == 0)
01066 {
01067
01068 #ifdef HAVE_LOCALE_T
01069 HeapTuple tp;
01070 Form_pg_collation collform;
01071 const char *collcollate;
01072 const char *collctype;
01073 locale_t result;
01074
01075 tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
01076 if (!HeapTupleIsValid(tp))
01077 elog(ERROR, "cache lookup failed for collation %u", collid);
01078 collform = (Form_pg_collation) GETSTRUCT(tp);
01079
01080 collcollate = NameStr(collform->collcollate);
01081 collctype = NameStr(collform->collctype);
01082
01083 if (strcmp(collcollate, collctype) == 0)
01084 {
01085
01086 #ifndef WIN32
01087 result = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate,
01088 NULL);
01089 #else
01090 result = _create_locale(LC_ALL, collcollate);
01091 #endif
01092 if (!result)
01093 report_newlocale_failure(collcollate);
01094 }
01095 else
01096 {
01097 #ifndef WIN32
01098
01099 locale_t loc1;
01100
01101 loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL);
01102 if (!loc1)
01103 report_newlocale_failure(collcollate);
01104 result = newlocale(LC_CTYPE_MASK, collctype, loc1);
01105 if (!result)
01106 report_newlocale_failure(collctype);
01107 #else
01108
01109
01110
01111
01112
01113
01114 ereport(ERROR,
01115 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01116 errmsg("collations with different collate and ctype values are not supported on this platform")));
01117 #endif
01118 }
01119
01120 cache_entry->locale = result;
01121
01122 ReleaseSysCache(tp);
01123 #else
01124
01125
01126
01127
01128
01129 ereport(ERROR,
01130 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
01131 errmsg("nondefault collations are not supported on this platform")));
01132 #endif
01133 }
01134
01135 return cache_entry->locale;
01136 }
01137
01138
01139
01140
01141
01142
01143
01144 #ifdef USE_WIDE_UPPER_LOWER
01145
01146
01147
01148
01149
01150
01151
01152
01153 size_t
01154 wchar2char(char *to, const wchar_t *from, size_t tolen, pg_locale_t locale)
01155 {
01156 size_t result;
01157
01158 if (tolen == 0)
01159 return 0;
01160
01161 #ifdef WIN32
01162
01163
01164
01165
01166
01167
01168 if (GetDatabaseEncoding() == PG_UTF8)
01169 {
01170 result = WideCharToMultiByte(CP_UTF8, 0, from, -1, to, tolen,
01171 NULL, NULL);
01172
01173 if (result <= 0)
01174 result = -1;
01175 else
01176 {
01177 Assert(result <= tolen);
01178
01179 result--;
01180 }
01181 }
01182 else
01183 #endif
01184 if (locale == (pg_locale_t) 0)
01185 {
01186
01187 result = wcstombs(to, from, tolen);
01188 }
01189 else
01190 {
01191 #ifdef HAVE_LOCALE_T
01192 #ifdef HAVE_WCSTOMBS_L
01193
01194 result = wcstombs_l(to, from, tolen, locale);
01195 #else
01196
01197 locale_t save_locale = uselocale(locale);
01198
01199 result = wcstombs(to, from, tolen);
01200
01201 uselocale(save_locale);
01202 #endif
01203 #else
01204
01205 elog(ERROR, "wcstombs_l is not available");
01206 result = 0;
01207 #endif
01208 }
01209
01210 return result;
01211 }
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222 size_t
01223 char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen,
01224 pg_locale_t locale)
01225 {
01226 size_t result;
01227
01228 if (tolen == 0)
01229 return 0;
01230
01231 #ifdef WIN32
01232
01233 if (GetDatabaseEncoding() == PG_UTF8)
01234 {
01235
01236 if (fromlen == 0)
01237 result = 0;
01238 else
01239 {
01240 result = MultiByteToWideChar(CP_UTF8, 0, from, fromlen, to, tolen - 1);
01241
01242 if (result == 0)
01243 result = -1;
01244 }
01245
01246 if (result != -1)
01247 {
01248 Assert(result < tolen);
01249
01250 to[result] = 0;
01251 }
01252 }
01253 else
01254 #endif
01255 {
01256
01257 char *str = pnstrdup(from, fromlen);
01258
01259 if (locale == (pg_locale_t) 0)
01260 {
01261
01262 result = mbstowcs(to, str, tolen);
01263 }
01264 else
01265 {
01266 #ifdef HAVE_LOCALE_T
01267 #ifdef HAVE_MBSTOWCS_L
01268
01269 result = mbstowcs_l(to, str, tolen, locale);
01270 #else
01271
01272 locale_t save_locale = uselocale(locale);
01273
01274 result = mbstowcs(to, str, tolen);
01275
01276 uselocale(save_locale);
01277 #endif
01278 #else
01279
01280 elog(ERROR, "mbstowcs_l is not available");
01281 result = 0;
01282 #endif
01283 }
01284
01285 pfree(str);
01286 }
01287
01288 if (result == -1)
01289 {
01290
01291
01292
01293
01294
01295
01296
01297
01298 pg_verifymbstr(from, fromlen, false);
01299
01300 ereport(ERROR,
01301 (errcode(ERRCODE_CHARACTER_NOT_IN_REPERTOIRE),
01302 errmsg("invalid multibyte character for locale"),
01303 errhint("The server's LC_CTYPE locale is probably incompatible with the database encoding.")));
01304 }
01305
01306 return result;
01307 }
01308
01309 #endif