freebsd-nq/lib/libc/locale/nl_langinfo.c

176 lines
5.1 KiB
C
Raw Normal View History

/*-
* Copyright (c) 2001, 2003 Alexey Zelkin <phantom@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
2001-02-09 22:43:39 +00:00
#include <langinfo.h>
2001-02-17 07:35:01 +00:00
#include <limits.h>
#include <locale.h>
2001-02-17 07:35:01 +00:00
#include <stdlib.h>
2001-02-09 22:43:39 +00:00
#include <string.h>
#include "lnumeric.h"
#include "lmessages.h"
#include "lmonetary.h"
#include "../stdtime/timelocal.h"
#define _REL(BASE) ((int)item-BASE)
char *
nl_langinfo(nl_item item)
{
2001-02-09 22:43:39 +00:00
char *ret, *s, *cs;
2001-02-17 07:35:01 +00:00
static char *csym = NULL;
switch (item) {
case CODESET:
2001-02-09 22:43:39 +00:00
ret = "";
if ((s = setlocale(LC_CTYPE, NULL)) != NULL) {
2003-06-10 01:26:04 +00:00
if ((cs = strchr(s, '.')) != NULL)
2001-02-09 22:43:39 +00:00
ret = cs + 1;
2003-06-10 01:26:04 +00:00
else if (strcmp(s, "C") == 0 ||
strcmp(s, "POSIX") == 0)
2001-02-09 22:43:39 +00:00
ret = "US-ASCII";
}
break;
case D_T_FMT:
ret = (char *) __get_current_time_locale()->c_fmt;
break;
case D_FMT:
ret = (char *) __get_current_time_locale()->x_fmt;
break;
case T_FMT:
ret = (char *) __get_current_time_locale()->X_fmt;
break;
case T_FMT_AMPM:
2001-03-02 22:12:19 +00:00
ret = (char *) __get_current_time_locale()->ampm_fmt;
break;
case AM_STR:
ret = (char *) __get_current_time_locale()->am;
break;
case PM_STR:
ret = (char *) __get_current_time_locale()->pm;
break;
case DAY_1: case DAY_2: case DAY_3:
case DAY_4: case DAY_5: case DAY_6: case DAY_7:
ret = (char*) __get_current_time_locale()->weekday[_REL(DAY_1)];
break;
case ABDAY_1: case ABDAY_2: case ABDAY_3:
case ABDAY_4: case ABDAY_5: case ABDAY_6: case ABDAY_7:
ret = (char*) __get_current_time_locale()->wday[_REL(ABDAY_1)];
break;
case MON_1: case MON_2: case MON_3: case MON_4:
case MON_5: case MON_6: case MON_7: case MON_8:
case MON_9: case MON_10: case MON_11: case MON_12:
ret = (char*) __get_current_time_locale()->month[_REL(MON_1)];
break;
case ABMON_1: case ABMON_2: case ABMON_3: case ABMON_4:
case ABMON_5: case ABMON_6: case ABMON_7: case ABMON_8:
case ABMON_9: case ABMON_10: case ABMON_11: case ABMON_12:
ret = (char*) __get_current_time_locale()->mon[_REL(ABMON_1)];
break;
case ERA:
2001-02-19 19:09:00 +00:00
/* XXX: need to be implemented */
ret = "";
break;
case ERA_D_FMT:
2001-02-19 19:09:00 +00:00
/* XXX: need to be implemented */
ret = "";
break;
case ERA_D_T_FMT:
2001-02-19 19:09:00 +00:00
/* XXX: need to be implemented */
ret = "";
break;
case ERA_T_FMT:
2001-02-19 19:09:00 +00:00
/* XXX: need to be implemented */
ret = "";
break;
case ALT_DIGITS:
2001-02-19 19:09:00 +00:00
/* XXX: need to be implemented */
ret = "";
break;
2001-02-19 19:09:00 +00:00
case RADIXCHAR:
ret = (char*) __get_current_numeric_locale()->decimal_point;
break;
2001-02-19 19:09:00 +00:00
case THOUSEP:
ret = (char*) __get_current_numeric_locale()->thousands_sep;
break;
case YESEXPR:
ret = (char*) __get_current_messages_locale()->yesexpr;
break;
case NOEXPR:
ret = (char*) __get_current_messages_locale()->noexpr;
break;
/*
* YESSTR and NOSTR items marked with LEGACY are available, but not
* recomended by SUSv2 to be used in portable applications since
* they're subject to remove in future specification editions.
2001-02-19 19:09:00 +00:00
*/
case YESSTR: /* LEGACY */
ret = (char*) __get_current_messages_locale()->yesstr;
break;
2001-02-19 19:09:00 +00:00
case NOSTR: /* LEGACY */
ret = (char*) __get_current_messages_locale()->nostr;
break;
/*
* SUSv2 special formatted currency string
*/
2001-02-19 19:09:00 +00:00
case CRNCYSTR:
ret = "";
2001-02-17 07:35:01 +00:00
cs = (char*) __get_current_monetary_locale()->currency_symbol;
if (*cs != '\0') {
char pos = localeconv()->p_cs_precedes;
2001-02-17 11:15:30 +00:00
if (pos == localeconv()->n_cs_precedes) {
char psn = '\0';
2001-02-17 07:35:01 +00:00
2001-02-17 11:15:30 +00:00
if (pos == CHAR_MAX) {
if (strcmp(cs, __get_current_monetary_locale()->mon_decimal_point) == 0)
psn = '.';
} else
psn = pos ? '-' : '+';
if (psn != '\0') {
int clen = strlen(cs);
if ((csym = reallocf(csym, clen + 2)) != NULL) {
*csym = psn;
strcpy(csym + 1, cs);
ret = csym;
}
2001-02-17 07:35:01 +00:00
}
}
}
break;
case D_MD_ORDER: /* FreeBSD local extension */
ret = (char *) __get_current_time_locale()->md_order;
break;
default:
ret = "";
}
return (ret);
}