]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/gdtoa/g__fmt.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / gdtoa / g__fmt.c
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) char *b; char *s; char *se; int decpt; ULong sign;
41 #else
42 g__fmt(char *b, char *s, char *se, int decpt, ULong sign)
43 #endif
44 {
45         int i, j, k;
46         char *s0 = s;
47 #ifdef USE_LOCALE
48         char decimalpoint = *localeconv()->decimal_point;
49 #else
50 #define decimalpoint '.'
51 #endif
52         if (sign)
53                 *b++ = '-';
54         if (decpt <= -4 || decpt > se - s + 5) {
55                 *b++ = *s++;
56                 if (*s) {
57                         *b++ = decimalpoint;
58                         while((*b = *s++) !=0)
59                                 b++;
60                         }
61                 *b++ = 'e';
62                 /* sprintf(b, "%+.2d", decpt - 1); */
63                 if (--decpt < 0) {
64                         *b++ = '-';
65                         decpt = -decpt;
66                         }
67                 else
68                         *b++ = '+';
69                 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
70                 for(;;) {
71                         i = decpt / k;
72                         *b++ = i + '0';
73                         if (--j <= 0)
74                                 break;
75                         decpt -= i*k;
76                         decpt *= 10;
77                         }
78                 *b = 0;
79                 }
80         else if (decpt <= 0) {
81                 *b++ = decimalpoint;
82                 for(; decpt < 0; decpt++)
83                         *b++ = '0';
84                 while((*b = *s++) !=0)
85                         b++;
86                 }
87         else {
88                 while((*b = *s++) !=0) {
89                         b++;
90                         if (--decpt == 0 && *s)
91                                 *b++ = decimalpoint;
92                         }
93                 for(; decpt > 0; decpt--)
94                         *b++ = '0';
95                 *b = 0;
96                 }
97         freedtoa(s0);
98         return b;
99         }