PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_localeconv_r.c File Reference
#include "c.h"
#include <langinfo.h>
#include <pthread.h>
#include <limits.h>
Include dependency graph for pg_localeconv_r.c:

Go to the source code of this file.

Data Structures

struct  lconv_member_info
 

Macros

#define LCONV_M(is_string, category, name, item)    { is_string, category, offsetof(struct lconv, name) }
 
#define LCONV_S(c, n, i)   LCONV_M(true, c, n, i)
 
#define LCONV_C(c, n, i)   LCONV_M(false, c, n, i)
 

Functions

static char ** lconv_string_member (struct lconv *lconv, int i)
 
static char * lconv_char_member (struct lconv *lconv, int i)
 
void pg_localeconv_free (struct lconv *lconv)
 
static int pg_localeconv_copy_members (struct lconv *dst, struct lconv *src, int category)
 
int pg_localeconv_r (const char *lc_monetary, const char *lc_numeric, struct lconv *output)
 

Variables

static const struct lconv_member_info table []
 

Macro Definition Documentation

◆ LCONV_C

#define LCONV_C (   c,
  n,
  i 
)    LCONV_M(false, c, n, i)

Definition at line 52 of file pg_localeconv_r.c.

◆ LCONV_M

#define LCONV_M (   is_string,
  category,
  name,
  item 
)     { is_string, category, offsetof(struct lconv, name) }

Definition at line 48 of file pg_localeconv_r.c.

◆ LCONV_S

#define LCONV_S (   c,
  n,
  i 
)    LCONV_M(true, c, n, i)

Definition at line 51 of file pg_localeconv_r.c.

Function Documentation

◆ lconv_char_member()

static char * lconv_char_member ( struct lconv *  lconv,
int  i 
)
inlinestatic

Definition at line 94 of file pg_localeconv_r.c.

95{
96 return (char *) lconv + table[i].offset;
97}
int i
Definition: isn.c:77
static const struct lconv_member_info table[]

References i, lconv_member_info::offset, and table.

Referenced by pg_localeconv_copy_members().

◆ lconv_string_member()

static char ** lconv_string_member ( struct lconv *  lconv,
int  i 
)
inlinestatic

Definition at line 88 of file pg_localeconv_r.c.

89{
90 return (char **) ((char *) lconv + table[i].offset);
91}

References i, lconv_member_info::offset, and table.

Referenced by pg_localeconv_copy_members(), and pg_localeconv_free().

◆ pg_localeconv_copy_members()

static int pg_localeconv_copy_members ( struct lconv *  dst,
struct lconv *  src,
int  category 
)
static

Definition at line 155 of file pg_localeconv_r.c.

158{
159 for (int i = 0; i < lengthof(table); ++i)
160 {
161 if (table[i].category != category)
162 continue;
163
164 if (table[i].is_string)
165 {
166 char *string;
167
168 string = *lconv_string_member(src, i);
169 if (!(string = strdup(string)))
170 {
172 errno = ENOMEM;
173 return -1;
174 }
176 }
177 else
178 {
179 *lconv_char_member(dst, i) = *lconv_char_member(src, i);
180 }
181 }
182
183 return 0;
184}
#define lengthof(array)
Definition: c.h:759
static char * lconv_char_member(struct lconv *lconv, int i)
static char ** lconv_string_member(struct lconv *lconv, int i)
void pg_localeconv_free(struct lconv *lconv)
char string[11]
Definition: preproc-type.c:52

References lconv_member_info::category, i, lconv_member_info::is_string, lconv_char_member(), lconv_string_member(), lengthof, pg_localeconv_free(), and table.

Referenced by pg_localeconv_r().

◆ pg_localeconv_free()

void pg_localeconv_free ( struct lconv *  lconv)

Definition at line 104 of file pg_localeconv_r.c.

105{
106 for (int i = 0; i < lengthof(table); ++i)
107 if (table[i].is_string)
108 free(*lconv_string_member(lconv, i));
109}
#define free(a)
Definition: header.h:65

References free, i, lconv_member_info::is_string, lconv_string_member(), lengthof, and table.

Referenced by pg_localeconv_copy_members(), and PGLC_localeconv().

◆ pg_localeconv_r()

int pg_localeconv_r ( const char *  lc_monetary,
const char *  lc_numeric,
struct lconv *  output 
)

