OSDN Git Service

49bd95a845f7dd3bac5aaf885375945c3c9651bf
[pf3gnuchains/pf3gnuchains3x.git] / winsup / mingw / mingwex / 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 *__g__fmt (char *b, char *s, char *se, int decpt, ULong sign, size_t blen)
39 {
40         int i, j, k;
41         char *be, *s0;
42         size_t len;
43 #ifdef USE_LOCALE
44 #ifdef NO_LOCALE_CACHE
45         char *decimalpoint = localeconv()->decimal_point;
46         size_t dlen = strlen(decimalpoint);
47 #else
48         char *decimalpoint;
49         static char *decimalpoint_cache;
50         static size_t dlen;
51         if (!(s0 = decimalpoint_cache)) {
52                 s0 = localeconv()->decimal_point;
53                 dlen = strlen(s0);
54                 if ((decimalpoint_cache = (char*)MALLOC(strlen(s0) + 1))) {
55                         strcpy(decimalpoint_cache, s0);
56                         s0 = decimalpoint_cache;
57                 }
58         }
59         decimalpoint = s0;
60 #endif
61 #else
62 #define dlen 0
63 #endif
64         s0 = s;
65         len = (se-s) + dlen + 6; /* 6 = sign + e+dd + trailing null */
66         if (blen < len)
67                 goto ret0;
68         be = b + blen - 1;
69         if (sign)
70                 *b++ = '-';
71         if (decpt <= -4 || decpt > se - s + 5) {
72                 *b++ = *s++;
73                 if (*s) {
74 #ifdef USE_LOCALE
75                         while((*b = *decimalpoint++))
76                                 ++b;
77 #else
78                         *b++ = '.';
79 #endif
80                         while((*b = *s++) !=0)
81                                 b++;
82                 }
83                 *b++ = 'e';
84                 /* sprintf(b, "%+.2d", decpt - 1); */
85                 if (--decpt < 0) {
86                         *b++ = '-';
87                         decpt = -decpt;
88                 }
89                 else
90                         *b++ = '+';
91                 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
92                 for(;;) {
93                         i = decpt / k;
94                         if (b >= be)
95                                 goto ret0;
96                         *b++ = i + '0';
97                         if (--j <= 0)
98                                 break;
99                         decpt -= i*k;
100                         decpt *= 10;
101                 }
102                 *b = 0;
103         }
104         else if (decpt <= 0) {
105 #ifdef USE_LOCALE
106                 while((*b = *decimalpoint++))
107                         ++b;
108 #else
109                 *b++ = '.';
110 #endif
111                 if (be < b - decpt + (se - s))
112                         goto ret0;
113                 for(; decpt < 0; decpt++)
114                         *b++ = '0';
115                 while((*b = *s++) != 0)
116                         b++;
117         }
118         else {
119                 while((*b = *s++) != 0) {
120                         b++;
121                         if (--decpt == 0 && *s) {
122 #ifdef USE_LOCALE
123                                 while((*b = *decimalpoint++))
124                                         ++b;
125 #else
126                                 *b++ = '.';
127 #endif
128                         }
129                 }
130                 if (b + decpt > be) {
131  ret0:
132                         b = 0;
133                         goto ret;
134                 }
135                 for(; decpt > 0; decpt--)
136                         *b++ = '0';
137                 *b = 0;
138         }
139  ret:
140         __freedtoa(s0);
141         return b;
142 }