g__fmt.c revision 165743
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
40112158Sdasg__fmt(b, s, se, decpt, sign) char *b; char *s; char *se; int decpt; ULong sign;
41112158Sdas#else
42112158Sdasg__fmt(char *b, char *s, char *se, int decpt, ULong sign)
43112158Sdas#endif
44112158Sdas{
45112158Sdas	int i, j, k;
46112158Sdas	char *s0 = s;
47112415Sdas#ifdef USE_LOCALE
48112415Sdas	char decimalpoint = *localeconv()->decimal_point;
49112415Sdas#else
50112415Sdas#define decimalpoint '.'
51112415Sdas#endif
52112158Sdas	if (sign)
53112158Sdas		*b++ = '-';
54112158Sdas	if (decpt <= -4 || decpt > se - s + 5) {
55112158Sdas		*b++ = *s++;
56112158Sdas		if (*s) {
57112415Sdas			*b++ = decimalpoint;
58112158Sdas			while((*b = *s++) !=0)
59112158Sdas				b++;
60112158Sdas			}
61112158Sdas		*b++ = 'e';
62112158Sdas		/* sprintf(b, "%+.2d", decpt - 1); */
63112158Sdas		if (--decpt < 0) {
64112158Sdas			*b++ = '-';
65112158Sdas			decpt = -decpt;
66112158Sdas			}
67112158Sdas		else
68112158Sdas			*b++ = '+';
69112158Sdas		for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
70112158Sdas		for(;;) {
71112158Sdas			i = decpt / k;
72112158Sdas			*b++ = i + '0';
73112158Sdas			if (--j <= 0)
74112158Sdas				break;
75112158Sdas			decpt -= i*k;
76112158Sdas			decpt *= 10;
77112158Sdas			}
78112158Sdas		*b = 0;
79112158Sdas		}
80112158Sdas	else if (decpt <= 0) {
81112415Sdas		*b++ = decimalpoint;
82112158Sdas		for(; decpt < 0; decpt++)
83112158Sdas			*b++ = '0';
84112158Sdas		while((*b = *s++) !=0)
85112158Sdas			b++;
86112158Sdas		}
87112158Sdas	else {
88112158Sdas		while((*b = *s++) !=0) {
89112158Sdas			b++;
90112158Sdas			if (--decpt == 0 && *s)
91112415Sdas				*b++ = decimalpoint;
92112158Sdas			}
93112158Sdas		for(; decpt > 0; decpt--)
94112158Sdas			*b++ = '0';
95112158Sdas		*b = 0;
96112158Sdas		}
97112158Sdas	freedtoa(s0);
98112158Sdas	return b;
99112158Sdas 	}
100