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