Header And Logo

PostgreSQL
| The world's most advanced open source database.

Typedefs | Functions | Variables

pg_locale.h File Reference

#include <locale.h>
#include "utils/guc.h"
Include dependency graph for pg_locale.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef int pg_locale_t

Functions

bool check_locale_messages (char **newval, void **extra, GucSource source)
void assign_locale_messages (const char *newval, void *extra)
bool check_locale_monetary (char **newval, void **extra, GucSource source)
void assign_locale_monetary (const char *newval, void *extra)
bool check_locale_numeric (char **newval, void **extra, GucSource source)
void assign_locale_numeric (const char *newval, void *extra)
bool check_locale_time (char **newval, void **extra, GucSource source)
void assign_locale_time (const char *newval, void *extra)
bool check_locale (int category, const char *locale, char **canonname)
char * pg_perm_setlocale (int category, const char *locale)
bool lc_collate_is_c (Oid collation)
bool lc_ctype_is_c (Oid collation)
struct lconv * PGLC_localeconv (void)
void cache_locale_time (void)
pg_locale_t pg_newlocale_from_collation (Oid collid)

Variables

char * locale_messages
char * locale_monetary
char * locale_numeric
char * locale_time
char * localized_abbrev_days []
char * localized_full_days []
char * localized_abbrev_months []
char * localized_full_months []

Typedef Documentation

typedef int pg_locale_t

Definition at line 70 of file pg_locale.h.


Function Documentation

void assign_locale_messages ( const char *  newval,
void *  extra 
)

Definition at line 347 of file pg_locale.c.

References pg_perm_setlocale().

{
    /*
     * LC_MESSAGES category does not exist everywhere, but accept it anyway.
     * We ignore failure, as per comment above.
     */
#ifdef LC_MESSAGES
    (void) pg_perm_setlocale(LC_MESSAGES, newval);
#endif
}

void assign_locale_monetary ( const char *  newval,
void *  extra 
)

Definition at line 284 of file pg_locale.c.

References CurrentLocaleConvValid.

void assign_locale_numeric ( const char *  newval,
void *  extra 
)

Definition at line 296 of file pg_locale.c.

References CurrentLocaleConvValid.

void assign_locale_time ( const char *  newval,
void *  extra 
)

Definition at line 308 of file pg_locale.c.

References CurrentLCTimeValid.

{
    CurrentLCTimeValid = false;
}

void cache_locale_time ( void   ) 

Definition at line 610 of file pg_locale.c.

References buf, CurrentLCTimeValid, DEBUG3, elog, i, locale_time, localized_abbrev_days, localized_abbrev_months, localized_full_days, localized_full_months, MAX_L10N_DATA, MemoryContextStrdup(), NULL, pfree(), pstrdup(), TopMemoryContext, and WARNING.

Referenced by DCH_to_char().

{
    char       *save_lc_time;
    time_t      timenow;
    struct tm  *timeinfo;
    char        buf[MAX_L10N_DATA];
    char       *ptr;
    int         i;

#ifdef WIN32
    char       *save_lc_ctype;
#endif

    /* did we do this already? */
    if (CurrentLCTimeValid)
        return;

    elog(DEBUG3, "cache_locale_time() executed; locale: \"%s\"", locale_time);

    /* save user's value of time locale */
    save_lc_time = setlocale(LC_TIME, NULL);
    if (save_lc_time)
        save_lc_time = pstrdup(save_lc_time);

#ifdef WIN32

    /*
     * On WIN32, there is no way to get locale-specific time values in a
     * specified locale, like we do for monetary/numeric.  We can only get
     * CP_ACP (see strftime_win32) or UTF16.  Therefore, we get UTF16 and
     * convert it to the database locale.  However, wcsftime() internally uses
     * LC_CTYPE, so we set it here.  See the WIN32 comment near the top of
     * PGLC_localeconv().
     */

    /* save user's value of ctype locale */
    save_lc_ctype = setlocale(LC_CTYPE, NULL);
    if (save_lc_ctype)
        save_lc_ctype = pstrdup(save_lc_ctype);

    /* use lc_time to set the ctype */
    setlocale(LC_CTYPE, locale_time);
#endif

    setlocale(LC_TIME, locale_time);

    timenow = time(NULL);
    timeinfo = localtime(&timenow);

    /* localized days */
    for (i = 0; i < 7; i++)
    {
        timeinfo->tm_wday = i;
        strftime(buf, MAX_L10N_DATA, "%a", timeinfo);
        ptr = MemoryContextStrdup(TopMemoryContext, buf);
        if (localized_abbrev_days[i])
            pfree(localized_abbrev_days[i]);
        localized_abbrev_days[i] = ptr;

        strftime(buf, MAX_L10N_DATA, "%A", timeinfo);
        ptr = MemoryContextStrdup(TopMemoryContext, buf);
        if (localized_full_days[i])
            pfree(localized_full_days[i]);
        localized_full_days[i] = ptr;
    }

    /* localized months */
    for (i = 0; i < 12; i++)
    {
        timeinfo->tm_mon = i;
        timeinfo->tm_mday = 1;  /* make sure we don't have invalid date */
        strftime(buf, MAX_L10N_DATA, "%b", timeinfo);
        ptr = MemoryContextStrdup(TopMemoryContext, buf);
        if (localized_abbrev_months[i])
            pfree(localized_abbrev_months[i]);
        localized_abbrev_months[i] = ptr;

        strftime(buf, MAX_L10N_DATA, "%B", timeinfo);
        ptr = MemoryContextStrdup(TopMemoryContext, buf);
        if (localized_full_months[i])
            pfree(localized_full_months[i]);
        localized_full_months[i] = ptr;
    }

    /* try to restore internal settings */
    if (save_lc_time)
    {
        if (!setlocale(LC_TIME, save_lc_time))
            elog(WARNING, "failed to restore old locale");
        pfree(save_lc_time);
    }

#ifdef WIN32
    /* try to restore internal ctype settings */
    if (save_lc_ctype)
    {
        if (!setlocale(LC_CTYPE, save_lc_ctype))
            elog(WARNING, "failed to restore old locale");
        pfree(save_lc_ctype);
    }
#endif

    CurrentLCTimeValid = true;
}

