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/types.h> 29 #include <ctype.h> 30 #include <errno.h> 31 #include <limits.h> 32 #include <locale.h> 33 #include <monetary.h> 34 #include <stdarg.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 40 /* internal flags */ 41 #define NEED_GROUPING 0x01 /* print digits grouped (default) */ 42 #define SIGN_POSN_USED 0x02 /* '+' or '(' usage flag */ 43 #define LOCALE_POSN 0x04 /* use locale defined +/- (default) */ 44 #define PARENTH_POSN 0x08 /* enclose negative amount in () */ 45 #define SUPRESS_CURR_SYMBOL 0x10 /* supress the currency from output */ 46 #define LEFT_JUSTIFY 0x20 /* left justify */ 47 #define USE_INTL_CURRENCY 0x40 /* use international currency symbol */ 48 #define IS_NEGATIVE 0x80 /* is argument value negative ? */ 49 50 /* internal macros */ 51 #define PRINT(CH) do { \ 52 if (dst >= s + maxsize) \ 53 goto e2big_error; \ 54 *dst++ = CH; \ 55 } while (0) 56 57 #define PRINTS(STR) do { \ 58 char *tmps = STR; \ 59 while (*tmps != '\0') \ 60 PRINT(*tmps++); \ 61 } while (0) 62 63 #define GET_NUMBER(VAR) do { \ 64 VAR = 0; \ 65 while (isdigit((unsigned char)*fmt)) { \ 66 if (VAR > INT_MAX / 10) \ 67 goto e2big_error; \ 68 VAR *= 10; \ 69 VAR += *fmt - '0'; \ 70 if (VAR < 0) \ 71 goto e2big_error; \ 72 fmt++; \ 73 } \ 74 } while (0) 75 76 #define GRPCPY(howmany) do { \ 77 int i = howmany; \ 78 while (i-- > 0) { \ 79 avalue_size--; \ 80 *--bufend = *(avalue+avalue_size+padded); \ 81 } \ 82 } while (0) 83 84 #define GRPSEP do { \ 85 *--bufend = thousands_sep; \ 86 groups++; \ 87 } while (0) 88 89 90 static void __setup_vars(int, char *, char *, char *, char **); 91 static int __calc_left_pad(int, char *); 92 static char *__format_grouped_double(double, int *, int, int, int); 93 94 95 ssize_t 96 strfmon(char *s, size_t maxsize, const char *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) - left_prec) 213 goto e2big_error; 214 } 215 216 /* Conversion Characters */ 217 switch (*fmt++) { 218 case 'i': /* use internaltion currency format */ 219 flags |= USE_INTL_CURRENCY; 220 break; 221 case 'n': /* use national currency format */ 222 flags &= ~(USE_INTL_CURRENCY); 223 break; 224 default: /* required character is missing or 225 premature EOS */ 226 goto format_error; 227 } 228 229 if (currency_symbol != NULL) 230 free(currency_symbol); 231 if (flags & USE_INTL_CURRENCY) { 232 currency_symbol = strdup(lc->int_curr_symbol); 233 if (currency_symbol != NULL) { 234 space_char = currency_symbol[3]; 235 currency_symbol[3] = '\0'; 236 } 237 } else 238 currency_symbol = strdup(lc->currency_symbol); 239 240 if (currency_symbol == NULL) 241 goto end_error; /* ENOMEM. */ 242 243 /* value itself */ 244 value = va_arg(ap, double); 245 246 /* detect sign */ 247 if (value < 0) { 248 flags |= IS_NEGATIVE; 249 value = -value; 250 } 251 252 /* fill left_prec with amount of padding chars */ 253 if (left_prec >= 0) { 254 pad_size = __calc_left_pad((flags ^ IS_NEGATIVE), 255 currency_symbol) - __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, left_prec, 263 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, &sign_posn, &signstr); 270 271 /* 272 * Description of some LC_MONETARY's values: 273 * 274 * p_cs_precedes & n_cs_precedes 275 * 276 * = 1 - $currency_symbol precedes the value 277 * for a monetary quantity with a non-negative value 278 * = 0 - symbol succeeds the value 279 * 280 * p_sep_by_space & n_sep_by_space 281 * 282 * = 0 - no space separates $currency_symbol 283 * from the value for a monetary quantity with a 284 * non-negative value 285 * = 1 - space separates the symbol from the value 286 * = 2 - space separates the symbol and the sign string, 287 * if adjacent. 288 * 289 * p_sign_posn & n_sign_posn 290 * 291 * = 0 - parentheses enclose the quantity and the 292 * $currency_symbol 293 * = 1 - the sign string precedes the quantity and the 294 * $currency_symbol 295 * = 2 - the sign string succeeds the quantity and the 296 * $currency_symbol 297 * = 3 - the sign string precedes the $currency_symbol 298 * = 4 - the sign string succeeds the $currency_symbol 299 * 300 */ 301 302 tmpptr = dst; 303 304 while (pad_size-- > 0) 305 PRINT(' '); 306 307 if (sign_posn == 0 && (flags & IS_NEGATIVE)) 308 PRINT('('); 309 310 if (cs_precedes == 1) { 311 if (sign_posn == 1 || sign_posn == 3) { 312 PRINTS(signstr); 313 if (sep_by_space == 2) /* XXX: ? */ 314 PRINT(' '); 315 } 316 317 if (!(flags & SUPRESS_CURR_SYMBOL)) { 318 PRINTS(currency_symbol); 319 320 if (sign_posn == 4) { 321 if (sep_by_space == 2) 322 PRINT(space_char); 323 PRINTS(signstr); 324 if (sep_by_space == 1) 325 PRINT(' '); 326 } else if (sep_by_space == 1) 327 PRINT(space_char); 328 } 329 } else if (sign_posn == 1) 330 PRINTS(signstr); 331 332 PRINTS(asciivalue); 333 334 if (cs_precedes == 0) { 335 if (sign_posn == 3) { 336 if (sep_by_space == 1) 337 PRINT(' '); 338 PRINTS(signstr); 339 } 340 341 if (!(flags & SUPRESS_CURR_SYMBOL)) { 342 if ((sign_posn == 3 && sep_by_space == 2) 343 || (sep_by_space == 1 344 && (sign_posn == 0 345 || sign_posn == 1 346 || sign_posn == 2 347 || sign_posn == 4))) 348 PRINT(space_char); 349 PRINTS(currency_symbol); /* XXX: len */ 350 if (sign_posn == 4) { 351 if (sep_by_space == 2) 352 PRINT(' '); 353 PRINTS(signstr); 354 } 355 } 356 } 357 358 if (sign_posn == 2) { 359 if (sep_by_space == 2) 360 PRINT(' '); 361 PRINTS(signstr); 362 } 363 364 if (sign_posn == 0 && (flags & IS_NEGATIVE)) 365 PRINT(')'); 366 367 if (dst - tmpptr < width) { 368 if (flags & LEFT_JUSTIFY) { 369 while (dst - tmpptr < width) 370 PRINT(' '); 371 } else { 372 pad_size = dst - tmpptr; 373 memmove(tmpptr + width - pad_size, tmpptr, pad_size); 374 memset(tmpptr, ' ', width - pad_size); 375 dst += width - pad_size; 376 } 377 } 378 } 379 380 PRINT('\0'); 381 va_end(ap); 382 free(asciivalue); 383 free(currency_symbol); 384 return dst - s - 1; /* return size of put data except trailing '\0' */ 385 386 e2big_error: 387 errno = E2BIG; 388 goto end_error; 389 390 format_error: 391 errno = EINVAL; 392 393 end_error: 394 sverrno = errno; 395 if (asciivalue != NULL) 396 free(asciivalue); 397 if (currency_symbol != NULL) 398 free(currency_symbol); 399 errno = sverrno; 400 va_end(ap); 401 return -1; 402 } 403 404 405 static void 406 __setup_vars(int flags, char *cs_precedes, char *sep_by_space, char *sign_posn, 407 char **signstr) 408 { 409 struct lconv *lc = localeconv(); 410 411 if ((flags & IS_NEGATIVE) && (flags & USE_INTL_CURRENCY)) { 412 *cs_precedes = lc->int_n_cs_precedes; 413 *sep_by_space = lc->int_n_sep_by_space; 414 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_n_sign_posn; 415 *signstr = (lc->negative_sign == '\0') ? "-" : lc->negative_sign; 416 } else if (flags & USE_INTL_CURRENCY) { 417 *cs_precedes = lc->int_p_cs_precedes; 418 *sep_by_space = lc->int_p_sep_by_space; 419 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->int_p_sign_posn; 420 *signstr = lc->positive_sign; 421 } else if (flags & IS_NEGATIVE) { 422 *cs_precedes = lc->n_cs_precedes; 423 *sep_by_space = lc->n_sep_by_space; 424 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->n_sign_posn; 425 *signstr = (lc->negative_sign == '\0') ? "-" : lc->negative_sign; 426 } else { 427 *cs_precedes = lc->p_cs_precedes; 428 *sep_by_space = lc->p_sep_by_space; 429 *sign_posn = (flags & PARENTH_POSN) ? 0 : lc->p_sign_posn; 430 *signstr = lc->positive_sign; 431 } 432 433 /* Set defult values for unspecified information. */ 434 if (*cs_precedes != 0) 435 *cs_precedes = 1; 436 if (*sep_by_space == CHAR_MAX) 437 *sep_by_space = 0; 438 if (*sign_posn == CHAR_MAX) 439 *sign_posn = 0; 440 } 441 442 443 static int 444 __calc_left_pad(int flags, char *cur_symb) 445 { 446 char cs_precedes, sep_by_space, sign_posn, *signstr; 447 int left_chars = 0; 448 449 __setup_vars(flags, &cs_precedes, &sep_by_space, &sign_posn, &signstr); 450 451 if (cs_precedes != 0) { 452 left_chars += strlen(cur_symb); 453 if (sep_by_space != 0) 454 left_chars++; 455 } 456 457 switch (sign_posn) { 458 case 1: 459 left_chars += strlen(signstr); 460 break; 461 case 3: 462 case 4: 463 if (cs_precedes != 0) 464 left_chars += strlen(signstr); 465 } 466 return left_chars; 467 } 468 469 470 static int 471 get_groups(int size, char *grouping) 472 { 473 int chars = 0; 474 475 if (*grouping == CHAR_MAX || *grouping <= 0) /* no grouping ? */ 476 return 0; 477 478 while (size > (int)*grouping) { 479 chars++; 480 size -= (int)*grouping++; 481 /* no more grouping ? */ 482 if (*grouping == CHAR_MAX) 483 break; 484 /* rest grouping with same value ? */ 485 if (*grouping == 0) { 486 chars += (size - 1) / *(grouping - 1); 487 break; 488 } 489 } 490 return chars; 491 } 492 493 494 /* convert double to ASCII */ 495 static char * 496 __format_grouped_double(double value, int *flags, int left_prec, int right_prec, 497 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, right_prec); 568 *--bufend = decimal_point; 569 avalue_size -= right_prec + 1; 570 } 571 572 if ((*flags & NEED_GROUPING) && *grouping != CHAR_MAX && *grouping > 0 573 && thousands_sep != '\0') { 574 while (avalue_size > (int)*grouping) { 575 GRPCPY(*grouping); 576 GRPSEP; 577 grouping++; 578 579 /* no more grouping ? */ 580 if (*grouping == CHAR_MAX) 581 break; 582 583 /* rest grouping with same value ? */ 584 if (*grouping == 0) { 585 grouping--; 586 while (avalue_size > *grouping) { 587 GRPCPY(*grouping); 588 GRPSEP; 589 } 590 } 591 } 592 if (avalue_size != 0) 593 GRPCPY(avalue_size); 594 padded -= groups; 595 } else { 596 bufend -= avalue_size; 597 memcpy(bufend, avalue + padded, avalue_size); 598 if (right_prec == 0) 599 padded--; /* decrease assumed $decimal_point */ 600 } 601 602 /* do padding with pad_char */ 603 if (padded > 0) { 604 bufend -= padded; 605 memset(bufend, pad_char, padded); 606 } 607 608 bufsize = bufsize - (bufend - rslt) + 1; 609 memmove(rslt, bufend, bufsize); 610 free(avalue); 611 612 return rslt; 613 } 614