2 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
5 * Copyright (c) 2011 The FreeBSD Foundation
7 * Portions of this software were developed by David Chisnall
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: head/lib/libc/stdlib/strfmon.c 227753 2011-11-20 14:45:42Z theraven $
34 #include <sys/types.h>
44 #include "xlocale_private.h"
47 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
48 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
49 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
50 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
51 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
52 #define LEFT_JUSTIFY 0x20 /* left justify */
53 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
54 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
57 #define PRINT(CH) do { \
58 if (dst >= s + maxsize) \
63 #define PRINTS(STR) do { \
65 while (*tmps != '\0') \
69 #define GET_NUMBER(VAR) do { \
71 while (isdigit((unsigned char)*fmt)) { \
72 if (VAR > INT_MAX / 10) \
82 #define GRPCPY(howmany) do { \
86 *--bufend = *(avalue+avalue_size+padded); \
91 *--bufend = thousands_sep; \
95 static void __setup_vars(int, char *, char *, char *, char **);
96 static int __calc_left_pad(int, char *);
97 static char *__format_grouped_double(double, int *, int, int, int);
100 vstrfmon_l(char * __restrict s
, size_t maxsize
, locale_t loc
,
101 const char * __restrict format
, va_list ap
)
103 char *dst
; /* output destination pointer */
104 const char *fmt
; /* current format poistion pointer */
105 struct lconv
*lc
; /* pointer to lconv structure */
106 char *asciivalue
; /* formatted double pointer */
108 int flags
; /* formatting options */
109 int pad_char
; /* padding character */
110 int pad_size
; /* pad size */
111 int width
; /* field width */
112 int left_prec
; /* left precision */
113 int right_prec
; /* right precision */
114 double value
; /* just value */
115 char space_char
= ' '; /* space after currency */
117 char cs_precedes
, /* values gathered from struct lconv */
123 char *tmpptr
; /* temporary vars */
128 lc
= localeconv_l(loc
);
132 currency_symbol
= NULL
;
136 /* pass nonformating characters AS IS */
142 /* "%%" mean just '%' */
143 if (*(fmt
+1) == '%') {
150 /* set up initial values */
151 flags
= (NEED_GROUPING
|LOCALE_POSN
);
152 pad_char
= ' '; /* padding character is "space" */
153 left_prec
= -1; /* no left precision specified */
154 right_prec
= -1; /* no right precision specified */
155 width
= -1; /* no width specified */
156 value
= 0; /* we have no value to print now */
161 case '=': /* fill character */
163 if (pad_char
== '\0')
166 case '^': /* not group currency */
167 flags
&= ~(NEED_GROUPING
);
169 case '+': /* use locale defined signs */
170 if (flags
& SIGN_POSN_USED
)
172 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
174 case '(': /* enclose negatives with () */
175 if (flags
& SIGN_POSN_USED
)
177 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
179 case '!': /* suppress currency symbol */
180 flags
|= SUPRESS_CURR_SYMBOL
;
182 case '-': /* alignment (left) */
183 flags
|= LEFT_JUSTIFY
;
192 if (isdigit((unsigned char)*fmt
)) {
194 /* Do we have enough space to put number with
197 if ((unsigned int)width
>= maxsize
- (dst
- s
))
203 if (!isdigit((unsigned char)*++fmt
))
205 GET_NUMBER(left_prec
);
206 if ((unsigned int)left_prec
>= maxsize
- (dst
- s
))
210 /* Right precision */
212 if (!isdigit((unsigned char)*++fmt
))
214 GET_NUMBER(right_prec
);
215 if ((unsigned int)right_prec
>= maxsize
- (dst
- s
) -
220 /* Conversion Characters */
222 case 'i': /* use internaltion currency format */
223 flags
|= USE_INTL_CURRENCY
;
225 case 'n': /* use national currency format */
226 flags
&= ~(USE_INTL_CURRENCY
);
228 default: /* required character is missing or
233 if (currency_symbol
!= NULL
)
234 free(currency_symbol
);
235 if (flags
& USE_INTL_CURRENCY
) {
236 currency_symbol
= strdup(lc
->int_curr_symbol
);
237 if (currency_symbol
!= NULL
)
238 space_char
= *(currency_symbol
+3);
240 currency_symbol
= strdup(lc
->currency_symbol
);
242 if (currency_symbol
== NULL
)
243 goto end_error
; /* ENOMEM. */
246 value
= va_arg(ap
, double);
250 flags
|= IS_NEGATIVE
;
254 /* fill left_prec with amount of padding chars */
255 if (left_prec
>= 0) {
256 pad_size
= __calc_left_pad((flags
^ IS_NEGATIVE
),
258 __calc_left_pad(flags
, currency_symbol
);
263 if (asciivalue
!= NULL
)
265 asciivalue
= __format_grouped_double(value
, &flags
,
266 left_prec
, right_prec
, pad_char
);
267 if (asciivalue
== NULL
)
268 goto end_error
; /* errno already set */
269 /* to ENOMEM by malloc() */
271 /* set some variables for later use */
272 __setup_vars(flags
, &cs_precedes
, &sep_by_space
,
273 &sign_posn
, &signstr
);
276 * Description of some LC_MONETARY's values:
278 * p_cs_precedes & n_cs_precedes
280 * = 1 - $currency_symbol precedes the value
281 * for a monetary quantity with a non-negative value
282 * = 0 - symbol succeeds the value
284 * p_sep_by_space & n_sep_by_space
286 * = 0 - no space separates $currency_symbol
287 * from the value for a monetary quantity with a
289 * = 1 - space separates the symbol from the value
290 * = 2 - space separates the symbol and the sign string,
293 * p_sign_posn & n_sign_posn
295 * = 0 - parentheses enclose the quantity and the
297 * = 1 - the sign string precedes the quantity and the
299 * = 2 - the sign string succeeds the quantity and the
301 * = 3 - the sign string precedes the $currency_symbol
302 * = 4 - the sign string succeeds the $currency_symbol
308 while (pad_size
-- > 0)
311 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
314 if (cs_precedes
== 1) {
315 if (sign_posn
== 1 || sign_posn
== 3) {
317 if (sep_by_space
== 2) /* XXX: ? */
321 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
322 PRINTS(currency_symbol
);
324 if (sign_posn
== 4) {
325 if (sep_by_space
== 2)
328 if (sep_by_space
== 1)
330 } else if (sep_by_space
== 1)
333 } else if (sign_posn
== 1)
338 if (cs_precedes
== 0) {
339 if (sign_posn
== 3) {
340 if (sep_by_space
== 1)
345 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
346 if ((sign_posn
== 3 && sep_by_space
== 2)
347 || (sep_by_space
== 1
353 PRINTS(currency_symbol
); /* XXX: len */
354 if (sign_posn
== 4) {
355 if (sep_by_space
== 2)
362 if (sign_posn
== 2) {
363 if (sep_by_space
== 2)
368 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
371 if (dst
- tmpptr
< width
) {
372 if (flags
& LEFT_JUSTIFY
) {
373 while (dst
- tmpptr
< width
)
376 pad_size
= dst
-tmpptr
;
377 memmove(tmpptr
+ width
-pad_size
, tmpptr
,
379 memset(tmpptr
, ' ', width
-pad_size
);
380 dst
+= width
-pad_size
;
387 free(currency_symbol
);
388 return (dst
- s
- 1); /* return size of put data except trailing '\0' */
399 if (asciivalue
!= NULL
)
401 if (currency_symbol
!= NULL
)
402 free(currency_symbol
);
407 strfmon_l(char * __restrict s
, size_t maxsize
, locale_t loc
, const char * __restrict format
,
412 va_start(ap
, format
);
413 ret
= vstrfmon_l(s
, maxsize
, loc
, format
, ap
);
419 strfmon(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
424 va_start(ap
, format
);
425 ret
= vstrfmon_l(s
, maxsize
, __get_locale(), format
, ap
);
432 __setup_vars(int flags
, char *cs_precedes
, char *sep_by_space
,
433 char *sign_posn
, char **signstr
) {
435 struct lconv
*lc
= localeconv();
437 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
438 *cs_precedes
= lc
->int_n_cs_precedes
;
439 *sep_by_space
= lc
->int_n_sep_by_space
;
440 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_n_sign_posn
;
441 *signstr
= (lc
->negative_sign
[0] == '\0') ? "-"
443 } else if (flags
& USE_INTL_CURRENCY
) {
444 *cs_precedes
= lc
->int_p_cs_precedes
;
445 *sep_by_space
= lc
->int_p_sep_by_space
;
446 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_p_sign_posn
;
447 *signstr
= lc
->positive_sign
;
448 } else if (flags
& IS_NEGATIVE
) {
449 *cs_precedes
= lc
->n_cs_precedes
;
450 *sep_by_space
= lc
->n_sep_by_space
;
451 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->n_sign_posn
;
452 *signstr
= (lc
->negative_sign
[0] == '\0') ? "-"
455 *cs_precedes
= lc
->p_cs_precedes
;
456 *sep_by_space
= lc
->p_sep_by_space
;
457 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->p_sign_posn
;
458 *signstr
= lc
->positive_sign
;
461 /* Set defult values for unspecified information. */
462 if (*cs_precedes
!= 0)
464 if (*sep_by_space
== CHAR_MAX
)
466 if (*sign_posn
== CHAR_MAX
)
471 __calc_left_pad(int flags
, char *cur_symb
) {
473 char cs_precedes
, sep_by_space
, sign_posn
, *signstr
;
476 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
);
478 if (cs_precedes
!= 0) {
479 left_chars
+= strlen(cur_symb
);
480 if (sep_by_space
!= 0)
486 left_chars
+= strlen(signstr
);
490 if (cs_precedes
!= 0)
491 left_chars
+= strlen(signstr
);
497 get_groups(int size
, char *grouping
) {
501 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
504 while (size
> (int)*grouping
) {
506 size
-= (int)*grouping
++;
507 /* no more grouping ? */
508 if (*grouping
== CHAR_MAX
)
510 /* rest grouping with same value ? */
511 if (*grouping
== 0) {
512 chars
+= (size
- 1) / *(grouping
- 1);
519 /* convert double to ASCII */
521 __format_grouped_double(double value
, int *flags
,
522 int left_prec
, int right_prec
, int pad_char
) {
534 struct lconv
*lc
= localeconv();
541 grouping
= lc
->mon_grouping
;
542 decimal_point
= *lc
->mon_decimal_point
;
543 if (decimal_point
== '\0')
544 decimal_point
= *lc
->decimal_point
;
545 thousands_sep
= *lc
->mon_thousands_sep
;
546 if (thousands_sep
== '\0')
547 thousands_sep
= *lc
->thousands_sep
;
549 /* fill left_prec with default value */
553 /* fill right_prec with default value */
554 if (right_prec
== -1) {
555 if (*flags
& USE_INTL_CURRENCY
)
556 right_prec
= lc
->int_frac_digits
;
558 right_prec
= lc
->frac_digits
;
560 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
564 if (*flags
& NEED_GROUPING
)
565 left_prec
+= get_groups(left_prec
, grouping
);
567 /* convert to string */
568 snprintf(fmt
, sizeof(fmt
), "%%%d.%df", left_prec
+ right_prec
+ 1,
570 avalue_size
= asprintf(&avalue
, fmt
, value
);
574 /* make sure that we've enough space for result string */
575 bufsize
= strlen(avalue
)*2+1;
576 rslt
= calloc(1, bufsize
);
581 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
583 /* skip spaces at beggining */
585 while (avalue
[padded
] == ' ') {
590 if (right_prec
> 0) {
591 bufend
-= right_prec
;
592 memcpy(bufend
, avalue
+ avalue_size
+padded
-right_prec
,
594 *--bufend
= decimal_point
;
595 avalue_size
-= (right_prec
+ 1);
598 if ((*flags
& NEED_GROUPING
) &&
599 thousands_sep
!= '\0' && /* XXX: need investigation */
600 *grouping
!= CHAR_MAX
&&
602 while (avalue_size
> (int)*grouping
) {
607 /* no more grouping ? */
608 if (*grouping
== CHAR_MAX
)
611 /* rest grouping with same value ? */
612 if (*grouping
== 0) {
614 while (avalue_size
> *grouping
) {
620 if (avalue_size
!= 0)
625 bufend
-= avalue_size
;
626 memcpy(bufend
, avalue
+padded
, avalue_size
);
628 padded
--; /* decrease assumed $decimal_point */
631 /* do padding with pad_char */
634 memset(bufend
, pad_char
, padded
);
637 bufsize
= bufsize
- (bufend
- rslt
) + 1;
638 memmove(rslt
, bufend
, bufsize
);