xref: /freebsd/lib/libc/locale/localeconv.c (revision 2f1da80f)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4f2d0f427SAlexey Zelkin  * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
558f0484fSRodney W. Grimes  * Copyright (c) 1991, 1993
658f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
758f0484fSRodney W. Grimes  *
83c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
95b5fa75aSEd Maste  *
103c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
113c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
123c87aa1dSDavid Chisnall  *
1358f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1458f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1558f0484fSRodney W. Grimes  * are met:
1658f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1758f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1858f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
2058f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
22e4efb830SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
23e4efb830SDavid E. O'Brien  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  */
3758f0484fSRodney W. Grimes 
3858f0484fSRodney W. Grimes #include <locale.h>
39683fe113SAlexey Zelkin 
40e690a556SAlexey Zelkin #include "lmonetary.h"
41e690a556SAlexey Zelkin #include "lnumeric.h"
42e690a556SAlexey Zelkin 
43e690a556SAlexey Zelkin /*
44e690a556SAlexey Zelkin  * The localeconv() function constructs a struct lconv from the current
45e690a556SAlexey Zelkin  * monetary and numeric locales.
46e690a556SAlexey Zelkin  *
47e690a556SAlexey Zelkin  * Because localeconv() may be called many times (especially by library
482f1da80fSrilysh  * routines like printf() & strtod()), the appropriate members of the
49e690a556SAlexey Zelkin  * lconv structure are computed only when the monetary or numeric
50e690a556SAlexey Zelkin  * locale has been changed.
51e690a556SAlexey Zelkin  */
52e690a556SAlexey Zelkin 
5358f0484fSRodney W. Grimes /*
5458f0484fSRodney W. Grimes  * Return the current locale conversion.
5558f0484fSRodney W. Grimes  */
5658f0484fSRodney W. Grimes struct lconv *
localeconv_l(locale_t loc)573c87aa1dSDavid Chisnall localeconv_l(locale_t loc)
5858f0484fSRodney W. Grimes {
593c87aa1dSDavid Chisnall 	FIX_LOCALE(loc);
603c87aa1dSDavid Chisnall     struct lconv *ret = &loc->lconv;
6158f0484fSRodney W. Grimes 
627eb138a9SMark Johnston     if (atomic_load_acq_int(&loc->monetary_locale_changed) != 0) {
63e690a556SAlexey Zelkin 	/* LC_MONETARY part */
64e690a556SAlexey Zelkin         struct lc_monetary_T * mptr;
65e690a556SAlexey Zelkin 
663c87aa1dSDavid Chisnall #define M_ASSIGN_STR(NAME) (ret->NAME = (char*)mptr->NAME)
673c87aa1dSDavid Chisnall #define M_ASSIGN_CHAR(NAME) (ret->NAME = mptr->NAME[0])
68e690a556SAlexey Zelkin 
693c87aa1dSDavid Chisnall 	mptr = __get_current_monetary_locale(loc);
70e690a556SAlexey Zelkin 	M_ASSIGN_STR(int_curr_symbol);
71e690a556SAlexey Zelkin 	M_ASSIGN_STR(currency_symbol);
72e690a556SAlexey Zelkin 	M_ASSIGN_STR(mon_decimal_point);
73e690a556SAlexey Zelkin 	M_ASSIGN_STR(mon_thousands_sep);
741bd7723dSAlexey Zelkin 	M_ASSIGN_STR(mon_grouping);
75e690a556SAlexey Zelkin 	M_ASSIGN_STR(positive_sign);
76e690a556SAlexey Zelkin 	M_ASSIGN_STR(negative_sign);
77e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(int_frac_digits);
78e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(frac_digits);
79e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(p_cs_precedes);
80e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(p_sep_by_space);
81e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(n_cs_precedes);
82e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(n_sep_by_space);
83e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(p_sign_posn);
84e690a556SAlexey Zelkin 	M_ASSIGN_CHAR(n_sign_posn);
85f4da1a75STim J. Robbins 	M_ASSIGN_CHAR(int_p_cs_precedes);
86f4da1a75STim J. Robbins 	M_ASSIGN_CHAR(int_n_cs_precedes);
87f4da1a75STim J. Robbins 	M_ASSIGN_CHAR(int_p_sep_by_space);
88f4da1a75STim J. Robbins 	M_ASSIGN_CHAR(int_n_sep_by_space);
89f4da1a75STim J. Robbins 	M_ASSIGN_CHAR(int_p_sign_posn);
90f4da1a75STim J. Robbins 	M_ASSIGN_CHAR(int_n_sign_posn);
917eb138a9SMark Johnston 	atomic_store_int(&loc->monetary_locale_changed, 0);
92e690a556SAlexey Zelkin     }
93e690a556SAlexey Zelkin 
947eb138a9SMark Johnston     if (atomic_load_acq_int(&loc->numeric_locale_changed) != 0) {
95e690a556SAlexey Zelkin 	/* LC_NUMERIC part */
96e690a556SAlexey Zelkin         struct lc_numeric_T * nptr;
97e690a556SAlexey Zelkin 
983c87aa1dSDavid Chisnall #define N_ASSIGN_STR(NAME) (ret->NAME = (char*)nptr->NAME)
99e690a556SAlexey Zelkin 
1003c87aa1dSDavid Chisnall 	nptr = __get_current_numeric_locale(loc);
101e690a556SAlexey Zelkin 	N_ASSIGN_STR(decimal_point);
102e690a556SAlexey Zelkin 	N_ASSIGN_STR(thousands_sep);
1031bd7723dSAlexey Zelkin 	N_ASSIGN_STR(grouping);
1047eb138a9SMark Johnston 	atomic_store_int(&loc->numeric_locale_changed, 0);
105e690a556SAlexey Zelkin     }
106e690a556SAlexey Zelkin 
1073c87aa1dSDavid Chisnall     return ret;
1083c87aa1dSDavid Chisnall }
1093c87aa1dSDavid Chisnall struct lconv *
localeconv(void)1103c87aa1dSDavid Chisnall localeconv(void)
1113c87aa1dSDavid Chisnall {
1123c87aa1dSDavid Chisnall 	return localeconv_l(__get_locale());
11358f0484fSRodney W. Grimes }
114