]>
git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/strfmon.c
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/cdefs.h>
29 __FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $");
31 #include <sys/types.h>
42 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
43 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
44 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
45 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
46 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
47 #define LEFT_JUSTIFY 0x20 /* left justify */
48 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
49 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
52 #define PRINT(CH) do { \
53 if (dst >= s + maxsize) \
58 #define PRINTS(STR) do { \
60 while (*tmps != '\0') \
64 #define GET_NUMBER(VAR) do { \
66 while (isdigit((unsigned char)*fmt)) { \
73 #define GRPCPY(howmany) do { \
77 *--bufend = *(avalue+avalue_size+padded); \
82 *--bufend = thousands_sep; \
86 static void __setup_vars(int, char *, char *, char *, char **);
87 static int __calc_left_pad(int, char *);
88 static char *__format_grouped_double(double, int *, int, int, int);
91 strfmon(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
95 char *dst
; /* output destination pointer */
96 const char *fmt
; /* current format poistion pointer */
97 struct lconv
*lc
; /* pointer to lconv structure */
98 char *asciivalue
; /* formatted double pointer */
100 int flags
; /* formatting options */
101 int pad_char
; /* padding character */
102 int pad_size
; /* pad size */
103 int width
; /* field width */
104 int left_prec
; /* left precision */
105 int right_prec
; /* right precision */
106 double value
; /* just value */
107 char space_char
= ' '; /* space after currency */
109 char cs_precedes
, /* values gathered from struct lconv */
115 char *tmpptr
; /* temporary vars */
118 va_start(ap
, format
);
124 currency_symbol
= NULL
;
128 /* pass nonformating characters AS IS */
134 /* "%%" mean just '%' */
135 if (*(fmt
+1) == '%') {
142 /* set up initial values */
143 flags
= (NEED_GROUPING
|LOCALE_POSN
);
144 pad_char
= ' '; /* padding character is "space" */
145 left_prec
= -1; /* no left precision specified */
146 right_prec
= -1; /* no right precision specified */
147 width
= -1; /* no width specified */
148 value
= 0; /* we have no value to print now */
153 case '=': /* fill character */
155 if (pad_char
== '\0')
158 case '^': /* not group currency */
159 flags
&= ~(NEED_GROUPING
);
161 case '+': /* use locale defined signs */
162 if (flags
& SIGN_POSN_USED
)
164 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
166 case '(': /* enclose negatives with () */
167 if (flags
& SIGN_POSN_USED
)
169 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
171 case '!': /* suppress currency symbol */
172 flags
|= SUPRESS_CURR_SYMBOL
;
174 case '-': /* alignment (left) */
175 flags
|= LEFT_JUSTIFY
;
184 if (isdigit((unsigned char)*fmt
)) {
186 /* Do we have enough space to put number with
189 if (dst
+ width
>= s
+ maxsize
)
195 if (!isdigit((unsigned char)*++fmt
))
197 GET_NUMBER(left_prec
);
200 /* Right precision */
202 if (!isdigit((unsigned char)*++fmt
))
204 GET_NUMBER(right_prec
);
207 /* Conversion Characters */
209 case 'i': /* use internaltion currency format */
210 flags
|= USE_INTL_CURRENCY
;
212 case 'n': /* use national currency format */
213 flags
&= ~(USE_INTL_CURRENCY
);
215 default: /* required character is missing or
220 if (flags
& USE_INTL_CURRENCY
) {
221 currency_symbol
= strdup(lc
->int_curr_symbol
);
222 if (currency_symbol
!= NULL
)
223 space_char
= *(currency_symbol
+3);
225 currency_symbol
= strdup(lc
->currency_symbol
);
227 if (currency_symbol
== NULL
)
228 goto end_error
; /* ENOMEM. */
231 value
= va_arg(ap
, double);
235 flags
|= IS_NEGATIVE
;
239 /* fill left_prec with amount of padding chars */
240 if (left_prec
>= 0) {
241 pad_size
= __calc_left_pad((flags
^ IS_NEGATIVE
),
243 __calc_left_pad(flags
, currency_symbol
);
248 asciivalue
= __format_grouped_double(value
, &flags
,
249 left_prec
, right_prec
, pad_char
);
250 if (asciivalue
== NULL
)
251 goto end_error
; /* errno already set */
252 /* to ENOMEM by malloc() */
254 /* set some variables for later use */
255 __setup_vars(flags
, &cs_precedes
, &sep_by_space
,
256 &sign_posn
, &signstr
);
259 * Description of some LC_MONETARY's values:
261 * p_cs_precedes & n_cs_precedes
263 * = 1 - $currency_symbol precedes the value
264 * for a monetary quantity with a non-negative value
265 * = 0 - symbol succeeds the value
267 * p_sep_by_space & n_sep_by_space
269 * = 0 - no space separates $currency_symbol
270 * from the value for a monetary quantity with a
272 * = 1 - space separates the symbol from the value
273 * = 2 - space separates the symbol and the sign string,
276 * p_sign_posn & n_sign_posn
278 * = 0 - parentheses enclose the quantity and the
280 * = 1 - the sign string precedes the quantity and the
282 * = 2 - the sign string succeeds the quantity and the
284 * = 3 - the sign string precedes the $currency_symbol
285 * = 4 - the sign string succeeds the $currency_symbol
291 while (pad_size
-- > 0)
294 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
297 if (cs_precedes
== 1) {
298 if (sign_posn
== 1 || sign_posn
== 3) {
300 if (sep_by_space
== 2) /* XXX: ? */
304 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
305 PRINTS(currency_symbol
);
307 if (sign_posn
== 4) {
308 if (sep_by_space
== 2)
311 if (sep_by_space
== 1)
313 } else if (sep_by_space
== 1)
316 } else if (sign_posn
== 1)
321 if (cs_precedes
== 0) {
322 if (sign_posn
== 3) {
323 if (sep_by_space
== 1)
328 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
329 if ((sign_posn
== 3 && sep_by_space
== 2)
330 || (sep_by_space
== 1
336 PRINTS(currency_symbol
); /* XXX: len */
337 if (sign_posn
== 4) {
338 if (sep_by_space
== 2)
345 if (sign_posn
== 2) {
346 if (sep_by_space
== 2)
351 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
354 if (dst
- tmpptr
< width
) {
355 if (flags
& LEFT_JUSTIFY
) {
356 while (dst
- tmpptr
< width
)
359 pad_size
= dst
-tmpptr
;
360 memmove(tmpptr
+ width
-pad_size
, tmpptr
,
362 memset(tmpptr
, ' ', width
-pad_size
);
363 dst
+= width
-pad_size
;
371 free(currency_symbol
);
372 return (dst
- s
- 1); /* return size of put data except trailing '\0' */
383 if (asciivalue
!= NULL
)
385 if (currency_symbol
!= NULL
)
386 free(currency_symbol
);
393 __setup_vars(int flags
, char *cs_precedes
, char *sep_by_space
,
394 char *sign_posn
, char **signstr
) {
396 struct lconv
*lc
= localeconv();
398 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
399 *cs_precedes
= lc
->int_n_cs_precedes
;
400 *sep_by_space
= lc
->int_n_sep_by_space
;
401 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_n_sign_posn
;
402 *signstr
= (lc
->negative_sign
== '\0') ? "-"
404 } else if (flags
& USE_INTL_CURRENCY
) {
405 *cs_precedes
= lc
->int_p_cs_precedes
;
406 *sep_by_space
= lc
->int_p_sep_by_space
;
407 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_p_sign_posn
;
408 *signstr
= lc
->positive_sign
;
409 } else if (flags
& IS_NEGATIVE
) {
410 *cs_precedes
= lc
->n_cs_precedes
;
411 *sep_by_space
= lc
->n_sep_by_space
;
412 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->n_sign_posn
;
413 *signstr
= (lc
->negative_sign
== '\0') ? "-"
416 *cs_precedes
= lc
->p_cs_precedes
;
417 *sep_by_space
= lc
->p_sep_by_space
;
418 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->p_sign_posn
;
419 *signstr
= lc
->positive_sign
;
422 /* Set defult values for unspecified information. */
423 if (*cs_precedes
!= 0)
425 if (*sep_by_space
== CHAR_MAX
)
427 if (*sign_posn
== CHAR_MAX
)
432 __calc_left_pad(int flags
, char *cur_symb
) {
434 char cs_precedes
, sep_by_space
, sign_posn
, *signstr
;
437 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
);
439 if (cs_precedes
!= 0) {
440 left_chars
+= strlen(cur_symb
);
441 if (sep_by_space
!= 0)
447 left_chars
+= strlen(signstr
);
451 if (cs_precedes
!= 0)
452 left_chars
+= strlen(signstr
);
458 get_groups(int size
, char *grouping
) {
462 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
465 while (size
> (int)*grouping
) {
467 size
-= (int)*grouping
++;
468 /* no more grouping ? */
469 if (*grouping
== CHAR_MAX
)
471 /* rest grouping with same value ? */
472 if (*grouping
== 0) {
473 chars
+= (size
- 1) / *(grouping
- 1);
480 /* convert double to ASCII */
482 __format_grouped_double(double value
, int *flags
,
483 int left_prec
, int right_prec
, int pad_char
) {
495 struct lconv
*lc
= localeconv();
502 grouping
= lc
->mon_grouping
;
503 decimal_point
= *lc
->mon_decimal_point
;
504 if (decimal_point
== '\0')
505 decimal_point
= *lc
->decimal_point
;
506 thousands_sep
= *lc
->mon_thousands_sep
;
507 if (thousands_sep
== '\0')
508 thousands_sep
= *lc
->thousands_sep
;
510 /* fill left_prec with default value */
514 /* fill right_prec with default value */
515 if (right_prec
== -1) {
516 if (*flags
& USE_INTL_CURRENCY
)
517 right_prec
= lc
->int_frac_digits
;
519 right_prec
= lc
->frac_digits
;
521 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
525 if (*flags
& NEED_GROUPING
)
526 left_prec
+= get_groups(left_prec
, grouping
);
528 /* convert to string */
529 snprintf(fmt
, sizeof(fmt
), "%%%d.%df", left_prec
+ right_prec
+ 1,
531 avalue_size
= asprintf(&avalue
, fmt
, value
);
535 /* make sure that we've enough space for result string */
536 bufsize
= strlen(avalue
)*2+1;
537 rslt
= malloc(bufsize
);
542 memset(rslt
, 0, bufsize
);
543 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
545 /* skip spaces at beggining */
547 while (avalue
[padded
] == ' ') {
552 if (right_prec
> 0) {
553 bufend
-= right_prec
;
554 memcpy(bufend
, avalue
+ avalue_size
+padded
-right_prec
,
556 *--bufend
= decimal_point
;
557 avalue_size
-= (right_prec
+ 1);
560 if ((*flags
& NEED_GROUPING
) &&
561 thousands_sep
!= '\0' && /* XXX: need investigation */
562 *grouping
!= CHAR_MAX
&&
564 while (avalue_size
> (int)*grouping
) {
569 /* no more grouping ? */
570 if (*grouping
== CHAR_MAX
)
573 /* rest grouping with same value ? */
574 if (*grouping
== 0) {
576 while (avalue_size
> *grouping
) {
582 if (avalue_size
!= 0)
587 bufend
-= avalue_size
;
588 memcpy(bufend
, avalue
+padded
, avalue_size
);
590 padded
--; /* decrease assumed $decimal_point */
593 /* do padding with pad_char */
596 memset(bufend
, pad_char
, padded
);
599 bufsize
= bufsize
- (bufend
- rslt
) + 1;
600 memmove(rslt
, bufend
, bufsize
);