]>
git.saurik.com Git - apple/libc.git/blob - stdlib/strfmon-fbsd.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 "xlocale_private.h"
33 #include <sys/types.h>
44 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
45 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
46 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
47 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
48 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
49 #define LEFT_JUSTIFY 0x20 /* left justify */
50 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
51 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
54 #define PRINT(CH) do { \
55 if (dst >= s + maxsize) \
60 #define PRINTS(STR) do { \
62 while (*tmps != '\0') \
66 #define GET_NUMBER(VAR,LOC) do { \
68 while (isdigit_l((unsigned char)*fmt, (LOC))) { \
75 #define GRPCPY(howmany) do { \
79 *--bufend = *(avalue+avalue_size+padded); \
84 *--bufend = thousands_sep; \
88 static void __setup_vars(int, char *, char *, char *, char **, struct lconv
*);
89 static int __calc_left_pad(int, char *, struct lconv
*);
90 static char *__format_grouped_double(double, int *, int, int, int, struct lconv
*, locale_t
);
93 _strfmon(char * __restrict s
, size_t maxsize
, locale_t loc
,
94 const char * __restrict format
, va_list ap
)
96 char *dst
; /* output destination pointer */
97 const char *fmt
; /* current format poistion pointer */
98 struct lconv
*lc
; /* pointer to lconv structure */
99 char *asciivalue
; /* formatted double pointer */
101 int flags
; /* formatting options */
102 int pad_char
; /* padding character */
103 int pad_size
; /* pad size */
104 int width
; /* field width */
105 int left_prec
; /* left precision */
106 int right_prec
; /* right precision */
107 double value
; /* just value */
108 char space_char
= ' '; /* space after currency */
110 char cs_precedes
, /* values gathered from struct lconv */
116 char *tmpptr
; /* temporary vars */
119 lc
= localeconv_l(loc
);
123 currency_symbol
= NULL
;
127 /* pass nonformating characters AS IS */
133 /* "%%" mean just '%' */
134 if (*(fmt
+1) == '%') {
141 /* set up initial values */
142 flags
= (NEED_GROUPING
|LOCALE_POSN
);
143 pad_char
= ' '; /* padding character is "space" */
144 left_prec
= -1; /* no left precision specified */
145 right_prec
= -1; /* no right precision specified */
146 width
= -1; /* no width specified */
147 value
= 0; /* we have no value to print now */
152 case '=': /* fill character */
154 if (pad_char
== '\0')
157 case '^': /* not group currency */
158 flags
&= ~(NEED_GROUPING
);
160 case '+': /* use locale defined signs */
161 if (flags
& SIGN_POSN_USED
)
163 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
165 case '(': /* enclose negatives with () */
166 if (flags
& SIGN_POSN_USED
)
168 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
170 case '!': /* suppress currency symbol */
171 flags
|= SUPRESS_CURR_SYMBOL
;
173 case '-': /* alignment (left) */
174 flags
|= LEFT_JUSTIFY
;
183 if (isdigit_l((unsigned char)*fmt
, loc
)) {
184 GET_NUMBER(width
, loc
);
185 /* Do we have enough space to put number with
188 if (dst
+ width
>= s
+ maxsize
)
194 if (!isdigit_l((unsigned char)*++fmt
, loc
))
196 GET_NUMBER(left_prec
, loc
);
199 /* Right precision */
201 if (!isdigit_l((unsigned char)*++fmt
, loc
))
203 GET_NUMBER(right_prec
, loc
);
206 /* Conversion Characters */
208 case 'i': /* use internaltion currency format */
209 flags
|= USE_INTL_CURRENCY
;
211 case 'n': /* use national currency format */
212 flags
&= ~(USE_INTL_CURRENCY
);
214 default: /* required character is missing or
219 if (flags
& USE_INTL_CURRENCY
) {
220 currency_symbol
= strdup(lc
->int_curr_symbol
);
221 if (currency_symbol
!= NULL
) {
222 space_char
= *(currency_symbol
+3);
223 currency_symbol
[3] = '\0';
226 currency_symbol
= strdup(lc
->currency_symbol
);
228 if (currency_symbol
== NULL
)
229 goto end_error
; /* ENOMEM. */
232 value
= va_arg(ap
, double);
236 flags
|= IS_NEGATIVE
;
240 /* fill left_prec with amount of padding chars */
241 if (left_prec
>= 0) {
242 pad_size
= __calc_left_pad((flags
^ IS_NEGATIVE
),
243 currency_symbol
, lc
) -
244 __calc_left_pad(flags
, currency_symbol
, lc
);
249 asciivalue
= __format_grouped_double(value
, &flags
,
250 left_prec
, right_prec
, pad_char
, lc
, loc
);
251 if (asciivalue
== NULL
)
252 goto end_error
; /* errno already set */
253 /* to ENOMEM by malloc() */
255 /* set some variables for later use */
256 __setup_vars(flags
, &cs_precedes
, &sep_by_space
,
257 &sign_posn
, &signstr
, lc
);
260 * Description of some LC_MONETARY's values:
262 * p_cs_precedes & n_cs_precedes
264 * = 1 - $currency_symbol precedes the value
265 * for a monetary quantity with a non-negative value
266 * = 0 - symbol succeeds the value
268 * p_sep_by_space & n_sep_by_space
270 * = 0 - no space separates $currency_symbol
271 * from the value for a monetary quantity with a
273 * = 1 - space separates the symbol from the value
274 * = 2 - space separates the symbol and the sign string,
277 * p_sign_posn & n_sign_posn
279 * = 0 - parentheses enclose the quantity and the
281 * = 1 - the sign string precedes the quantity and the
283 * = 2 - the sign string succeeds the quantity and the
285 * = 3 - the sign string precedes the $currency_symbol
286 * = 4 - the sign string succeeds the $currency_symbol
292 while (pad_size
-- > 0)
295 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
298 if (cs_precedes
== 1) {
299 if (sign_posn
== 1 || sign_posn
== 3) {
301 if (sep_by_space
== 2) /* XXX: ? */
305 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
306 PRINTS(currency_symbol
);
308 if (sign_posn
== 4) {
309 if (sep_by_space
== 2)
312 if (sep_by_space
== 1)
314 } else if (sep_by_space
== 1)
317 } else if (sign_posn
== 1) {
319 if (sep_by_space
== 2)
325 if (cs_precedes
== 0) {
326 if (sign_posn
== 3) {
327 if (sep_by_space
== 1)
332 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
333 if ((sign_posn
== 3 && sep_by_space
== 2)
334 || (sep_by_space
== 1
340 PRINTS(currency_symbol
); /* XXX: len */
341 if (sign_posn
== 4) {
342 if (sep_by_space
== 2)
349 if (sign_posn
== 2) {
350 if (sep_by_space
== 2)
355 if (sign_posn
== 0) {
356 if (flags
& IS_NEGATIVE
)
358 else if (left_prec
>= 0)
362 if (dst
- tmpptr
< width
) {
363 if (flags
& LEFT_JUSTIFY
) {
364 while (dst
- tmpptr
< width
)
367 pad_size
= dst
-tmpptr
;
368 memmove(tmpptr
+ width
-pad_size
, tmpptr
,
370 memset(tmpptr
, ' ', width
-pad_size
);
371 dst
+= width
-pad_size
;
378 free(currency_symbol
);
379 return (dst
- s
- 1); /* return size of put data except trailing '\0' */
390 if (asciivalue
!= NULL
)
392 if (currency_symbol
!= NULL
)
393 free(currency_symbol
);
399 __setup_vars(int flags
, char *cs_precedes
, char *sep_by_space
,
400 char *sign_posn
, char **signstr
, struct lconv
*lc
) {
402 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
403 *cs_precedes
= lc
->int_n_cs_precedes
;
404 *sep_by_space
= lc
->int_n_sep_by_space
;
405 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_n_sign_posn
;
406 *signstr
= (lc
->negative_sign
== '\0') ? "-"
408 } else if (flags
& USE_INTL_CURRENCY
) {
409 *cs_precedes
= lc
->int_p_cs_precedes
;
410 *sep_by_space
= lc
->int_p_sep_by_space
;
411 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_p_sign_posn
;
412 *signstr
= lc
->positive_sign
;
413 } else if (flags
& IS_NEGATIVE
) {
414 *cs_precedes
= lc
->n_cs_precedes
;
415 *sep_by_space
= lc
->n_sep_by_space
;
416 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->n_sign_posn
;
417 *signstr
= (lc
->negative_sign
== '\0') ? "-"
420 *cs_precedes
= lc
->p_cs_precedes
;
421 *sep_by_space
= lc
->p_sep_by_space
;
422 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->p_sign_posn
;
423 *signstr
= lc
->positive_sign
;
426 /* Set defult values for unspecified information. */
427 if (*cs_precedes
!= 0)
429 if (*sep_by_space
== CHAR_MAX
)
431 if (*sign_posn
== CHAR_MAX
)
436 __calc_left_pad(int flags
, char *cur_symb
, struct lconv
*lc
) {
438 char cs_precedes
, sep_by_space
, sign_posn
, *signstr
;
441 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
, lc
);
443 if (cs_precedes
!= 0) {
444 left_chars
+= strlen(cur_symb
);
445 if (sep_by_space
!= 0)
451 if (flags
& IS_NEGATIVE
)
455 left_chars
+= strlen(signstr
);
459 if (cs_precedes
!= 0)
460 left_chars
+= strlen(signstr
);
466 get_groups(int size
, char *grouping
) {
470 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
473 while (size
> (int)*grouping
) {
475 size
-= (int)*grouping
++;
476 /* no more grouping ? */
477 if (*grouping
== CHAR_MAX
)
479 /* rest grouping with same value ? */
480 if (*grouping
== 0) {
481 chars
+= (size
- 1) / *(grouping
- 1);
488 /* convert double to ASCII */
489 __private_extern__
const char *__fix_nogrouping(const char *);
492 __format_grouped_double(double value
, int *flags
,
493 int left_prec
, int right_prec
, int pad_char
, struct lconv
*lc
, locale_t loc
) {
511 grouping
= __fix_nogrouping(lc
->mon_grouping
);
512 decimal_point
= *lc
->mon_decimal_point
;
513 if (decimal_point
== '\0')
514 decimal_point
= *lc
->decimal_point
;
515 thousands_sep
= *lc
->mon_thousands_sep
;
516 if (thousands_sep
== '\0')
517 thousands_sep
= *lc
->thousands_sep
;
519 /* fill left_prec with default value */
523 /* fill right_prec with default value */
524 if (right_prec
== -1) {
525 if (*flags
& USE_INTL_CURRENCY
)
526 right_prec
= lc
->int_frac_digits
;
528 right_prec
= lc
->frac_digits
;
530 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
534 if (*flags
& NEED_GROUPING
)
535 left_prec
+= get_groups(left_prec
, grouping
);
537 /* convert to string */
538 snprintf_l(fmt
, sizeof(fmt
), loc
, "%%%d.%df", left_prec
+ right_prec
+ 1,
540 avalue_size
= asprintf_l(&avalue
, loc
, fmt
, value
);
544 /* make sure that we've enough space for result string */
545 bufsize
= strlen(avalue
)*2+1;
546 rslt
= malloc(bufsize
);
551 memset(rslt
, 0, bufsize
);
552 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
554 /* skip spaces at beggining */
556 while (avalue
[padded
] == ' ') {
561 if (right_prec
> 0) {
562 bufend
-= right_prec
;
563 memcpy(bufend
, avalue
+ avalue_size
+padded
-right_prec
,
565 *--bufend
= decimal_point
;
566 avalue_size
-= (right_prec
+ 1);
569 if ((*flags
& NEED_GROUPING
) &&
570 thousands_sep
!= '\0' && /* XXX: need investigation */
571 *grouping
!= CHAR_MAX
&&
573 while (avalue_size
> (int)*grouping
) {
578 /* no more grouping ? */
579 if (*grouping
== CHAR_MAX
)
582 /* rest grouping with same value ? */
583 if (*grouping
== 0) {
585 while (avalue_size
> *grouping
) {
591 if (avalue_size
!= 0)
596 bufend
-= avalue_size
;
597 memcpy(bufend
, avalue
+padded
, avalue_size
);
599 padded
--; /* decrease assumed $decimal_point */
602 /* do padding with pad_char */
605 memset(bufend
, pad_char
, padded
);
608 bufsize
= bufsize
- (bufend
- rslt
) + 1;
609 memmove(rslt
, bufend
, bufsize
);
615 strfmon(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
621 va_start(ap
, format
);
622 ret
= _strfmon(s
, maxsize
, __current_locale(), format
, ap
);
628 strfmon_l(char * __restrict s
, size_t maxsize
, locale_t loc
,
629 const char * __restrict format
, ...)
634 NORMALIZE_LOCALE(loc
);
635 va_start(ap
, format
);
636 ret
= _strfmon(s
, maxsize
, loc
, format
, ap
);