strfmon.c revision 150065
1109187Sdillon/*- 2109187Sdillon * Copyright (c) 2001 Alexey Zelkin <phantom@FreeBSD.org> 3109187Sdillon * All rights reserved. 4109187Sdillon * 5109187Sdillon * Redistribution and use in source and binary forms, with or without 6109187Sdillon * modification, are permitted provided that the following conditions 7109187Sdillon * are met: 8109187Sdillon * 1. Redistributions of source code must retain the above copyright 9109187Sdillon * notice, this list of conditions and the following disclaimer. 10109187Sdillon * 2. Redistributions in binary form must reproduce the above copyright 11109187Sdillon * notice, this list of conditions and the following disclaimer in the 12109187Sdillon * documentation and/or other materials provided with the distribution. 13109187Sdillon * 14109187Sdillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15109187Sdillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16109187Sdillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17109187Sdillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18109187Sdillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19109187Sdillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20109187Sdillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21109187Sdillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22109187Sdillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23109187Sdillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24109187Sdillon * SUCH DAMAGE. 25109187Sdillon * 26109187Sdillon */ 27109187Sdillon 28109187Sdillon#include <sys/cdefs.h> 29109187Sdillon__FBSDID("$FreeBSD: head/lib/libc/stdlib/strfmon.c 150065 2005-09-12 19:52:42Z stefanf $"); 30109187Sdillon 31109187Sdillon#include <sys/types.h> 32109187Sdillon#include <ctype.h> 33109187Sdillon#include <errno.h> 34109187Sdillon#include <limits.h> 35109187Sdillon#include <locale.h> 36109187Sdillon#include <monetary.h> 37109187Sdillon#include <stdarg.h> 38109187Sdillon#include <stdio.h> 39109187Sdillon#include <stdlib.h> 40109187Sdillon#include <string.h> 41109187Sdillon 42109187Sdillon/* internal flags */ 43109187Sdillon#define NEED_GROUPING 0x01 /* print digits grouped (default) */ 44109187Sdillon#define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 45109187Sdillon#define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 46109187Sdillon#define PARENTH_POSN 0x08 /* enclose negative amount in () */ 47109187Sdillon#define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 48109187Sdillon#define LEFT_JUSTIFY 0x20 /* left justify */ 49109187Sdillon#define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 50109187Sdillon#define IS_NEGATIVE 0x80 /* is argument value negative ? */ 51109187Sdillon 52109187Sdillon/* internal macros */ 53109187Sdillon#define PRINT(CH) do { \ 54109187Sdillon if (dst >= s + maxsize) \ 55109187Sdillon goto e2big_error; \ 56109187Sdillon *dst++ = CH; \ 57109187Sdillon} while (0) 58109187Sdillon 59109187Sdillon#define PRINTS(STR) do { \ 60109187Sdillon char *tmps = STR; \ 61109187Sdillon while (*tmps != '\0') \ 62109187Sdillon PRINT(*tmps++); \ 63109187Sdillon} while (0) 64109187Sdillon 65109187Sdillon#define GET_NUMBER(VAR) do { \ 66109187Sdillon VAR = 0; \ 67109187Sdillon while (isdigit((unsigned char)*fmt)) { \ 68109187Sdillon VAR *= 10; \ 69109187Sdillon VAR += *fmt - '0'; \ 70109187Sdillon fmt++; \ 71109187Sdillon } \ 72109187Sdillon} while (0) 73109187Sdillon 74109187Sdillon#define GRPCPY(howmany) do { \ 75109187Sdillon int i = howmany; \ 76109187Sdillon while (i-- > 0) { \ 77109187Sdillon avalue_size--; \ 78109187Sdillon *--bufend = *(avalue+avalue_size+padded); \ 79109187Sdillon } \ 80109187Sdillon} while (0) 81109187Sdillon 82109187Sdillon#define GRPSEP do { \ 83109187Sdillon *--bufend = thousands_sep; \ 84109187Sdillon groups++; \ 85109187Sdillon} while (0) 86109187Sdillon 87109187Sdillonstatic void __setup_vars(int, char *, char *, char *, char **); 88109187Sdillonstatic int __calc_left_pad(int, char *); 89109187Sdillonstatic char *__format_grouped_double(double, int *, int, int, int); 90109187Sdillon 91109825Sdillonssize_t 92109825Sdillonstrfmon(char * __restrict s, size_t maxsize, const char * __restrict format, 93109825Sdillon ...) 94109187Sdillon{ 95109825Sdillon va_list ap; 96109825Sdillon char *dst; /* output destination pointer */ 97109825Sdillon const char *fmt; /* current format poistion pointer */ 98109187Sdillon struct lconv *lc; /* pointer to lconv structure */ 99109187Sdillon char *asciivalue; /* formatted double pointer */ 100109187Sdillon 101109187Sdillon int flags; /* formatting options */ 102109187Sdillon int pad_char; /* padding character */ 103109187Sdillon int pad_size; /* pad size */ 104109187Sdillon int width; /* field width */ 105109187Sdillon int left_prec; /* left precision */ 106109187Sdillon int right_prec; /* right precision */ 107109187Sdillon double value; /* just value */ 108109187Sdillon char space_char = ' '; /* space after currency */ 109109187Sdillon 110109187Sdillon char cs_precedes, /* values gathered from struct lconv */ 111109187Sdillon sep_by_space, 112109187Sdillon sign_posn, 113109187Sdillon *signstr, 114109187Sdillon *currency_symbol; 115109187Sdillon 116109187Sdillon char *tmpptr; /* temporary vars */ 117109187Sdillon int sverrno; 118109187Sdillon 119109187Sdillon va_start(ap, format); 120109187Sdillon 121109825Sdillon lc = localeconv(); 122109187Sdillon dst = s; 123109187Sdillon fmt = format; 124109187Sdillon asciivalue = NULL; 125109187Sdillon currency_symbol = NULL; 126109187Sdillon pad_size = 0; 127109187Sdillon 128109187Sdillon while (*fmt) { 129109187Sdillon /* pass nonformating characters AS IS */ 130109187Sdillon if (*fmt != '%') 131109187Sdillon goto literal; 132109187Sdillon 133109187Sdillon /* '%' found ! */ 134109187Sdillon 135109187Sdillon /* "%%" mean just '%' */ 136109187Sdillon if (*(fmt+1) == '%') { 137109187Sdillon fmt++; 138109187Sdillon literal: 139109187Sdillon PRINT(*fmt++); 140109187Sdillon continue; 141109187Sdillon } 142109187Sdillon 143109187Sdillon /* set up initial values */ 144109187Sdillon flags = (NEED_GROUPING|LOCALE_POSN); 145109187Sdillon pad_char = ' '; /* padding character is "space" */ 146109187Sdillon 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 free(asciivalue); 372 free(currency_symbol); 373 return (dst - s - 1); /* return size of put data except trailing '\0' */ 374 375e2big_error: 376 errno = E2BIG; 377 goto end_error; 378 379format_error: 380 errno = EINVAL; 381 382end_error: 383 sverrno = errno; 384 if (asciivalue != NULL) 385 free(asciivalue); 386 if (currency_symbol != NULL) 387 free(currency_symbol); 388 errno = sverrno; 389 va_end(ap); 390 return (-1); 391} 392 393static void 394__setup_vars(int flags, char *cs_precedes, char *sep_by_space, 395 char *sign_posn, char **signstr) { 396 397 struct lconv *lc = localeconv(); 398 399 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 400 *cs_precedes = lc->int_n_cs_precedes; 401 *sep_by_space = lc->int_n_sep_by_space; 402 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 403 *signstr = (lc->negative_sign == '\0') ? "-" 404 : lc->negative_sign; 405 } else if (flags & USE_INTL_CURRENCY) { 406 *cs_precedes = lc->int_p_cs_precedes; 407 *sep_by_space = lc->int_p_sep_by_space; 408 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 409 *signstr = lc->positive_sign; 410 } else if (flags & IS_NEGATIVE) { 411 *cs_precedes = lc->n_cs_precedes; 412 *sep_by_space = lc->n_sep_by_space; 413 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 414 *signstr = (lc->negative_sign == '\0') ? "-" 415 : lc->negative_sign; 416 } else { 417 *cs_precedes = lc->p_cs_precedes; 418 *sep_by_space = lc->p_sep_by_space; 419 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 420 *signstr = lc->positive_sign; 421 } 422 423 /* Set defult values for unspecified information. */ 424 if (*cs_precedes != 0) 425 *cs_precedes = 1; 426 if (*sep_by_space == CHAR_MAX) 427 *sep_by_space = 0; 428 if (*sign_posn == CHAR_MAX) 429 *sign_posn = 0; 430} 431 432static int 433__calc_left_pad(int flags, char *cur_symb) { 434 435 char cs_precedes, sep_by_space, sign_posn, *signstr; 436 int left_chars = 0; 437 438 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 439 440 if (cs_precedes != 0) { 441 left_chars += strlen(cur_symb); 442 if (sep_by_space != 0) 443 left_chars++; 444 } 445 446 switch (sign_posn) { 447 case 1: 448 left_chars += strlen(signstr); 449 break; 450 case 3: 451 case 4: 452 if (cs_precedes != 0) 453 left_chars += strlen(signstr); 454 } 455 return (left_chars); 456} 457 458static int 459get_groups(int size, char *grouping) { 460 461 int chars = 0; 462 463 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 464 return (0); 465 466 while (size > (int)*grouping) { 467 chars++; 468 size -= (int)*grouping++; 469 /* no more grouping ? */ 470 if (*grouping == CHAR_MAX) 471 break; 472 /* rest grouping with same value ? */ 473 if (*grouping == 0) { 474 chars += (size - 1) / *(grouping - 1); 475 break; 476 } 477 } 478 return (chars); 479} 480 481/* convert double to ASCII */ 482static char * 483__format_grouped_double(double value, int *flags, 484 int left_prec, int right_prec, int pad_char) { 485 486 char *rslt; 487 char *avalue; 488 int avalue_size; 489 char fmt[32]; 490 491 size_t bufsize; 492 char *bufend; 493 494 int padded; 495 496 struct lconv *lc = localeconv(); 497 char *grouping; 498 char decimal_point; 499 char thousands_sep; 500 501 int groups = 0; 502 503 grouping = lc->mon_grouping; 504 decimal_point = *lc->mon_decimal_point; 505 if (decimal_point == '\0') 506 decimal_point = *lc->decimal_point; 507 thousands_sep = *lc->mon_thousands_sep; 508 if (thousands_sep == '\0') 509 thousands_sep = *lc->thousands_sep; 510 511 /* fill left_prec with default value */ 512 if (left_prec == -1) 513 left_prec = 0; 514 515 /* fill right_prec with default value */ 516 if (right_prec == -1) { 517 if (*flags & USE_INTL_CURRENCY) 518 right_prec = lc->int_frac_digits; 519 else 520 right_prec = lc->frac_digits; 521 522 if (right_prec == CHAR_MAX) /* POSIX locale ? */ 523 right_prec = 2; 524 } 525 526 if (*flags & NEED_GROUPING) 527 left_prec += get_groups(left_prec, grouping); 528 529 /* convert to string */ 530 snprintf(fmt, sizeof(fmt), "%%%d.%df", left_prec + right_prec + 1, 531 right_prec); 532 avalue_size = asprintf(&avalue, fmt, value); 533 if (avalue_size < 0) 534 return (NULL); 535 536 /* make sure that we've enough space for result string */ 537 bufsize = strlen(avalue)*2+1; 538 rslt = malloc(bufsize); 539 if (rslt == NULL) { 540 free(avalue); 541 return (NULL); 542 } 543 memset(rslt, 0, bufsize); 544 bufend = rslt + bufsize - 1; /* reserve space for trailing '\0' */ 545 546 /* skip spaces at beggining */ 547 padded = 0; 548 while (avalue[padded] == ' ') { 549 padded++; 550 avalue_size--; 551 } 552 553 if (right_prec > 0) { 554 bufend -= right_prec; 555 memcpy(bufend, avalue + avalue_size+padded-right_prec, 556 right_prec); 557 *--bufend = decimal_point; 558 avalue_size -= (right_prec + 1); 559 } 560 561 if ((*flags & NEED_GROUPING) && 562 thousands_sep != '\0' && /* XXX: need investigation */ 563 *grouping != CHAR_MAX && 564 *grouping > 0) { 565 while (avalue_size > (int)*grouping) { 566 GRPCPY(*grouping); 567 GRPSEP; 568 grouping++; 569 570 /* no more grouping ? */ 571 if (*grouping == CHAR_MAX) 572 break; 573 574 /* rest grouping with same value ? */ 575 if (*grouping == 0) { 576 grouping--; 577 while (avalue_size > *grouping) { 578 GRPCPY(*grouping); 579 GRPSEP; 580 } 581 } 582 } 583 if (avalue_size != 0) 584 GRPCPY(avalue_size); 585 padded -= groups; 586 587 } else { 588 bufend -= avalue_size; 589 memcpy(bufend, avalue+padded, avalue_size); 590 if (right_prec == 0) 591 padded--; /* decrease assumed $decimal_point */ 592 } 593 594 /* do padding with pad_char */ 595 if (padded > 0) { 596 bufend -= padded; 597 memset(bufend, pad_char, padded); 598 } 599 600 bufsize = bufsize - (bufend - rslt) + 1; 601 memmove(rslt, bufend, bufsize); 602 free(avalue); 603 return (rslt); 604} 605