]> git.saurik.com Git - apple/libc.git/blob - locale/nl_langinfo-fbsd.c
Libc-498.tar.gz
[apple/libc.git] / locale / nl_langinfo-fbsd.c
1 /*-
2 * Copyright (c) 2001, 2003 Alexey Zelkin <phantom@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libc/locale/nl_langinfo.c,v 1.17 2003/06/26 10:46:16 phantom Exp $");
29
30 #include "xlocale_private.h"
31
32 #include <langinfo.h>
33 #include <limits.h>
34 #include <locale.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "lnumeric.h"
39 #include "lmessages.h"
40 #include "lmonetary.h"
41 #include "timelocal.h"
42
43 #define _REL(BASE) ((int)item-BASE)
44
45 char *
46 nl_langinfo_l(nl_item item, locale_t loc)
47 {
48 char *ret, *cs;
49 const char *s;
50 static char *csym = NULL;
51
52 NORMALIZE_LOCALE(loc);
53 switch (item) {
54 case CODESET:
55 ret = "";
56 if ((s = querylocale(LC_CTYPE_MASK, loc)) != NULL) {
57 if ((cs = strchr(s, '.')) != NULL)
58 ret = cs + 1;
59 else if (strcmp(s, "C") == 0 ||
60 strcmp(s, "POSIX") == 0)
61 ret = "US-ASCII";
62 }
63 break;
64 case D_T_FMT:
65 ret = (char *) __get_current_time_locale(loc)->c_fmt;
66 break;
67 case D_FMT:
68 ret = (char *) __get_current_time_locale(loc)->x_fmt;
69 break;
70 case T_FMT:
71 ret = (char *) __get_current_time_locale(loc)->X_fmt;
72 break;
73 case T_FMT_AMPM:
74 ret = (char *) __get_current_time_locale(loc)->ampm_fmt;
75 break;
76 case AM_STR:
77 ret = (char *) __get_current_time_locale(loc)->am;
78 break;
79 case PM_STR:
80 ret = (char *) __get_current_time_locale(loc)->pm;
81 break;
82 case DAY_1: case DAY_2: case DAY_3:
83 case DAY_4: case DAY_5: case DAY_6: case DAY_7:
84 ret = (char*) __get_current_time_locale(loc)->weekday[_REL(DAY_1)];
85 break;
86 case ABDAY_1: case ABDAY_2: case ABDAY_3:
87 case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
88 ret = (char*) __get_current_time_locale(loc)->wday[_REL(ABDAY_1)];
89 break;
90 case MON_1: case MON_2: case MON_3: case MON_4:
91 case MON_5: case MON_6: case MON_7: case MON_8:
92 case MON_9: case MON_10: case MON_11: case MON_12:
93 ret = (char*) __get_current_time_locale(loc)->month[_REL(MON_1)];
94 break;
95 case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
96 case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
97 case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
98 ret = (char*) __get_current_time_locale(loc)->mon[_REL(ABMON_1)];
99 break;
100 case ERA:
101 /* XXX: need to be implemented */
102 ret = "";
103 break;
104 case ERA_D_FMT:
105 /* XXX: need to be implemented */
106 ret = "";
107 break;
108 case ERA_D_T_FMT:
109 /* XXX: need to be implemented */
110 ret = "";
111 break;
112 case ERA_T_FMT:
113 /* XXX: need to be implemented */
114 ret = "";
115 break;
116 case ALT_DIGITS:
117 /* XXX: need to be implemented */
118 ret = "";
119 break;
120 case RADIXCHAR:
121 ret = (char*) __get_current_numeric_locale(loc)->decimal_point;
122 break;
123 case THOUSEP:
124 ret = (char*) __get_current_numeric_locale(loc)->thousands_sep;
125 break;
126 case YESEXPR:
127 ret = (char*) __get_current_messages_locale(loc)->yesexpr;
128 break;
129 case NOEXPR:
130 ret = (char*) __get_current_messages_locale(loc)->noexpr;
131 break;
132 /*
133 * YESSTR and NOSTR items marked with LEGACY are available, but not
134 * recomended by SUSv2 to be used in portable applications since
135 * they're subject to remove in future specification editions.
136 */
137 case YESSTR: /* LEGACY */
138 ret = (char*) __get_current_messages_locale(loc)->yesstr;
139 break;
140 case NOSTR: /* LEGACY */
141 ret = (char*) __get_current_messages_locale(loc)->nostr;
142 break;
143 /*
144 * SUSv2 special formatted currency string
145 */
146 case CRNCYSTR:
147 ret = "";
148 cs = (char*) __get_current_monetary_locale(loc)->currency_symbol;
149 if (*cs != '\0') {
150 char pos = localeconv_l(loc)->p_cs_precedes;
151
152 if (pos == localeconv_l(loc)->n_cs_precedes) {
153 char psn = '\0';
154
155 if (pos == CHAR_MAX) {
156 if (strcmp(cs, __get_current_monetary_locale(loc)->mon_decimal_point) == 0)
157 psn = '.';
158 } else
159 psn = pos ? '-' : '+';
160 if (psn != '\0') {
161 int clen = strlen(cs);
162
163 if ((csym = reallocf(csym, clen + 2)) != NULL) {
164 *csym = psn;
165 strcpy(csym + 1, cs);
166 ret = csym;
167 }
168 }
169 }
170 }
171 break;
172 case D_MD_ORDER: /* FreeBSD local extension */
173 ret = (char *) __get_current_time_locale(loc)->md_order;
174 break;
175 default:
176 return ""; /* do not consult POSIX */
177 }
178 if (ret && !ret[0] && item != D_MD_ORDER && item != CODESET && loc != _c_locale) {
179 ret = nl_langinfo_l(item, _c_locale);
180 }
181 return (ret);
182 }
183
184 char *
185 nl_langinfo(nl_item item)
186 {
187 return (nl_langinfo_l(item, __current_locale()));
188 }