2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
45 #include "localeimpl.h"
46 #include "lmonetary.h"
50 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
51 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
52 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
53 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
54 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
55 #define LEFT_JUSTIFY 0x20 /* left justify */
56 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
57 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
61 if (dst >= s + maxsize) \
66 #define PRINTS(STR) { \
67 const char *tmps = STR; \
68 while (*tmps != '\0') \
72 #define GET_NUMBER(VAR) { \
74 while (isdigit((unsigned char)*fmt)) { \
75 if (VAR > INT_MAX / 10) \
85 #define GRPCPY(howmany) { \
89 *--bufend = *(avalue+avalue_size+padded); \
94 bufend -= thousands_len; \
95 (void) memcpy(bufend, thousands_sep, thousands_len); \
99 static void setup_vars(const struct lc_monetary
*, int, char *, char *, char *,
101 static int calc_left_pad(const struct lc_monetary
*, int, const char *);
102 static char *format_grouped_double(const struct lc_monetary
*,
103 const struct lc_numeric
*, double, int *, int, int, int);
106 strfmon_impl(char *_RESTRICT_KYWD s
, size_t maxsize
, locale_t loc
,
107 const char *_RESTRICT_KYWD format
, va_list ap
)
109 char *dst
; /* output destination pointer */
110 const char *fmt
; /* current format poistion pointer */
111 char *asciivalue
; /* formatted double pointer */
113 int flags
; /* formatting options */
114 int pad_char
; /* padding character */
115 int pad_size
; /* pad size */
116 int width
; /* field width */
117 int left_prec
; /* left precision */
118 int right_prec
; /* right precision */
119 double value
; /* just value */
120 char space_char
= ' '; /* space after currency */
122 char cs_precedes
; /* values from struct lc_monetary */
126 const char *currency_symbol
;
128 char *tmpptr
; /* temporary vars */
130 const struct lc_monetary
*lmon
; /* monetary structure */
131 const struct lc_numeric
*lnum
; /* numeric structure */
133 lmon
= loc
->monetary
;
139 currency_symbol
= NULL
;
143 /* pass nonformating characters AS IS */
149 /* "%%" mean just '%' */
150 if (*(fmt
+1) == '%') {
157 /* set up initial values */
158 flags
= (NEED_GROUPING
|LOCALE_POSN
);
159 pad_char
= ' '; /* padding character is "space" */
160 left_prec
= -1; /* no left precision specified */
161 right_prec
= -1; /* no right precision specified */
162 width
= -1; /* no width specified */
163 value
= 0; /* we have no value to print now */
168 case '=': /* fill character */
170 if (pad_char
== '\0')
173 case '^': /* not group currency */
174 flags
&= ~(NEED_GROUPING
);
176 case '+': /* use locale defined signs */
177 if (flags
& SIGN_POSN_USED
)
179 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
181 case '(': /* enclose negatives with () */
182 if (flags
& SIGN_POSN_USED
)
184 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
186 case '!': /* suppress currency symbol */
187 flags
|= SUPRESS_CURR_SYMBOL
;
189 case '-': /* alignment (left) */
190 flags
|= LEFT_JUSTIFY
;
199 if (isdigit((unsigned char)*fmt
)) {
202 * Do we have enough space to put number with
205 if ((unsigned int)width
>= maxsize
- (dst
- s
))
211 if (!isdigit((unsigned char)*++fmt
))
213 GET_NUMBER(left_prec
);
214 if ((unsigned int)left_prec
>= maxsize
- (dst
- s
))
218 /* Right precision */
220 if (!isdigit((unsigned char)*++fmt
))
222 GET_NUMBER(right_prec
);
223 if ((unsigned int)right_prec
>= maxsize
- (dst
- s
) -
228 /* Conversion Characters */
230 case 'i': /* use internaltion currency format */
231 flags
|= USE_INTL_CURRENCY
;
233 case 'n': /* use national currency format */
234 flags
&= ~(USE_INTL_CURRENCY
);
237 /* required char missing or premature EOS */
241 if (flags
& USE_INTL_CURRENCY
) {
242 currency_symbol
= lmon
->int_curr_symbol
;
243 /* by definition three letters followed by a space */
244 if (currency_symbol
!= NULL
)
245 space_char
= currency_symbol
[3];
247 currency_symbol
= lmon
->currency_symbol
;
250 value
= va_arg(ap
, double);
254 flags
|= IS_NEGATIVE
;
258 /* fill left_prec with amount of padding chars */
259 if (left_prec
>= 0) {
260 pad_size
= calc_left_pad(lmon
, (flags
^ IS_NEGATIVE
),
262 calc_left_pad(lmon
, flags
, currency_symbol
);
268 asciivalue
= format_grouped_double(lmon
, lnum
, value
, &flags
,
269 left_prec
, right_prec
, pad_char
);
270 if (asciivalue
== NULL
)
271 goto end_error
; /* errno already set */
272 /* to ENOMEM by malloc() */
274 /* set some variables for later use */
275 setup_vars(lmon
, flags
, &cs_precedes
, &sep_by_space
,
276 &sign_posn
, &signstr
);
279 * Description of some LC_MONETARY's values:
281 * p_cs_precedes & n_cs_precedes
283 * = 1 - $currency_symbol precedes the value
284 * for a monetary quantity with a non-negative value
285 * = 0 - symbol succeeds the value
287 * p_sep_by_space & n_sep_by_space
289 * = 0 - no space separates $currency_symbol
290 * from the value for a monetary quantity with a
292 * = 1 - space separates the symbol from the value
293 * = 2 - space separates the symbol and the sign string,
296 * p_sign_posn & n_sign_posn
298 * = 0 - parentheses enclose the quantity and the
300 * = 1 - the sign string precedes the quantity and the
302 * = 2 - the sign string succeeds the quantity and the
304 * = 3 - the sign string precedes the $currency_symbol
305 * = 4 - the sign string succeeds the $currency_symbol
311 while (pad_size
-- > 0)
314 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
317 if (cs_precedes
== 1) {
318 if (sign_posn
== 1 || sign_posn
== 3) {
320 if (sep_by_space
== 2)
324 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
325 PRINTS(currency_symbol
);
327 if (sign_posn
== 4) {
328 if (sep_by_space
== 2)
331 if (sep_by_space
== 1)
333 } else if (sep_by_space
== 1)
336 } else if (sign_posn
== 1)
341 if (cs_precedes
== 0) {
342 if (sign_posn
== 3) {
343 if (sep_by_space
== 1)
348 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
349 if ((sign_posn
== 3 && sep_by_space
== 2) ||
350 (sep_by_space
== 1 && (sign_posn
== 0 ||
351 sign_posn
== 1 || sign_posn
== 2 ||
354 PRINTS(currency_symbol
); /* XXX: len */
355 if (sign_posn
== 4) {
356 if (sep_by_space
== 2)
363 if (sign_posn
== 2) {
364 if (sep_by_space
== 2)
369 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
372 if (dst
- tmpptr
< width
) {
373 if (flags
& LEFT_JUSTIFY
) {
374 while (dst
- tmpptr
< width
)
377 pad_size
= dst
-tmpptr
;
378 (void) memmove(tmpptr
+ width
-pad_size
, tmpptr
,
380 (void) memset(tmpptr
, ' ', width
-pad_size
);
381 dst
+= width
-pad_size
;
388 return (dst
- s
- 1); /* size of put data except trailing '\0' */
405 strfmon(char *_RESTRICT_KYWD s
, size_t maxsize
,
406 const char *_RESTRICT_KYWD format
, ...)
411 va_start(ap
, format
);
412 ret
= strfmon_impl(s
, maxsize
, uselocale(NULL
), format
, ap
);
418 strfmon_l(char *_RESTRICT_KYWD s
, size_t maxsize
, locale_t loc
,
419 const char *_RESTRICT_KYWD format
, ...)
423 va_start(ap
, format
);
424 ret
= strfmon_impl(s
, maxsize
, loc
, format
, ap
);
430 setup_vars(const struct lc_monetary
*lmon
, int flags
, char *cs_precedes
,
431 char *sep_by_space
, char *sign_posn
, const char **signstr
)
433 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
434 *cs_precedes
= lmon
->int_n_cs_precedes
[0];
435 *sep_by_space
= lmon
->int_n_sep_by_space
[0];
436 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 :
437 lmon
->int_n_sign_posn
[0];
438 *signstr
= (lmon
->negative_sign
[0] == '\0') ? "-" :
440 } else if (flags
& USE_INTL_CURRENCY
) {
441 *cs_precedes
= lmon
->int_p_cs_precedes
[0];
442 *sep_by_space
= lmon
->int_p_sep_by_space
[0];
443 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 :
444 lmon
->int_p_sign_posn
[0];
445 *signstr
= lmon
->positive_sign
;
446 } else if (flags
& IS_NEGATIVE
) {
447 *cs_precedes
= lmon
->n_cs_precedes
[0];
448 *sep_by_space
= lmon
->n_sep_by_space
[0];
449 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lmon
->n_sign_posn
[0];
450 *signstr
= (lmon
->negative_sign
[0] == '\0') ? "-" :
453 *cs_precedes
= lmon
->p_cs_precedes
[0];
454 *sep_by_space
= lmon
->p_sep_by_space
[0];
455 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lmon
->p_sign_posn
[0];
456 *signstr
= lmon
->positive_sign
;
459 /* Set default values for unspecified information. */
460 if (*cs_precedes
!= 0)
462 if (*sep_by_space
== CHAR_MAX
)
464 if (*sign_posn
== CHAR_MAX
)
469 calc_left_pad(const struct lc_monetary
*lmon
, int flags
, const char *cur_symb
)
471 char cs_precedes
, sep_by_space
, sign_posn
;
475 setup_vars(lmon
, flags
, &cs_precedes
, &sep_by_space
, &sign_posn
,
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
, const char *grouping
)
502 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
505 while (size
> (int)*grouping
) {
507 size
-= (int)*grouping
++;
508 /* no more grouping ? */
509 if (*grouping
== CHAR_MAX
)
511 /* rest grouping with same value ? */
512 if (*grouping
== 0) {
513 chars
+= (size
- 1) / *(grouping
- 1);
520 /* convert double to ASCII */
522 format_grouped_double(const struct lc_monetary
*lmon
,
523 const struct lc_numeric
*lnum
,
524 double value
, int *flags
, int left_prec
, int right_prec
, int pad_char
)
537 const char *grouping
;
538 const char *decimal_point
;
539 const char *thousands_sep
;
545 grouping
= lmon
->mon_grouping
;
546 decimal_point
= lmon
->mon_decimal_point
;
547 if (*decimal_point
== '\0')
548 decimal_point
= lnum
->decimal_point
;
549 thousands_sep
= lmon
->mon_thousands_sep
;
550 if (*thousands_sep
== '\0')
551 thousands_sep
= lnum
->thousands_sep
;
553 decimal_len
= strlen(decimal_point
); /* usually 1 */
554 thousands_len
= strlen(thousands_sep
); /* 0 or 1 usually */
556 /* fill left_prec with default value */
560 /* fill right_prec with default value */
561 if (right_prec
== -1) {
562 if (*flags
& USE_INTL_CURRENCY
)
563 right_prec
= lmon
->int_frac_digits
[0];
565 right_prec
= lmon
->frac_digits
[0];
567 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
571 if (*flags
& NEED_GROUPING
)
572 left_prec
+= get_groups(left_prec
, grouping
);
574 /* convert to string */
575 (void) snprintf(fmt
, sizeof (fmt
), "%%%d.%df",
576 left_prec
+ right_prec
+ 1, right_prec
);
577 avalue_size
= asprintf(&avalue
, fmt
, value
);
582 * Make sure that we've enough space for result string.
583 * This assumes that digits take up at least much space as
584 * grouping and radix characters. The worst case currently known
585 * is for Arabic, where two-byte UTF-8 sequences are used for both
586 * decimal and thousands seperators, and groups can be a small as two
587 * decimal digits. This will do no worse than doubling the storage
590 bufsize
= strlen(avalue
)*2+1;
591 rslt
= calloc(1, bufsize
);
596 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
598 /* skip spaces at beginning */
600 while (avalue
[padded
] == ' ') {
605 if (right_prec
> 0) {
606 bufend
-= right_prec
;
607 (void) memcpy(bufend
, avalue
+ avalue_size
+padded
-right_prec
,
609 bufend
-= decimal_len
;
610 (void) memcpy(bufend
, decimal_point
, decimal_len
);
611 avalue_size
-= (right_prec
+ decimal_len
);
614 if ((*flags
& NEED_GROUPING
) &&
615 thousands_len
!= 0 &&
616 *grouping
!= CHAR_MAX
&&
618 while (avalue_size
> (int)*grouping
) {
623 /* no more grouping ? */
624 if (*grouping
== CHAR_MAX
)
627 /* rest grouping with same value ? */
628 if (*grouping
== 0) {
630 while (avalue_size
> *grouping
) {
636 if (avalue_size
!= 0)
641 bufend
-= avalue_size
;
642 (void) memcpy(bufend
, avalue
+padded
, avalue_size
);
644 padded
--; /* decrease assumed $decimal_point */
647 /* do padding with pad_char */
650 (void) memset(bufend
, pad_char
, padded
);
653 bufsize
= bufsize
- (bufend
- rslt
) + 1;
654 (void) memmove(rslt
, bufend
, bufsize
);