]>
git.saurik.com Git - apple/libc.git/blob - stdlib/NetBSD/strfmon.c
1 /* $NetBSD: strfmon.c,v 1.7 2009/01/30 23:46:03 lukem Exp $ */
4 * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
33 __FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $");
35 __RCSID("$NetBSD: strfmon.c,v 1.7 2009/01/30 23:46:03 lukem Exp $");
37 #endif /* LIBC_SCCS and not lint */
39 #if defined(__NetBSD__)
40 #include "namespace.h"
44 #include "xlocale_private.h"
46 #include <sys/types.h>
58 #define NEED_GROUPING 0x01 /* print digits grouped (default) */
59 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
60 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
61 #define PARENTH_POSN 0x08 /* enclose negative amount in () */
62 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
63 #define LEFT_JUSTIFY 0x20 /* left justify */
64 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
65 #define IS_NEGATIVE 0x80 /* is argument value negative ? */
68 #define PRINT(CH) do { \
69 if (dst >= s + maxsize) \
72 } while (/* CONSTCOND */ 0)
74 #define PRINTS(STR) do { \
75 const char *tmps = STR; \
76 while (*tmps != '\0') \
78 } while (/* CONSTCOND */ 0)
80 #define GET_NUMBER(VAR,LOC) do { \
82 while (isdigit_l((unsigned char)*fmt, (LOC))) { \
85 if (VAR > 0x00ffffff) \
89 } while (/* CONSTCOND */ 0)
91 #define GRPCPY(howmany) do { \
95 *--bufend = *(avalue+avalue_size+padded); \
97 } while (/* CONSTCOND */ 0)
100 *--bufend = thousands_sep; \
102 } while (/* CONSTCOND */ 0)
104 static void __setup_vars(int, char *, char *, char *, const char **, struct lconv
*);
105 static int __calc_left_pad(int, char *, struct lconv
*);
106 static char *__format_grouped_double(double, int *, int, int, int, struct lconv
*, locale_t
);
109 _strfmon(char * __restrict s
, size_t maxsize
, locale_t loc
, const char * __restrict format
, va_list ap
)
111 char *dst
; /* output destination pointer */
112 const char *fmt
; /* current format poistion pointer */
113 struct lconv
*lc
; /* pointer to lconv structure */
114 char *asciivalue
; /* formatted double pointer */
116 int flags
; /* formatting options */
117 int pad_char
; /* padding character */
118 int pad_size
; /* pad size */
119 int width
; /* field width */
120 int left_prec
; /* left precision */
121 int right_prec
; /* right precision */
122 double value
; /* just value */
123 char space_char
= ' '; /* space after currency */
125 char cs_precedes
, /* values gathered from struct lconv */
131 char *tmpptr
; /* temporary vars */
134 lc
= localeconv_l(loc
);
138 currency_symbol
= NULL
;
142 /* pass nonformating characters AS IS */
148 /* "%%" mean just '%' */
149 if (*(fmt
+1) == '%') {
156 /* set up initial values */
157 flags
= (NEED_GROUPING
|LOCALE_POSN
);
158 pad_char
= ' '; /* padding character is "space" */
159 left_prec
= -1; /* no left precision specified */
160 right_prec
= -1; /* no right precision specified */
161 width
= -1; /* no width specified */
162 value
= 0; /* we have no value to print now */
165 while (/* CONSTCOND */ 1) {
167 case '=': /* fill character */
169 if (pad_char
== '\0')
172 case '^': /* not group currency */
173 flags
&= ~(NEED_GROUPING
);
175 case '+': /* use locale defined signs */
176 if (flags
& SIGN_POSN_USED
)
178 flags
|= (SIGN_POSN_USED
|LOCALE_POSN
);
180 case '(': /* enclose negatives with () */
181 if (flags
& SIGN_POSN_USED
)
183 flags
|= (SIGN_POSN_USED
|PARENTH_POSN
);
185 case '!': /* suppress currency symbol */
186 flags
|= SUPRESS_CURR_SYMBOL
;
188 case '-': /* alignment (left) */
189 flags
|= LEFT_JUSTIFY
;
198 if (isdigit_l((unsigned char)*fmt
, loc
)) {
199 ptrdiff_t d
= dst
- s
;
200 GET_NUMBER(width
, loc
);
201 /* Do we have enough space to put number with
205 if ((size_t)(d
+ width
) >= maxsize
)
211 if (!isdigit_l((unsigned char)*++fmt
, loc
))
213 GET_NUMBER(left_prec
, loc
);
216 /* Right precision */
218 if (!isdigit_l((unsigned char)*++fmt
, loc
))
220 GET_NUMBER(right_prec
, loc
);
223 /* Conversion Characters */
225 case 'i': /* use internaltion currency format */
226 flags
|= USE_INTL_CURRENCY
;
228 case 'n': /* use national currency format */
229 flags
&= ~(USE_INTL_CURRENCY
);
231 default: /* required character is missing or
237 free(currency_symbol
);
238 if (flags
& USE_INTL_CURRENCY
) {
239 currency_symbol
= strdup(lc
->int_curr_symbol
);
240 if (currency_symbol
!= NULL
) {
241 space_char
= *(currency_symbol
+3);
242 currency_symbol
[3] = '\0';
245 currency_symbol
= strdup(lc
->currency_symbol
);
247 if (currency_symbol
== NULL
)
248 goto end_error
; /* ENOMEM. */
251 value
= va_arg(ap
, double);
255 flags
|= IS_NEGATIVE
;
259 /* fill left_prec with amount of padding chars */
260 if (left_prec
>= 0) {
261 pad_size
= __calc_left_pad((flags
^ IS_NEGATIVE
),
262 currency_symbol
, lc
) -
263 __calc_left_pad(flags
, currency_symbol
, lc
);
268 asciivalue
= __format_grouped_double(value
, &flags
,
269 left_prec
, right_prec
, pad_char
, lc
, loc
);
270 if (asciivalue
== NULL
)
271 goto end_error
; /* errno already set */
272 /* to ENOMEM by malloc() */
274 /* set some variables for later use */
275 __setup_vars(flags
, &cs_precedes
, &sep_by_space
,
276 &sign_posn
, &signstr
, lc
);
279 * Description of some LC_MONETARY's values:
281 * p_cs_precedes & n_cs_precedes
283 * = 1 - $currency_symbol precedes the value
284 * for a monetary quantity with a non-negative value
285 * = 0 - symbol succeeds the value
287 * p_sep_by_space & n_sep_by_space
289 * = 0 - no space separates $currency_symbol
290 * from the value for a monetary quantity with a
292 * = 1 - space separates the symbol from the value
293 * = 2 - space separates the symbol and the sign string,
296 * p_sign_posn & n_sign_posn
298 * = 0 - parentheses enclose the quantity and the
300 * = 1 - the sign string precedes the quantity and the
302 * = 2 - the sign string succeeds the quantity and the
304 * = 3 - the sign string precedes the $currency_symbol
305 * = 4 - the sign string succeeds the $currency_symbol
311 while (pad_size
-- > 0)
314 if (sign_posn
== 0 && (flags
& IS_NEGATIVE
))
317 if (cs_precedes
== 1) {
318 if (sign_posn
== 1 || sign_posn
== 3) {
320 if (sep_by_space
== 2) /* XXX: ? */
324 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
325 PRINTS(currency_symbol
);
327 if (sign_posn
== 4) {
328 if (sep_by_space
== 2)
331 if (sep_by_space
== 1)
333 } else if (sep_by_space
== 1)
336 } else if (sign_posn
== 1) {
338 if (sep_by_space
== 2)
344 if (cs_precedes
== 0) {
345 if (sign_posn
== 3) {
346 if (sep_by_space
== 1)
351 if (!(flags
& SUPRESS_CURR_SYMBOL
)) {
352 if ((sign_posn
== 3 && sep_by_space
== 2)
353 || (sep_by_space
== 1
359 PRINTS(currency_symbol
); /* XXX: len */
360 if (sign_posn
== 4) {
361 if (sep_by_space
== 2)
368 if (sign_posn
== 2) {
369 if (sep_by_space
== 2)
374 if (sign_posn
== 0) {
375 if (flags
& IS_NEGATIVE
)
377 else if (left_prec
>= 0)
381 if (dst
- tmpptr
< width
) {
382 if (flags
& LEFT_JUSTIFY
) {
383 while (dst
- tmpptr
< width
)
386 pad_size
= dst
-tmpptr
;
387 memmove(tmpptr
+ width
-pad_size
, tmpptr
,
389 memset(tmpptr
, ' ', (size_t) width
-pad_size
);
390 dst
+= width
-pad_size
;
397 free(currency_symbol
);
398 return (dst
- s
- 1); /* return size of put data except trailing '\0' */
409 if (asciivalue
!= NULL
)
411 if (currency_symbol
!= NULL
)
412 free(currency_symbol
);
418 __setup_vars(int flags
, char *cs_precedes
, char *sep_by_space
,
419 char *sign_posn
, const char **signstr
, struct lconv
*lc
) {
421 if ((flags
& IS_NEGATIVE
) && (flags
& USE_INTL_CURRENCY
)) {
422 *cs_precedes
= lc
->int_n_cs_precedes
;
423 *sep_by_space
= lc
->int_n_sep_by_space
;
424 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_n_sign_posn
;
425 *signstr
= (lc
->negative_sign
== '\0') ? "-"
427 } else if (flags
& USE_INTL_CURRENCY
) {
428 *cs_precedes
= lc
->int_p_cs_precedes
;
429 *sep_by_space
= lc
->int_p_sep_by_space
;
430 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->int_p_sign_posn
;
431 *signstr
= lc
->positive_sign
;
432 } else if (flags
& IS_NEGATIVE
) {
433 *cs_precedes
= lc
->n_cs_precedes
;
434 *sep_by_space
= lc
->n_sep_by_space
;
435 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->n_sign_posn
;
436 *signstr
= (lc
->negative_sign
== '\0') ? "-"
439 *cs_precedes
= lc
->p_cs_precedes
;
440 *sep_by_space
= lc
->p_sep_by_space
;
441 *sign_posn
= (flags
& PARENTH_POSN
) ? 0 : lc
->p_sign_posn
;
442 *signstr
= lc
->positive_sign
;
445 /* Set defult values for unspecified information. */
446 if (*cs_precedes
!= 0)
448 if (*sep_by_space
== CHAR_MAX
)
450 if (*sign_posn
== CHAR_MAX
)
455 __calc_left_pad(int flags
, char *cur_symb
, struct lconv
*lc
) {
457 char cs_precedes
, sep_by_space
, sign_posn
;
461 __setup_vars(flags
, &cs_precedes
, &sep_by_space
, &sign_posn
, &signstr
, lc
);
463 if (cs_precedes
!= 0) {
464 left_chars
+= strlen(cur_symb
);
465 if (sep_by_space
!= 0)
471 if (flags
& IS_NEGATIVE
)
475 left_chars
+= strlen(signstr
);
479 if (cs_precedes
!= 0)
480 left_chars
+= strlen(signstr
);
486 get_groups(int size
, const char *grouping
) {
490 if (*grouping
== CHAR_MAX
|| *grouping
<= 0) /* no grouping ? */
493 while (size
> (int)*grouping
) {
495 size
-= (int)*grouping
++;
496 /* no more grouping ? */
497 if (*grouping
== CHAR_MAX
)
499 /* rest grouping with same value ? */
500 if (*grouping
== 0) {
501 chars
+= (size
- 1) / *(grouping
- 1);
508 /* convert double to ASCII */
509 __private_extern__
const char *__fix_nogrouping(const char *);
512 __format_grouped_double(double value
, int *flags
,
513 int left_prec
, int right_prec
, int pad_char
, struct lconv
*lc
, locale_t loc
) {
525 const char *grouping
;
531 grouping
= __fix_nogrouping(lc
->mon_grouping
);
532 decimal_point
= *lc
->mon_decimal_point
;
533 if (decimal_point
== '\0')
534 decimal_point
= *lc
->decimal_point
;
535 thousands_sep
= *lc
->mon_thousands_sep
;
536 if (thousands_sep
== '\0')
537 thousands_sep
= *lc
->thousands_sep
;
539 /* fill left_prec with default value */
543 /* fill right_prec with default value */
544 if (right_prec
== -1) {
545 if (*flags
& USE_INTL_CURRENCY
)
546 right_prec
= lc
->int_frac_digits
;
548 right_prec
= lc
->frac_digits
;
550 if (right_prec
== CHAR_MAX
) /* POSIX locale ? */
554 if (*flags
& NEED_GROUPING
)
555 left_prec
+= get_groups(left_prec
, grouping
);
557 /* convert to string */
558 snprintf_l(fmt
, sizeof(fmt
), loc
, "%%%d.%df", left_prec
+ right_prec
+ 1,
560 avalue_size
= asprintf_l(&avalue
, loc
, fmt
, value
);
564 /* make sure that we've enough space for result string */
565 bufsize
= strlen(avalue
)*2+1;
566 rslt
= malloc(bufsize
);
571 memset(rslt
, 0, bufsize
);
572 bufend
= rslt
+ bufsize
- 1; /* reserve space for trailing '\0' */
574 /* skip spaces at beggining */
576 while (avalue
[padded
] == ' ') {
581 if (right_prec
> 0) {
582 bufend
-= right_prec
;
583 memcpy(bufend
, avalue
+ avalue_size
+padded
-right_prec
,
584 (size_t) right_prec
);
585 *--bufend
= decimal_point
;
586 avalue_size
-= (right_prec
+ 1);
589 if ((*flags
& NEED_GROUPING
) &&
590 thousands_sep
!= '\0' && /* XXX: need investigation */
591 *grouping
!= CHAR_MAX
&&
593 while (avalue_size
> (int)*grouping
) {
598 /* no more grouping ? */
599 if (*grouping
== CHAR_MAX
)
602 /* rest grouping with same value ? */
603 if (*grouping
== 0) {
605 while (avalue_size
> *grouping
) {
611 if (avalue_size
!= 0)
616 bufend
-= avalue_size
;
617 memcpy(bufend
, avalue
+padded
, (size_t) avalue_size
);
619 padded
--; /* decrease assumed $decimal_point */
622 /* do padding with pad_char */
625 memset(bufend
, pad_char
, (size_t) padded
);
628 bufsize
= bufsize
- (bufend
- rslt
) + 1;
629 memmove(rslt
, bufend
, bufsize
);
635 strfmon(char * __restrict s
, size_t maxsize
, const char * __restrict format
,
641 va_start(ap
, format
);
642 ret
= _strfmon(s
, maxsize
, __current_locale(), format
, ap
);
648 strfmon_l(char * __restrict s
, size_t maxsize
, locale_t loc
,
649 const char * __restrict format
, ...)
654 NORMALIZE_LOCALE(loc
);
655 va_start(ap
, format
);
656 ret
= _strfmon(s
, maxsize
, loc
, format
, ap
);