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
26 * $FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.19 2008/04/24 07:49:00 ru Exp $
27 * $NetBSD: strfmon.c,v 1.3 2005/12/02 14:19:43 yamt Exp $
28 * $DragonFly: src/lib/libc/stdlib/strfmon.c,v 1.1 2008/10/06 21:01:37 swildner Exp $
31 #include <sys/types.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 { \
60 const char *tmps = STR; \
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; \
91 static void __setup_vars(int, char *, char *, char *, const char **);
92 static int __calc_left_pad(int, char *);
93 static char *__format_grouped_double(double, int *, int, int, int);
96 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
100 char *dst; /* output destination pointer */
101 const char *fmt; /* current format poistion pointer */
102 struct lconv *lc; /* pointer to lconv structure */
103 char *asciivalue; /* formatted double pointer */
105 int flags; /* formatting options */
106 int pad_char; /* padding character */
107 int pad_size; /* pad size */
108 int width; /* field width */
109 int left_prec; /* left precision */
110 int right_prec; /* right precision */
111 double value; /* just value */
112 char space_char = ' '; /* space after currency */
114 char cs_precedes, /* values gathered from struct lconv */
119 char *tmpptr; /* temporary vars */
122 va_start(ap, format);
128 currency_symbol = NULL;
132 /* pass nonformating characters AS IS */
138 /* "%%" mean just '%' */
139 if (*(fmt+1) == '%') {
146 /* set up initial values */
147 flags = (NEED_GROUPING|LOCALE_POSN);
148 pad_char = ' '; /* padding character is "space" */
149 left_prec = -1; /* no left precision specified */
150 right_prec = -1; /* no right precision specified */
151 width = -1; /* no width specified */
152 value = 0; /* we have no value to print now */
157 case '=': /* fill character */
159 if (pad_char == '\0')
162 case '^': /* not group currency */
163 flags &= ~(NEED_GROUPING);
165 case '+': /* use locale defined signs */
166 if (flags & SIGN_POSN_USED)
168 flags |= (SIGN_POSN_USED|LOCALE_POSN);
170 case '(': /* enclose negatives with () */
171 if (flags & SIGN_POSN_USED)
173 flags |= (SIGN_POSN_USED|PARENTH_POSN);
175 case '!': /* suppress currency symbol */
176 flags |= SUPRESS_CURR_SYMBOL;
178 case '-': /* alignment (left) */
179 flags |= LEFT_JUSTIFY;
188 if (isdigit((unsigned char)*fmt)) {
190 /* Do we have enough space to put number with
193 if ((unsigned int)width >= maxsize - (dst - s))
199 if (!isdigit((unsigned char)*++fmt))
201 GET_NUMBER(left_prec);
202 if ((unsigned int)left_prec >= maxsize - (dst - s))
206 /* Right precision */
208 if (!isdigit((unsigned char)*++fmt))
210 GET_NUMBER(right_prec);
211 if ((unsigned int)right_prec >= maxsize - (dst - s) -
216 /* Conversion Characters */
218 case 'i': /* use internaltion currency format */
219 flags |= USE_INTL_CURRENCY;
221 case 'n': /* use national currency format */
222 flags &= ~(USE_INTL_CURRENCY);
224 default: /* required character is missing or
229 if (currency_symbol != NULL)
230 free(currency_symbol);
231 if (flags & USE_INTL_CURRENCY) {
232 currency_symbol = strdup(lc->int_curr_symbol);
233 if (currency_symbol != NULL)
234 space_char = *(currency_symbol+3);
236 currency_symbol = strdup(lc->currency_symbol);
238 if (currency_symbol == NULL)
239 goto end_error; /* ENOMEM. */
242 value = va_arg(ap, double);
246 flags |= IS_NEGATIVE;
250 /* fill left_prec with amount of padding chars */
251 if (left_prec >= 0) {
252 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
254 __calc_left_pad(flags, currency_symbol);
259 if (asciivalue != NULL)
261 asciivalue = __format_grouped_double(value, &flags,
262 left_prec, right_prec, pad_char);
263 if (asciivalue == NULL)
264 goto end_error; /* errno already set */
265 /* to ENOMEM by malloc() */
267 /* set some variables for later use */
268 __setup_vars(flags, &cs_precedes, &sep_by_space,
269 &sign_posn, &signstr);
272 * Description of some LC_MONETARY's values:
274 * p_cs_precedes & n_cs_precedes
276 * = 1 - $currency_symbol precedes the value
277 * for a monetary quantity with a non-negative value
278 * = 0 - symbol succeeds the value
280 * p_sep_by_space & n_sep_by_space
282 * = 0 - no space separates $currency_symbol
283 * from the value for a monetary quantity with a
285 * = 1 - space separates the symbol from the value
286 * = 2 - space separates the symbol and the sign string,
289 * p_sign_posn & n_sign_posn
291 * = 0 - parentheses enclose the quantity and the
293 * = 1 - the sign string precedes the quantity and the
295 * = 2 - the sign string succeeds the quantity and the
297 * = 3 - the sign string precedes the $currency_symbol
298 * = 4 - the sign string succeeds the $currency_symbol
304 while (pad_size-- > 0)
307 if (sign_posn == 0 && (flags & IS_NEGATIVE))
310 if (cs_precedes == 1) {
311 if (sign_posn == 1 || sign_posn == 3) {
313 if (sep_by_space == 2) /* XXX: ? */
317 if (!(flags & SUPRESS_CURR_SYMBOL)) {
318 PRINTS(currency_symbol);
320 if (sign_posn == 4) {
321 if (sep_by_space == 2)
324 if (sep_by_space == 1)
326 } else if (sep_by_space == 1)
329 } else if (sign_posn == 1)
334 if (cs_precedes == 0) {
335 if (sign_posn == 3) {
336 if (sep_by_space == 1)
341 if (!(flags & SUPRESS_CURR_SYMBOL)) {
342 if ((sign_posn == 3 && sep_by_space == 2)
343 || (sep_by_space == 1
349 PRINTS(currency_symbol); /* XXX: len */
350 if (sign_posn == 4) {
351 if (sep_by_space == 2)
358 if (sign_posn == 2) {
359 if (sep_by_space == 2)
364 if (sign_posn == 0 && (flags & IS_NEGATIVE))
367 if (dst - tmpptr < width) {
368 if (flags & LEFT_JUSTIFY) {
369 while (dst - tmpptr < width)
372 pad_size = dst-tmpptr;
373 memmove(tmpptr + width-pad_size, tmpptr,
375 memset(tmpptr, ' ', width-pad_size);
376 dst += width-pad_size;
384 free(currency_symbol);
385 return (dst - s - 1); /* return size of put data except trailing '\0' */
396 if (asciivalue != NULL)
398 if (currency_symbol != NULL)
399 free(currency_symbol);
406 __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
407 char *sign_posn, const char **signstr) {
408 struct lconv *lc = localeconv();
410 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
411 *cs_precedes = lc->int_n_cs_precedes;
412 *sep_by_space = lc->int_n_sep_by_space;
413 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
414 *signstr = (lc->negative_sign == '\0') ? "-"
416 } else if (flags & USE_INTL_CURRENCY) {
417 *cs_precedes = lc->int_p_cs_precedes;
418 *sep_by_space = lc->int_p_sep_by_space;
419 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
420 *signstr = lc->positive_sign;
421 } else if (flags & IS_NEGATIVE) {
422 *cs_precedes = lc->n_cs_precedes;
423 *sep_by_space = lc->n_sep_by_space;
424 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
425 *signstr = (lc->negative_sign == '\0') ? "-"
428 *cs_precedes = lc->p_cs_precedes;
429 *sep_by_space = lc->p_sep_by_space;
430 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
431 *signstr = lc->positive_sign;
434 /* Set defult values for unspecified information. */
435 if (*cs_precedes != 0)
437 if (*sep_by_space == CHAR_MAX)
439 if (*sign_posn == CHAR_MAX)
444 __calc_left_pad(int flags, char *cur_symb) {
446 char cs_precedes, sep_by_space, sign_posn;
450 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
452 if (cs_precedes != 0) {
453 left_chars += strlen(cur_symb);
454 if (sep_by_space != 0)
460 left_chars += strlen(signstr);
464 if (cs_precedes != 0)
465 left_chars += strlen(signstr);
471 get_groups(int size, char *grouping) {
475 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
478 while (size > (int)*grouping) {
480 size -= (int)*grouping++;
481 /* no more grouping ? */
482 if (*grouping == CHAR_MAX)
484 /* rest grouping with same value ? */
485 if (*grouping == 0) {
486 chars += (size - 1) / *(grouping - 1);
493 /* convert double to ASCII */
495 __format_grouped_double(double value, int *flags,
496 int left_prec, int right_prec, int pad_char) {
508 struct lconv *lc = localeconv();
515 grouping = lc->mon_grouping;
516 decimal_point = *lc->mon_decimal_point;
517 if (decimal_point == '\0')
518 decimal_point = *lc->decimal_point;
519 thousands_sep = *lc->mon_thousands_sep;
520 if (thousands_sep == '\0')
521 thousands_sep = *lc->thousands_sep;
523 /* fill left_prec with default value */
527 /* fill right_prec with default value */
528 if (right_prec == -1) {
529 if (*flags & USE_INTL_CURRENCY)
530 right_prec = lc->int_frac_digits;
532 right_prec = lc->frac_digits;
534 if (right_prec == CHAR_MAX) /* POSIX locale ? */
538 if (*flags & NEED_GROUPING)
539 left_prec += get_groups(left_prec, grouping);
541 /* convert to string */
542 snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1,
544 avalue_size = asprintf(&avalue, fmt, value);
548 /* make sure that we've enough space for result string */
549 bufsize = strlen(avalue)*2+1;
550 rslt = calloc(1, bufsize);
555 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
557 /* skip spaces at beggining */
559 while (avalue[padded] == ' ') {
564 if (right_prec > 0) {
565 bufend -= right_prec;
566 memcpy(bufend, avalue + avalue_size+padded-right_prec,
568 *--bufend = decimal_point;
569 avalue_size -= (right_prec + 1);
572 if ((*flags & NEED_GROUPING) &&
573 thousands_sep != '\0' && /* XXX: need investigation */
574 *grouping != CHAR_MAX &&
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)
599 bufend -= avalue_size;
600 memcpy(bufend, avalue+padded, avalue_size);
602 padded--; /* decrease assumed $decimal_point */
605 /* do padding with pad_char */
608 memset(bufend, pad_char, padded);
611 bufsize = bufsize - (bufend - rslt) + 1;
612 memmove(rslt, bufend, bufsize);