2N/A/* nl_langinfo() replacement: query locale dependent information.
2N/A
2N/A Copyright (C) 2007-2010 Free Software Foundation, Inc.
2N/A
2N/A This program is free software: you can redistribute it and/or modify
2N/A it under the terms of the GNU General Public License as published by
2N/A the Free Software Foundation; either version 3 of the License, or
2N/A (at your option) any later version.
2N/A
2N/A This program is distributed in the hope that it will be useful,
2N/A but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A GNU General Public License for more details.
2N/A
2N/A You should have received a copy of the GNU General Public License
2N/A along with this program. If not, see <http://www.gnu.org/licenses/>. */
2N/A
2N/A#include <config.h>
2N/A
2N/A/* Specification. */
2N/A#include <langinfo.h>
2N/A
2N/A#if REPLACE_NL_LANGINFO
2N/A
2N/A/* Override nl_langinfo with support for added nl_item values. */
2N/A
2N/A# include <locale.h>
2N/A# include <string.h>
2N/A
2N/A# undef nl_langinfo
2N/A
2N/Achar *
2N/Arpl_nl_langinfo (nl_item item)
2N/A{
2N/A switch (item)
2N/A {
2N/A# if GNULIB_defined_CODESET
2N/A case CODESET:
2N/A {
2N/A const char *locale;
2N/A static char buf[2 + 10 + 1];
2N/A
2N/A locale = setlocale (LC_CTYPE, NULL);
2N/A if (locale != NULL && locale[0] != '\0')
2N/A {
2N/A /* If the locale name contains an encoding after the dot, return
2N/A it. */
2N/A const char *dot = strchr (locale, '.');
2N/A
2N/A if (dot != NULL)
2N/A {
2N/A const char *modifier;
2N/A
2N/A dot++;
2N/A /* Look for the possible @... trailer and remove it, if any. */
2N/A modifier = strchr (dot, '@');
2N/A if (modifier == NULL)
2N/A return dot;
2N/A if (modifier - dot < sizeof (buf))
2N/A {
2N/A memcpy (buf, dot, modifier - dot);
2N/A buf [modifier - dot] = '\0';
2N/A return buf;
2N/A }
2N/A }
2N/A }
2N/A return "";
2N/A }
2N/A# endif
2N/A# if GNULIB_defined_T_FMT_AMPM
2N/A case T_FMT_AMPM:
2N/A return "%I:%M:%S %p";
2N/A# endif
2N/A# if GNULIB_defined_ERA
2N/A case ERA:
2N/A /* The format is not standardized. In glibc it is a sequence of strings
2N/A of the form "direction:offset:start_date:end_date:era_name:era_format"
2N/A with an empty string at the end. */
2N/A return "";
2N/A case ERA_D_FMT:
2N/A /* The %Ex conversion in strftime behaves like %x if the locale does not
2N/A have an alternative time format. */
2N/A item = D_FMT;
2N/A break;
2N/A case ERA_D_T_FMT:
2N/A /* The %Ec conversion in strftime behaves like %c if the locale does not
2N/A have an alternative time format. */
2N/A item = D_T_FMT;
2N/A break;
2N/A case ERA_T_FMT:
2N/A /* The %EX conversion in strftime behaves like %X if the locale does not
2N/A have an alternative time format. */
2N/A item = T_FMT;
2N/A break;
2N/A case ALT_DIGITS:
2N/A /* The format is not standardized. In glibc it is a sequence of 10
2N/A strings, appended in memory. */
2N/A return "\0\0\0\0\0\0\0\0\0\0";
2N/A# endif
2N/A# if GNULIB_defined_YESEXPR
2N/A case YESEXPR:
2N/A return "^[yY]";
2N/A case NOEXPR:
2N/A return "^[nN]";
2N/A# endif
2N/A default:
2N/A break;
2N/A }
2N/A return nl_langinfo (item);
2N/A}
2N/A
2N/A#else
2N/A
2N/A/* Provide nl_langinfo from scratch. */
2N/A
2N/A# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
2N/A
2N/A/* Native Windows platforms. */
2N/A
2N/A# define WIN32_LEAN_AND_MEAN /* avoid including junk */
2N/A# include <windows.h>
2N/A
2N/A# include <stdio.h>
2N/A
2N/A# else
2N/A
2N/A/* An old Unix platform without locales, such as Linux libc5 or BeOS. */
2N/A
2N/A# endif
2N/A
2N/A# include <locale.h>
2N/A
2N/Achar *
2N/Anl_langinfo (nl_item item)
2N/A{
2N/A switch (item)
2N/A {
2N/A /* nl_langinfo items of the LC_CTYPE category */
2N/A case CODESET:
2N/A# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
2N/A {
2N/A static char buf[2 + 10 + 1];
2N/A
2N/A /* Woe32 has a function returning the locale's codepage as a number. */
2N/A sprintf (buf, "CP%u", GetACP ());
2N/A return buf;
2N/A }
2N/A# elif defined __BEOS__
2N/A return "UTF-8";
2N/A# else
2N/A return "ISO-8859-1";
2N/A# endif
2N/A /* nl_langinfo items of the LC_NUMERIC category */
2N/A case RADIXCHAR:
2N/A return localeconv () ->decimal_point;
2N/A case THOUSEP:
2N/A return localeconv () ->thousands_sep;
2N/A /* nl_langinfo items of the LC_TIME category.
2N/A TODO: Really use the locale. */
2N/A case D_T_FMT:
2N/A case ERA_D_T_FMT:
2N/A return "%a %b %e %H:%M:%S %Y";
2N/A case D_FMT:
2N/A case ERA_D_FMT:
2N/A return "%m/%d/%y";
2N/A case T_FMT:
2N/A case ERA_T_FMT:
2N/A return "%H:%M:%S";
2N/A case T_FMT_AMPM:
2N/A return "%I:%M:%S %p";
2N/A case AM_STR:
2N/A return "AM";
2N/A case PM_STR:
2N/A return "PM";
2N/A case DAY_1:
2N/A return "Sunday";
2N/A case DAY_2:
2N/A return "Monday";
2N/A case DAY_3:
2N/A return "Tuesday";
2N/A case DAY_4:
2N/A return "Wednesday";
2N/A case DAY_5:
2N/A return "Thursday";
2N/A case DAY_6:
2N/A return "Friday";
2N/A case DAY_7:
2N/A return "Saturday";
2N/A case ABDAY_1:
2N/A return "Sun";
2N/A case ABDAY_2:
2N/A return "Mon";
2N/A case ABDAY_3:
2N/A return "Tue";
2N/A case ABDAY_4:
2N/A return "Wed";
2N/A case ABDAY_5:
2N/A return "Thu";
2N/A case ABDAY_6:
2N/A return "Fri";
2N/A case ABDAY_7:
2N/A return "Sat";
2N/A case MON_1:
2N/A return "January";
2N/A case MON_2:
2N/A return "February";
2N/A case MON_3:
2N/A return "March";
2N/A case MON_4:
2N/A return "April";
2N/A case MON_5:
2N/A return "May";
2N/A case MON_6:
2N/A return "June";
2N/A case MON_7:
2N/A return "July";
2N/A case MON_8:
2N/A return "August";
2N/A case MON_9:
2N/A return "September";
2N/A case MON_10:
2N/A return "October";
2N/A case MON_11:
2N/A return "November";
2N/A case MON_12:
2N/A return "December";
2N/A case ABMON_1:
2N/A return "Jan";
2N/A case ABMON_2:
2N/A return "Feb";
2N/A case ABMON_3:
2N/A return "Mar";
2N/A case ABMON_4:
2N/A return "Apr";
2N/A case ABMON_5:
2N/A return "May";
2N/A case ABMON_6:
2N/A return "Jun";
2N/A case ABMON_7:
2N/A return "Jul";
2N/A case ABMON_8:
2N/A return "Aug";
2N/A case ABMON_9:
2N/A return "Sep";
2N/A case ABMON_10:
2N/A return "Oct";
2N/A case ABMON_11:
2N/A return "Nov";
2N/A case ABMON_12:
2N/A return "Dec";
2N/A case ERA:
2N/A return "";
2N/A case ALT_DIGITS:
2N/A return "\0\0\0\0\0\0\0\0\0\0";
2N/A /* nl_langinfo items of the LC_MONETARY category
2N/A TODO: Really use the locale. */
2N/A case CRNCYSTR:
2N/A return "-";
2N/A /* nl_langinfo items of the LC_MESSAGES category
2N/A TODO: Really use the locale. */
2N/A case YESEXPR:
2N/A return "^[yY]";
2N/A case NOEXPR:
2N/A return "^[nN]";
2N/A default:
2N/A return "";
2N/A }
2N/A}
2N/A
2N/A#endif