nl_langinfo.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* nl_langinfo() replacement: query locale dependent information.
  2. Copyright (C) 2007-2017 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 Lesser 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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. case GROUPING:
  153. return localeconv () ->grouping;
  154. /* nl_langinfo items of the LC_TIME category.
  155. TODO: Really use the locale. */
  156. case D_T_FMT:
  157. case ERA_D_T_FMT:
  158. return (char *) "%a %b %e %H:%M:%S %Y";
  159. case D_FMT:
  160. case ERA_D_FMT:
  161. return (char *) "%m/%d/%y";
  162. case T_FMT:
  163. case ERA_T_FMT:
  164. return (char *) "%H:%M:%S";
  165. case T_FMT_AMPM:
  166. return (char *) "%I:%M:%S %p";
  167. case AM_STR:
  168. if (!strftime (nlbuf, sizeof nlbuf, "%p", &tmm))
  169. return (char *) "AM";
  170. return nlbuf;
  171. case PM_STR:
  172. tmm.tm_hour = 12;
  173. if (!strftime (nlbuf, sizeof nlbuf, "%p", &tmm))
  174. return (char *) "PM";
  175. return nlbuf;
  176. case DAY_1:
  177. case DAY_2:
  178. case DAY_3:
  179. case DAY_4:
  180. case DAY_5:
  181. case DAY_6:
  182. case DAY_7:
  183. {
  184. static char const days[][sizeof "Wednesday"] = {
  185. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
  186. "Friday", "Saturday"
  187. };
  188. tmm.tm_wday = item - DAY_1;
  189. if (!strftime (nlbuf, sizeof nlbuf, "%A", &tmm))
  190. return (char *) days[item - DAY_1];
  191. return nlbuf;
  192. }
  193. case ABDAY_1:
  194. case ABDAY_2:
  195. case ABDAY_3:
  196. case ABDAY_4:
  197. case ABDAY_5:
  198. case ABDAY_6:
  199. case ABDAY_7:
  200. {
  201. static char const abdays[][sizeof "Sun"] = {
  202. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  203. };
  204. tmm.tm_wday = item - ABDAY_1;
  205. if (!strftime (nlbuf, sizeof nlbuf, "%a", &tmm))
  206. return (char *) abdays[item - ABDAY_1];
  207. return nlbuf;
  208. }
  209. case MON_1:
  210. case MON_2:
  211. case MON_3:
  212. case MON_4:
  213. case MON_5:
  214. case MON_6:
  215. case MON_7:
  216. case MON_8:
  217. case MON_9:
  218. case MON_10:
  219. case MON_11:
  220. case MON_12:
  221. {
  222. static char const months[][sizeof "September"] = {
  223. "January", "February", "March", "April", "May", "June", "July",
  224. "September", "October", "November", "December"
  225. };
  226. tmm.tm_mon = item - MON_1;
  227. if (!strftime (nlbuf, sizeof nlbuf, "%B", &tmm))
  228. return (char *) months[item - MON_1];
  229. return nlbuf;
  230. }
  231. case ABMON_1:
  232. case ABMON_2:
  233. case ABMON_3:
  234. case ABMON_4:
  235. case ABMON_5:
  236. case ABMON_6:
  237. case ABMON_7:
  238. case ABMON_8:
  239. case ABMON_9:
  240. case ABMON_10:
  241. case ABMON_11:
  242. case ABMON_12:
  243. {
  244. static char const abmonths[][sizeof "Jan"] = {
  245. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  246. "Sep", "Oct", "Nov", "Dec"
  247. };
  248. tmm.tm_mon = item - ABMON_1;
  249. if (!strftime (nlbuf, sizeof nlbuf, "%b", &tmm))
  250. return (char *) abmonths[item - ABMON_1];
  251. return nlbuf;
  252. }
  253. case ERA:
  254. return (char *) "";
  255. case ALT_DIGITS:
  256. return (char *) "\0\0\0\0\0\0\0\0\0\0";
  257. /* nl_langinfo items of the LC_MONETARY category. */
  258. case CRNCYSTR:
  259. return localeconv () ->currency_symbol;
  260. case INT_CURR_SYMBOL:
  261. return localeconv () ->int_curr_symbol;
  262. case MON_DECIMAL_POINT:
  263. return localeconv () ->mon_decimal_point;
  264. case MON_THOUSANDS_SEP:
  265. return localeconv () ->mon_thousands_sep;
  266. case MON_GROUPING:
  267. return localeconv () ->mon_grouping;
  268. case POSITIVE_SIGN:
  269. return localeconv () ->positive_sign;
  270. case NEGATIVE_SIGN:
  271. return localeconv () ->negative_sign;
  272. case FRAC_DIGITS:
  273. return & localeconv () ->frac_digits;
  274. case INT_FRAC_DIGITS:
  275. return & localeconv () ->int_frac_digits;
  276. case P_CS_PRECEDES:
  277. return & localeconv () ->p_cs_precedes;
  278. case N_CS_PRECEDES:
  279. return & localeconv () ->n_cs_precedes;
  280. case P_SEP_BY_SPACE:
  281. return & localeconv () ->p_sep_by_space;
  282. case N_SEP_BY_SPACE:
  283. return & localeconv () ->n_sep_by_space;
  284. case P_SIGN_POSN:
  285. return & localeconv () ->p_sign_posn;
  286. case N_SIGN_POSN:
  287. return & localeconv () ->n_sign_posn;
  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