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