nl_langinfo.c 9.0 KB

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