nl_langinfo.c 8.9 KB

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