1 /* $NetBSD: citrus_lc_monetary.c,v 1.5 2012/03/04 21:14:55 tnozaki Exp $ */ 2 3 /*- 4 * Copyright (c)2008 Citrus Project, 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #if defined(LIBC_SCCS) && !defined(lint) 31 __RCSID("$NetBSD: citrus_lc_monetary.c,v 1.5 2012/03/04 21:14:55 tnozaki Exp $"); 32 #endif /* LIBC_SCCS and not lint */ 33 34 #include "namespace.h" 35 #include "reentrant.h" 36 #include <sys/types.h> 37 #include <sys/localedef.h> 38 #include <sys/queue.h> 39 #include <assert.h> 40 #include <errno.h> 41 #include <langinfo.h> 42 #include <limits.h> 43 #define __SETLOCALE_SOURCE__ 44 #include <locale.h> 45 #include <stddef.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 50 #include "setlocale_local.h" 51 52 #include "citrus_namespace.h" 53 #include "citrus_types.h" 54 #include "citrus_bcs.h" 55 #include "citrus_region.h" 56 #include "citrus_lookup.h" 57 #include "citrus_aliasname_local.h" 58 #include "citrus_module.h" 59 #include "citrus_mmap.h" 60 #include "citrus_hash.h" 61 #include "citrus_db.h" 62 #include "citrus_db_hash.h" 63 #include "citrus_memstream.h" 64 #include "runetype_local.h" 65 66 #include "fix_grouping.h" 67 #include "citrus_fix_grouping.h" 68 69 /* 70 * macro required by all template headers 71 */ 72 #define _PREFIX(name) __CONCAT(_citrus_LC_MONETARY_, name) 73 74 #include "nb_lc_monetary_misc.h" 75 #include "citrus_lc_template_decl.h" 76 77 static __inline void 78 _citrus_LC_MONETARY_uninit(_MonetaryLocale *data) 79 { 80 _DIAGASSERT(data != NULL); 81 82 free(__UNCONST(data->int_curr_symbol)); 83 free(__UNCONST(data->currency_symbol)); 84 free(__UNCONST(data->mon_decimal_point)); 85 free(__UNCONST(data->mon_thousands_sep)); 86 free(__UNCONST(data->mon_grouping)); 87 free(__UNCONST(data->positive_sign)); 88 free(__UNCONST(data->negative_sign)); 89 } 90 91 #include "citrus_lc_monetary.h" 92 93 struct _citrus_LC_MONETARY_key { 94 const char *name; 95 size_t offset; 96 }; 97 98 #define OFFSET(field) (offsetof(_MonetaryLocale, field)) 99 static const struct _citrus_LC_MONETARY_key keys_string[] = { 100 { _CITRUS_LC_MONETARY_SYM_INT_CURR_SYMBOL, OFFSET(int_curr_symbol ) }, 101 { _CITRUS_LC_MONETARY_SYM_CURRENCY_SYMBOL, OFFSET(currency_symbol ) }, 102 { _CITRUS_LC_MONETARY_SYM_MON_DECIMAL_POINT, OFFSET(mon_decimal_point ) }, 103 { _CITRUS_LC_MONETARY_SYM_MON_THOUSANDS_SEP, OFFSET(mon_thousands_sep ) }, 104 { _CITRUS_LC_MONETARY_SYM_MON_GROUPING, OFFSET(mon_grouping ) }, 105 { _CITRUS_LC_MONETARY_SYM_POSITIVE_SIGN, OFFSET(positive_sign ) }, 106 { _CITRUS_LC_MONETARY_SYM_NEGATIVE_SIGN, OFFSET(negative_sign ) }, 107 { NULL, (size_t)0 } 108 }; 109 static const struct _citrus_LC_MONETARY_key keys_char[] = { 110 { _CITRUS_LC_MONETARY_SYM_INT_FRAC_DIGITS, OFFSET(int_frac_digits ) }, 111 { _CITRUS_LC_MONETARY_SYM_FRAC_DIGITS, OFFSET(frac_digits ) }, 112 { _CITRUS_LC_MONETARY_SYM_P_CS_PRECEDES, OFFSET(p_cs_precedes ) }, 113 { _CITRUS_LC_MONETARY_SYM_P_SEP_BY_SPACE, OFFSET(p_sep_by_space ) }, 114 { _CITRUS_LC_MONETARY_SYM_N_CS_PRECEDES, OFFSET(n_cs_precedes ) }, 115 { _CITRUS_LC_MONETARY_SYM_N_SEP_BY_SPACE, OFFSET(n_sep_by_space ) }, 116 { _CITRUS_LC_MONETARY_SYM_P_SIGN_POSN, OFFSET(p_sign_posn ) }, 117 { _CITRUS_LC_MONETARY_SYM_N_SIGN_POSN, OFFSET(n_sign_posn ) }, 118 { _CITRUS_LC_MONETARY_SYM_INT_P_CS_PRECEDES, OFFSET(int_p_cs_precedes ) }, 119 { _CITRUS_LC_MONETARY_SYM_INT_N_CS_PRECEDES, OFFSET(int_n_cs_precedes ) }, 120 { _CITRUS_LC_MONETARY_SYM_INT_P_SEP_BY_SPACE, OFFSET(int_p_sep_by_space) }, 121 { _CITRUS_LC_MONETARY_SYM_INT_N_SEP_BY_SPACE, OFFSET(int_n_sep_by_space) }, 122 { _CITRUS_LC_MONETARY_SYM_INT_P_SIGN_POSN, OFFSET(int_p_sign_posn ) }, 123 { _CITRUS_LC_MONETARY_SYM_INT_N_SIGN_POSN, OFFSET(int_n_sign_posn ) }, 124 { NULL, (size_t)0 } 125 }; 126 127 static __inline int 128 _citrus_LC_MONETARY_init_normal(_MonetaryLocale * __restrict data, 129 struct _citrus_db * __restrict db) 130 { 131 const struct _citrus_LC_MONETARY_key *key; 132 char **p_string, *p_char; 133 const char *s; 134 uint8_t u8; 135 136 _DIAGASSERT(data != NULL); 137 _DIAGASSERT(db != NULL); 138 139 memset(data, 0, sizeof(*data)); 140 for (key = &keys_string[0]; key->name != NULL; ++key) { 141 if (_db_lookupstr_by_s(db, key->name, &s, NULL)) 142 goto fatal; 143 p_string = (char **)(void *) 144 (((char *)(void *)data) + key->offset); 145 *p_string = strdup(s); 146 if (*p_string == NULL) 147 goto fatal; 148 } 149 for (key = &keys_char[0]; key->name != NULL; ++key) { 150 if (_db_lookup8_by_s(db, key->name, &u8, NULL)) 151 goto fatal; 152 p_char = ((char *)(void *)data) + key->offset; 153 *p_char = (char)(unsigned char)u8; 154 } 155 _CITRUS_FIXUP_CHAR_MAX_MD(data->mon_grouping); 156 157 return 0; 158 159 fatal: 160 _citrus_LC_MONETARY_uninit(data); 161 return EFTYPE; 162 } 163 164 static __inline int 165 _citrus_LC_MONETARY_init_fallback(_MonetaryLocale * __restrict data, 166 struct _memstream * __restrict ms) 167 { 168 const struct _citrus_LC_MONETARY_key *key; 169 char **p_string, *p_char; 170 const char *s; 171 size_t n; 172 char *t; 173 long int l; 174 175 _DIAGASSERT(data != NULL); 176 _DIAGASSERT(ms != NULL); 177 178 memset(data, 0, sizeof(*data)); 179 for (key = &keys_string[0]; key->name != NULL; ++key) { 180 if ((s = _memstream_getln(ms, &n)) == NULL) 181 goto fatal; 182 p_string = (char **)(void *) 183 (((char *)(void *)data) + key->offset); 184 *p_string = strndup(s, n - 1); 185 if (*p_string == NULL) 186 goto fatal; 187 } 188 for (key = &keys_char[0]; key->name != NULL; ++key) { 189 if ((s = _memstream_getln(ms, &n)) == NULL) 190 goto fatal; 191 t = strndup(s, n - 1); 192 if (t == NULL) 193 goto fatal; 194 s = (const char *)t; 195 l = _bcs_strtol(s, &t, 0); 196 if (s == t || l < 0 || l > 0x7fL) { 197 free(t); 198 goto fatal; 199 } 200 free(t); 201 p_char = ((char *)(void *)data) + key->offset; 202 *p_char = (char)(l & 0x7fL); 203 } 204 data->mon_grouping = 205 __fix_locale_grouping_str(data->mon_grouping); 206 return 0; 207 208 fatal: 209 _citrus_LC_MONETARY_uninit(data); 210 return EFTYPE; 211 } 212 213 /* 214 * macro required by citrus_lc_template.h 215 */ 216 #define _CATEGORY_DB "LC_MONETARY" 217 #define _CATEGORY_MAGIC _CITRUS_LC_MONETARY_MAGIC_1 218 219 #include "citrus_lc_template.h" 220