1 /* $NetBSD: strfmon.c,v 1.7 2009/01/30 23:46:03 lukem Exp $ */
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
30 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
33 __FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $");
35 __RCSID("$NetBSD: strfmon.c,v 1.7 2009/01/30 23:46:03 lukem Exp $");
37 #endif /* LIBC_SCCS and not lint */
39 #if defined(__NetBSD__) || defined(__minix)
40 #include "namespace.h"
44 #include <sys/types.h>
56 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
57 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
58 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
59 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
60 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
61 #define LEFT_JUSTIFY 0x20 /* left justify */
62 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
63 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
66 #define PRINT(CH) do { \
67 if (dst >= s + maxsize) \
70 } while (/* CONSTCOND */ 0)
72 #define PRINTS(STR) do { \
73 const char *tmps = STR; \
74 while (*tmps != '\0') \
76 } while (/* CONSTCOND */ 0)
78 #define GET_NUMBER(VAR) do { \
80 while (isdigit((unsigned char)*fmt)) { \
83 if (VAR > 0x00ffffff) \
87 } while (/* CONSTCOND */ 0)
89 #define GRPCPY(howmany) do { \
93 *--bufend = *(avalue+avalue_size+padded); \
95 } while (/* CONSTCOND */ 0)
98 *--bufend = thousands_sep; \
100 } while (/* CONSTCOND */ 0)
102 static void __setup_vars(int, char *, char *, char *, const char **);
103 static int __calc_left_pad(int, char *);
104 static char *__format_grouped_double(double, int *, int, int, int);
107 strfmon(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
111 char *dst
; /* output destination pointer */
112 const char *fmt
; /* current format poistion pointer */
113 struct lconv
*lc
; /* pointer to lconv structure */
114 char *asciivalue
; /* formatted double pointer */
116 int flags
; /* formatting options */
117 int pad_char
; /* padding character */
118 int pad_size
; /* pad size */
119 int width
; /* field width */
120 int left_prec
; /* left precision */
121 int right_prec
; /* right precision */
122 double value
; /* just value */
123 char space_char
= ' '; /* space after currency */
125 char cs_precedes
, /* values gathered from struct lconv */
131 char *tmpptr
; /* temporary vars */
134 va_start(ap
, format
);
140 currency_symbol
= NULL
;
144 /* pass nonformating characters AS IS */
150 /* "%%" mean just '%' */
151 if (*(fmt
+1) == '%') {
158 /* set up initial values */
159 flags
= (NEED_GROUPING
|LOCALE_POSN
);
160 pad_char
= ' '; /* padding character is "space" */
161 left_prec
= -1; /* no left precision specified */
162 right_prec
= -1; /* no right precision specified */
163 width
= -1; /* no width specified */
164 value
= 0; /* we have no value to print now */
167 while (/* CONSTCOND */ 1) {
169 case '=': /* fill character */
171 if (pad_char
== '\0')
174 case '^': /* not group currency */
175 flags
&= ~(NEED_GROUPING
);
177 case '+': /* use locale defined signs */
178 if (flags
& SIGN_POSN_USED
)
180 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
182 case '(': /* enclose negatives with () */
183 if (flags
& SIGN_POSN_USED
)
185 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
187 case '!': /* suppress currency symbol */
188 flags
|= SUPRESS_CURR_SYMBOL
;
190 case '-': /* alignment (left) */
191 flags
|= LEFT_JUSTIFY
;
200 if (isdigit((unsigned char)*fmt
)) {
201 ptrdiff_t d
= dst
- s
;
203 /* Do we have enough space to put number with
207 if ((size_t)(d
+ width
) >= maxsize
)
213 if (!isdigit((unsigned char)*++fmt
))
215 GET_NUMBER(left_prec
);
218 /* Right precision */
220 if (!isdigit((unsigned char)*++fmt
))
222 GET_NUMBER(right_prec
);
225 /* Conversion Characters */
227 case 'i': /* use internaltion currency format */
228 flags
|= USE_INTL_CURRENCY
;
230 case 'n': /* use national currency format */
231 flags
&= ~(USE_INTL_CURRENCY
);
233 default: /* required character is missing or
239 free(currency_symbol
);
240 if (flags
& USE_INTL_CURRENCY
) {
241 currency_symbol
= strdup(lc
->int_curr_symbol
);
242 if (currency_symbol
!= NULL
)
243 space_char
= *(currency_symbol
+3);
245 currency_symbol
= strdup(lc
->currency_symbol
);
247 if (currency_symbol
== NULL
)
248 goto end_error
; /* ENOMEM. */
251 value
= va_arg(ap
, double);
255 flags
|= IS_NEGATIVE
;
259 /* fill left_prec with amount of padding chars */
260 if (left_prec
>= 0) {
261 pad_size
= __calc_left_pad((flags
^ IS_NEGATIVE
),
263 __calc_left_pad(flags
, currency_symbol
);
268 asciivalue
= __format_grouped_double(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(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) /* XXX: ? */
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
356 PRINTS(currency_symbol
); /* XXX: len */
357 if (sign_posn
== 4) {
358 if (sep_by_space
== 2)
365 if (sign_posn
== 2) {
366 if (sep_by_space
== 2)
371 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
374 if (dst
- tmpptr
< width
) {
375 if (flags
& LEFT_JUSTIFY
) {
376 while (dst
- tmpptr
< width
)
379 pad_size
= dst
-tmpptr
;
380 memmove(tmpptr
+ width
-pad_size
, tmpptr
,
382 memset(tmpptr
, ' ', (size_t) width
-pad_size
);
383 dst
+= width
-pad_size
;
391 free(currency_symbol
);
392 return (dst
- s
- 1); /* return size of put data except trailing '\0' */
403 if (asciivalue
!= NULL
)
405 if (currency_symbol
!= NULL
)
406 free(currency_symbol
);
413 __setup_vars(int flags
, char *cs_precedes
, char *sep_by_space
,
414 char *sign_posn
, const char **signstr
) {
415 struct lconv
*lc
= localeconv();
417 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
418 *cs_precedes
= lc
->int_n_cs_precedes
;
419 *sep_by_space
= lc
->int_n_sep_by_space
;
420 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_n_sign_posn
;
421 *signstr
= (lc
->negative_sign
== '\0') ? "-"
423 } else if (flags
& USE_INTL_CURRENCY
) {
424 *cs_precedes
= lc
->int_p_cs_precedes
;
425 *sep_by_space
= lc
->int_p_sep_by_space
;
426 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_p_sign_posn
;
427 *signstr
= lc
->positive_sign
;
428 } else if (flags
& IS_NEGATIVE
) {
429 *cs_precedes
= lc
->n_cs_precedes
;
430 *sep_by_space
= lc
->n_sep_by_space
;
431 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->n_sign_posn
;
432 *signstr
= (lc
->negative_sign
== '\0') ? "-"
435 *cs_precedes
= lc
->p_cs_precedes
;
436 *sep_by_space
= lc
->p_sep_by_space
;
437 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->p_sign_posn
;
438 *signstr
= lc
->positive_sign
;
441 /* Set defult values for unspecified information. */
442 if (*cs_precedes
!= 0)
444 if (*sep_by_space
== CHAR_MAX
)
446 if (*sign_posn
== CHAR_MAX
)
451 __calc_left_pad(int flags
, char *cur_symb
) {
453 char cs_precedes
, sep_by_space
, sign_posn
;
457 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
);
459 if (cs_precedes
!= 0) {
460 left_chars
+= strlen(cur_symb
);
461 if (sep_by_space
!= 0)
467 left_chars
+= strlen(signstr
);
471 if (cs_precedes
!= 0)
472 left_chars
+= strlen(signstr
);
478 get_groups(int size
, char *grouping
) {
482 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
485 while (size
> (int)*grouping
) {
487 size
-= (int)*grouping
++;
488 /* no more grouping ? */
489 if (*grouping
== CHAR_MAX
)
491 /* rest grouping with same value ? */
492 if (*grouping
== 0) {
493 chars
+= (size
- 1) / *(grouping
- 1);
500 /* convert double to ASCII */
502 __format_grouped_double(double value
, int *flags
,
503 int left_prec
, int right_prec
, int pad_char
) {
515 struct lconv
*lc
= localeconv();
522 grouping
= lc
->mon_grouping
;
523 decimal_point
= *lc
->mon_decimal_point
;
524 if (decimal_point
== '\0')
525 decimal_point
= *lc
->decimal_point
;
526 thousands_sep
= *lc
->mon_thousands_sep
;
527 if (thousands_sep
== '\0')
528 thousands_sep
= *lc
->thousands_sep
;
530 /* fill left_prec with default value */
534 /* fill right_prec with default value */
535 if (right_prec
== -1) {
536 if (*flags
& USE_INTL_CURRENCY
)
537 right_prec
= lc
->int_frac_digits
;
539 right_prec
= lc
->frac_digits
;
541 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
545 if (*flags
& NEED_GROUPING
)
546 left_prec
+= get_groups(left_prec
, grouping
);
548 /* convert to string */
549 snprintf(fmt
, sizeof(fmt
), "%%%d.%df", left_prec
+ right_prec
+ 1,
551 avalue_size
= asprintf(&avalue
, fmt
, value
);
555 /* make sure that we've enough space for result string */
556 bufsize
= strlen(avalue
)*2+1;
557 rslt
= malloc(bufsize
);
562 memset(rslt
, 0, bufsize
);
563 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
565 /* skip spaces at beggining */
567 while (avalue
[padded
] == ' ') {
572 if (right_prec
> 0) {
573 bufend
-= right_prec
;
574 memcpy(bufend
, avalue
+ avalue_size
+padded
-right_prec
,
575 (size_t) right_prec
);
576 *--bufend
= decimal_point
;
577 avalue_size
-= (right_prec
+ 1);
580 if ((*flags
& NEED_GROUPING
) &&
581 thousands_sep
!= '\0' && /* XXX: need investigation */
582 *grouping
!= CHAR_MAX
&&
584 while (avalue_size
> (int)*grouping
) {
589 /* no more grouping ? */
590 if (*grouping
== CHAR_MAX
)
593 /* rest grouping with same value ? */
594 if (*grouping
== 0) {
596 while (avalue_size
> *grouping
) {
602 if (avalue_size
!= 0)
607 bufend
-= avalue_size
;
608 memcpy(bufend
, avalue
+padded
, (size_t) avalue_size
);
610 padded
--; /* decrease assumed $decimal_point */
613 /* do padding with pad_char */
616 memset(bufend
, pad_char
, (size_t) padded
);
619 bufsize
= bufsize
- (bufend
- rslt
) + 1;
620 memmove(rslt
, bufend
, bufsize
);