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