pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post ac0cef by Anonymous on Tue 16th May 2023 15:58
original post ac0d96179f by Anonymous on Tue 31st Jan 2023 12:43
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>
11+
#include <string.h>
1112
#include <locale.h>
13+
#include <errno.h>
1214
13-
#ifdef __GLIBC__
15+
#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1
1716
18-
#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
17+
#if defined(_WIN32) && !defined(LC_GLOBAL_LOCATE)
19-
#include <stdarg.h>
18+
#define EMULATE_POSIX_NEWLOCALE_ON_WINDOWS 1
20-
#include <errno.h>
19+
#define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1
21-
#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
20+
#endif /* defined(_WIN32) && !defined(LC_GLOBAL_LOCATE) */
2221
3022
31-
	prev_loc = uselocale(loc);
23+
#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
24+
#include <xlocinfo.h>
3226
3527
36-
	(void)uselocale(prev_loc);
28+
#define LC_ALL_MASK	(LC_ALL)
3729
38-
	errno = saved_errno;
30+
locale_t newlocale(int mask, const char * locale, locale_t base_not_implemented)
31+
{
4033
}
4134
42-
int printf_l(locale_t loc, const char *format, ...)
35+
locale_t uselocale(locale_t newloc)
4336
{
44-
	va_list args;
37+
	locale_t old_locale = _get_current_locale();
4638
47-
	va_start(args, format);
39+
	/*
48-
	res = vprintf_l(loc, format, args);
40+
	 * uselocale() sets the thread's locale by definition, so
49-
	va_end(args);
41+
	 * unconditionally use thread-local locale
42+
	 */
5044
51-
	return res;
45+
#define ElementsInArray(x)   (sizeof(x) / sizeof((x)[0]))
52-
}
46+
	/*
53-
#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
47+
	 * sets all categories - right now our poor man's emulation
54-
48+
	 * can't do it better
49+
	 */
5558
56-
int main(int ac, char *av[])
59+
	/* return the old locale_t */
60+
	return old_locale;
5764
{
58-
	locale_t my_en_l;
65+
	_free_locale(locobj);
59-
	locale_t my_de_l;
66+
}
60-
	double x;
67+
#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
6168
62-
	(void)puts("# thread_multi_locale1: start");
69+
#ifdef __GLIBC__
70+
/* { BSD, MacOSX, Illumos, ... } do not need this! */
6373
64-
	my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
74+
#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
65-
	my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
75+
#include <stdarg.h>
76+
#include <errno.h>
6678
67-
	if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
79+
#ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L
68-
		perror("Could not create locale object");
80+
/* cursed glibc does not provide |printf_l()| */
69-
		return EXIT_FAILURE;
81+
int vprintf_l(locale_t loc, const char *format, va_list ap)
70-
	}
82+
{
83+
	locale_t prev_loc;
7186
72-
	x = 123456.789123456789;
87+
	prev_loc = uselocale(loc);
7588
76-
	/*
89+
	ret = vprintf(format, ap);
77-
	 * If we use |uselocale()| we must set the current thread
90+
	saved_errno = errno;
8391
84-
	(void)puts("# thread_multi_locale1: done.");
92+
	(void)uselocale(prev_loc);
8593
86-
	return EXIT_SUCCESS;
94+
	errno = saved_errno;
95+
	return ret;
8796
}
8897
89-
98+
int printf_l(locale_t loc, const char *format, ...)
90-
99+
{
91-
100+
	va_list args;
92-
101+
	int res;
93102
94-
103+
	va_start(args, format);
95-
104+
	res = vprintf_l(loc, format, args);
96-
105+
	va_end(args);
97106
98-
107+
	return res;
99-
108+
}
100-
109+
#endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */
101110
102111
103-
112+
int main(int ac, char *av[])
104-
113+
{
105-
114+
	locale_t my_en_l;
106-
115+
	locale_t my_de_l;
107-
116+
	double x;
108117
109-
118+
	(void)puts("# thread_multi_locale1: start"
110-
119+
#ifdef EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
111-
120+
	" (emulated)"
112-
121+
#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
113-
122+
	".");
114123
115-
124+
#if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS
116-
125+
	my_en_l = newlocale(LC_ALL_MASK, "C", NULL);
117-
126+
	my_de_l = newlocale(LC_ALL_MASK, "German", NULL);
118-
127+
#else
119-
128+
	my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL);
120-
129+
	my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL);
121-
130+
#endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */
122-
131+
	if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) {
123-
132+
		fprintf(stderr, "Could not create locale object [%s], %tx/%tx",
124-
133+
			strerror(errno),
125-
134+
			(ptrdiff_t)((char*)my_en_l-0),
126-
135+
			(ptrdiff_t)((char*)my_de_l-0));
127-
136+
		return EXIT_FAILURE;
128-
137+
	}
129-
138+
#if 0
130-
139+
	(void)printf("de_DE locale decimal_point=|%s|\n",
131-
140+
		my_de_l->locinfo->lconv->decimal_point);
132-
141+
#endif
133-
142+
	x = 123456.789123456789;
134-
143+
	(void)printf_l(my_en_l, "%8.8f\n", x);
135-
144+
        (void)printf_l(my_de_l, "%8.8f\n", x);
136145
137-
146+
	/*
138-
147+
	 * If we use |uselocale()| we must set the current thread
139-
148+
	 * locale to something else before freeing the current
140-
149+
	 * locale_t object!!
141-
150+
	 */
142-
151+
	freelocale(my_de_l);
143-
152+
	freelocale(my_en_l);
144153
145-
154+
	(void)puts("# thread_multi_locale1: done.");
146155
147-
156+
	return EXIT_SUCCESS;
148-
157+
}
149158

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