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
| 1 | 1 | /* | |
| 2 | 2 | * thread_multi_locale1.c - Per-thread locale demo | |
| 3 | 3 | * | |
| 4 | 4 | * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 5 | 5 | */ | |
| 6 | 6 | ||
| 7 | 7 | #define _XOPEN_SOURCE 700 | |
| 8 | 8 | ||
| 9 | 9 | #include <stdlib.h> | |
| 10 | 10 | #include <stdio.h> | |
| 11 | 11 | #include <string.h> | |
| 12 | 12 | #include <locale.h> | |
| 13 | 13 | #include <errno.h> | |
| 14 | 14 | ||
| 15 | 15 | #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L 1 | |
| 16 | 16 | ||
| 17 | - | #if defined(_WIN32) && !defined(LC_GLOBAL_LOCATE) | |
| 17 | + | #if defined(WIN32) && !defined(LC_GLOBAL_LOCATE) | |
| 18 | 18 | #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) */ | |
| 21 | 20 | ||
| 22 | 21 | ||
| 23 | 22 | #if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS | |
| 24 | 23 | #include <xlocinfo.h> | |
| 26 | 24 | ||
| 25 | + | typedef _locale_t locale_t; | |
| 27 | 26 | ||
| 28 | 27 | #define LC_ALL_MASK (LC_ALL) | |
| 29 | 28 | ||
| 30 | 29 | locale_t newlocale(int mask, const char * locale, locale_t base_not_implemented) | |
| 31 | 30 | { | |
| 32 | 31 | return _create_locale(mask, locale); | |
| 33 | 32 | } | |
| 34 | 33 | ||
| 35 | - | locale_t uselocale(locale_t newloc) | |
| 34 | + | locale_t uselocale(locale_t newlocobj) | |
| 36 | 35 | { | |
| 37 | 36 | locale_t old_locale = _get_current_locale(); | |
| 38 | 37 | ||
| 39 | 38 | /* | |
| 40 | 39 | * uselocale() sets the thread's locale by definition, so | |
| 41 | 40 | * unconditionally use thread-local locale | |
| 42 | 41 | */ | |
| 43 | - | _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); | |
| 42 | + | (void)_configthreadlocale(_ENABLE_PER_THREAD_LOCALE); | |
| 44 | 43 | ||
| 45 | 44 | #define ElementsInArray(x) (sizeof(x) / sizeof((x)[0])) | |
| 46 | 45 | /* | |
| 47 | 46 | * sets all categories - right now our poor man's emulation | |
| 48 | 47 | * can't do it better | |
| 49 | 48 | */ | |
| 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; | |
| 52 | 51 | #if 0 | |
| 53 | - | (void)fprintf(stderr, "switching to locale %d/|%s|\n", | |
| 52 | + | (void)fprintf(stderr, | |
| 53 | + | "uselocale: switching to locale %d/|%s|\n", | |
| 54 | 54 | i, curr_ln); | |
| 55 | 55 | #endif | |
| 56 | - | setlocale(i, curr_ln); | |
| 56 | + | (void)setlocale(i, curr_ln); | |
| 57 | 57 | } | |
| 58 | 58 | ||
| 59 | 59 | /* return the old locale_t */ | |
| 60 | 60 | return old_locale; | |
| 61 | 61 | } | |
| 62 | 62 | ||
| 63 | 63 | void freelocale(locale_t locobj) | |
| 64 | 64 | { | |
| 65 | 65 | _free_locale(locobj); | |
| 66 | 66 | } | |
| 67 | 67 | #endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */ | |
| 68 | 68 | ||
| 69 | 69 | #ifdef __GLIBC__ | |
| 70 | 70 | /* { BSD, MacOSX, Illumos, ... } do not need this! */ | |
| 71 | 71 | #define CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L | |
| 72 | 72 | #endif | |
| 73 | 73 | ||
| 74 | 74 | #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L | |
| 75 | 75 | #include <stdarg.h> | |
| 76 | 76 | #include <errno.h> | |
| 77 | 77 | #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */ | |
| 78 | 78 | ||
| 79 | 79 | #ifdef CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L | |
| 80 | 80 | /* cursed glibc does not provide |printf_l()| */ | |
| 81 | 81 | int vprintf_l(locale_t loc, const char *format, va_list ap) | |
| 82 | 82 | { | |
| 83 | 83 | locale_t prev_loc; | |
| 84 | 84 | int ret; | |
| 85 | 85 | int saved_errno; | |
| 86 | 86 | ||
| 87 | 87 | prev_loc = uselocale(loc); | |
| 88 | 88 | ||
| 89 | 89 | ret = vprintf(format, ap); | |
| 90 | 90 | saved_errno = errno; | |
| 91 | 91 | ||
| 92 | 92 | (void)uselocale(prev_loc); | |
| 93 | 93 | ||
| 94 | 94 | errno = saved_errno; | |
| 95 | 95 | return ret; | |
| 96 | 96 | } | |
| 97 | 97 | ||
| 98 | 98 | int printf_l(locale_t loc, const char *format, ...) | |
| 99 | 99 | { | |
| 100 | 100 | va_list args; | |
| 101 | 101 | int res; | |
| 102 | 102 | ||
| 103 | 103 | va_start(args, format); | |
| 104 | 104 | res = vprintf_l(loc, format, args); | |
| 105 | 105 | va_end(args); | |
| 106 | 106 | ||
| 107 | 107 | return res; | |
| 108 | 108 | } | |
| 109 | 109 | #endif /* CURSED_GLIBC_DOES_NOT_HAVE_PRINTF_L */ | |
| 110 | 110 | ||
| 111 | 111 | ||
| 112 | 112 | int main(int ac, char *av[]) | |
| 113 | 113 | { | |
| 114 | 114 | locale_t my_en_l; | |
| 115 | 115 | locale_t my_de_l; | |
| 116 | 116 | double x; | |
| 117 | 117 | ||
| 118 | 118 | (void)puts("# thread_multi_locale1: start" | |
| 119 | 119 | #ifdef EMULATE_POSIX_NEWLOCALE_ON_WINDOWS | |
| 120 | 120 | " (emulated)" | |
| 121 | 121 | #endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */ | |
| 122 | 122 | "."); | |
| 123 | 123 | ||
| 124 | 124 | #if EMULATE_POSIX_NEWLOCALE_ON_WINDOWS | |
| 125 | 125 | my_en_l = newlocale(LC_ALL_MASK, "C", NULL); | |
| 126 | 126 | my_de_l = newlocale(LC_ALL_MASK, "German", NULL); | |
| 127 | 127 | #else | |
| 128 | 128 | my_en_l = newlocale(LC_ALL_MASK, "en_US.UTF-8", NULL); | |
| 129 | 129 | my_de_l = newlocale(LC_ALL_MASK, "de_DE.UTF-8", NULL); | |
| 130 | 130 | #endif /* EMULATE_POSIX_NEWLOCALE_ON_WINDOWS */ | |
| 131 | 131 | if ((my_en_l == (locale_t)0) || (my_de_l == (locale_t)0)) { | |
| 132 | 132 | fprintf(stderr, "Could not create locale object [%s], %tx/%tx", | |
| 133 | 133 | strerror(errno), | |
| 134 | 134 | (ptrdiff_t)((char*)my_en_l-0), | |
| 135 | 135 | (ptrdiff_t)((char*)my_de_l-0)); | |
| 136 | 136 | return EXIT_FAILURE; | |
| 137 | 137 | } | |
| 138 | 138 | #if 0 | |
| 139 | 139 | (void)printf("de_DE locale decimal_point=|%s|\n", | |
| 140 | 140 | my_de_l->locinfo->lconv->decimal_point); | |
| 141 | 141 | #endif | |
| 142 | 142 | x = 123456.789123456789; | |
| 143 | 143 | (void)printf_l(my_en_l, "%8.8f\n", x); | |
| 144 | 144 | (void)printf_l(my_de_l, "%8.8f\n", x); | |
| 145 | 145 | ||
| 146 | 146 | /* | |
| 147 | 147 | * If we use |uselocale()| we must set the current thread | |
| 148 | 148 | * locale to something else before freeing the current | |
| 149 | 149 | * locale_t object!! | |
| 150 | 150 | */ | |
| 151 | 151 | freelocale(my_de_l); | |
| 152 | 152 | freelocale(my_en_l); | |
| 153 | 153 | ||
| 154 | 154 | (void)puts("# thread_multi_locale1: done."); | |
| 155 | 155 | ||
| 156 | 156 | return EXIT_SUCCESS; | |
| 157 | 157 | } | |
| 158 | 158 |
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.
rovema.kpaste.net RSS