1 /* Formatting a monetary value according to the given locale.
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
24 #include "../libio/libioP.h"
25 #include "../libio/strfile.h"
30 #include "../locale/localeinfo.h"
31 #include <bits/floatn.h>
34 #define out_char(Ch) \
36 if (dest >= s + maxsize - 1) \
38 __set_errno (E2BIG); \
45 #define out_string(String) \
47 const char *_s = (String); \
52 #define out_nstring(String, N) \
55 const char *_s = (String); \
60 #define to_digit(Ch) ((Ch) - '0')
63 /* We use this code also for the extended locale handling where the
64 function gets as an additional argument the locale which has to be
65 used. To access the values we have to redefine the _NL_CURRENT
68 #define _NL_CURRENT(category, item) \
69 (current->values[_NL_ITEM_INDEX (item)].string)
72 /* We have to overcome some problems with this implementation. On the
73 one hand the strfmon() function is specified in XPG4 and of course
74 it has to follow this. But on the other hand POSIX.2 specifies
75 some information in the LC_MONETARY category which should be used,
76 too. Some of the information contradicts the information which can
77 be specified in format string. */
79 __vstrfmon_l_internal (char *s
, size_t maxsize
, locale_t loc
,
80 const char *format
, va_list ap
, unsigned int flags
)
82 struct __locale_data
*current
= loc
->__locales
[LC_MONETARY
];
84 struct printf_info info
;
85 char *dest
; /* Pointer so copy the output. */
86 const char *fmt
; /* Pointer that walks through format. */
91 /* Loop through the format-string. */
94 /* The floating-point value to output. */
99 #if __HAVE_DISTINCT_FLOAT128
105 int print_curr_symbol
;
120 int other_sep_by_space
;
122 int other_cs_precedes
;
123 const char *sign_string
;
124 const char *other_sign_string
;
126 const char *currency_symbol
;
127 size_t currency_symbol_len
;
133 /* Process all character which do not introduce a format
141 /* "%%" means a single '%' character. */
149 /* Defaults for formatting. */
150 int_format
= 0; /* Use international curr. symbol */
151 print_curr_symbol
= 1; /* Print the currency symbol. */
152 left_prec
= -1; /* No left precision specified. */
153 right_prec
= -1; /* No right precision specified. */
154 group
= 1; /* Print digits grouped. */
155 pad
= ' '; /* Fill character is <SP>. */
156 is_long_double
= 0; /* Double argument by default. */
157 is_binary128
= 0; /* Long double argument by default. */
158 p_sign_posn
= -2; /* This indicates whether the */
159 n_sign_posn
= -2; /* '(' flag is given. */
160 width
= -1; /* No width specified so far. */
161 left
= 0; /* Right justified by default. */
163 /* Parse group characters. */
168 case '=': /* Set fill character. */
173 __set_errno (EINVAL
);
177 case '^': /* Don't group digits. */
180 case '+': /* Use +/- for sign of number. */
181 if (n_sign_posn
!= -2)
183 __set_errno (EINVAL
);
186 p_sign_posn
= *_NL_CURRENT (LC_MONETARY
, P_SIGN_POSN
);
187 n_sign_posn
= *_NL_CURRENT (LC_MONETARY
, N_SIGN_POSN
);
189 case '(': /* Use ( ) for negative sign. */
190 if (n_sign_posn
!= -2)
192 __set_errno (EINVAL
);
198 case '!': /* Don't print the currency symbol. */
199 print_curr_symbol
= 0;
201 case '-': /* Print left justified. */
205 /* Will stop the loop. */;
212 /* Parse field width. */
213 width
= to_digit (*fmt
);
215 while (isdigit (*++fmt
))
217 int val
= to_digit (*fmt
);
219 if (width
> LONG_MAX
/ 10
220 || (width
== LONG_MAX
&& val
> LONG_MAX
% 10))
226 width
= width
* 10 + val
;
229 /* If we don't have enough room for the demanded width we
230 can stop now and return an error. */
231 if (width
>= maxsize
- (dest
- s
))
238 /* Recognize left precision. */
241 if (!isdigit (*++fmt
))
243 __set_errno (EINVAL
);
246 left_prec
= to_digit (*fmt
);
248 while (isdigit (*++fmt
))
251 left_prec
+= to_digit (*fmt
);
255 /* Recognize right precision. */
258 if (!isdigit (*++fmt
))
260 __set_errno (EINVAL
);
263 right_prec
= to_digit (*fmt
);
265 while (isdigit (*++fmt
))
268 right_prec
+= to_digit (*fmt
);
272 /* Handle modifier. This is an extension. */
276 if (__glibc_likely ((flags
& STRFMON_LDBL_IS_DBL
) == 0))
278 #if __HAVE_DISTINCT_FLOAT128
279 if (__glibc_likely ((flags
& STRFMON_LDBL_USES_FLOAT128
) != 0))
280 is_binary128
= is_long_double
;
284 /* Handle format specifier. */
288 case 'i': { /* Use international currency symbol. */
289 const char *int_curr_symbol
;
291 int_curr_symbol
= _NL_CURRENT (LC_MONETARY
, INT_CURR_SYMBOL
);
292 strncpy(int_symbol
, int_curr_symbol
, 3);
293 int_symbol
[3] = '\0';
295 currency_symbol_len
= 3;
296 currency_symbol
= &int_symbol
[0];
297 space_char
= int_curr_symbol
[3];
301 case 'n': /* Use national currency symbol. */
302 currency_symbol
= _NL_CURRENT (LC_MONETARY
, CURRENCY_SYMBOL
);
303 currency_symbol_len
= strlen (currency_symbol
);
307 default: /* Any unrecognized format is an error. */
308 __set_errno (EINVAL
);
312 /* If not specified by the format string now find the values for
313 the format specification. */
314 if (p_sign_posn
== -2)
315 p_sign_posn
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_P_SIGN_POSN
: P_SIGN_POSN
);
316 if (n_sign_posn
== -2)
317 n_sign_posn
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_N_SIGN_POSN
: N_SIGN_POSN
);
319 if (right_prec
== -1)
321 right_prec
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_FRAC_DIGITS
: FRAC_DIGITS
);
323 if (right_prec
== '\377')
327 /* If we have to print the digits grouped determine how many
328 extra characters this means. */
329 if (group
&& left_prec
!= -1)
330 left_prec
+= __guess_grouping (left_prec
,
331 _NL_CURRENT (LC_MONETARY
, MON_GROUPING
));
333 /* Now it's time to get the value. */
334 if (is_long_double
== 1)
336 #if __HAVE_DISTINCT_FLOAT128
337 if (is_binary128
== 1)
339 fpnum
.f128
= va_arg (ap
, _Float128
);
340 is_negative
= fpnum
.f128
< 0;
342 fpnum
.f128
= -fpnum
.f128
;
347 fpnum
.ldbl
= va_arg (ap
, long double);
348 is_negative
= fpnum
.ldbl
< 0;
350 fpnum
.ldbl
= -fpnum
.ldbl
;
355 fpnum
.dbl
= va_arg (ap
, double);
356 is_negative
= fpnum
.dbl
< 0;
358 fpnum
.dbl
= -fpnum
.dbl
;
361 /* We now know the sign of the value and can determine the format. */
364 sign_string
= _NL_CURRENT (LC_MONETARY
, NEGATIVE_SIGN
);
365 /* If the locale does not specify a character for the
366 negative sign we use a '-'. */
367 if (*sign_string
== '\0')
368 sign_string
= (const char *) "-";
369 cs_precedes
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_N_CS_PRECEDES
: N_CS_PRECEDES
);
370 sep_by_space
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_N_SEP_BY_SPACE
: N_SEP_BY_SPACE
);
371 sign_posn
= n_sign_posn
;
373 other_sign_string
= _NL_CURRENT (LC_MONETARY
, POSITIVE_SIGN
);
374 other_cs_precedes
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_P_CS_PRECEDES
: P_CS_PRECEDES
);
375 other_sep_by_space
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_P_SEP_BY_SPACE
: P_SEP_BY_SPACE
);
376 other_sign_posn
= p_sign_posn
;
380 sign_string
= _NL_CURRENT (LC_MONETARY
, POSITIVE_SIGN
);
381 cs_precedes
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_P_CS_PRECEDES
: P_CS_PRECEDES
);
382 sep_by_space
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_P_SEP_BY_SPACE
: P_SEP_BY_SPACE
);
383 sign_posn
= p_sign_posn
;
385 other_sign_string
= _NL_CURRENT (LC_MONETARY
, NEGATIVE_SIGN
);
386 if (*other_sign_string
== '\0')
387 other_sign_string
= (const char *) "-";
388 other_cs_precedes
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_N_CS_PRECEDES
: N_CS_PRECEDES
);
389 other_sep_by_space
= *_NL_CURRENT (LC_MONETARY
, int_format
? INT_N_SEP_BY_SPACE
: N_SEP_BY_SPACE
);
390 other_sign_posn
= n_sign_posn
;
393 /* Set default values for unspecified information. */
394 if (cs_precedes
!= 0)
396 if (other_cs_precedes
!= 0)
397 other_cs_precedes
= 1;
398 if (sep_by_space
== '\377')
400 if (other_sep_by_space
== '\377')
401 other_sep_by_space
= 0;
402 if (sign_posn
== '\377')
404 if (other_sign_posn
== '\377')
407 /* Check for degenerate cases */
408 if (sep_by_space
== 2)
411 || (sign_posn
== 1 && !cs_precedes
)
412 || (sign_posn
== 2 && cs_precedes
))
413 /* sign and symbol are not adjacent, so no separator */
416 if (other_sep_by_space
== 2)
418 if (other_sign_posn
== 0
419 || (other_sign_posn
== 1 && !other_cs_precedes
)
420 || (other_sign_posn
== 2 && other_cs_precedes
))
421 /* sign and symbol are not adjacent, so no separator */
422 other_sep_by_space
= 0;
425 /* Set the left precision and padding needed for alignment */
433 /* Set left_pad to number of spaces needed to align positive
434 and negative formats */
437 int other_left_bytes
= 0;
439 /* Work out number of bytes for currency string and separator
440 preceding the value */
443 left_bytes
+= currency_symbol_len
;
444 if (sep_by_space
!= 0)
448 if (other_cs_precedes
)
450 other_left_bytes
+= currency_symbol_len
;
451 if (other_sep_by_space
!= 0)
455 /* Work out number of bytes for the sign (or left parenthesis)
456 preceding the value */
457 if (sign_posn
== 0 && is_negative
)
459 else if (sign_posn
== 1)
460 left_bytes
+= strlen (sign_string
);
461 else if (cs_precedes
&& (sign_posn
== 3 || sign_posn
== 4))
462 left_bytes
+= strlen (sign_string
);
464 if (other_sign_posn
== 0 && !is_negative
)
466 else if (other_sign_posn
== 1)
467 other_left_bytes
+= strlen (other_sign_string
);
468 else if (other_cs_precedes
469 && (other_sign_posn
== 3 || other_sign_posn
== 4))
470 other_left_bytes
+= strlen (other_sign_string
);
472 /* Compare the number of bytes preceding the value for
473 each format, and set the padding accordingly */
474 if (other_left_bytes
> left_bytes
)
475 left_pad
= other_left_bytes
- left_bytes
;
480 /* Perhaps we'll someday make these things configurable so
481 better start using symbolic names now. */
482 #define left_paren '('
483 #define right_paren ')'
485 startp
= dest
; /* Remember start so we can compute length. */
487 while (left_pad
-- > 0)
490 if (sign_posn
== 0 && is_negative
)
491 out_char (left_paren
);
495 if (sign_posn
!= 0 && sign_posn
!= 2 && sign_posn
!= 4
498 out_string (sign_string
);
499 if (sep_by_space
== 2)
503 if (print_curr_symbol
)
504 out_string (currency_symbol
);
508 if (print_curr_symbol
&& sep_by_space
== 2)
509 out_char (space_char
);
510 out_string (sign_string
);
511 if (sep_by_space
== 1)
512 /* POSIX.2 and SUS are not clear on this case, but C99
513 says a space follows the adjacent-symbol-and-sign */
517 if (print_curr_symbol
&& sep_by_space
== 1)
518 out_char (space_char
);
521 if (sign_posn
!= 0 && sign_posn
!= 2 && sign_posn
!= 3
522 && sign_posn
!= 4 && sign_posn
!= 5)
523 out_string (sign_string
);
525 /* Print the number. */
527 f
._sbf
._f
._lock
= NULL
;
529 _IO_init_internal (&f
._sbf
._f
, 0);
530 _IO_JUMPS (&f
._sbf
) = &_IO_str_jumps
;
531 _IO_str_init_static_internal (&f
, dest
, (s
+ maxsize
) - dest
, dest
);
532 /* We clear the last available byte so we can find out whether
533 the numeric representation is too long. */
534 s
[maxsize
- 1] = '\0';
536 memset (&info
, '\0', sizeof (info
));
537 info
.prec
= right_prec
;
538 info
.width
= left_prec
+ (right_prec
? (right_prec
+ 1) : 0);
540 info
.is_long_double
= is_long_double
;
541 info
.is_binary128
= is_binary128
;
544 info
.extra
= 1; /* This means use values from LC_MONETARY. */
547 done
= __printf_fp_l (&f
._sbf
._f
, loc
, &info
, &ptr
);
551 if (s
[maxsize
- 1] != '\0')
563 if (sep_by_space
== 1)
565 out_string (sign_string
);
568 if (print_curr_symbol
)
570 if ((sign_posn
== 3 && sep_by_space
== 2)
571 || (sign_posn
== 4 && sep_by_space
== 1)
572 || (sign_posn
== 2 && sep_by_space
== 1)
573 || (sign_posn
== 1 && sep_by_space
== 1)
574 || (sign_posn
== 0 && sep_by_space
== 1))
575 out_char (space_char
);
576 out_nstring (currency_symbol
, currency_symbol_len
);
581 if (sep_by_space
== 2)
583 out_string (sign_string
);
589 if (sep_by_space
== 2)
591 out_string (sign_string
);
594 if (sign_posn
== 0 && is_negative
)
595 out_char (right_paren
);
597 /* Now test whether the output width is filled. */
598 if (dest
- startp
< width
)
601 /* We simply have to fill using spaces. */
604 while (dest
- startp
< width
);
607 long int dist
= width
- (dest
- startp
);
608 for (char *cp
= dest
- 1; cp
>= startp
; --cp
)
614 startp
[--dist
] = ' ';
620 /* Terminate the string. */
627 ___strfmon_l (char *s
, size_t maxsize
, locale_t loc
, const char *format
, ...)
631 va_start (ap
, format
);
633 ssize_t res
= __vstrfmon_l_internal (s
, maxsize
, loc
, format
, ap
, 0);
639 ldbl_strong_alias (___strfmon_l
, __strfmon_l
)
640 ldbl_weak_alias (___strfmon_l
, strfmon_l
)