/****************************************************************12The author of this software is David M. Gay.34Copyright (C) 1998 by Lucent Technologies5All Rights Reserved67Permission to use, copy, modify, and distribute this software and8its documentation for any purpose and without fee is hereby9granted, provided that the above copyright notice appear in all10copies and that both that the copyright notice and this11permission notice and warranty disclaimer appear in supporting12documentation, and that the name of Lucent or any of its entities13not be used in advertising or publicity pertaining to14distribution of the software without specific, written prior15permission.1617LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,18INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.19IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY20SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES21WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER22IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,23ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF24THIS SOFTWARE.2526****************************************************************/2728/* Please send bug reports to David M. Gay (dmg at acm dot org,29* with " at " changed at "@" and " dot " changed to "."). */3031#include "gdtoaimp.h"3233#ifdef USE_LOCALE34#include "locale.h"35#endif3637char *38#ifdef KR_headers39g__fmt(b, s, se, decpt, sign, blen) char *b; char *s; char *se; int decpt; ULong sign; size_t blen;40#else41g__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen)42#endif43{44int i, j, k;45char *be, *s0;46size_t len;47#ifdef USE_LOCALE48#ifdef NO_LOCALE_CACHE49char *decimalpoint = localeconv()->decimal_point;50size_t dlen = strlen(decimalpoint);51#else52char *decimalpoint;53static char *decimalpoint_cache;54static size_t dlen;55if (!(s0 = decimalpoint_cache)) {56s0 = localeconv()->decimal_point;57dlen = strlen(s0);58if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) {59strcpy(decimalpoint_cache, s0);60s0 = decimalpoint_cache;61}62}63decimalpoint = s0;64#endif65#else66#define dlen 067#endif68s0 = s;69len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */70if (blen < len)71goto ret0;72be = b + blen - 1;73if (sign)74*b++ = '-';75if (decpt <= -4 || decpt > se - s + 5) {76*b++ = *s++;77if (*s) {78#ifdef USE_LOCALE79while((*b = *decimalpoint++))80++b;81#else82*b++ = '.';83#endif84while((*b = *s++) !=0)85b++;86}87*b++ = 'e';88/* sprintf(b, "%+.2d", decpt - 1); */89if (--decpt < 0) {90*b++ = '-';91decpt = -decpt;92}93else94*b++ = '+';95for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}96for(;;) {97i = decpt / k;98if (b >= be)99goto ret0;100*b++ = i + '0';101if (--j <= 0)102break;103decpt -= i*k;104decpt *= 10;105}106*b = 0;107}108else if (decpt <= 0) {109#ifdef USE_LOCALE110while((*b = *decimalpoint++))111++b;112#else113*b++ = '.';114#endif115if (be < b - decpt + (se - s))116goto ret0;117for(; decpt < 0; decpt++)118*b++ = '0';119while((*b = *s++) != 0)120b++;121}122else {123while((*b = *s++) != 0) {124b++;125if (--decpt == 0 && *s) {126#ifdef USE_LOCALE127while(*b = *decimalpoint++)128++b;129#else130*b++ = '.';131#endif132}133}134if (b + decpt > be) {135ret0:136b = 0;137goto ret;138}139for(; decpt > 0; decpt--)140*b++ = '0';141*b = 0;142}143ret:144freedtoa(s0);145return b;146}147148149