strfmon_l.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Formatting a monetary value according to the given locale.
  2. Copyright (C) 1996-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <ctype.h>
  17. #include <errno.h>
  18. #include <langinfo.h>
  19. #include <locale.h>
  20. #include <monetary.h>
  21. #include "../libio/libioP.h"
  22. #include "../libio/strfile.h"
  23. #include <printf.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "../locale/localeinfo.h"
  28. #define out_char(Ch) \
  29. do { \
  30. if (dest >= s + maxsize - 1) \
  31. { \
  32. __set_errno (E2BIG); \
  33. va_end (ap); \
  34. return -1; \
  35. } \
  36. *dest++ = (Ch); \
  37. } while (0)
  38. #define out_string(String) \
  39. do { \
  40. const char *_s = (String); \
  41. while (*_s) \
  42. out_char (*_s++); \
  43. } while (0)
  44. #define out_nstring(String, N) \
  45. do { \
  46. int _n = (N); \
  47. const char *_s = (String); \
  48. while (_n-- > 0) \
  49. out_char (*_s++); \
  50. } while (0)
  51. #define to_digit(Ch) ((Ch) - '0')
  52. /* We use this code also for the extended locale handling where the
  53. function gets as an additional argument the locale which has to be
  54. used. To access the values we have to redefine the _NL_CURRENT
  55. macro. */
  56. #undef _NL_CURRENT
  57. #define _NL_CURRENT(category, item) \
  58. (current->values[_NL_ITEM_INDEX (item)].string)
  59. /* We have to overcome some problems with this implementation. On the
  60. one hand the strfmon() function is specified in XPG4 and of course
  61. it has to follow this. But on the other hand POSIX.2 specifies
  62. some information in the LC_MONETARY category which should be used,
  63. too. Some of the information contradicts the information which can
  64. be specified in format string. */
  65. ssize_t
  66. __vstrfmon_l_internal (char *s, size_t maxsize, locale_t loc,
  67. const char *format, va_list ap, unsigned int flags)
  68. {
  69. struct __locale_data *current = loc->__locales[LC_MONETARY];
  70. _IO_strfile f;
  71. struct printf_info info;
  72. char *dest; /* Pointer so copy the output. */
  73. const char *fmt; /* Pointer that walks through format. */
  74. dest = s;
  75. fmt = format;
  76. /* Loop through the format-string. */
  77. while (*fmt != '\0')
  78. {
  79. /* The floating-point value to output. */
  80. union
  81. {
  82. double dbl;
  83. long double ldbl;
  84. }
  85. fpnum;
  86. int int_format;
  87. int print_curr_symbol;
  88. int left_prec;
  89. int left_pad;
  90. int right_prec;
  91. int group;
  92. char pad;
  93. int is_long_double;
  94. int p_sign_posn;
  95. int n_sign_posn;
  96. int sign_posn;
  97. int other_sign_posn;
  98. int left;
  99. int is_negative;
  100. int sep_by_space;
  101. int other_sep_by_space;
  102. int cs_precedes;
  103. int other_cs_precedes;
  104. const char *sign_string;
  105. const char *other_sign_string;
  106. int done;
  107. const char *currency_symbol;
  108. size_t currency_symbol_len;
  109. long int width;
  110. char *startp;
  111. const void *ptr;
  112. char space_char;
  113. /* Process all character which do not introduce a format
  114. specification. */
  115. if (*fmt != '%')
  116. {
  117. out_char (*fmt++);
  118. continue;
  119. }
  120. /* "%%" means a single '%' character. */
  121. if (fmt[1] == '%')
  122. {
  123. out_char (*++fmt);
  124. ++fmt;
  125. continue;
  126. }
  127. /* Defaults for formatting. */
  128. int_format = 0; /* Use international curr. symbol */
  129. print_curr_symbol = 1; /* Print the currency symbol. */
  130. left_prec = -1; /* No left precision specified. */
  131. right_prec = -1; /* No right precision specified. */
  132. group = 1; /* Print digits grouped. */
  133. pad = ' '; /* Fill character is <SP>. */
  134. is_long_double = 0; /* Double argument by default. */
  135. p_sign_posn = -2; /* This indicates whether the */
  136. n_sign_posn = -2; /* '(' flag is given. */
  137. width = -1; /* No width specified so far. */
  138. left = 0; /* Right justified by default. */
  139. /* Parse group characters. */
  140. while (1)
  141. {
  142. switch (*++fmt)
  143. {
  144. case '=': /* Set fill character. */
  145. pad = *++fmt;
  146. if (pad == '\0')
  147. {
  148. /* Premature EOS. */
  149. __set_errno (EINVAL);
  150. return -1;
  151. }
  152. continue;
  153. case '^': /* Don't group digits. */
  154. group = 0;
  155. continue;
  156. case '+': /* Use +/- for sign of number. */
  157. if (n_sign_posn != -2)
  158. {
  159. __set_errno (EINVAL);
  160. return -1;
  161. }
  162. p_sign_posn = *_NL_CURRENT (LC_MONETARY, P_SIGN_POSN);
  163. n_sign_posn = *_NL_CURRENT (LC_MONETARY, N_SIGN_POSN);
  164. continue;
  165. case '(': /* Use ( ) for negative sign. */
  166. if (n_sign_posn != -2)
  167. {
  168. __set_errno (EINVAL);
  169. return -1;
  170. }
  171. p_sign_posn = 0;
  172. n_sign_posn = 0;
  173. continue;
  174. case '!': /* Don't print the currency symbol. */
  175. print_curr_symbol = 0;
  176. continue;
  177. case '-': /* Print left justified. */
  178. left = 1;
  179. continue;
  180. default:
  181. /* Will stop the loop. */;
  182. }
  183. break;
  184. }
  185. if (isdigit (*fmt))
  186. {
  187. /* Parse field width. */
  188. width = to_digit (*fmt);
  189. while (isdigit (*++fmt))
  190. {
  191. int val = to_digit (*fmt);
  192. if (width > LONG_MAX / 10
  193. || (width == LONG_MAX && val > LONG_MAX % 10))
  194. {
  195. __set_errno (E2BIG);
  196. return -1;
  197. }
  198. width = width * 10 + val;
  199. }
  200. /* If we don't have enough room for the demanded width we
  201. can stop now and return an error. */
  202. if (width >= maxsize - (dest - s))
  203. {
  204. __set_errno (E2BIG);
  205. return -1;
  206. }
  207. }
  208. /* Recognize left precision. */
  209. if (*fmt == '#')
  210. {
  211. if (!isdigit (*++fmt))
  212. {
  213. __set_errno (EINVAL);
  214. return -1;
  215. }
  216. left_prec = to_digit (*fmt);
  217. while (isdigit (*++fmt))
  218. {
  219. left_prec *= 10;
  220. left_prec += to_digit (*fmt);
  221. }
  222. }
  223. /* Recognize right precision. */
  224. if (*fmt == '.')
  225. {
  226. if (!isdigit (*++fmt))
  227. {
  228. __set_errno (EINVAL);
  229. return -1;
  230. }
  231. right_prec = to_digit (*fmt);
  232. while (isdigit (*++fmt))
  233. {
  234. right_prec *= 10;
  235. right_prec += to_digit (*fmt);
  236. }
  237. }
  238. /* Handle modifier. This is an extension. */
  239. if (*fmt == 'L')
  240. {
  241. ++fmt;
  242. if (__glibc_likely ((flags & STRFMON_LDBL_IS_DBL) == 0))
  243. is_long_double = 1;
  244. }
  245. /* Handle format specifier. */
  246. char int_symbol[4];
  247. switch (*fmt++)
  248. {
  249. case 'i': { /* Use international currency symbol. */
  250. const char *int_curr_symbol;
  251. int_curr_symbol = _NL_CURRENT (LC_MONETARY, INT_CURR_SYMBOL);
  252. strncpy(int_symbol, int_curr_symbol, 3);
  253. int_symbol[3] = '\0';
  254. currency_symbol_len = 3;
  255. currency_symbol = &int_symbol[0];
  256. space_char = int_curr_symbol[3];
  257. int_format = 1;
  258. break;
  259. }
  260. case 'n': /* Use national currency symbol. */
  261. currency_symbol = _NL_CURRENT (LC_MONETARY, CURRENCY_SYMBOL);
  262. currency_symbol_len = strlen (currency_symbol);
  263. space_char = ' ';
  264. int_format = 0;
  265. break;
  266. default: /* Any unrecognized format is an error. */
  267. __set_errno (EINVAL);
  268. return -1;
  269. }
  270. /* If not specified by the format string now find the values for
  271. the format specification. */
  272. if (p_sign_posn == -2)
  273. p_sign_posn = *_NL_CURRENT (LC_MONETARY, int_format ? INT_P_SIGN_POSN : P_SIGN_POSN);
  274. if (n_sign_posn == -2)
  275. n_sign_posn = *_NL_CURRENT (LC_MONETARY, int_format ? INT_N_SIGN_POSN : N_SIGN_POSN);
  276. if (right_prec == -1)
  277. {
  278. right_prec = *_NL_CURRENT (LC_MONETARY, int_format ? INT_FRAC_DIGITS : FRAC_DIGITS);
  279. if (right_prec == '\377')
  280. right_prec = 2;
  281. }
  282. /* If we have to print the digits grouped determine how many
  283. extra characters this means. */
  284. if (group && left_prec != -1)
  285. left_prec += __guess_grouping (left_prec,
  286. _NL_CURRENT (LC_MONETARY, MON_GROUPING));
  287. /* Now it's time to get the value. */
  288. if (is_long_double == 1)
  289. {
  290. fpnum.ldbl = va_arg (ap, long double);
  291. is_negative = fpnum.ldbl < 0;
  292. if (is_negative)
  293. fpnum.ldbl = -fpnum.ldbl;
  294. }
  295. else
  296. {
  297. fpnum.dbl = va_arg (ap, double);
  298. is_negative = fpnum.dbl < 0;
  299. if (is_negative)
  300. fpnum.dbl = -fpnum.dbl;
  301. }
  302. /* We now know the sign of the value and can determine the format. */
  303. if (is_negative)
  304. {
  305. sign_string = _NL_CURRENT (LC_MONETARY, NEGATIVE_SIGN);
  306. /* If the locale does not specify a character for the
  307. negative sign we use a '-'. */
  308. if (*sign_string == '\0')
  309. sign_string = (const char *) "-";
  310. cs_precedes = *_NL_CURRENT (LC_MONETARY, int_format ? INT_N_CS_PRECEDES : N_CS_PRECEDES);
  311. sep_by_space = *_NL_CURRENT (LC_MONETARY, int_format ? INT_N_SEP_BY_SPACE : N_SEP_BY_SPACE);
  312. sign_posn = n_sign_posn;
  313. other_sign_string = _NL_CURRENT (LC_MONETARY, POSITIVE_SIGN);
  314. other_cs_precedes = *_NL_CURRENT (LC_MONETARY, int_format ? INT_P_CS_PRECEDES : P_CS_PRECEDES);
  315. other_sep_by_space = *_NL_CURRENT (LC_MONETARY, int_format ? INT_P_SEP_BY_SPACE : P_SEP_BY_SPACE);
  316. other_sign_posn = p_sign_posn;
  317. }
  318. else
  319. {
  320. sign_string = _NL_CURRENT (LC_MONETARY, POSITIVE_SIGN);
  321. cs_precedes = *_NL_CURRENT (LC_MONETARY, int_format ? INT_P_CS_PRECEDES : P_CS_PRECEDES);
  322. sep_by_space = *_NL_CURRENT (LC_MONETARY, int_format ? INT_P_SEP_BY_SPACE : P_SEP_BY_SPACE);
  323. sign_posn = p_sign_posn;
  324. other_sign_string = _NL_CURRENT (LC_MONETARY, NEGATIVE_SIGN);
  325. if (*other_sign_string == '\0')
  326. other_sign_string = (const char *) "-";
  327. other_cs_precedes = *_NL_CURRENT (LC_MONETARY, int_format ? INT_N_CS_PRECEDES : N_CS_PRECEDES);
  328. other_sep_by_space = *_NL_CURRENT (LC_MONETARY, int_format ? INT_N_SEP_BY_SPACE : N_SEP_BY_SPACE);
  329. other_sign_posn = n_sign_posn;
  330. }
  331. /* Set default values for unspecified information. */
  332. if (cs_precedes != 0)
  333. cs_precedes = 1;
  334. if (other_cs_precedes != 0)
  335. other_cs_precedes = 1;
  336. if (sep_by_space == '\377')
  337. sep_by_space = 0;
  338. if (other_sep_by_space == '\377')
  339. other_sep_by_space = 0;
  340. if (sign_posn == '\377')
  341. sign_posn = 1;
  342. if (other_sign_posn == '\377')
  343. other_sign_posn = 1;
  344. /* Check for degenerate cases */
  345. if (sep_by_space == 2)
  346. {
  347. if (sign_posn == 0 ||
  348. (sign_posn == 1 && !cs_precedes) ||
  349. (sign_posn == 2 && cs_precedes))
  350. /* sign and symbol are not adjacent, so no separator */
  351. sep_by_space = 0;
  352. }
  353. if (other_sep_by_space == 2)
  354. {
  355. if (other_sign_posn == 0 ||
  356. (other_sign_posn == 1 && !other_cs_precedes) ||
  357. (other_sign_posn == 2 && other_cs_precedes))
  358. /* sign and symbol are not adjacent, so no separator */
  359. other_sep_by_space = 0;
  360. }
  361. /* Set the left precision and padding needed for alignment */
  362. if (left_prec == -1)
  363. {
  364. left_prec = 0;
  365. left_pad = 0;
  366. }
  367. else
  368. {
  369. /* Set left_pad to number of spaces needed to align positive
  370. and negative formats */
  371. int left_bytes = 0;
  372. int other_left_bytes = 0;
  373. /* Work out number of bytes for currency string and separator
  374. preceding the value */
  375. if (cs_precedes)
  376. {
  377. left_bytes += currency_symbol_len;
  378. if (sep_by_space != 0)
  379. ++left_bytes;
  380. }
  381. if (other_cs_precedes)
  382. {
  383. other_left_bytes += currency_symbol_len;
  384. if (other_sep_by_space != 0)
  385. ++other_left_bytes;
  386. }
  387. /* Work out number of bytes for the sign (or left parenthesis)
  388. preceding the value */
  389. if (sign_posn == 0 && is_negative)
  390. ++left_bytes;
  391. else if (sign_posn == 1)
  392. left_bytes += strlen (sign_string);
  393. else if (cs_precedes && (sign_posn == 3 || sign_posn == 4))
  394. left_bytes += strlen (sign_string);
  395. if (other_sign_posn == 0 && !is_negative)
  396. ++other_left_bytes;
  397. else if (other_sign_posn == 1)
  398. other_left_bytes += strlen (other_sign_string);
  399. else if (other_cs_precedes &&
  400. (other_sign_posn == 3 || other_sign_posn == 4))
  401. other_left_bytes += strlen (other_sign_string);
  402. /* Compare the number of bytes preceding the value for
  403. each format, and set the padding accordingly */
  404. if (other_left_bytes > left_bytes)
  405. left_pad = other_left_bytes - left_bytes;
  406. else
  407. left_pad = 0;
  408. }
  409. /* Perhaps we'll someday make these things configurable so
  410. better start using symbolic names now. */
  411. #define left_paren '('
  412. #define right_paren ')'
  413. startp = dest; /* Remember start so we can compute length. */
  414. while (left_pad-- > 0)
  415. out_char (' ');
  416. if (sign_posn == 0 && is_negative)
  417. out_char (left_paren);
  418. if (cs_precedes)
  419. {
  420. if (sign_posn != 0 && sign_posn != 2 && sign_posn != 4
  421. && sign_posn != 5)
  422. {
  423. out_string (sign_string);
  424. if (sep_by_space == 2)
  425. out_char (' ');
  426. }
  427. if (print_curr_symbol)
  428. out_string (currency_symbol);
  429. if (sign_posn == 4)
  430. {
  431. if (print_curr_symbol && sep_by_space == 2)
  432. out_char (space_char);
  433. out_string (sign_string);
  434. if (sep_by_space == 1)
  435. /* POSIX.2 and SUS are not clear on this case, but C99
  436. says a space follows the adjacent-symbol-and-sign */
  437. out_char (' ');
  438. }
  439. else
  440. if (print_curr_symbol && sep_by_space == 1)
  441. out_char (space_char);
  442. }
  443. else
  444. if (sign_posn != 0 && sign_posn != 2 && sign_posn != 3
  445. && sign_posn != 4 && sign_posn != 5)
  446. out_string (sign_string);
  447. /* Print the number. */
  448. #ifdef _IO_MTSAFE_IO
  449. f._sbf._f._lock = NULL;
  450. #endif
  451. _IO_init_internal (&f._sbf._f, 0);
  452. _IO_JUMPS (&f._sbf) = &_IO_str_jumps;
  453. _IO_str_init_static_internal (&f, dest, (s + maxsize) - dest, dest);
  454. /* We clear the last available byte so we can find out whether
  455. the numeric representation is too long. */
  456. s[maxsize - 1] = '\0';
  457. memset (&info, '\0', sizeof (info));
  458. info.prec = right_prec;
  459. info.width = left_prec + (right_prec ? (right_prec + 1) : 0);
  460. info.spec = 'f';
  461. info.is_long_double = is_long_double;
  462. info.group = group;
  463. info.pad = pad;
  464. info.extra = 1; /* This means use values from LC_MONETARY. */
  465. ptr = &fpnum;
  466. done = __printf_fp_l (&f._sbf._f, loc, &info, &ptr);
  467. if (done < 0)
  468. return -1;
  469. if (s[maxsize - 1] != '\0')
  470. {
  471. __set_errno (E2BIG);
  472. return -1;
  473. }
  474. dest += done;
  475. if (!cs_precedes)
  476. {
  477. if (sign_posn == 3)
  478. {
  479. if (sep_by_space == 1)
  480. out_char (' ');
  481. out_string (sign_string);
  482. }
  483. if (print_curr_symbol)
  484. {
  485. if ((sign_posn == 3 && sep_by_space == 2)
  486. || (sign_posn == 4 && sep_by_space == 1)
  487. || (sign_posn == 2 && sep_by_space == 1)
  488. || (sign_posn == 1 && sep_by_space == 1)
  489. || (sign_posn == 0 && sep_by_space == 1))
  490. out_char (space_char);
  491. out_nstring (currency_symbol, currency_symbol_len);
  492. }
  493. if (sign_posn == 4)
  494. {
  495. if (sep_by_space == 2)
  496. out_char (' ');
  497. out_string (sign_string);
  498. }
  499. }
  500. if (sign_posn == 2)
  501. {
  502. if (sep_by_space == 2)
  503. out_char (' ');
  504. out_string (sign_string);
  505. }
  506. if (sign_posn == 0 && is_negative)
  507. out_char (right_paren);
  508. /* Now test whether the output width is filled. */
  509. if (dest - startp < width)
  510. {
  511. if (left)
  512. /* We simply have to fill using spaces. */
  513. do
  514. out_char (' ');
  515. while (dest - startp < width);
  516. else
  517. {
  518. long int dist = width - (dest - startp);
  519. for (char *cp = dest - 1; cp >= startp; --cp)
  520. cp[dist] = cp[0];
  521. dest += dist;
  522. do
  523. startp[--dist] = ' ';
  524. while (dist > 0);
  525. }
  526. }
  527. }
  528. /* Terminate the string. */
  529. *dest = '\0';
  530. return dest - s;
  531. }
  532. ssize_t
  533. ___strfmon_l (char *s, size_t maxsize, locale_t loc, const char *format, ...)
  534. {
  535. va_list ap;
  536. va_start (ap, format);
  537. ssize_t res = __vstrfmon_l_internal (s, maxsize, loc, format, ap, 0);
  538. va_end (ap);
  539. return res;
  540. }
  541. ldbl_strong_alias (___strfmon_l, __strfmon_l)
  542. ldbl_weak_alias (___strfmon_l, strfmon_l)