2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
7 * Copyright (c) 2011 The FreeBSD Foundation
9 * Portions of this software were developed by David Chisnall
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
48 #include "xlocale_private.h"
51 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
52 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
53 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
54 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
55 #define SUPRESS_CURR_SYMBOL 0x10 /* suppress the currency from output */
56 #define LEFT_JUSTIFY 0x20 /* left justify */
57 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
58 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
61 #define PRINT(CH) do { \
62 if (dst >= s + maxsize) \
67 #define PRINTS(STR) do { \
69 while (*tmps != '\0') \
73 #define GET_NUMBER(VAR) do { \
75 while (isdigit((unsigned char)*fmt)) { \
76 if (VAR > INT_MAX / 10) \
86 #define GRPCPY(howmany) do { \
90 *--bufend = *(avalue+avalue_size+padded); \
95 *--bufend = thousands_sep; \
99 static void __setup_vars(int, char *, char *, char *, char **);
100 static int __calc_left_pad(int, char *);
101 static char *__format_grouped_double(double, int *, int, int, int);
104 vstrfmon_l(char * __restrict s, size_t maxsize, locale_t loc,
105 const char * __restrict format, va_list ap)
107 char *dst; /* output destination pointer */
108 const char *fmt; /* current format poistion pointer */
109 struct lconv *lc; /* pointer to lconv structure */
110 char *asciivalue; /* formatted double pointer */
112 int flags; /* formatting options */
113 int pad_char; /* padding character */
114 int pad_size; /* pad size */
115 int width; /* field width */
116 int left_prec; /* left precision */
117 int right_prec; /* right precision */
118 double value; /* just value */
119 char space_char = ' '; /* space after currency */
121 char cs_precedes, /* values gathered from struct lconv */
127 char *tmpptr; /* temporary vars */
132 lc = localeconv_l(loc);
136 currency_symbol = NULL;
140 /* pass nonformating characters AS IS */
146 /* "%%" mean just '%' */
147 if (*(fmt+1) == '%') {
154 /* set up initial values */
155 flags = (NEED_GROUPING|LOCALE_POSN);
156 pad_char = ' '; /* padding character is "space" */
157 left_prec = -1; /* no left precision specified */
158 right_prec = -1; /* no right precision specified */
159 width = -1; /* no width specified */
160 value = 0; /* we have no value to print now */
165 case '=': /* fill character */
167 if (pad_char == '\0')
170 case '^': /* not group currency */
171 flags &= ~(NEED_GROUPING);
173 case '+': /* use locale defined signs */
174 if (flags & SIGN_POSN_USED)
176 flags |= (SIGN_POSN_USED|LOCALE_POSN);
178 case '(': /* enclose negatives with () */
179 if (flags & SIGN_POSN_USED)
181 flags |= (SIGN_POSN_USED|PARENTH_POSN);
183 case '!': /* suppress currency symbol */
184 flags |= SUPRESS_CURR_SYMBOL;
186 case '-': /* alignment (left) */
187 flags |= LEFT_JUSTIFY;
196 if (isdigit((unsigned char)*fmt)) {
198 /* Do we have enough space to put number with
201 if ((unsigned int)width >= maxsize - (dst - s))
207 if (!isdigit((unsigned char)*++fmt))
209 GET_NUMBER(left_prec);
210 if ((unsigned int)left_prec >= maxsize - (dst - s))
214 /* Right precision */
216 if (!isdigit((unsigned char)*++fmt))
218 GET_NUMBER(right_prec);
219 if ((unsigned int)right_prec >= maxsize - (dst - s) -
224 /* Conversion Characters */
226 case 'i': /* use internaltion currency format */
227 flags |= USE_INTL_CURRENCY;
229 case 'n': /* use national currency format */
230 flags &= ~(USE_INTL_CURRENCY);
232 default: /* required character is missing or
237 if (currency_symbol != NULL)
238 free(currency_symbol);
239 if (flags & USE_INTL_CURRENCY) {
240 currency_symbol = strdup(lc->int_curr_symbol);
241 if (currency_symbol != NULL)
242 space_char = *(currency_symbol+3);
244 currency_symbol = strdup(lc->currency_symbol);
246 if (currency_symbol == NULL)
247 goto end_error; /* ENOMEM. */
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((flags ^ IS_NEGATIVE),
262 __calc_left_pad(flags, currency_symbol);
267 if (asciivalue != NULL)
269 asciivalue = __format_grouped_double(value, &flags,
270 left_prec, right_prec, pad_char);
271 if (asciivalue == NULL)
272 goto end_error; /* errno already set */
273 /* to ENOMEM by malloc() */
275 /* set some variables for later use */
276 __setup_vars(flags, &cs_precedes, &sep_by_space,
277 &sign_posn, &signstr);
280 * Description of some LC_MONETARY's values:
282 * p_cs_precedes & n_cs_precedes
284 * = 1 - $currency_symbol precedes the value
285 * for a monetary quantity with a non-negative value
286 * = 0 - symbol succeeds the value
288 * p_sep_by_space & n_sep_by_space
290 * = 0 - no space separates $currency_symbol
291 * from the value for a monetary quantity with a
293 * = 1 - space separates the symbol from the value
294 * = 2 - space separates the symbol and the sign string,
297 * p_sign_posn & n_sign_posn
299 * = 0 - parentheses enclose the quantity and the
301 * = 1 - the sign string precedes the quantity and the
303 * = 2 - the sign string succeeds the quantity and the
305 * = 3 - the sign string precedes the $currency_symbol
306 * = 4 - the sign string succeeds the $currency_symbol
312 while (pad_size-- > 0)
315 if (sign_posn == 0 && (flags & IS_NEGATIVE))
318 if (cs_precedes == 1) {
319 if (sign_posn == 1 || sign_posn == 3) {
321 if (sep_by_space == 2) /* XXX: ? */
325 if (!(flags & SUPRESS_CURR_SYMBOL)) {
326 PRINTS(currency_symbol);
328 if (sign_posn == 4) {
329 if (sep_by_space == 2)
332 if (sep_by_space == 1)
334 } else if (sep_by_space == 1)
337 } else if (sign_posn == 1)
342 if (cs_precedes == 0) {
343 if (sign_posn == 3) {
344 if (sep_by_space == 1)
349 if (!(flags & SUPRESS_CURR_SYMBOL)) {
350 if ((sign_posn == 3 && sep_by_space == 2)
351 || (sep_by_space == 1
357 PRINTS(currency_symbol); /* XXX: len */
358 if (sign_posn == 4) {
359 if (sep_by_space == 2)
366 if (sign_posn == 2) {
367 if (sep_by_space == 2)
372 if (sign_posn == 0 && (flags & IS_NEGATIVE))
375 if (dst - tmpptr < width) {
376 if (flags & LEFT_JUSTIFY) {
377 while (dst - tmpptr < width)
380 pad_size = dst-tmpptr;
381 memmove(tmpptr + width-pad_size, tmpptr,
383 memset(tmpptr, ' ', width-pad_size);
384 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);
411 strfmon_l(char * __restrict s, size_t maxsize, locale_t loc, const char * __restrict format,
416 va_start(ap, format);
417 ret = vstrfmon_l(s, maxsize, loc, format, ap);
423 strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
428 va_start(ap, format);
429 ret = vstrfmon_l(s, maxsize, __get_locale(), format, ap);
436 __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
437 char *sign_posn, char **signstr) {
439 struct lconv *lc = localeconv();
441 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
442 *cs_precedes = lc->int_n_cs_precedes;
443 *sep_by_space = lc->int_n_sep_by_space;
444 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
445 *signstr = (lc->negative_sign[0] == '\0') ? "-"
447 } else if (flags & USE_INTL_CURRENCY) {
448 *cs_precedes = lc->int_p_cs_precedes;
449 *sep_by_space = lc->int_p_sep_by_space;
450 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
451 *signstr = lc->positive_sign;
452 } else if (flags & IS_NEGATIVE) {
453 *cs_precedes = lc->n_cs_precedes;
454 *sep_by_space = lc->n_sep_by_space;
455 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
456 *signstr = (lc->negative_sign[0] == '\0') ? "-"
459 *cs_precedes = lc->p_cs_precedes;
460 *sep_by_space = lc->p_sep_by_space;
461 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
462 *signstr = lc->positive_sign;
465 /* Set defult values for unspecified information. */
466 if (*cs_precedes != 0)
468 if (*sep_by_space == CHAR_MAX)
470 if (*sign_posn == CHAR_MAX)
475 __calc_left_pad(int flags, char *cur_symb) {
477 char cs_precedes, sep_by_space, sign_posn, *signstr;
480 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
482 if (cs_precedes != 0) {
483 left_chars += strlen(cur_symb);
484 if (sep_by_space != 0)
490 left_chars += strlen(signstr);
494 if (cs_precedes != 0)
495 left_chars += strlen(signstr);
501 get_groups(int size, char *grouping) {
505 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
508 while (size > (int)*grouping) {
510 size -= (int)*grouping++;
511 /* no more grouping ? */
512 if (*grouping == CHAR_MAX)
514 /* rest grouping with same value ? */
515 if (*grouping == 0) {
516 chars += (size - 1) / *(grouping - 1);
523 /* convert double to ASCII */
525 __format_grouped_double(double value, int *flags,
526 int left_prec, int right_prec, int pad_char) {
537 struct lconv *lc = localeconv();
544 grouping = lc->mon_grouping;
545 decimal_point = *lc->mon_decimal_point;
546 if (decimal_point == '\0')
547 decimal_point = *lc->decimal_point;
548 thousands_sep = *lc->mon_thousands_sep;
549 if (thousands_sep == '\0')
550 thousands_sep = *lc->thousands_sep;
552 /* fill left_prec with default value */
556 /* fill right_prec with default value */
557 if (right_prec == -1) {
558 if (*flags & USE_INTL_CURRENCY)
559 right_prec = lc->int_frac_digits;
561 right_prec = lc->frac_digits;
563 if (right_prec == CHAR_MAX) /* POSIX locale ? */
567 if (*flags & NEED_GROUPING)
568 left_prec += get_groups(left_prec, grouping);
570 /* convert to string */
571 avalue_size = asprintf(&avalue, "%*.*f", left_prec + right_prec + 1,
576 /* make sure that we've enough space for result string */
577 bufsize = avalue_size * 2 + 1;
578 rslt = calloc(1, bufsize);
583 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
585 /* skip spaces at beginning */
587 while (avalue[padded] == ' ') {
592 if (right_prec > 0) {
593 bufend -= right_prec;
594 memcpy(bufend, avalue + avalue_size+padded-right_prec,
596 *--bufend = decimal_point;
597 avalue_size -= (right_prec + 1);
600 if ((*flags & NEED_GROUPING) &&
601 thousands_sep != '\0' && /* XXX: need investigation */
602 *grouping != CHAR_MAX &&
604 while (avalue_size > (int)*grouping) {
609 /* no more grouping ? */
610 if (*grouping == CHAR_MAX)
613 /* rest grouping with same value ? */
614 if (*grouping == 0) {
616 while (avalue_size > *grouping) {
622 if (avalue_size != 0)
627 bufend -= avalue_size;
628 memcpy(bufend, avalue+padded, avalue_size);
630 padded--; /* decrease assumed $decimal_point */
633 /* do padding with pad_char */
636 memset(bufend, pad_char, padded);
639 bufsize = bufsize - (bufend - rslt) + 1;
640 memmove(rslt, bufend, bufsize);