2 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
39 #include <errno_private.h>
43 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
44 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
45 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
46 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
47 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
48 #define LEFT_JUSTIFY 0x20 /* left justify */
49 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
50 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
53 #define PRINT(CH) do { \
54 if (dst >= s + maxsize) \
59 #define PRINTS(STR) do { \
61 while (*tmps != '\0') \
65 #define GET_NUMBER(VAR) do { \
67 while (isdigit((unsigned char)*fmt)) { \
68 if (VAR > INT_MAX / 10) \
78 #define GRPCPY(howmany) do { \
82 *--bufend = *(avalue+avalue_size+padded); \
87 *--bufend = thousands_sep; \
92 static void __setup_vars(int, char *, char *, char *, char **);
93 static int __calc_left_pad(int, char *);
94 static char *__format_grouped_double(double, int *, int, int, int);
98 strfmon(char *s
, size_t maxsize
, const char *format
,
102 char *dst
; /* output destination pointer */
103 const char *fmt
; /* current format poistion pointer */
104 struct lconv
*lc
; /* pointer to lconv structure */
105 char *asciivalue
; /* formatted double pointer */
107 int flags
; /* formatting options */
108 int pad_char
; /* padding character */
109 int pad_size
; /* pad size */
110 int width
; /* field width */
111 int left_prec
; /* left precision */
112 int right_prec
; /* right precision */
113 double value
; /* just value */
114 char space_char
= ' '; /* space after currency */
116 char cs_precedes
, /* values gathered from struct lconv */
122 char *tmpptr
; /* temporary vars */
125 va_start(ap
, format
);
131 currency_symbol
= NULL
;
135 /* pass nonformating characters AS IS */
141 /* "%%" mean just '%' */
142 if (*(fmt
+1) == '%') {
149 /* set up initial values */
150 flags
= (NEED_GROUPING
|LOCALE_POSN
);
151 pad_char
= ' '; /* padding character is "space" */
152 left_prec
= -1; /* no left precision specified */
153 right_prec
= -1; /* no right precision specified */
154 width
= -1; /* no width specified */
155 value
= 0; /* we have no value to print now */
160 case '=': /* fill character */
162 if (pad_char
== '\0')
165 case '^': /* not group currency */
166 flags
&= ~(NEED_GROUPING
);
168 case '+': /* use locale defined signs */
169 if (flags
& SIGN_POSN_USED
)
171 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
173 case '(': /* enclose negatives with () */
174 if (flags
& SIGN_POSN_USED
)
176 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
178 case '!': /* suppress currency symbol */
179 flags
|= SUPRESS_CURR_SYMBOL
;
181 case '-': /* alignment (left) */
182 flags
|= LEFT_JUSTIFY
;
191 if (isdigit((unsigned char)*fmt
)) {
193 /* Do we have enough space to put number with
196 if ((unsigned int)width
>= maxsize
- (dst
- s
))
202 if (!isdigit((unsigned char)*++fmt
))
204 GET_NUMBER(left_prec
);
205 if ((unsigned int)left_prec
>= maxsize
- (dst
- s
))
209 /* Right precision */
211 if (!isdigit((unsigned char)*++fmt
))
213 GET_NUMBER(right_prec
);
214 if ((unsigned int)right_prec
>= maxsize
- (dst
- s
) - left_prec
)
218 /* Conversion Characters */
220 case 'i': /* use internaltion currency format */
221 flags
|= USE_INTL_CURRENCY
;
223 case 'n': /* use national currency format */
224 flags
&= ~(USE_INTL_CURRENCY
);
226 default: /* required character is missing or
231 if (currency_symbol
!= NULL
)
232 free(currency_symbol
);
233 if (flags
& USE_INTL_CURRENCY
) {
234 currency_symbol
= strdup(lc
->int_curr_symbol
);
235 if (currency_symbol
!= NULL
) {
236 space_char
= currency_symbol
[3];
237 currency_symbol
[3] = '\0';
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
),
257 currency_symbol
) - __calc_left_pad(flags
, currency_symbol
);
262 if (asciivalue
!= NULL
)
264 asciivalue
= __format_grouped_double(value
, &flags
, left_prec
,
265 right_prec
, pad_char
);
266 if (asciivalue
== NULL
)
267 goto end_error
; /* errno already set */
268 /* to ENOMEM by malloc() */
270 /* set some variables for later use */
271 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
);
274 * Description of some LC_MONETARY's values:
276 * p_cs_precedes & n_cs_precedes
278 * = 1 - $currency_symbol precedes the value
279 * for a monetary quantity with a non-negative value
280 * = 0 - symbol succeeds the value
282 * p_sep_by_space & n_sep_by_space
284 * = 0 - no space separates $currency_symbol
285 * from the value for a monetary quantity with a
287 * = 1 - space separates the symbol from the value
288 * = 2 - space separates the symbol and the sign string,
291 * p_sign_posn & n_sign_posn
293 * = 0 - parentheses enclose the quantity and the
295 * = 1 - the sign string precedes the quantity and the
297 * = 2 - the sign string succeeds the quantity and the
299 * = 3 - the sign string precedes the $currency_symbol
300 * = 4 - the sign string succeeds the $currency_symbol
306 while (pad_size
-- > 0)
309 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
312 if (cs_precedes
== 1) {
313 if (sign_posn
== 1 || sign_posn
== 3) {
315 if (sep_by_space
== 2) /* XXX: ? */
319 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
320 PRINTS(currency_symbol
);
322 if (sign_posn
== 4) {
323 if (sep_by_space
== 2)
326 if (sep_by_space
== 1)
328 } else if (sep_by_space
== 1)
331 } else if (sign_posn
== 1)
336 if (cs_precedes
== 0) {
337 if (sign_posn
== 3) {
338 if (sep_by_space
== 1)
343 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
344 if ((sign_posn
== 3 && sep_by_space
== 2)
345 || (sep_by_space
== 1
351 PRINTS(currency_symbol
); /* XXX: len */
352 if (sign_posn
== 4) {
353 if (sep_by_space
== 2)
360 if (sign_posn
== 2) {
361 if (sep_by_space
== 2)
366 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
369 if (dst
- tmpptr
< width
) {
370 if (flags
& LEFT_JUSTIFY
) {
371 while (dst
- tmpptr
< width
)
374 pad_size
= dst
- tmpptr
;
375 memmove(tmpptr
+ width
- pad_size
, tmpptr
, pad_size
);
376 memset(tmpptr
, ' ', width
- pad_size
);
377 dst
+= width
- pad_size
;
385 free(currency_symbol
);
386 return dst
- s
- 1; /* return size of put data except trailing '\0' */
397 if (asciivalue
!= NULL
)
399 if (currency_symbol
!= NULL
)
400 free(currency_symbol
);
401 __set_errno(sverrno
);
408 __setup_vars(int flags
, char *cs_precedes
, char *sep_by_space
, char *sign_posn
,
411 struct lconv
*lc
= localeconv();
413 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
414 *cs_precedes
= lc
->int_n_cs_precedes
;
415 *sep_by_space
= lc
->int_n_sep_by_space
;
416 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_n_sign_posn
;
417 *signstr
= (lc
->negative_sign
== '\0') ? "-" : lc
->negative_sign
;
418 } else if (flags
& USE_INTL_CURRENCY
) {
419 *cs_precedes
= lc
->int_p_cs_precedes
;
420 *sep_by_space
= lc
->int_p_sep_by_space
;
421 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_p_sign_posn
;
422 *signstr
= lc
->positive_sign
;
423 } else if (flags
& IS_NEGATIVE
) {
424 *cs_precedes
= lc
->n_cs_precedes
;
425 *sep_by_space
= lc
->n_sep_by_space
;
426 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->n_sign_posn
;
427 *signstr
= (lc
->negative_sign
== '\0') ? "-" : lc
->negative_sign
;
429 *cs_precedes
= lc
->p_cs_precedes
;
430 *sep_by_space
= lc
->p_sep_by_space
;
431 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->p_sign_posn
;
432 *signstr
= lc
->positive_sign
;
435 /* Set defult values for unspecified information. */
436 if (*cs_precedes
!= 0)
438 if (*sep_by_space
== CHAR_MAX
)
440 if (*sign_posn
== CHAR_MAX
)
446 __calc_left_pad(int flags
, char *cur_symb
)
448 char cs_precedes
, sep_by_space
, sign_posn
, *signstr
;
451 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
);
453 if (cs_precedes
!= 0) {
454 left_chars
+= strlen(cur_symb
);
455 if (sep_by_space
!= 0)
461 left_chars
+= strlen(signstr
);
465 if (cs_precedes
!= 0)
466 left_chars
+= strlen(signstr
);
473 get_groups(int size
, char *grouping
)
477 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
480 while (size
> (int)*grouping
) {
482 size
-= (int)*grouping
++;
483 /* no more grouping ? */
484 if (*grouping
== CHAR_MAX
)
486 /* rest grouping with same value ? */
487 if (*grouping
== 0) {
488 chars
+= (size
- 1) / *(grouping
- 1);
496 /* convert double to ASCII */
498 __format_grouped_double(double value
, int *flags
, int left_prec
, int right_prec
,
511 struct lconv
*lc
= localeconv();
518 grouping
= lc
->mon_grouping
;
519 decimal_point
= *lc
->mon_decimal_point
;
520 if (decimal_point
== '\0')
521 decimal_point
= *lc
->decimal_point
;
522 thousands_sep
= *lc
->mon_thousands_sep
;
523 if (thousands_sep
== '\0')
524 thousands_sep
= *lc
->thousands_sep
;
526 /* fill left_prec with default value */
530 /* fill right_prec with default value */
531 if (right_prec
== -1) {
532 if (*flags
& USE_INTL_CURRENCY
)
533 right_prec
= lc
->int_frac_digits
;
535 right_prec
= lc
->frac_digits
;
537 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
541 if (*flags
& NEED_GROUPING
)
542 left_prec
+= get_groups(left_prec
, grouping
);
544 /* convert to string */
545 snprintf(fmt
, sizeof(fmt
), "%%%d.%df", left_prec
+ right_prec
+ 1,
547 avalue_size
= asprintf(&avalue
, fmt
, value
);
551 /* make sure that we've enough space for result string */
552 bufsize
= strlen(avalue
) * 2 + 1;
553 rslt
= calloc(1, bufsize
);
558 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
560 /* skip spaces at beggining */
562 while (avalue
[padded
] == ' ') {
567 if (right_prec
> 0) {
568 bufend
-= right_prec
;
569 memcpy(bufend
, avalue
+ avalue_size
+ padded
- right_prec
, right_prec
);
570 *--bufend
= decimal_point
;
571 avalue_size
-= right_prec
+ 1;
574 if ((*flags
& NEED_GROUPING
) && *grouping
!= CHAR_MAX
&& *grouping
> 0
575 && thousands_sep
!= '\0') {
576 while (avalue_size
> (int)*grouping
) {
581 /* no more grouping ? */
582 if (*grouping
== CHAR_MAX
)
585 /* rest grouping with same value ? */
586 if (*grouping
== 0) {
588 while (avalue_size
> *grouping
) {
594 if (avalue_size
!= 0)
598 bufend
-= avalue_size
;
599 memcpy(bufend
, avalue
+ padded
, avalue_size
);
601 padded
--; /* decrease assumed $decimal_point */
604 /* do padding with pad_char */
607 memset(bufend
, pad_char
, padded
);
610 bufsize
= bufsize
- (bufend
- rslt
) + 1;
611 memmove(rslt
, bufend
, bufsize
);