bool check_locale ( int  category,
const char *  locale,
char **  canonname 
)

Definition at line 235 of file pg_locale.c.

References elog, NULL, pfree(), pstrdup(), and WARNING.

Referenced by check_locale_messages(), check_locale_monetary(), check_locale_numeric(), check_locale_time(), and createdb().

{
    char       *save;
    char       *res;

    if (canonname)
        *canonname = NULL;      /* in case of failure */

    save = setlocale(category, NULL);
    if (!save)
        return false;           /* won't happen, we hope */

    /* save may be pointing at a modifiable scratch variable, see above. */
    save = pstrdup(save);

    /* set the locale with setlocale, to see if it accepts it. */
    res = setlocale(category, locale);

    /* save canonical name if requested. */
    if (res && canonname)
        *canonname = pstrdup(res);

    /* restore old value. */
    if (!setlocale(category, save))
        elog(WARNING, "failed to restore old locale \"%s\"", save);
    pfree(save);

    return (res != NULL);
}

bool check_locale_messages ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 324 of file pg_locale.c.

References check_locale(), NULL, and PGC_S_DEFAULT.

{
    if (**newval == '\0')
    {
        if (source == PGC_S_DEFAULT)
            return true;
        else
            return false;
    }

    /*
     * LC_MESSAGES category does not exist everywhere, but accept it anyway
     *
     * On Windows, we can't even check the value, so accept blindly
     */
#if defined(LC_MESSAGES) && !defined(WIN32)
    return check_locale(LC_MESSAGES, *newval, NULL);
#else
    return true;
#endif
}

bool check_locale_monetary ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 278 of file pg_locale.c.

References check_locale(), and NULL.

{
    return check_locale(LC_MONETARY, *newval, NULL);
}

bool check_locale_numeric ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 290 of file pg_locale.c.

References check_locale(), and NULL.

{
    return check_locale(LC_NUMERIC, *newval, NULL);
}

bool check_locale_time ( char **  newval,
void **  extra,
GucSource  source 
)

Definition at line 302 of file pg_locale.c.

References check_locale(), and NULL.

{
    return check_locale(LC_TIME, *newval, NULL);
}

bool lc_collate_is_c ( Oid  collation  ) 

Definition at line 915 of file pg_locale.c.

References C_COLLATION_OID, DEFAULT_COLLATION_OID, elog, ERROR, lookup_collation_cache(), NULL, OidIsValid, and POSIX_COLLATION_OID.

Referenced by convert_string_datum(), make_greater_string(), match_special_index_operator(), spg_text_inner_consistent(), and varstr_cmp().

