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