1 /*-----------------------------------------------------------------------
2  *
3  * PostgreSQL locale utilities
4  *
5  * src/include/utils/pg_locale.h
6  *
7  * Copyright (c) 2002-2017, PostgreSQL Global Development Group
8  *
9  *-----------------------------------------------------------------------
10  */
11 
12 #ifndef _PG_LOCALE_
13 #define _PG_LOCALE_
14 
15 #if defined(LOCALE_T_IN_XLOCALE) || defined(WCSTOMBS_L_IN_XLOCALE)
16 #include <xlocale.h>
17 #endif
18 #ifdef USE_ICU
19 #include <unicode/ucol.h>
20 /* ICU might have a different definition of "bool", don't buy it */
21 #ifdef bool
22 #undef bool
23 #endif
24 #endif
25 
26 #include "utils/guc.h"
27 
28 #ifdef USE_ICU
29 /*
30  * ucol_strcollUTF8() was introduced in ICU 50, but it is buggy before ICU 53.
31  * (see
32  * <https://www.postgresql.org/message-id/flat/f1438ec6-22aa-4029-9a3b-26f79d330e72%40manitou-mail.org>)
33  */
34 #if U_ICU_VERSION_MAJOR_NUM >= 53
35 #define HAVE_UCOL_STRCOLLUTF8 1
36 #else
37 #undef HAVE_UCOL_STRCOLLUTF8
38 #endif
39 #endif
40 
41 
42 /* GUC settings */
43 extern char *locale_messages;
44 extern char *locale_monetary;
45 extern char *locale_numeric;
46 extern char *locale_time;
47 
48 /* lc_time localization cache */
49 extern char *localized_abbrev_days[];
50 extern char *localized_full_days[];
51 extern char *localized_abbrev_months[];
52 extern char *localized_full_months[];
53 
54 
55 extern bool check_locale_messages(char **newval, void **extra, GucSource source);
56 extern void assign_locale_messages(const char *newval, void *extra);
57 extern bool check_locale_monetary(char **newval, void **extra, GucSource source);
58 extern void assign_locale_monetary(const char *newval, void *extra);
59 extern bool check_locale_numeric(char **newval, void **extra, GucSource source);
60 extern void assign_locale_numeric(const char *newval, void *extra);
61 extern bool check_locale_time(char **newval, void **extra, GucSource source);
62 extern void assign_locale_time(const char *newval, void *extra);
63 
64 extern bool check_locale(int category, const char *locale, char **canonname);
65 extern char *pg_perm_setlocale(int category, const char *locale);
66 extern void check_strxfrm_bug(void);
67 
68 extern bool lc_collate_is_c(Oid collation);
69 extern bool lc_ctype_is_c(Oid collation);
70 
71 /*
72  * Return the POSIX lconv struct (contains number/money formatting
73  * information) with locale information for all categories.
74  */
75 extern struct lconv *PGLC_localeconv(void);
76 
77 extern void cache_locale_time(void);
78 
79 
80 /*
81  * We define our own wrapper around locale_t so we can keep the same
82  * function signatures for all builds, while not having to create a
83  * fake version of the standard type locale_t in the global namespace.
84  * pg_locale_t is occasionally checked for truth, so make it a pointer.
85  */
86 struct pg_locale_struct
87 {
88 	char		provider;
89 	union
90 	{
91 #ifdef HAVE_LOCALE_T
92 		locale_t	lt;
93 #endif
94 #ifdef USE_ICU
95 		struct
96 		{
97 			const char *locale;
98 			UCollator  *ucol;
99 		}			icu;
100 #endif
101 		int			dummy;		/* in case we have neither LOCALE_T nor ICU */
102 	}			info;
103 };
104 
105 typedef struct pg_locale_struct *pg_locale_t;
106 
107 extern pg_locale_t pg_newlocale_from_collation(Oid collid);
108 
109 extern char *get_collation_actual_version(char collprovider, const char *collcollate);
110 
111 #ifdef USE_ICU
112 extern int32_t icu_to_uchar(UChar **buff_uchar, const char *buff, size_t nbytes);
113 extern int32_t icu_from_uchar(char **result, const UChar *buff_uchar, int32_t len_uchar);
114 #endif
115 
116 /* These functions convert from/to libc's wchar_t, *not* pg_wchar_t */
117 #ifdef USE_WIDE_UPPER_LOWER
118 extern size_t wchar2char(char *to, const wchar_t *from, size_t tolen,
119 		   pg_locale_t locale);
120 extern size_t char2wchar(wchar_t *to, size_t tolen,
121 		   const char *from, size_t fromlen, pg_locale_t locale);
122 #endif
123 
124 #endif							/* _PG_LOCALE_ */
125