{
    /*
     * If we're asked about "collation 0", return false, so that the code will
     * go into the non-C path and report that the collation is bogus.
     */
    if (!OidIsValid(collation))
        return false;

    /*
     * If we're asked about the default collation, we have to inquire of the C
     * library.  Cache the result so we only have to compute it once.
     */
    if (collation == DEFAULT_COLLATION_OID)
    {
        static int  result = -1;
        char       *localeptr;

        if (result >= 0)
            return (bool) result;
        localeptr = setlocale(LC_COLLATE, NULL);
        if (!localeptr)
            elog(ERROR, "invalid LC_COLLATE setting");

        if (strcmp(localeptr, "C") == 0)
            result = true;
        else if (strcmp(localeptr, "POSIX") == 0)
            result = true;
        else
            result = false;
        return (bool) result;
    }

    /*
     * If we're asked about the built-in C/POSIX collations, we know that.
     */
    if (collation == C_COLLATION_OID ||
        collation == POSIX_COLLATION_OID)
        return true;

    /*
     * Otherwise, we have to consult pg_collation, but we cache that.
     */
    return (lookup_collation_cache(collation, true))->collate_is_c;
}

bool lc_ctype_is_c ( Oid  collation  ) 

Definition at line 965 of file pg_locale.c.

References C_COLLATION_OID, DEFAULT_COLLATION_OID, elog, ERROR, lookup_collation_cache(), NULL, OidIsValid, and POSIX_COLLATION_OID.

Referenced by Generic_Text_IC_like(), like_fixed_prefix(), lowerstr_with_len(), pg_set_regex_collation(), str_initcap(), str_tolower(), str_toupper(), and TParserInit().

{
    /*
     * If we're asked about "collation 0", return false, so that the code will
     * go into the non-C path and report that the collation is bogus.
     */
    if (!OidIsValid(collation))
        return false;

    /*
     * If we're asked about the default collation, we have to inquire of the C
     * library.  Cache the result so we only have to compute it once.
     */
    if (collation == DEFAULT_COLLATION_OID)
    {
        static int  result = -1;
        char       *localeptr;

        if (result >= 0)
            return (bool) result;
        localeptr = setlocale(LC_CTYPE, NULL);
        if (!localeptr)
            elog(ERROR, "invalid LC_CTYPE setting");

        if (strcmp(localeptr, "C") == 0)
            result = true;
        else if (strcmp(localeptr, "POSIX") == 0)
            result = true;
        else
            result = false;
        return (bool) result;
    }

    /*
     * If we're asked about the built-in C/POSIX collations, we know that.
     */
    if (collation == C_COLLATION_OID ||
        collation == POSIX_COLLATION_OID)
        return true;

    /*
     * Otherwise, we have to consult pg_collation, but we cache that.
     */
    return (lookup_collation_cache(collation, true))->ctype_is_c;
}

pg_locale_t pg_newlocale_from_collation ( Oid  collid  ) 

Definition at line 1052 of file pg_locale.c.

References Assert, COLLOID, DEFAULT_COLLATION_OID, elog, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, collation_cache_entry::locale, locale_t, lookup_collation_cache(), NameStr, NULL, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), and SearchSysCache1.

Referenced by DefineCollation(), Generic_Text_IC_like(), like_fixed_prefix(), pg_set_regex_collation(), str_initcap(), str_tolower(), str_toupper(), and varstr_cmp().

{
    collation_cache_entry *cache_entry;

    /* Callers must pass a valid OID */
    Assert(OidIsValid(collid));

    /* Return 0 for "default" collation, just in case caller forgets */
    if (collid == DEFAULT_COLLATION_OID)
        return (pg_locale_t) 0;

    cache_entry = lookup_collation_cache(collid, false);

    if (cache_entry->locale == 0)
    {
        /* We haven't computed this yet in this session, so do it */
#ifdef HAVE_LOCALE_T
        HeapTuple   tp;
        Form_pg_collation collform;
        const char *collcollate;
        const char *collctype;
        locale_t    result;

        tp = SearchSysCache1(COLLOID, ObjectIdGetDatum(collid));
        if (!HeapTupleIsValid(tp))
            elog(ERROR, "cache lookup failed for collation %u", collid);
        collform = (Form_pg_collation) GETSTRUCT(tp);

        collcollate = NameStr(collform->collcollate);
        collctype = NameStr(collform->collctype);

        if (strcmp(collcollate, collctype) == 0)
        {
            /* Normal case where they're the same */
#ifndef WIN32
            result = newlocale(LC_COLLATE_MASK | LC_CTYPE_MASK, collcollate,
                               NULL);
#else
            result = _create_locale(LC_ALL, collcollate);
#endif
            if (!result)
                report_newlocale_failure(collcollate);
        }
        else
        {
#ifndef WIN32
            /* We need two newlocale() steps */
            locale_t    loc1;

            loc1 = newlocale(LC_COLLATE_MASK, collcollate, NULL);
            if (!loc1)
                report_newlocale_failure(collcollate);
            result = newlocale(LC_CTYPE_MASK, collctype, loc1);
            if (!result)
                report_newlocale_failure(collctype);
#else

            /*
             * XXX The _create_locale() API doesn't appear to support this.
             * Could perhaps be worked around by changing pg_locale_t to
             * contain two separate fields.
             */
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("collations with different collate and ctype values are not supported on this platform")));
#endif
        }

        cache_entry->locale = result;

        ReleaseSysCache(tp);