Definition at line 231 of file pg_localeconv_r.c.

234{
235#ifdef WIN32
236 wchar_t *save_lc_ctype = NULL;
237 wchar_t *save_lc_monetary = NULL;
238 wchar_t *save_lc_numeric = NULL;
239 int save_config_thread_locale;
240 int result = -1;
241
242 /* Put setlocale() into thread-local mode. */
243 save_config_thread_locale = _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
244
245 /*
246 * Capture the current values as wide strings. Otherwise, we might not be
247 * able to restore them if their names contain non-ASCII characters and
248 * the intermediate locale changes the expected encoding. We don't want
249 * to leave the caller in an unexpected state by failing to restore, or
250 * crash the runtime library.
251 */
252 save_lc_ctype = _wsetlocale(LC_CTYPE, NULL);
253 if (!save_lc_ctype || !(save_lc_ctype = wcsdup(save_lc_ctype)))
254 goto exit;
255 save_lc_monetary = _wsetlocale(LC_MONETARY, NULL);
256 if (!save_lc_monetary || !(save_lc_monetary = wcsdup(save_lc_monetary)))
257 goto exit;
258 save_lc_numeric = _wsetlocale(LC_NUMERIC, NULL);
259 if (!save_lc_numeric || !(save_lc_numeric = wcsdup(save_lc_numeric)))
260 goto exit;
261
262 memset(output, 0, sizeof(*output));
263
264 /* Copy the LC_MONETARY members. */
265 if (!setlocale(LC_ALL, lc_monetary))
266 goto exit;
267 result = pg_localeconv_copy_members(output, localeconv(), LC_MONETARY);
268 if (result != 0)
269 goto exit;
270
271 /* Copy the LC_NUMERIC members. */
272 if (!setlocale(LC_ALL, lc_numeric))
273 goto exit;
274 result = pg_localeconv_copy_members(output, localeconv(), LC_NUMERIC);
275
276exit:
277 /* Restore everything we changed. */
278 if (save_lc_ctype)
279 {
280 _wsetlocale(LC_CTYPE, save_lc_ctype);
281 free(save_lc_ctype);
282 }
283 if (save_lc_monetary)
284 {
285 _wsetlocale(LC_MONETARY, save_lc_monetary);
286 free(save_lc_monetary);
287 }
288 if (save_lc_numeric)
289 {
290 _wsetlocale(LC_NUMERIC, save_lc_numeric);
291 free(save_lc_numeric);
292 }
293 _configthreadlocale(save_config_thread_locale);
294
295 return result;
296
297#else /* !WIN32 */
298 locale_t monetary_locale;
299 locale_t numeric_locale;
300 int result;
301
302 /*
303 * All variations on Unix require locale_t objects for LC_MONETARY and
304 * LC_NUMERIC. We'll set all locale categories, so that we can don't have
305 * to worry about POSIX's undefined behavior if LC_CTYPE's encoding
306 * doesn't match.
307 */
308 errno = ENOENT;
309 monetary_locale = newlocale(LC_ALL_MASK, lc_monetary, 0);
310 if (monetary_locale == 0)
311 return -1;
312 numeric_locale = newlocale(LC_ALL_MASK, lc_numeric, 0);
313 if (numeric_locale == 0)
314 {
315 freelocale(monetary_locale);
316 return -1;
317 }
318
319 memset(output, 0, sizeof(*output));
320#if defined(TRANSLATE_FROM_LANGINFO)
321 /* Copy from non-standard nl_langinfo_l() extended items. */
322 result = pg_localeconv_from_langinfo(output,
323 monetary_locale,
324 numeric_locale);
325#elif defined(HAVE_LOCALECONV_L)
326 /* Copy the LC_MONETARY members from a thread-safe lconv object. */
328 localeconv_l(monetary_locale),
329 LC_MONETARY);
330 if (result == 0)
331 {
332 /* Copy the LC_NUMERIC members from a thread-safe lconv object. */
334 localeconv_l(numeric_locale),
335 LC_NUMERIC);
336 }
337#else
338 /* We have nothing better than standard POSIX facilities. */
339 {
341 locale_t save_locale;
342
343 pthread_mutex_lock(&big_lock);
344 /* Copy the LC_MONETARY members. */
345 save_locale = uselocale(monetary_locale);
347 localeconv(),
348 LC_MONETARY);
349 if (result == 0)
350 {
351 /* Copy the LC_NUMERIC members. */
352 uselocale(numeric_locale);
354 localeconv(),
355 LC_NUMERIC);
356 }
357 pthread_mutex_unlock(&big_lock);
358
359 uselocale(save_locale);
360 }
361#endif
362
363 freelocale(monetary_locale);
364 freelocale(numeric_locale);
365
366 return result;
367#endif /* !WIN32 */
368}
FILE * output
static char * lc_numeric
Definition: initdb.c:144
static char * lc_monetary
Definition: initdb.c:143
static int pg_localeconv_copy_members(struct lconv *dst, struct lconv *src, int category)
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:60
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:42
#define PTHREAD_MUTEX_INITIALIZER
Definition: pthread-win32.h:16
#define locale_t
Definition: win32_port.h:432
#define setlocale(a, b)
Definition: win32_port.h:475

