localeconv.c revision 72283
1/* 2 * Copyright (c) 2001 Alexey Zelkin 3 * Copyright (c) 1991, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#if defined(LIBC_SCCS) && !defined(lint) 29#if 0 30static char sccsid[] = "@(#)localeconv.c 8.1 (Berkeley) 6/4/93"; 31#endif 32static char rcsid[] = "$FreeBSD: head/lib/libc/locale/localeconv.c 72283 2001-02-10 03:31:23Z ache $"; 33#endif /* LIBC_SCCS and not lint */ 34 35#include <locale.h> 36#include <stdlib.h> 37#include <limits.h> 38#include "lmonetary.h" 39#include "lnumeric.h" 40 41/* 42 * The localeconv() function constructs a struct lconv from the current 43 * monetary and numeric locales. 44 * 45 * Because localeconv() may be called many times (especially by library 46 * routines like printf() & strtod()), the approprate members of the 47 * lconv structure are computed only when the monetary or numeric 48 * locale has been changed. 49 */ 50int __mlocale_changed = 1; 51int __nlocale_changed = 1; 52 53static char 54cnv(char *str) { 55 int i = strtol(str, NULL, 10); 56 if (i == -1) 57 i = CHAR_MAX; 58 return (char)i; 59} 60 61/* 62 * Return the current locale conversion. 63 */ 64struct lconv * 65localeconv() 66{ 67 static struct lconv ret; 68 69 if (__mlocale_changed) { 70 /* LC_MONETARY part */ 71 struct lc_monetary_T * mptr; 72 73#define M_ASSIGN_STR(NAME) (ret.NAME = (char*)mptr->NAME) 74#define M_ASSIGN_CHAR(NAME) (ret.NAME = cnv((char*)mptr->NAME)) 75 76 mptr = __get_current_monetary_locale(); 77 M_ASSIGN_STR(int_curr_symbol); 78 M_ASSIGN_STR(currency_symbol); 79 M_ASSIGN_STR(mon_decimal_point); 80 M_ASSIGN_STR(mon_thousands_sep); 81 M_ASSIGN_STR(mon_grouping); 82 M_ASSIGN_STR(positive_sign); 83 M_ASSIGN_STR(negative_sign); 84 M_ASSIGN_CHAR(int_frac_digits); 85 M_ASSIGN_CHAR(frac_digits); 86 M_ASSIGN_CHAR(p_cs_precedes); 87 M_ASSIGN_CHAR(p_sep_by_space); 88 M_ASSIGN_CHAR(n_cs_precedes); 89 M_ASSIGN_CHAR(n_sep_by_space); 90 M_ASSIGN_CHAR(p_sign_posn); 91 M_ASSIGN_CHAR(n_sign_posn); 92 __mlocale_changed = 0; 93 } 94 95 if (__nlocale_changed) { 96 /* LC_NUMERIC part */ 97 struct lc_numeric_T * nptr; 98 99#define N_ASSIGN_STR(NAME) (ret.NAME = (char*)nptr->NAME) 100 101 nptr = __get_current_numeric_locale(); 102 N_ASSIGN_STR(decimal_point); 103 N_ASSIGN_STR(thousands_sep); 104 N_ASSIGN_STR(grouping); 105 __nlocale_changed = 0; 106 } 107 108 return (&ret); 109} 110