#else                           /* not HAVE_LOCALE_T */

        /*
         * For platforms that don't support locale_t, we can't do anything
         * with non-default collations.
         */
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
        errmsg("nondefault collations are not supported on this platform")));
#endif   /* not HAVE_LOCALE_T */
    }

    return cache_entry->locale;
}

char* pg_perm_setlocale ( int  category,
const char *  locale 
)

Definition at line 144 of file pg_locale.c.

References elog, FATAL, lc_collate_envbuf, lc_ctype_envbuf, LC_ENV_BUFSIZE, lc_monetary_envbuf, lc_numeric_envbuf, lc_time_envbuf, NULL, putenv, and snprintf().

Referenced by assign_locale_messages(), CheckMyDatabase(), and main().

{
    char       *result;
    const char *envvar;
    char       *envbuf;

#ifndef WIN32
    result = setlocale(category, locale);
#else

    /*
     * On Windows, setlocale(LC_MESSAGES) does not work, so just assume that
     * the given value is good and set it in the environment variables. We
     * must ignore attempts to set to "", which means "keep using the old
     * environment value".
     */
#ifdef LC_MESSAGES
    if (category == LC_MESSAGES)
    {
        result = (char *) locale;
        if (locale == NULL || locale[0] == '\0')
            return result;
    }
    else
#endif
        result = setlocale(category, locale);
#endif   /* WIN32 */

    if (result == NULL)
        return result;          /* fall out immediately on failure */

    switch (category)
    {
        case LC_COLLATE:
            envvar = "LC_COLLATE";
            envbuf = lc_collate_envbuf;
            break;
        case LC_CTYPE:
            envvar = "LC_CTYPE";
            envbuf = lc_ctype_envbuf;
            break;
#ifdef LC_MESSAGES
        case LC_MESSAGES:
            envvar = "LC_MESSAGES";
            envbuf = lc_messages_envbuf;
#ifdef WIN32
            result = IsoLocaleName(locale);
            if (result == NULL)
                result = (char *) locale;
#endif   /* WIN32 */
            break;
#endif   /* LC_MESSAGES */
        case LC_MONETARY:
            envvar = "LC_MONETARY";
            envbuf = lc_monetary_envbuf;
            break;
        case LC_NUMERIC:
            envvar = "LC_NUMERIC";
            envbuf = lc_numeric_envbuf;
            break;
        case LC_TIME:
            envvar = "LC_TIME";
            envbuf = lc_time_envbuf;
            break;
        default:
            elog(FATAL, "unrecognized LC category: %d", category);
            envvar = NULL;      /* keep compiler quiet */
            envbuf = NULL;
            return NULL;
    }

    snprintf(envbuf, LC_ENV_BUFSIZE - 1, "%s=%s", envvar, result);

    if (putenv(envbuf))
        return NULL;

    return result;
}

struct lconv* PGLC_localeconv ( void   )  [read]

Definition at line 419 of file pg_locale.c.

References CurrentLocaleConvValid, db_encoding_strdup(), decimal_point, elog, encoding, free_struct_lconv(), grouping, locale_monetary, locale_numeric, NULL, pfree(), pg_get_encoding_from_locale(), pstrdup(), thousands_sep, and WARNING.

Referenced by cash_in(), cash_numeric(), cash_out(), int4_cash(), int8_cash(), NUM_prepare_locale(), and numeric_cash().