References free, lc_monetary, lc_numeric, locale_t, output, pg_localeconv_copy_members(), PTHREAD_MUTEX_INITIALIZER, pthread_mutex_lock(), pthread_mutex_unlock(), and setlocale.

Referenced by PGLC_localeconv().

Variable Documentation

◆ table

const struct lconv_member_info table[]
static
Initial value:
= {
LCONV_S(LC_NUMERIC, decimal_point, DECIMAL_POINT),
LCONV_S(LC_NUMERIC, thousands_sep, THOUSANDS_SEP),
LCONV_S(LC_NUMERIC, grouping, GROUPING),
LCONV_S(LC_MONETARY, int_curr_symbol, INT_CURR_SYMBOL),
LCONV_S(LC_MONETARY, currency_symbol, CURRENCY_SYMBOL),
LCONV_S(LC_MONETARY, mon_decimal_point, MON_DECIMAL_POINT),
LCONV_S(LC_MONETARY, mon_thousands_sep, MON_THOUSANDS_SEP),
LCONV_S(LC_MONETARY, mon_grouping, MON_GROUPING),
LCONV_S(LC_MONETARY, positive_sign, POSITIVE_SIGN),
LCONV_S(LC_MONETARY, negative_sign, NEGATIVE_SIGN),
LCONV_C(LC_MONETARY, int_frac_digits, INT_FRAC_DIGITS),
LCONV_C(LC_MONETARY, frac_digits, FRAC_DIGITS),
LCONV_C(LC_MONETARY, p_cs_precedes, P_CS_PRECEDES),
LCONV_C(LC_MONETARY, p_sep_by_space, P_SEP_BY_SPACE),
LCONV_C(LC_MONETARY, n_cs_precedes, N_CS_PRECEDES),
LCONV_C(LC_MONETARY, n_sep_by_space, N_SEP_BY_SPACE),
LCONV_C(LC_MONETARY, p_sign_posn, P_SIGN_POSN),
LCONV_C(LC_MONETARY, n_sign_posn, N_SIGN_POSN),
}
static char * thousands_sep
Definition: print.c:50
static char * decimal_point
Definition: print.c:48
#define LCONV_S(c, n, i)
#define LCONV_C(c, n, i)

Definition at line 63 of file pg_localeconv_r.c.

Referenced by afterTriggerDeleteHeadEventChunk(), AfterTriggerFreeQuery(), appendQualifiedRelation(), before_stmt_triggers_fired(), byword(), cancel_prior_stmt_triggers(), check_selective_binary_conversion(), cluster_one_database(), convert_int_from_base_unit(), convert_real_from_base_unit(), convert_to_base_unit(), create_foreign_modify(), deparseRelation(), fileGetOptions(), get_batch_size_option(), get_table_relkind(), GetAfterTriggersStoreSlot(), GetAfterTriggersTableData(), initCreateTables(), initialize_worker_spi(), initPopulateTable(), lconv_char_member(), lconv_string_member(), MakeTransitionCaptureState(), mbbisearch(), pg_localeconv_copy_members(), pg_localeconv_free(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresExecForeignTruncate(), postgresGetAnalyzeInfoForForeignTable(), postgresIsForeignRelUpdatable(), prepare_vacuum_command(), run_vacuum_command(), splitTableColumnsSpec(), stats_lock_check_privileges(), TransitionTableAddTuple(), vacuumlo(), and worker_spi_main().