1 /**************************************************************** 2 3 The author of this software is David M. Gay. 4 5 Copyright (C) 1998 by Lucent Technologies 6 All Rights Reserved 7 8 Permission to use, copy, modify, and distribute this software and 9 its documentation for any purpose and without fee is hereby 10 granted, provided that the above copyright notice appear in all 11 copies and that both that the copyright notice and this 12 permission notice and warranty disclaimer appear in supporting 13 documentation, and that the name of Lucent or any of its entities 14 not be used in advertising or publicity pertaining to 15 distribution of the software without specific, written prior 16 permission. 17 18 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 20 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 21 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 22 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 23 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 25 THIS SOFTWARE. 26 27 ****************************************************************/ 28 29 /* Please send bug reports to David M. Gay (dmg at acm dot org, 30 * with " at " changed at "@" and " dot " changed to "."). */ 31 32 #include "gdtoaimp.h" 33 34 #ifdef USE_LOCALE 35 #include "locale.h" 36 #endif 37 38 char * 39 #ifdef KR_headers 40 g__fmt(b, s, se, decpt, sign, blen) char *b; char *s; char *se; int decpt; ULong sign; size_t blen; 41 #else 42 g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen) 43 #endif 44 { 45 int i, j, k; 46 char *be, *s0; 47 size_t len; 48 #ifdef USE_LOCALE 49 #ifdef NO_LOCALE_CACHE 50 char *decimalpoint = localeconv()->decimal_point; 51 size_t dlen = strlen(decimalpoint); 52 #else 53 char *decimalpoint; 54 static char *decimalpoint_cache; 55 static size_t dlen; 56 if (!(s0 = decimalpoint_cache)) { 57 s0 = localeconv()->decimal_point; 58 dlen = strlen(s0); 59 if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) { 60 strcpy(decimalpoint_cache, s0); 61 s0 = decimalpoint_cache; 62 } 63 } 64 decimalpoint = s0; 65 #endif 66 #else 67 #define dlen 0 68 #endif 69 s0 = s; 70 len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */ 71 if (blen < len) 72 goto ret0; 73 be = b + blen - 1; 74 if (sign) 75 *b++ = '-'; 76 if (decpt <= -4 || decpt > se - s + 5) { 77 *b++ = *s++; 78 if (*s) { 79 #ifdef USE_LOCALE 80 while((*b = *decimalpoint++)) 81 ++b; 82 #else 83 *b++ = '.'; 84 #endif 85 while((*b = *s++) !=0) 86 b++; 87 } 88 *b++ = 'e'; 89 /* sprintf(b, "%+.2d", decpt - 1); */ 90 if (--decpt < 0) { 91 *b++ = '-'; 92 decpt = -decpt; 93 } 94 else 95 *b++ = '+'; 96 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){} 97 for(;;) { 98 i = decpt / k; 99 if (b >= be) 100 goto ret0; 101 *b++ = i + '0'; 102 if (--j <= 0) 103 break; 104 decpt -= i*k; 105 decpt *= 10; 106 } 107 *b = 0; 108 } 109 else if (decpt <= 0) { 110 #ifdef USE_LOCALE 111 while((*b = *decimalpoint++)) 112 ++b; 113 #else 114 *b++ = '.'; 115 #endif 116 if (be < b - decpt + (se - s)) 117 goto ret0; 118 for(; decpt < 0; decpt++) 119 *b++ = '0'; 120 while((*b = *s++) != 0) 121 b++; 122 } 123 else { 124 while((*b = *s++) != 0) { 125 b++; 126 if (--decpt == 0 && *s) { 127 #ifdef USE_LOCALE 128 while(*b = *decimalpoint++) 129 ++b; 130 #else 131 *b++ = '.'; 132 #endif 133 } 134 } 135 if (b + decpt > be) { 136 ret0: 137 b = 0; 138 goto ret; 139 } 140 for(; decpt > 0; decpt--) 141 *b++ = '0'; 142 *b = 0; 143 } 144 ret: 145 freedtoa(s0); 146 return b; 147 } 148