pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 79f2c8 by Anonymous on Tue 16th May 2023 16:16
original post ac0cef by Anonymous on Tue 16th May 2023 15:58
Download diff
Show old version | new version | both versions

    
11
/*
22
 * thread_multi_locale1.c - Per-thread locale demo
33
 *
44
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
55
 */
66
77
#define _XOPEN_SOURCE 700
88
99
#include <stdlib.h>
1010
#include <stdio.h>
1111
#include <string.h>
1212
#include <locale.h>
1313
#include <errno.h>
1414
1515
#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1
1616
17-
#if defined(_WIN32) && !defined(LC_GLOBAL_LOCATE)
17+
#if defined(WIN32) && !defined(LC_GLOBAL_LOCATE)
1818
#define EMULATE_POSIX_NEWLOCALE_ON_WINDOWS 1
19-
#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1
19+
#endif /* defined(WIN32) && !defined(LC_GLOBAL_LOCATE) */
2120
2221
2322
#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
2423
#include <xlocinfo.h>
2624
25+
typedef _locale_t locale_t;
2726
2827
#define LC_ALL_MASK	(LC_ALL)
2928
3029
locale_t newlocale(int mask, const char * locale, locale_t base_not_implemented)
3130
{
3231
	return _create_locale(mask, locale);
3332
}
3433
35-
locale_t uselocale(locale_t newloc)
34+
locale_t uselocale(locale_t newlocobj)
3635
{
3736
	locale_t old_locale = _get_current_locale();
3837
3938
	/*
4039
	 * uselocale() sets the thread's locale by definition, so
4140
	 * unconditionally use thread-local locale
4241
	 */
43-
	_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
42+
	(void)_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
4443
4544
#define ElementsInArray(x)   (sizeof(x) / sizeof((x)[0]))
4645
	/*
4746
	 * sets all categories - right now our poor man's emulation
4847
	 * can't do it better
4948
	 */
50-
	for(int i=0 ; i < ElementsInArray(newloc->locinfo->lc_category) ; i++) {
49+
	for(int i=0 ; i < ElementsInArray(newlocobj->locinfo->lc_category) ; i++) {
51-
		const char *curr_ln = newloc->locinfo->lc_category[1].locale;
50+
		const char *curr_ln = newlocobj->locinfo->lc_category[1].locale;
5251
#if 0
53-
		(void)fprintf(stderr, "switching to locale %d/|%s|\n",
52+
		(void)fprintf(stderr,
53+
			"uselocale: switching to locale %d/|%s|\n",
5454
			i, curr_ln);
5555
#endif
56-
		setlocale(i, curr_ln);
56+
		(void)setlocale(i, curr_ln);
5757
	}
5858
5959
	/* return the old locale_t */
6060
	return old_locale;
6161
}
6262
6363
void freelocale(locale_t locobj)
6464
{
6565
	_free_locale(locobj);
6666
}
6767
#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
6868
6969
#ifdef __GLIBC__
7070
/* { BSD, MacOSX, Illumos, ... } do not need this! */
7171
#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
7272
#endif
7373
7474
#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
7575
#include <stdarg.h>
7676
#include <errno.h>
7777
#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
7878
7979
#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
8080
/* cursed glibc does not provide |printf_l()| */
8181
int vprintf_l(locale_t loc, const char *format, va_list ap)
8282
{
8383
	locale_t prev_loc;
8484
	int ret;
8585
	int saved_errno;
8686
8787
	prev_loc = uselocale(loc);
8888
8989
	ret = vprintf(format, ap);
9090
	saved_errno = errno;
9191
9292
	(void)uselocale(prev_loc);
9393
9494
	errno = saved_errno;
9595
	return ret;
9696
}
9797
9898
int printf_l(locale_t loc, const char *format, ...)
9999
{
100100
	va_list args;
101101
	int res;
102102
103103
	va_start(args, format);
104104
	res = vprintf_l(loc, format, args);
105105
	va_end(args);
106106
107107
	return res;
108108
}
109109
#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
110110
111111
112112
int main(int ac, char *av[])
113113
{
114114
	locale_t my_en_l;
115115
	locale_t my_de_l;
116116
	double x;
117117
118118
	(void)puts("# thread_multi_locale1: start"
119119
#ifdef EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
120120
	" (emulated)"
121121
#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
122122
	".");
123123
124124
#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
125125
	my_en_l = newlocale(LC_ALL_MASK, "C", NULL);
126126
	my_de_l = newlocale(LC_ALL_MASK, "German", NULL);
127127
#else
128128
	my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
129129
	my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
130130
#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
131131
	if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
132132
		fprintf(stderr, "Could not create locale object [%s], %tx/%tx",
133133
			strerror(errno),
134134
			(ptrdiff_t)((char*)my_en_l-0),
135135
			(ptrdiff_t)((char*)my_de_l-0));
136136
		return EXIT_FAILURE;
137137
	}
138138
#if 0
139139
	(void)printf("de_DE locale decimal_point=|%s|\n",
140140
		my_de_l->locinfo->lconv->decimal_point);
141141
#endif
142142
	x = 123456.789123456789;
143143
	(void)printf_l(my_en_l, "%8.8f\n", x);
144144
        (void)printf_l(my_de_l, "%8.8f\n", x);
145145
146146
	/*
147147
	 * If we use |uselocale()| we must set the current thread
148148
	 * locale to something else before freeing the current
149149
	 * locale_t object!!
150150
	 */
151151
	freelocale(my_de_l);
152152
	freelocale(my_en_l);
153153
154154
	(void)puts("# thread_multi_locale1: done.");
155155
156156
	return EXIT_SUCCESS;
157157
}
158158

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at