2002-04-03 07:39:33 +02:00
|
|
|
/*-----------------------------------------------------------------------
|
2000-01-07 18:22:47 +01:00
|
|
|
*
|
2002-04-03 07:39:33 +02:00
|
|
|
* PostgreSQL locale utilities
|
2000-01-07 18:22:47 +01:00
|
|
|
*
|
2002-04-03 07:39:33 +02:00
|
|
|
* Portions Copyright (c) 2002, PostgreSQL Global Development Group
|
2000-02-08 16:57:01 +01:00
|
|
|
*
|
2003-07-27 06:53:12 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/pg_locale.c,v 1.21 2003/07/27 04:53:07 tgl Exp $
|
2002-10-18 22:44:02 +02:00
|
|
|
*
|
2002-04-03 07:39:33 +02:00
|
|
|
*-----------------------------------------------------------------------
|
2000-01-07 18:22:47 +01:00
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2002-10-18 22:44:02 +02:00
|
|
|
/*----------
|
2002-08-10 00:52:04 +02:00
|
|
|
* Here is how the locale stuff is handled: LC_COLLATE and LC_CTYPE
|
|
|
|
* are fixed by initdb, stored in pg_control, and cannot be changed.
|
|
|
|
* Thus, the effects of strcoll(), strxfrm(), isupper(), toupper(),
|
|
|
|
* etc. are always in the same fixed locale.
|
|
|
|
*
|
|
|
|
* LC_MESSAGES is settable at run time and will take effect
|
|
|
|
* immediately.
|
|
|
|
*
|
|
|
|
* The other categories, LC_MONETARY, LC_NUMERIC, and LC_TIME are also
|
|
|
|
* settable at run-time. However, we don't actually set those locale
|
2002-10-18 22:44:02 +02:00
|
|
|
* categories permanently. This would have bizarre effects like no
|
2002-08-10 00:52:04 +02:00
|
|
|
* longer accepting standard floating-point literals in some locales.
|
|
|
|
* Instead, we only set the locales briefly when needed, cache the
|
|
|
|
* required information obtained from localeconv(), and set them back.
|
2002-10-18 22:44:02 +02:00
|
|
|
* The cached information is only used by the formatting functions
|
|
|
|
* (to_char, etc.) and the money type. For the user, this should all be
|
2002-08-10 00:52:04 +02:00
|
|
|
* transparent. (Actually, LC_TIME doesn't do anything at all right
|
|
|
|
* now.)
|
2002-10-18 22:44:02 +02:00
|
|
|
*
|
|
|
|
* !!! NOW HEAR THIS !!!
|
|
|
|
*
|
|
|
|
* We've been bitten repeatedly by this bug, so let's try to keep it in
|
|
|
|
* mind in future: on some platforms, the locale functions return pointers
|
|
|
|
* to static data that will be overwritten by any later locale function.
|
|
|
|
* Thus, for example, the obvious-looking sequence
|
|
|
|
* save = setlocale(category, NULL);
|
|
|
|
* if (!setlocale(category, value))
|
|
|
|
* fail = true;
|
|
|
|
* setlocale(category, save);
|
|
|
|
* DOES NOT WORK RELIABLY: on some platforms the second setlocale() call
|
|
|
|
* will change the memory save is pointing at. To do this sort of thing
|
|
|
|
* safely, you *must* pstrdup what setlocale returns the first time.
|
|
|
|
*----------
|
2002-08-10 00:52:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2000-01-07 18:22:47 +01:00
|
|
|
#include "postgres.h"
|
2002-05-17 03:19:19 +02:00
|
|
|
|
2002-04-03 07:39:33 +02:00
|
|
|
#include <locale.h>
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
#include "utils/pg_locale.h"
|
|
|
|
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
/* indicated whether locale information cache is valid */
|
|
|
|
static bool CurrentLocaleConvValid = false;
|
|
|
|
|
|
|
|
|
2002-04-03 07:39:33 +02:00
|
|
|
/* GUC storage area */
|
2001-09-29 23:16:30 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
char *locale_messages;
|
|
|
|
char *locale_monetary;
|
|
|
|
char *locale_numeric;
|
|
|
|
char *locale_time;
|
2000-08-29 06:41:48 +02:00
|
|
|
|
2000-11-25 23:43:08 +01:00
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
/* GUC assign hooks */
|
2001-09-29 23:16:30 +02:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
/*
|
|
|
|
* This is common code for several locale categories. This doesn't
|
|
|
|
* actually set the locale permanently, it only tests if the locale is
|
|
|
|
* valid. (See explanation at the top of this file.)
|
|
|
|
*/
|
2002-05-17 03:19:19 +02:00
|
|
|
static const char *
|
|
|
|
locale_xxx_assign(int category, const char *value, bool doit, bool interactive)
|
2000-01-07 18:22:47 +01:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
char *save;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
save = setlocale(category, NULL);
|
|
|
|
if (!save)
|
2002-10-18 22:44:02 +02:00
|
|
|
return NULL; /* won't happen, we hope */
|
|
|
|
|
|
|
|
/* save may be pointing at a modifiable scratch variable, see above */
|
|
|
|
save = pstrdup(save);
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
if (!setlocale(category, value))
|
2002-10-18 22:44:02 +02:00
|
|
|
value = NULL; /* set failure return marker */
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2002-10-18 22:44:02 +02:00
|
|
|
setlocale(category, save); /* assume this won't fail */
|
|
|
|
pfree(save);
|
2002-08-10 00:52:04 +02:00
|
|
|
|
2002-10-18 22:44:02 +02:00
|
|
|
/* need to reload cache next time? */
|
|
|
|
if (doit && value != NULL)
|
2002-08-10 00:52:04 +02:00
|
|
|
CurrentLocaleConvValid = false;
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
return value;
|
2000-01-07 18:22:47 +01:00
|
|
|
}
|
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
const char *
|
|
|
|
locale_monetary_assign(const char *value, bool doit, bool interactive)
|
2002-04-03 07:39:33 +02:00
|
|
|
{
|
2002-05-17 03:19:19 +02:00
|
|
|
return locale_xxx_assign(LC_MONETARY, value, doit, interactive);
|
2002-04-03 07:39:33 +02:00
|
|
|
}
|
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
const char *
|
|
|
|
locale_numeric_assign(const char *value, bool doit, bool interactive)
|
2002-04-03 07:39:33 +02:00
|
|
|
{
|
2002-05-17 03:19:19 +02:00
|
|
|
return locale_xxx_assign(LC_NUMERIC, value, doit, interactive);
|
2002-04-03 07:39:33 +02:00
|
|
|
}
|
|
|
|
|
2002-05-17 03:19:19 +02:00
|
|
|
const char *
|
|
|
|
locale_time_assign(const char *value, bool doit, bool interactive)
|
2000-01-07 18:22:47 +01:00
|
|
|
{
|
2002-05-17 03:19:19 +02:00
|
|
|
return locale_xxx_assign(LC_TIME, value, doit, interactive);
|
2002-04-03 07:39:33 +02:00
|
|
|
}
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2000-11-25 23:43:08 +01:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
/*
|
2002-10-18 22:44:02 +02:00
|
|
|
* We allow LC_MESSAGES to actually be set globally.
|
2002-08-10 00:52:04 +02:00
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
locale_messages_assign(const char *value, bool doit, bool interactive)
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
|
|
|
* LC_MESSAGES category does not exist everywhere, but accept it
|
|
|
|
* anyway
|
|
|
|
*/
|
2002-08-10 00:52:04 +02:00
|
|
|
#ifdef LC_MESSAGES
|
|
|
|
if (doit)
|
|
|
|
{
|
|
|
|
if (!setlocale(LC_MESSAGES, value))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-10-18 22:44:02 +02:00
|
|
|
value = locale_xxx_assign(LC_MESSAGES, value, false, interactive);
|
2002-08-10 00:52:04 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-03 07:39:33 +02:00
|
|
|
/*
|
|
|
|
* We'd like to cache whether LC_COLLATE is C (or POSIX), so we can
|
|
|
|
* optimize a few code paths in various places.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
lc_collate_is_c(void)
|
|
|
|
{
|
|
|
|
/* Cache result so we only have to compute it once */
|
|
|
|
static int result = -1;
|
|
|
|
char *localeptr;
|
|
|
|
|
|
|
|
if (result >= 0)
|
|
|
|
return (bool) result;
|
|
|
|
localeptr = setlocale(LC_COLLATE, NULL);
|
|
|
|
if (!localeptr)
|
2003-07-27 06:53:12 +02:00
|
|
|
elog(ERROR, "invalid LC_COLLATE setting");
|
2002-04-03 07:39:33 +02:00
|
|
|
|
|
|
|
if (strcmp(localeptr, "C") == 0)
|
|
|
|
result = true;
|
|
|
|
else if (strcmp(localeptr, "POSIX") == 0)
|
|
|
|
result = true;
|
|
|
|
else
|
|
|
|
result = false;
|
|
|
|
return (bool) result;
|
2000-01-07 18:22:47 +01:00
|
|
|
}
|
|
|
|
|
2002-04-03 07:39:33 +02:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
/*
|
|
|
|
* Frees the malloced content of a struct lconv. (But not the struct
|
|
|
|
* itself.)
|
|
|
|
*/
|
|
|
|
static void
|
2002-09-04 22:31:48 +02:00
|
|
|
free_struct_lconv(struct lconv * s)
|
2002-08-10 00:52:04 +02:00
|
|
|
{
|
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (s->currency_symbol)
|
|
|
|
free(s->currency_symbol);
|
|
|
|
if (s->decimal_point)
|
|
|
|
free(s->decimal_point);
|
|
|
|
if (s->grouping)
|
|
|
|
free(s->grouping);
|
|
|
|
if (s->thousands_sep)
|
|
|
|
free(s->thousands_sep);
|
|
|
|
if (s->int_curr_symbol)
|
|
|
|
free(s->int_curr_symbol);
|
|
|
|
if (s->mon_decimal_point)
|
|
|
|
free(s->mon_decimal_point);
|
|
|
|
if (s->mon_grouping)
|
|
|
|
free(s->mon_grouping);
|
|
|
|
if (s->mon_thousands_sep)
|
|
|
|
free(s->mon_thousands_sep);
|
|
|
|
if (s->negative_sign)
|
|
|
|
free(s->negative_sign);
|
|
|
|
if (s->positive_sign)
|
|
|
|
free(s->positive_sign);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-03 07:39:33 +02:00
|
|
|
/*
|
|
|
|
* Return the POSIX lconv struct (contains number/money formatting
|
|
|
|
* information) with locale information for all categories.
|
2000-01-07 18:22:47 +01:00
|
|
|
*/
|
|
|
|
struct lconv *
|
2000-03-18 19:57:16 +01:00
|
|
|
PGLC_localeconv(void)
|
2000-01-07 18:22:47 +01:00
|
|
|
{
|
2002-04-03 07:39:33 +02:00
|
|
|
static struct lconv CurrentLocaleConv;
|
2002-05-17 03:19:19 +02:00
|
|
|
struct lconv *extlconv;
|
2002-08-10 00:52:04 +02:00
|
|
|
char *save_lc_monetary;
|
|
|
|
char *save_lc_numeric;
|
2002-05-17 03:19:19 +02:00
|
|
|
|
2000-11-25 23:43:08 +01:00
|
|
|
/* Did we do it already? */
|
2001-09-29 23:16:30 +02:00
|
|
|
if (CurrentLocaleConvValid)
|
|
|
|
return &CurrentLocaleConv;
|
2000-01-07 18:22:47 +01:00
|
|
|
|
2002-08-10 00:52:04 +02:00
|
|
|
free_struct_lconv(&CurrentLocaleConv);
|
|
|
|
|
2002-10-18 22:44:02 +02:00
|
|
|
/* Set user's values of monetary and numeric locales */
|
2002-08-10 00:52:04 +02:00
|
|
|
save_lc_monetary = setlocale(LC_MONETARY, NULL);
|
2002-10-18 22:44:02 +02:00
|
|
|
if (save_lc_monetary)
|
|
|
|
save_lc_monetary = pstrdup(save_lc_monetary);
|
2002-08-10 00:52:04 +02:00
|
|
|
save_lc_numeric = setlocale(LC_NUMERIC, NULL);
|
2002-10-18 22:44:02 +02:00
|
|
|
if (save_lc_numeric)
|
|
|
|
save_lc_numeric = pstrdup(save_lc_numeric);
|
2002-08-10 00:52:04 +02:00
|
|
|
|
|
|
|
setlocale(LC_MONETARY, locale_monetary);
|
|
|
|
setlocale(LC_NUMERIC, locale_numeric);
|
|
|
|
|
|
|
|
/* Get formatting information */
|
2001-09-29 23:16:30 +02:00
|
|
|
extlconv = localeconv();
|
|
|
|
|
2001-10-25 07:50:21 +02:00
|
|
|
/*
|
|
|
|
* Must copy all values since restoring internal settings may
|
2002-10-18 22:44:02 +02:00
|
|
|
* overwrite localeconv()'s results.
|
2001-10-25 07:50:21 +02:00
|
|
|
*/
|
2001-09-29 23:16:30 +02:00
|
|
|
CurrentLocaleConv = *extlconv;
|
|
|
|
CurrentLocaleConv.currency_symbol = strdup(extlconv->currency_symbol);
|
|
|
|
CurrentLocaleConv.decimal_point = strdup(extlconv->decimal_point);
|
|
|
|
CurrentLocaleConv.grouping = strdup(extlconv->grouping);
|
|
|
|
CurrentLocaleConv.thousands_sep = strdup(extlconv->thousands_sep);
|
|
|
|
CurrentLocaleConv.int_curr_symbol = strdup(extlconv->int_curr_symbol);
|
|
|
|
CurrentLocaleConv.mon_decimal_point = strdup(extlconv->mon_decimal_point);
|
|
|
|
CurrentLocaleConv.mon_grouping = strdup(extlconv->mon_grouping);
|
|
|
|
CurrentLocaleConv.mon_thousands_sep = strdup(extlconv->mon_thousands_sep);
|
|
|
|
CurrentLocaleConv.negative_sign = strdup(extlconv->negative_sign);
|
|
|
|
CurrentLocaleConv.positive_sign = strdup(extlconv->positive_sign);
|
2002-08-10 00:52:04 +02:00
|
|
|
CurrentLocaleConv.n_sign_posn = extlconv->n_sign_posn;
|
|
|
|
|
2002-10-18 22:44:02 +02:00
|
|
|
/* Try to restore internal settings */
|
|
|
|
if (save_lc_monetary)
|
|
|
|
{
|
|
|
|
setlocale(LC_MONETARY, save_lc_monetary);
|
|
|
|
pfree(save_lc_monetary);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (save_lc_numeric)
|
|
|
|
{
|
|
|
|
setlocale(LC_NUMERIC, save_lc_numeric);
|
|
|
|
pfree(save_lc_numeric);
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2001-09-29 23:16:30 +02:00
|
|
|
CurrentLocaleConvValid = true;
|
|
|
|
return &CurrentLocaleConv;
|
2000-01-07 18:22:47 +01:00
|
|
|
}
|