Annotation of src/lib/libc/stdlib/strfmon.c, Revision 1.4.14.1
1.4.14.1! bouyer 1: /* $NetBSD$ */
1.1 christos 2:
3: /*-
4: * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org>
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
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.
15: *
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
26: * SUCH DAMAGE.
27: *
28: */
29:
30: #include <sys/cdefs.h>
31: #if defined(LIBC_SCCS) && !defined(lint)
32: #if 0
33: __FBSDID("$FreeBSD: src/lib/libc/stdlib/strfmon.c,v 1.14 2003/03/20 08:18:55 ache Exp $");
34: #else
1.4.14.1! bouyer 35: __RCSID("$NetBSD$");
1.1 christos 36: #endif
37: #endif /* LIBC_SCCS and not lint */
38:
39: #if defined(__NetBSD__)
40: #include "namespace.h"
41: #include <monetary.h>
42: #endif
43:
44: #include <sys/types.h>
45: #include <ctype.h>
46: #include <errno.h>
47: #include <limits.h>
48: #include <locale.h>
49: #include <stdarg.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53:
54: /* internal flags */
55: #define NEED_GROUPING 0x01 /* print digits grouped (default) */
56: #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */
57: #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */
58: #define PARENTH_POSN 0x08 /* enclose negative amount in () */
59: #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */
60: #define LEFT_JUSTIFY 0x20 /* left justify */
61: #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */
62: #define IS_NEGATIVE 0x80 /* is argument value negative ? */
63:
64: /* internal macros */
65: #define PRINT(CH) do { \
66: if (dst >= s + maxsize) \
67: goto e2big_error; \
68: *dst++ = CH; \
69: } while (/* CONSTCOND */ 0)
70:
71: #define PRINTS(STR) do { \
1.3 yamt 72: const char *tmps = STR; \
1.1 christos 73: while (*tmps != '\0') \
74: PRINT(*tmps++); \
75: } while (/* CONSTCOND */ 0)
76:
77: #define GET_NUMBER(VAR) do { \
1.4.14.1! bouyer 78: int ovar; \
! 79: ovar = VAR = 0; \
1.1 christos 80: while (isdigit((unsigned char)*fmt)) { \
81: VAR *= 10; \
82: VAR += *fmt - '0'; \
1.4.14.1! bouyer 83: if (ovar > VAR) \
! 84: goto e2big_error; \
! 85: else \
! 86: ovar = VAR; \
1.1 christos 87: fmt++; \
88: } \
89: } while (/* CONSTCOND */ 0)
90:
91: #define GRPCPY(howmany) do { \
92: int i = howmany; \
93: while (i-- > 0) { \
94: avalue_size--; \
95: *--bufend = *(avalue+avalue_size+padded); \
96: } \
97: } while (/* CONSTCOND */ 0)
98:
99: #define GRPSEP do { \
100: *--bufend = thousands_sep; \
101: groups++; \
102: } while (/* CONSTCOND */ 0)
103:
1.3 yamt 104: static void __setup_vars(int, char *, char *, char *, const char **);
1.1 christos 105: static int __calc_left_pad(int, char *);
106: static char *__format_grouped_double(double, int *, int, int, int);
107:
108: ssize_t
109: strfmon(char * __restrict s, size_t maxsize, const char * __restrict format,
110: ...)
111: {
112: va_list ap;
113: char *dst; /* output destination pointer */
114: const char *fmt; /* current format poistion pointer */
115: struct lconv *lc; /* pointer to lconv structure */
116: char *asciivalue; /* formatted double pointer */
117:
118: int flags; /* formatting options */
119: int pad_char; /* padding character */
120: int pad_size; /* pad size */
121: int width; /* field width */
122: int left_prec; /* left precision */
123: int right_prec; /* right precision */
124: double value; /* just value */
125: char space_char = ' '; /* space after currency */
126:
127: char cs_precedes, /* values gathered from struct lconv */
128: sep_by_space,
129: sign_posn,
130: *currency_symbol;
1.3 yamt 131: const char *signstr;
1.1 christos 132:
133: char *tmpptr; /* temporary vars */
134: int sverrno;
135:
136: va_start(ap, format);
137:
138: lc = localeconv();
139: dst = s;
140: fmt = format;
141: asciivalue = NULL;
142: currency_symbol = NULL;
143: pad_size = 0;
144:
145: while (*fmt) {
146: /* pass nonformating characters AS IS */
147: if (*fmt != '%')
148: goto literal;
149:
150: /* '%' found ! */
151:
152: /* "%%" mean just '%' */
153: if (*(fmt+1) == '%') {
154: fmt++;
155: literal:
156: PRINT(*fmt++);
157: continue;
158: }
159:
160: /* set up initial values */
161: flags = (NEED_GROUPING|LOCALE_POSN);
162: pad_char = ' '; /* padding character is "space" */
163: left_prec = -1; /* no left precision specified */
164: right_prec = -1; /* no right precision specified */
165: width = -1; /* no width specified */
166: value = 0; /* we have no value to print now */
167:
168: /* Flags */
169: while (/* CONSTCOND */ 1) {
170: switch (*++fmt) {
171: case '=': /* fill character */
172: pad_char = *++fmt;
173: if (pad_char == '\0')
174: goto format_error;
175: continue;
176: case '^': /* not group currency */
177: flags &= ~(NEED_GROUPING);
178: continue;
179: case '+': /* use locale defined signs */
180: if (flags & SIGN_POSN_USED)
181: goto format_error;
182: flags |= (SIGN_POSN_USED|LOCALE_POSN);
183: continue;
184: case '(': /* enclose negatives with () */
185: if (flags & SIGN_POSN_USED)
186: goto format_error;
187: flags |= (SIGN_POSN_USED|PARENTH_POSN);
188: continue;
189: case '!': /* suppress currency symbol */
190: flags |= SUPRESS_CURR_SYMBOL;
191: continue;
192: case '-': /* alignment (left) */
193: flags |= LEFT_JUSTIFY;
194: continue;
195: default:
196: break;
197: }
198: break;
199: }
200:
201: /* field Width */
202: if (isdigit((unsigned char)*fmt)) {
203: GET_NUMBER(width);
204: /* Do we have enough space to put number with
205: * required width ?
206: */
207: if (dst + width >= s + maxsize)
208: goto e2big_error;
209: }
210:
211: /* Left precision */
212: if (*fmt == '#') {
213: if (!isdigit((unsigned char)*++fmt))
214: goto format_error;
215: GET_NUMBER(left_prec);
216: }
217:
218: /* Right precision */
219: if (*fmt == '.') {
220: if (!isdigit((unsigned char)*++fmt))
221: goto format_error;
222: GET_NUMBER(right_prec);
223: }
224:
225: /* Conversion Characters */
226: switch (*fmt++) {
227: case 'i': /* use internaltion currency format */
228: flags |= USE_INTL_CURRENCY;
229: break;
230: case 'n': /* use national currency format */
231: flags &= ~(USE_INTL_CURRENCY);
232: break;
233: default: /* required character is missing or
234: premature EOS */
235: goto format_error;
236: }
237:
1.4 christos 238: if (currency_symbol)
239: free(currency_symbol);
1.1 christos 240: if (flags & USE_INTL_CURRENCY) {
241: currency_symbol = strdup(lc->int_curr_symbol);
242: if (currency_symbol != NULL)
243: space_char = *(currency_symbol+3);
244: } else
245: currency_symbol = strdup(lc->currency_symbol);
246:
247: if (currency_symbol == NULL)
248: goto end_error; /* ENOMEM. */
249:
250: /* value itself */
251: value = va_arg(ap, double);
252:
253: /* detect sign */
254: if (value < 0) {
255: flags |= IS_NEGATIVE;
256: value = -value;
257: }
258:
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) -
263: __calc_left_pad(flags, currency_symbol);
264: if (pad_size < 0)
265: pad_size = 0;
266: }
267:
268: asciivalue = __format_grouped_double(value, &flags,
269: left_prec, right_prec, pad_char);
270: if (asciivalue == NULL)
271: goto end_error; /* errno already set */
272: /* to ENOMEM by malloc() */
273:
274: /* set some variables for later use */
275: __setup_vars(flags, &cs_precedes, &sep_by_space,
276: &sign_posn, &signstr);
277:
278: /*
279: * Description of some LC_MONETARY's values:
280: *
281: * p_cs_precedes & n_cs_precedes
282: *
283: * = 1 - $currency_symbol precedes the value
284: * for a monetary quantity with a non-negative value
285: * = 0 - symbol succeeds the value
286: *
287: * p_sep_by_space & n_sep_by_space
288: *
289: * = 0 - no space separates $currency_symbol
290: * from the value for a monetary quantity with a
291: * non-negative value
292: * = 1 - space separates the symbol from the value
293: * = 2 - space separates the symbol and the sign string,
294: * if adjacent.
295: *
296: * p_sign_posn & n_sign_posn
297: *
298: * = 0 - parentheses enclose the quantity and the
299: * $currency_symbol
300: * = 1 - the sign string precedes the quantity and the
301: * $currency_symbol
302: * = 2 - the sign string succeeds the quantity and the
303: * $currency_symbol
304: * = 3 - the sign string precedes the $currency_symbol
305: * = 4 - the sign string succeeds the $currency_symbol
306: *
307: */
308:
309: tmpptr = dst;
310:
311: while (pad_size-- > 0)
312: PRINT(' ');
313:
314: if (sign_posn == 0 && (flags & IS_NEGATIVE))
315: PRINT('(');
316:
317: if (cs_precedes == 1) {
318: if (sign_posn == 1 || sign_posn == 3) {
319: PRINTS(signstr);
320: if (sep_by_space == 2) /* XXX: ? */
321: PRINT(' ');
322: }
323:
324: if (!(flags & SUPRESS_CURR_SYMBOL)) {
325: PRINTS(currency_symbol);
326:
327: if (sign_posn == 4) {
328: if (sep_by_space == 2)
329: PRINT(space_char);
330: PRINTS(signstr);
331: if (sep_by_space == 1)
332: PRINT(' ');
333: } else if (sep_by_space == 1)
334: PRINT(space_char);
335: }
336: } else if (sign_posn == 1)
337: PRINTS(signstr);
338:
339: PRINTS(asciivalue);
340:
341: if (cs_precedes == 0) {
342: if (sign_posn == 3) {
343: if (sep_by_space == 1)
344: PRINT(' ');
345: PRINTS(signstr);
346: }
347:
348: if (!(flags & SUPRESS_CURR_SYMBOL)) {
349: if ((sign_posn == 3 && sep_by_space == 2)
350: || (sep_by_space == 1
351: && (sign_posn == 0
352: || sign_posn == 1
353: || sign_posn == 2
354: || sign_posn == 4)))
355: PRINT(space_char);
356: PRINTS(currency_symbol); /* XXX: len */
357: if (sign_posn == 4) {
358: if (sep_by_space == 2)
359: PRINT(' ');
360: PRINTS(signstr);
361: }
362: }
363: }
364:
365: if (sign_posn == 2) {
366: if (sep_by_space == 2)
367: PRINT(' ');
368: PRINTS(signstr);
369: }
370:
371: if (sign_posn == 0 && (flags & IS_NEGATIVE))
372: PRINT(')');
373:
374: if (dst - tmpptr < width) {
375: if (flags & LEFT_JUSTIFY) {
376: while (dst - tmpptr < width)
377: PRINT(' ');
378: } else {
379: pad_size = dst-tmpptr;
380: memmove(tmpptr + width-pad_size, tmpptr,
381: (size_t) pad_size);
382: memset(tmpptr, ' ', (size_t) width-pad_size);
383: dst += width-pad_size;
384: }
385: }
386: }
387:
388: PRINT('\0');
389: va_end(ap);
390: free(asciivalue);
391: free(currency_symbol);
392: return (dst - s - 1); /* return size of put data except trailing '\0' */
393:
394: e2big_error:
395: errno = E2BIG;
396: goto end_error;
397:
398: format_error:
399: errno = EINVAL;
400:
401: end_error:
402: sverrno = errno;
403: if (asciivalue != NULL)
404: free(asciivalue);
405: if (currency_symbol != NULL)
406: free(currency_symbol);
407: errno = sverrno;
408: va_end(ap);
409: return (-1);
410: }
411:
412: static void
413: __setup_vars(int flags, char *cs_precedes, char *sep_by_space,
1.3 yamt 414: char *sign_posn, const char **signstr) {
1.1 christos 415: struct lconv *lc = localeconv();
416:
417: if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) {
418: *cs_precedes = lc->int_n_cs_precedes;
419: *sep_by_space = lc->int_n_sep_by_space;
420: *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn;
1.3 yamt 421: *signstr = (lc->negative_sign == '\0') ? "-"
1.1 christos 422: : lc->negative_sign;
423: } else if (flags & USE_INTL_CURRENCY) {
424: *cs_precedes = lc->int_p_cs_precedes;
425: *sep_by_space = lc->int_p_sep_by_space;
426: *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn;
427: *signstr = lc->positive_sign;
428: } else if (flags & IS_NEGATIVE) {
429: *cs_precedes = lc->n_cs_precedes;
430: *sep_by_space = lc->n_sep_by_space;
431: *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn;
1.3 yamt 432: *signstr = (lc->negative_sign == '\0') ? "-"
1.1 christos 433: : lc->negative_sign;
434: } else {
435: *cs_precedes = lc->p_cs_precedes;
436: *sep_by_space = lc->p_sep_by_space;
437: *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn;
438: *signstr = lc->positive_sign;
439: }
440:
441: /* Set defult values for unspecified information. */
442: if (*cs_precedes != 0)
443: *cs_precedes = 1;
444: if (*sep_by_space == CHAR_MAX)
445: *sep_by_space = 0;
446: if (*sign_posn == CHAR_MAX)
447: *sign_posn = 0;
448: }
449:
450: static int
451: __calc_left_pad(int flags, char *cur_symb) {
452:
1.3 yamt 453: char cs_precedes, sep_by_space, sign_posn;
454: const char *signstr;
1.1 christos 455: int left_chars = 0;
456:
457: __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr);
458:
459: if (cs_precedes != 0) {
460: left_chars += strlen(cur_symb);
461: if (sep_by_space != 0)
462: left_chars++;
463: }
464:
465: switch (sign_posn) {
466: case 1:
467: left_chars += strlen(signstr);
468: break;
469: case 3:
470: case 4:
471: if (cs_precedes != 0)
472: left_chars += strlen(signstr);
473: }
474: return (left_chars);
475: }
476:
477: static int
478: get_groups(int size, char *grouping) {
479:
480: int chars = 0;
481:
482: if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */
483: return (0);
484:
485: while (size > (int)*grouping) {
486: chars++;
487: size -= (int)*grouping++;
488: /* no more grouping ? */
489: if (*grouping == CHAR_MAX)
490: break;
491: /* rest grouping with same value ? */
492: if (*grouping == 0) {
493: chars += (size - 1) / *(grouping - 1);
494: break;
495: }
496: }
497: return (chars);
498: }
499:
500: /* convert double to ASCII */
501: static char *
502: __format_grouped_double(double value, int *flags,
503: int left_prec, int right_prec, int pad_char) {
504:
505: char *rslt;
506: char *avalue;
507: int avalue_size;
508: char fmt[32];
509:
510: size_t bufsize;
511: char *bufend;
512:
513: int padded;
514:
515: struct lconv *lc = localeconv();
516: char *grouping;
517: char decimal_point;
518: char thousands_sep;
519:
520: int groups = 0;
521:
522: grouping = lc->mon_grouping;
523: decimal_point = *lc->mon_decimal_point;
524: if (decimal_point == '\0')
525: decimal_point = *lc->decimal_point;
526: thousands_sep = *lc->mon_thousands_sep;
527: if (thousands_sep == '\0')
528: thousands_sep = *lc->thousands_sep;
529:
530: /* fill left_prec with default value */
531: if (left_prec == -1)
532: left_prec = 0;
533:
534: /* fill right_prec with default value */
535: if (right_prec == -1) {
536: if (*flags & USE_INTL_CURRENCY)
537: right_prec = lc->int_frac_digits;
538: else
539: right_prec = lc->frac_digits;
540:
541: if (right_prec == CHAR_MAX) /* POSIX locale ? */
542: right_prec = 2;
543: }
544:
545: if (*flags & NEED_GROUPING)
546: left_prec += get_groups(left_prec, grouping);
547:
548: /* convert to string */
549: snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1,
550: right_prec);
551: avalue_size = asprintf(&avalue, fmt, value);
552: if (avalue_size < 0)
553: return (NULL);
554:
555: /* make sure that we've enough space for result string */
556: bufsize = strlen(avalue)*2+1;
557: rslt = malloc(bufsize);
558: if (rslt == NULL) {
559: free(avalue);
560: return (NULL);
561: }
562: memset(rslt, 0, bufsize);
563: bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */
564:
565: /* skip spaces at beggining */
566: padded = 0;
567: while (avalue[padded] == ' ') {
568: padded++;
569: avalue_size--;
570: }
571:
572: if (right_prec > 0) {
573: bufend -= right_prec;
574: memcpy(bufend, avalue + avalue_size+padded-right_prec,
575: (size_t) right_prec);
576: *--bufend = decimal_point;
577: avalue_size -= (right_prec + 1);
578: }
579:
580: if ((*flags & NEED_GROUPING) &&
581: thousands_sep != '\0' && /* XXX: need investigation */
582: *grouping != CHAR_MAX &&
583: *grouping > 0) {
584: while (avalue_size > (int)*grouping) {
585: GRPCPY(*grouping);
586: GRPSEP;
587: grouping++;
588:
589: /* no more grouping ? */
590: if (*grouping == CHAR_MAX)
591: break;
592:
593: /* rest grouping with same value ? */
594: if (*grouping == 0) {
595: grouping--;
596: while (avalue_size > *grouping) {
597: GRPCPY(*grouping);
598: GRPSEP;
599: }
600: }
601: }
602: if (avalue_size != 0)
603: GRPCPY(avalue_size);
604: padded -= groups;
605:
606: } else {
607: bufend -= avalue_size;
608: memcpy(bufend, avalue+padded, (size_t) avalue_size);
609: if (right_prec == 0)
610: padded--; /* decrease assumed $decimal_point */
611: }
612:
613: /* do padding with pad_char */
614: if (padded > 0) {
615: bufend -= padded;
616: memset(bufend, pad_char, (size_t) padded);
617: }
618:
619: bufsize = bufsize - (bufend - rslt) + 1;
620: memmove(rslt, bufend, bufsize);
621: free(avalue);
622: return (rslt);
623: }
CVSweb <webmaster@jp.NetBSD.org>