Import upstream version 1.28
[debian/tar] / gnu / nl_langinfo.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* nl_langinfo() replacement: query locale dependent information.
4
5    Copyright (C) 2007-2014 Free Software Foundation, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <langinfo.h>
24
25 #if REPLACE_NL_LANGINFO
26
27 /* Override nl_langinfo with support for added nl_item values.  */
28
29 # include <locale.h>
30 # include <string.h>
31
32 # undef nl_langinfo
33
34 char *
35 rpl_nl_langinfo (nl_item item)
36 {
37   switch (item)
38     {
39 # if GNULIB_defined_CODESET
40     case CODESET:
41       {
42         const char *locale;
43         static char buf[2 + 10 + 1];
44
45         locale = setlocale (LC_CTYPE, NULL);
46         if (locale != NULL && locale[0] != '\0')
47           {
48             /* If the locale name contains an encoding after the dot, return
49                it.  */
50             const char *dot = strchr (locale, '.');
51
52             if (dot != NULL)
53               {
54                 const char *modifier;
55
56                 dot++;
57                 /* Look for the possible @... trailer and remove it, if any.  */
58                 modifier = strchr (dot, '@');
59                 if (modifier == NULL)
60                   return dot;
61                 if (modifier - dot < sizeof (buf))
62                   {
63                     memcpy (buf, dot, modifier - dot);
64                     buf [modifier - dot] = '\0';
65                     return buf;
66                   }
67               }
68           }
69         return "";
70       }
71 # endif
72 # if GNULIB_defined_T_FMT_AMPM
73     case T_FMT_AMPM:
74       return "%I:%M:%S %p";
75 # endif
76 # if GNULIB_defined_ERA
77     case ERA:
78       /* The format is not standardized.  In glibc it is a sequence of strings
79          of the form "direction:offset:start_date:end_date:era_name:era_format"
80          with an empty string at the end.  */
81       return "";
82     case ERA_D_FMT:
83       /* The %Ex conversion in strftime behaves like %x if the locale does not
84          have an alternative time format.  */
85       item = D_FMT;
86       break;
87     case ERA_D_T_FMT:
88       /* The %Ec conversion in strftime behaves like %c if the locale does not
89          have an alternative time format.  */
90       item = D_T_FMT;
91       break;
92     case ERA_T_FMT:
93       /* The %EX conversion in strftime behaves like %X if the locale does not
94          have an alternative time format.  */
95       item = T_FMT;
96       break;
97     case ALT_DIGITS:
98       /* The format is not standardized.  In glibc it is a sequence of 10
99          strings, appended in memory.  */
100       return "\0\0\0\0\0\0\0\0\0\0";
101 # endif
102 # if GNULIB_defined_YESEXPR || !FUNC_NL_LANGINFO_YESEXPR_WORKS
103     case YESEXPR:
104       return "^[yY]";
105     case NOEXPR:
106       return "^[nN]";
107 # endif
108     default:
109       break;
110     }
111   return nl_langinfo (item);
112 }
113
114 #else
115
116 /* Provide nl_langinfo from scratch.  */
117
118 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
119
120 /* Native Windows platforms.  */
121
122 #  define WIN32_LEAN_AND_MEAN  /* avoid including junk */
123 #  include <windows.h>
124
125 #  include <stdio.h>
126
127 # else
128
129 /* An old Unix platform without locales, such as Linux libc5 or BeOS.  */
130
131 # endif
132
133 # include <locale.h>
134
135 char *
136 nl_langinfo (nl_item item)
137 {
138   switch (item)
139     {
140     /* nl_langinfo items of the LC_CTYPE category */
141     case CODESET:
142 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
143       {
144         static char buf[2 + 10 + 1];
145
146         /* The Windows API has a function returning the locale's codepage as
147            a number.  */
148         sprintf (buf, "CP%u", GetACP ());
149         return buf;
150       }
151 # elif defined __BEOS__
152       return "UTF-8";
153 # else
154       return "ISO-8859-1";
155 # endif
156     /* nl_langinfo items of the LC_NUMERIC category */
157     case RADIXCHAR:
158       return localeconv () ->decimal_point;
159     case THOUSEP:
160       return localeconv () ->thousands_sep;
161     /* nl_langinfo items of the LC_TIME category.
162        TODO: Really use the locale.  */
163     case D_T_FMT:
164     case ERA_D_T_FMT:
165       return "%a %b %e %H:%M:%S %Y";
166     case D_FMT:
167     case ERA_D_FMT:
168       return "%m/%d/%y";
169     case T_FMT:
170     case ERA_T_FMT:
171       return "%H:%M:%S";
172     case T_FMT_AMPM:
173       return "%I:%M:%S %p";
174     case AM_STR:
175       return "AM";
176     case PM_STR:
177       return "PM";
178     case DAY_1:
179       return "Sunday";
180     case DAY_2:
181       return "Monday";
182     case DAY_3:
183       return "Tuesday";
184     case DAY_4:
185       return "Wednesday";
186     case DAY_5:
187       return "Thursday";
188     case DAY_6:
189       return "Friday";
190     case DAY_7:
191       return "Saturday";
192     case ABDAY_1:
193       return "Sun";
194     case ABDAY_2:
195       return "Mon";
196     case ABDAY_3:
197       return "Tue";
198     case ABDAY_4:
199       return "Wed";
200     case ABDAY_5:
201       return "Thu";
202     case ABDAY_6:
203       return "Fri";
204     case ABDAY_7:
205       return "Sat";
206     case MON_1:
207       return "January";
208     case MON_2:
209       return "February";
210     case MON_3:
211       return "March";
212     case MON_4:
213       return "April";
214     case MON_5:
215       return "May";
216     case MON_6:
217       return "June";
218     case MON_7:
219       return "July";
220     case MON_8:
221       return "August";
222     case MON_9:
223       return "September";
224     case MON_10:
225       return "October";
226     case MON_11:
227       return "November";
228     case MON_12:
229       return "December";
230     case ABMON_1:
231       return "Jan";
232     case ABMON_2:
233       return "Feb";
234     case ABMON_3:
235       return "Mar";
236     case ABMON_4:
237       return "Apr";
238     case ABMON_5:
239       return "May";
240     case ABMON_6:
241       return "Jun";
242     case ABMON_7:
243       return "Jul";
244     case ABMON_8:
245       return "Aug";
246     case ABMON_9:
247       return "Sep";
248     case ABMON_10:
249       return "Oct";
250     case ABMON_11:
251       return "Nov";
252     case ABMON_12:
253       return "Dec";
254     case ERA:
255       return "";
256     case ALT_DIGITS:
257       return "\0\0\0\0\0\0\0\0\0\0";
258     /* nl_langinfo items of the LC_MONETARY category
259        TODO: Really use the locale. */
260     case CRNCYSTR:
261       return "-";
262     /* nl_langinfo items of the LC_MESSAGES category
263        TODO: Really use the locale. */
264     case YESEXPR:
265       return "^[yY]";
266     case NOEXPR:
267       return "^[nN]";
268     default:
269       return "";
270     }
271 }
272
273 #endif