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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
46 #include "xlocale_private.h"
49 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
50 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
51 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
52 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
53 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
54 #define LEFT_JUSTIFY 0x20 /* left justify */
55 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
56 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
59 #define PRINT(CH) do { \
60 if (dst >= s + maxsize) \
65 #define PRINTS(STR) do { \
67 while (*tmps != '\0') \
71 #define GET_NUMBER(VAR) do { \
73 while (isdigit((unsigned char)*fmt)) { \
74 if (VAR > INT_MAX / 10) \
84 #define GRPCPY(howmany) do { \
88 *--bufend = *(avalue+avalue_size+padded); \
93 *--bufend = thousands_sep; \
97 static void __setup_vars(int, char *, char *, char *, char **);
98 static int __calc_left_pad(int, char *);
99 static char *__format_grouped_double(double, int *, int, int, int);
102 vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
103 const char * __restrict format, va_list ap)
105 char *dst; /* output destination pointer */
106 const char *fmt; /* current format poistion pointer */
107 struct lconv *lc; /* pointer to lconv structure */
108 char *asciivalue; /* formatted double pointer */
110 int flags; /* formatting options */
111 int pad_char; /* padding character */
112 int pad_size; /* pad size */
113 int width; /* field width */
114 int left_prec; /* left precision */
115 int right_prec; /* right precision */
116 double value; /* just value */
117 char space_char = ' '; /* space after currency */
119 char cs_precedes, /* values gathered from struct lconv */
125 char *tmpptr; /* temporary vars */
130 lc = localeconv_l(loc);
134 currency_symbol = NULL;
138 /* pass nonformating characters AS IS */
144 /* "%%" mean just '%' */
145 if (*(fmt+1) == '%') {
152 /* set up initial values */
153 flags = (NEED_GROUPING|LOCALE_POSN);
154 pad_char = ' '; /* padding character is "space" */
155 left_prec = -1; /* no left precision specified */
156 right_prec = -1; /* no right precision specified */
157 width = -1; /* no width specified */
158 value = 0; /* we have no value to print now */
163 case '=': /* fill character */
165 if (pad_char == '\0')
168 case '^': /* not group currency */
169 flags &= ~(NEED_GROUPING);
171 case '+': /* use locale defined signs */
172 if (flags & SIGN_POSN_USED)
174 flags |= (SIGN_POSN_USED|LOCALE_POSN);
176 case '(': /* enclose negatives with () */
177 if (flags & SIGN_POSN_USED)
179 flags |= (SIGN_POSN_USED|PARENTH_POSN);
181 case '!': /* suppress currency symbol */
182 flags |= SUPRESS_CURR_SYMBOL;
184 case '-': /* alignment (left) */
185 flags |= LEFT_JUSTIFY;
194 if (isdigit((unsigned char)*fmt)) {
196 /* Do we have enough space to put number with
199 if ((unsigned int)width >= maxsize - (dst - s))
205 if (!isdigit((unsigned char)*++fmt))
207 GET_NUMBER(left_prec);
208 if ((unsigned int)left_prec >= maxsize - (dst - s))
212 /* Right precision */
214 if (!isdigit((unsigned char)*++fmt))
216 GET_NUMBER(right_prec);
217 if ((unsigned int)right_prec >= maxsize - (dst - s) -
222 /* Conversion Characters */
224 case 'i': /* use internaltion currency format */
225 flags |= USE_INTL_CURRENCY;
227 case 'n': /* use national currency format */
228 flags &= ~(USE_INTL_CURRENCY);
230 default: /* required character is missing or
235 if (currency_symbol != NULL)
236 free(currency_symbol);
237 if (flags & USE_INTL_CURRENCY) {
238 currency_symbol = strdup(lc->int_curr_symbol);
239 if (currency_symbol != NULL)
240 space_char = *(currency_symbol+3);
242 currency_symbol = strdup(lc->currency_symbol);
244 if (currency_symbol == NULL)
245 goto end_error; /* ENOMEM. */
248 value = va_arg(ap, double);
252 flags |= IS_NEGATIVE;
256 /* fill left_prec with amount of padding chars */
257 if (left_prec >= 0) {
258 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE),
260 __calc_left_pad(flags, currency_symbol);
265 if (asciivalue != NULL)
267 asciivalue = __format_grouped_double(value, &flags,
268 left_prec, right_prec, pad_char);
269 if (asciivalue == NULL)
270 goto end_error; /* errno already set */
271 /* to ENOMEM by malloc() */
273 /* set some variables for later use */
274 __setup_vars(flags, &cs_precedes, &sep_by_space,
275 &sign_posn, &signstr);
278 * Description of some LC_MONETARY's values:
280 * p_cs_precedes & n_cs_precedes
282 * = 1 - $currency_symbol precedes the value
283 * for a monetary quantity with a non-negative value
284 * = 0 - symbol succeeds the value
286 * p_sep_by_space & n_sep_by_space
288 * = 0 - no space separates $currency_symbol
289 * from the value for a monetary quantity with a
291 * = 1 - space separates the symbol from the value
292 * = 2 - space separates the symbol and the sign string,
295 * p_sign_posn & n_sign_posn
297 * = 0 - parentheses enclose the quantity and the
299 * = 1 - the sign string precedes the quantity and the
301 * = 2 - the sign string succeeds the quantity and the
303 * = 3 - the sign string precedes the $currency_symbol
304 * = 4 - the sign string succeeds the $currency_symbol
310 while (pad_size-- > 0)
313 if (sign_posn == 0 && (flags & IS_NEGATIVE))
316 if (cs_precedes == 1) {
317 if (sign_posn == 1 || sign_posn == 3) {
319 if (sep_by_space == 2) /* XXX: ? */
323 if (!(flags & SUPRESS_CURR_SYMBOL)) {
324 PRINTS(currency_symbol);
326 if (sign_posn == 4) {
327 if (sep_by_space == 2)
330 if (sep_by_space == 1)
332 } else if (sep_by_space == 1)
335 } else if (sign_posn == 1)
340 if (cs_precedes == 0) {
341 if (sign_posn == 3) {
342 if (sep_by_space == 1)
347 if (!(flags & SUPRESS_CURR_SYMBOL)) {
348 if ((sign_posn == 3 && sep_by_space == 2)
349 || (sep_by_space == 1
355 PRINTS(currency_symbol); /* XXX: len */
356 if (sign_posn == 4) {
357 if (sep_by_space == 2)
364 if (sign_posn == 2) {
365 if (sep_by_space == 2)
370 if (sign_posn == 0 && (flags & IS_NEGATIVE))
373 if (dst - tmpptr < width) {
374 if (flags & LEFT_JUSTIFY) {
375 while (dst - tmpptr < width)
378 pad_size = dst-tmpptr;
379 memmove(tmpptr + width-pad_size, tmpptr,
381 memset(tmpptr, ' ', width-pad_size);
382 dst += width-pad_size;
389 free(currency_symbol);
390 return (dst - s - 1); /* return size of put data except trailing '\0' */
401 if (asciivalue != NULL)
403 if (currency_symbol != NULL)
404 free(currency_symbol);
409 strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format,
414 va_start(ap, format);
415 ret = vstrfmon_l(s, maxsize, loc, format, ap);
421 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
426 va_start(ap, format);
427 ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
434 __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
435 char *sign_posn, char **signstr) {
437 struct lconv *lc = localeconv();
439 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
440 *cs_precedes = lc->int_n_cs_precedes;
441 *sep_by_space = lc->int_n_sep_by_space;
442 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
443 *signstr = (lc->negative_sign[0] == '\0') ? "-"
445 } else if (flags & USE_INTL_CURRENCY) {
446 *cs_precedes = lc->int_p_cs_precedes;
447 *sep_by_space = lc->int_p_sep_by_space;
448 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
449 *signstr = lc->positive_sign;
450 } else if (flags & IS_NEGATIVE) {
451 *cs_precedes = lc->n_cs_precedes;
452 *sep_by_space = lc->n_sep_by_space;
453 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
454 *signstr = (lc->negative_sign[0] == '\0') ? "-"
457 *cs_precedes = lc->p_cs_precedes;
458 *sep_by_space = lc->p_sep_by_space;
459 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
460 *signstr = lc->positive_sign;
463 /* Set defult values for unspecified information. */
464 if (*cs_precedes != 0)
466 if (*sep_by_space == CHAR_MAX)
468 if (*sign_posn == CHAR_MAX)
473 __calc_left_pad(int flags, char *cur_symb) {
475 char cs_precedes, sep_by_space, sign_posn, *signstr;
478 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
480 if (cs_precedes != 0) {
481 left_chars += strlen(cur_symb);
482 if (sep_by_space != 0)
488 left_chars += strlen(signstr);
492 if (cs_precedes != 0)
493 left_chars += strlen(signstr);
499 get_groups(int size, char *grouping) {
503 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
506 while (size > (int)*grouping) {
508 size -= (int)*grouping++;
509 /* no more grouping ? */
510 if (*grouping == CHAR_MAX)
512 /* rest grouping with same value ? */
513 if (*grouping == 0) {
514 chars += (size - 1) / *(grouping - 1);
521 /* convert double to ASCII */
523 __format_grouped_double(double value, int *flags,
524 int left_prec, int right_prec, int pad_char) {
535 struct lconv *lc = localeconv();
542 grouping = lc->mon_grouping;
543 decimal_point = *lc->mon_decimal_point;
544 if (decimal_point == '\0')
545 decimal_point = *lc->decimal_point;
546 thousands_sep = *lc->mon_thousands_sep;
547 if (thousands_sep == '\0')
548 thousands_sep = *lc->thousands_sep;
550 /* fill left_prec with default value */
554 /* fill right_prec with default value */
555 if (right_prec == -1) {
556 if (*flags & USE_INTL_CURRENCY)
557 right_prec = lc->int_frac_digits;
559 right_prec = lc->frac_digits;
561 if (right_prec == CHAR_MAX) /* POSIX locale ? */
565 if (*flags & NEED_GROUPING)
566 left_prec += get_groups(left_prec, grouping);
568 /* convert to string */
569 avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1,
574 /* make sure that we've enough space for result string */
575 bufsize = avalue_size * 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);