1 /* 2 * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Copyright (c) 2011 The FreeBSD Foundation 6 * All rights reserved. 7 * Portions of this software were developed by David Chisnall 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD: head/lib/libc/locale/lmonetary.c 227753 2011-11-20 14:45:42Z theraven $ 32 */ 33 34 35 #include <limits.h> 36 #include <stddef.h> 37 #include <stdlib.h> 38 39 #include "ldpart.h" 40 #include "lmonetary.h" 41 42 extern const char * __fix_locale_grouping_str(const char *); 43 44 #define LCMONETARY_SIZE_FULL (sizeof(struct lc_monetary_T) / sizeof(char *)) 45 #define LCMONETARY_SIZE_MIN \ 46 (offsetof(struct lc_monetary_T, int_p_cs_precedes) / \ 47 sizeof(char *)) 48 49 static char empty[] = ""; 50 static char numempty[] = { CHAR_MAX, '\0'}; 51 52 static const struct lc_monetary_T _C_monetary_locale = { 53 empty, /* int_curr_symbol */ 54 empty, /* currency_symbol */ 55 empty, /* mon_decimal_point */ 56 empty, /* mon_thousands_sep */ 57 numempty, /* mon_grouping */ 58 empty, /* positive_sign */ 59 empty, /* negative_sign */ 60 numempty, /* int_frac_digits */ 61 numempty, /* frac_digits */ 62 numempty, /* p_cs_precedes */ 63 numempty, /* p_sep_by_space */ 64 numempty, /* n_cs_precedes */ 65 numempty, /* n_sep_by_space */ 66 numempty, /* p_sign_posn */ 67 numempty, /* n_sign_posn */ 68 numempty, /* int_p_cs_precedes */ 69 numempty, /* int_n_cs_precedes */ 70 numempty, /* int_p_sep_by_space */ 71 numempty, /* int_n_sep_by_space */ 72 numempty, /* int_p_sign_posn */ 73 numempty /* int_n_sign_posn */ 74 }; 75 76 struct xlocale_monetary __xlocale_global_monetary; 77 78 static char 79 cnv(const char *str) 80 { 81 int i = strtol(str, NULL, 10); 82 83 if (i == -1) 84 i = CHAR_MAX; 85 return ((char)i); 86 } 87 88 static void 89 destruct_monetary(void *v) 90 { 91 struct xlocale_monetary *l = v; 92 if (l->buffer) 93 free(l->buffer); 94 free(l); 95 } 96 97 static int 98 monetary_load_locale_l(struct xlocale_monetary *loc, int *using_locale, 99 int *changed, const char *name) 100 { 101 int ret; 102 struct lc_monetary_T *l = &loc->locale; 103 104 ret = __part_load_locale(name, using_locale, 105 &loc->buffer, "LC_MONETARY", 106 LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN, 107 (const char **)l); 108 if (ret != _LDP_ERROR) 109 *changed = 1; 110 if (ret == _LDP_LOADED) { 111 l->mon_grouping = 112 __fix_locale_grouping_str(l->mon_grouping); 113 114 #define M_ASSIGN_CHAR(NAME) (((char *)l->NAME)[0] = \ 115 cnv(l->NAME)) 116 117 M_ASSIGN_CHAR(int_frac_digits); 118 M_ASSIGN_CHAR(frac_digits); 119 M_ASSIGN_CHAR(p_cs_precedes); 120 M_ASSIGN_CHAR(p_sep_by_space); 121 M_ASSIGN_CHAR(n_cs_precedes); 122 M_ASSIGN_CHAR(n_sep_by_space); 123 M_ASSIGN_CHAR(p_sign_posn); 124 M_ASSIGN_CHAR(n_sign_posn); 125 126 /* 127 * The six additional C99 international monetary formatting 128 * parameters default to the national parameters when 129 * reading FreeBSD LC_MONETARY data files. 130 */ 131 #define M_ASSIGN_ICHAR(NAME) \ 132 do { \ 133 if (l->int_##NAME == NULL) \ 134 l->int_##NAME = \ 135 l->NAME; \ 136 else \ 137 M_ASSIGN_CHAR(int_##NAME); \ 138 } while (0) 139 140 M_ASSIGN_ICHAR(p_cs_precedes); 141 M_ASSIGN_ICHAR(n_cs_precedes); 142 M_ASSIGN_ICHAR(p_sep_by_space); 143 M_ASSIGN_ICHAR(n_sep_by_space); 144 M_ASSIGN_ICHAR(p_sign_posn); 145 M_ASSIGN_ICHAR(n_sign_posn); 146 } 147 return (ret); 148 } 149 int 150 __monetary_load_locale(const char *name) 151 { 152 return monetary_load_locale_l(&__xlocale_global_monetary, 153 &__xlocale_global_locale.using_monetary_locale, 154 &__xlocale_global_locale.monetary_locale_changed, name); 155 } 156 void* __monetary_load(const char *name, locale_t l) 157 { 158 struct xlocale_monetary *new = calloc(sizeof(struct xlocale_monetary), 1); 159 new->header.header.destructor = destruct_monetary; 160 if (monetary_load_locale_l(new, &l->using_monetary_locale, 161 &l->monetary_locale_changed, name) == _LDP_ERROR) 162 { 163 xlocale_release(new); 164 return NULL; 165 } 166 return new; 167 } 168 169 170 struct lc_monetary_T * 171 __get_current_monetary_locale(locale_t loc) 172 { 173 return (loc->using_monetary_locale 174 ? &((struct xlocale_monetary*)loc->components[XLC_MONETARY])->locale 175 : (struct lc_monetary_T *)&_C_monetary_locale); 176 } 177 178 #ifdef LOCALE_DEBUG 179 void 180 monetdebug() { 181 printf( "int_curr_symbol = %s\n" 182 "currency_symbol = %s\n" 183 "mon_decimal_point = %s\n" 184 "mon_thousands_sep = %s\n" 185 "mon_grouping = %s\n" 186 "positive_sign = %s\n" 187 "negative_sign = %s\n" 188 "int_frac_digits = %d\n" 189 "frac_digits = %d\n" 190 "p_cs_precedes = %d\n" 191 "p_sep_by_space = %d\n" 192 "n_cs_precedes = %d\n" 193 "n_sep_by_space = %d\n" 194 "p_sign_posn = %d\n" 195 "n_sign_posn = %d\n" 196 "int_p_cs_precedes = %d\n" 197 "int_p_sep_by_space = %d\n" 198 "int_n_cs_precedes = %d\n" 199 "int_n_sep_by_space = %d\n" 200 "int_p_sign_posn = %d\n" 201 "int_n_sign_posn = %d\n", 202 _monetary_locale.int_curr_symbol, 203 _monetary_locale.currency_symbol, 204 _monetary_locale.mon_decimal_point, 205 _monetary_locale.mon_thousands_sep, 206 _monetary_locale.mon_grouping, 207 _monetary_locale.positive_sign, 208 _monetary_locale.negative_sign, 209 _monetary_locale.int_frac_digits[0], 210 _monetary_locale.frac_digits[0], 211 _monetary_locale.p_cs_precedes[0], 212 _monetary_locale.p_sep_by_space[0], 213 _monetary_locale.n_cs_precedes[0], 214 _monetary_locale.n_sep_by_space[0], 215 _monetary_locale.p_sign_posn[0], 216 _monetary_locale.n_sign_posn[0], 217 _monetary_locale.int_p_cs_precedes[0], 218 _monetary_locale.int_p_sep_by_space[0], 219 _monetary_locale.int_n_cs_precedes[0], 220 _monetary_locale.int_n_sep_by_space[0], 221 _monetary_locale.int_p_sign_posn[0], 222 _monetary_locale.int_n_sign_posn[0] 223 ); 224 } 225 #endif /* LOCALE_DEBUG */ 226