{
    static struct lconv CurrentLocaleConv;
    struct lconv *extlconv;
    char       *save_lc_monetary;
    char       *save_lc_numeric;
    char       *decimal_point;
    char       *grouping;
    char       *thousands_sep;
    int         encoding;

#ifdef WIN32
    char       *save_lc_ctype;
#endif

    /* Did we do it already? */
    if (CurrentLocaleConvValid)
        return &CurrentLocaleConv;

    free_struct_lconv(&CurrentLocaleConv);

    /* Save user's values of monetary and numeric locales */
    save_lc_monetary = setlocale(LC_MONETARY, NULL);
    if (save_lc_monetary)
        save_lc_monetary = pstrdup(save_lc_monetary);

    save_lc_numeric = setlocale(LC_NUMERIC, NULL);
    if (save_lc_numeric)
        save_lc_numeric = pstrdup(save_lc_numeric);

#ifdef WIN32

    /*
     * Ideally, monetary and numeric local symbols could be returned in any
     * server encoding.  Unfortunately, the WIN32 API does not allow
     * setlocale() to return values in a codepage/CTYPE that uses more than
     * two bytes per character, like UTF-8:
     *
     * http://msdn.microsoft.com/en-us/library/x99tb11d.aspx
     *
     * Evidently, LC_CTYPE allows us to control the encoding used for strings
     * returned by localeconv().  The Open Group standard, mentioned at the
     * top of this C file, doesn't explicitly state this.
     *
     * Therefore, we set LC_CTYPE to match LC_NUMERIC or LC_MONETARY (which
     * cannot be UTF8), call localeconv(), and then convert from the
     * numeric/monitary LC_CTYPE to the server encoding.  One example use of
     * this is for the Euro symbol.
     *
     * Perhaps someday we will use GetLocaleInfoW() which returns values in
     * UTF16 and convert from that.
     */

    /* save user's value of ctype locale */
    save_lc_ctype = setlocale(LC_CTYPE, NULL);
    if (save_lc_ctype)
        save_lc_ctype = pstrdup(save_lc_ctype);

    /* use numeric to set the ctype */
    setlocale(LC_CTYPE, locale_numeric);
#endif

    /* Get formatting information for numeric */
    setlocale(LC_NUMERIC, locale_numeric);
    extlconv = localeconv();
    encoding = pg_get_encoding_from_locale(locale_numeric, true);

    decimal_point = db_encoding_strdup(encoding, extlconv->decimal_point);
    thousands_sep = db_encoding_strdup(encoding, extlconv->thousands_sep);
    grouping = strdup(extlconv->grouping);

#ifdef WIN32
    /* use monetary to set the ctype */
    setlocale(LC_CTYPE, locale_monetary);
#endif

    /* Get formatting information for monetary */
    setlocale(LC_MONETARY, locale_monetary);
    extlconv = localeconv();
    encoding = pg_get_encoding_from_locale(locale_monetary, true);

    /*
     * Must copy all values since restoring internal settings may overwrite
     * localeconv()'s results.
     */
    CurrentLocaleConv = *extlconv;
    CurrentLocaleConv.decimal_point = decimal_point;
    CurrentLocaleConv.grouping = grouping;
    CurrentLocaleConv.thousands_sep = thousands_sep;
    CurrentLocaleConv.int_curr_symbol = db_encoding_strdup(encoding, extlconv->int_curr_symbol);
    CurrentLocaleConv.currency_symbol = db_encoding_strdup(encoding, extlconv->currency_symbol);
    CurrentLocaleConv.mon_decimal_point = db_encoding_strdup(encoding, extlconv->mon_decimal_point);
    CurrentLocaleConv.mon_grouping = strdup(extlconv->mon_grouping);
    CurrentLocaleConv.mon_thousands_sep = db_encoding_strdup(encoding, extlconv->mon_thousands_sep);
    CurrentLocaleConv.negative_sign = db_encoding_strdup(encoding, extlconv->negative_sign);
    CurrentLocaleConv.positive_sign = db_encoding_strdup(encoding, extlconv->positive_sign);

    /* Try to restore internal settings */
    if (save_lc_monetary)
    {
        if (!setlocale(LC_MONETARY, save_lc_monetary))
            elog(WARNING, "failed to restore old locale");
        pfree(save_lc_monetary);
    }

    if (save_lc_numeric)
    {
        if (!setlocale(LC_NUMERIC, save_lc_numeric))
            elog(WARNING, "failed to restore old locale");
        pfree(save_lc_numeric);
    }

#ifdef WIN32
    /* Try to restore internal ctype settings */
    if (save_lc_ctype)
    {
        if (!setlocale(LC_CTYPE, save_lc_ctype))
            elog(WARNING, "failed to restore old locale");
        pfree(save_lc_ctype);
    }
#endif

    CurrentLocaleConvValid = true;
    return &CurrentLocaleConv;
}


Variable Documentation

Definition at line 83 of file pg_locale.c.

Definition at line 84 of file pg_locale.c.

Referenced by PGLC_localeconv().

Definition at line 85 of file pg_locale.c.

Referenced by PGLC_localeconv().

char* locale_time

Definition at line 86 of file pg_locale.c.

Referenced by cache_locale_time().

Definition at line 89 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().

Definition at line 91 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().

Definition at line 90 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().

Definition at line 92 of file pg_locale.c.

Referenced by cache_locale_time(), and DCH_to_char().