1112158Sdas/****************************************************************
2112158Sdas
3112158SdasThe author of this software is David M. Gay.
4112158Sdas
5112158SdasCopyright (C) 1998 by Lucent Technologies
6112158SdasAll Rights Reserved
7112158Sdas
8112158SdasPermission to use, copy, modify, and distribute this software and
9112158Sdasits documentation for any purpose and without fee is hereby
10112158Sdasgranted, provided that the above copyright notice appear in all
11112158Sdascopies and that both that the copyright notice and this
12112158Sdaspermission notice and warranty disclaimer appear in supporting
13112158Sdasdocumentation, and that the name of Lucent or any of its entities
14112158Sdasnot be used in advertising or publicity pertaining to
15112158Sdasdistribution of the software without specific, written prior
16112158Sdaspermission.
17112158Sdas
18112158SdasLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19112158SdasINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
20112158SdasIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
21112158SdasSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22112158SdasWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
23112158SdasIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24112158SdasARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25112158SdasTHIS SOFTWARE.
26112158Sdas
27112158Sdas****************************************************************/
28112158Sdas
29165743Sdas/* Please send bug reports to David M. Gay (dmg at acm dot org,
30165743Sdas * with " at " changed at "@" and " dot " changed to ".").	*/
31112158Sdas
32112158Sdas#include "gdtoaimp.h"
33112158Sdas
34112415Sdas#ifdef USE_LOCALE
35112415Sdas#include "locale.h"
36112415Sdas#endif
37112415Sdas
38112158Sdas char *
39112158Sdas#ifdef KR_headers
40187808Sdasg__fmt(b, s, se, decpt, sign, blen) char *b; char *s; char *se; int decpt; ULong sign; size_t blen;
41112158Sdas#else
42187808Sdasg__fmt(char *b, char *s, char *se, int decpt, ULong sign, size_t blen)
43112158Sdas#endif
44112158Sdas{
45112158Sdas	int i, j, k;
46187808Sdas	char *be, *s0;
47187808Sdas	size_t len;
48112415Sdas#ifdef USE_LOCALE
49187808Sdas#ifdef NO_LOCALE_CACHE
50187808Sdas	char *decimalpoint = localeconv()->decimal_point;
51187808Sdas	size_t dlen = strlen(decimalpoint);
52112415Sdas#else
53187808Sdas	char *decimalpoint;
54187808Sdas	static char *decimalpoint_cache;
55187808Sdas	static size_t dlen;
56187808Sdas	if (!(s0 = decimalpoint_cache)) {
57187808Sdas		s0 = localeconv()->decimal_point;
58187808Sdas		dlen = strlen(s0);
59219557Sdas		if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) {
60187808Sdas			strcpy(decimalpoint_cache, s0);
61187808Sdas			s0 = decimalpoint_cache;
62187808Sdas			}
63187808Sdas		}
64187808Sdas	decimalpoint = s0;
65112415Sdas#endif
66187808Sdas#else
67187808Sdas#define dlen 0
68187808Sdas#endif
69187808Sdas	s0 = s;
70187808Sdas	len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */
71187808Sdas	if (blen < len)
72187808Sdas		goto ret0;
73187808Sdas	be = b + blen - 1;
74112158Sdas	if (sign)
75112158Sdas		*b++ = '-';
76112158Sdas	if (decpt <= -4 || decpt > se - s + 5) {
77112158Sdas		*b++ = *s++;
78112158Sdas		if (*s) {
79187808Sdas#ifdef USE_LOCALE
80187808Sdas			while((*b = *decimalpoint++))
81187808Sdas				++b;
82187808Sdas#else
83187808Sdas			*b++ = '.';
84187808Sdas#endif
85112158Sdas			while((*b = *s++) !=0)
86112158Sdas				b++;
87112158Sdas			}
88112158Sdas		*b++ = 'e';
89112158Sdas		/* sprintf(b, "%+.2d", decpt - 1); */
90112158Sdas		if (--decpt < 0) {
91112158Sdas			*b++ = '-';
92112158Sdas			decpt = -decpt;
93112158Sdas			}
94112158Sdas		else
95112158Sdas			*b++ = '+';
96112158Sdas		for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
97112158Sdas		for(;;) {
98112158Sdas			i = decpt / k;
99187808Sdas			if (b >= be)
100187808Sdas				goto ret0;
101112158Sdas			*b++ = i + '0';
102112158Sdas			if (--j <= 0)
103112158Sdas				break;
104112158Sdas			decpt -= i*k;
105112158Sdas			decpt *= 10;
106112158Sdas			}
107112158Sdas		*b = 0;
108112158Sdas		}
109112158Sdas	else if (decpt <= 0) {
110187808Sdas#ifdef USE_LOCALE
111187808Sdas		while((*b = *decimalpoint++))
112187808Sdas			++b;
113187808Sdas#else
114187808Sdas		*b++ = '.';
115187808Sdas#endif
116187808Sdas		if (be < b - decpt + (se - s))
117187808Sdas			goto ret0;
118112158Sdas		for(; decpt < 0; decpt++)
119112158Sdas			*b++ = '0';
120187808Sdas		while((*b = *s++) != 0)
121112158Sdas			b++;
122112158Sdas		}
123112158Sdas	else {
124187808Sdas		while((*b = *s++) != 0) {
125112158Sdas			b++;
126187808Sdas			if (--decpt == 0 && *s) {
127187808Sdas#ifdef USE_LOCALE
128187808Sdas				while(*b = *decimalpoint++)
129187808Sdas					++b;
130187808Sdas#else
131187808Sdas				*b++ = '.';
132187808Sdas#endif
133187808Sdas				}
134112158Sdas			}
135187808Sdas		if (b + decpt > be) {
136187808Sdas ret0:
137187808Sdas			b = 0;
138187808Sdas			goto ret;
139187808Sdas			}
140112158Sdas		for(; decpt > 0; decpt--)
141112158Sdas			*b++ = '0';
142112158Sdas		*b = 0;
143112158Sdas		}
144187808Sdas ret:
145112158Sdas	freedtoa(s0);
146112158Sdas	return b;
147112158Sdas 	}
148