- add sources.
[platform/framework/web/crosswalk.git] / src / base / third_party / dmg_fp / g_fmt.cc
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991, 1996 by Lucent Technologies.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose without fee is hereby granted, provided that this entire notice
9  * is included in all copies of any software which is or includes a copy
10  * or modification of this software and in all copies of the supporting
11  * documentation for such software.
12  *
13  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17  *
18  ***************************************************************/
19
20 /* g_fmt(buf,x) stores the closest decimal approximation to x in buf;
21  * it suffices to declare buf
22  *      char buf[32];
23  */
24
25 #include "dmg_fp.h"
26
27 namespace dmg_fp {
28
29  char *
30 g_fmt(register char *b, double x)
31 {
32         register int i, k;
33         register char *s;
34         int decpt, j, sign;
35         char *b0, *s0, *se;
36
37         b0 = b;
38 #ifdef IGNORE_ZERO_SIGN
39         if (!x) {
40                 *b++ = '0';
41                 *b = 0;
42                 goto done;
43                 }
44 #endif
45         s = s0 = dtoa(x, 0, 0, &decpt, &sign, &se);
46         if (sign)
47                 *b++ = '-';
48         if (decpt == 9999) /* Infinity or Nan */ {
49                 while((*b++ = *s++)) {}
50                 goto done0;
51                 }
52         if (decpt <= -4 || decpt > se - s + 5) {
53                 *b++ = *s++;
54                 if (*s) {
55                         *b++ = '.';
56                         while((*b = *s++))
57                                 b++;
58                         }
59                 *b++ = 'e';
60                 /* sprintf(b, "%+.2d", decpt - 1); */
61                 if (--decpt < 0) {
62                         *b++ = '-';
63                         decpt = -decpt;
64                         }
65                 else
66                         *b++ = '+';
67                 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10) {}
68                 for(;;) {
69                         i = decpt / k;
70                         *b++ = i + '0';
71                         if (--j <= 0)
72                                 break;
73                         decpt -= i*k;
74                         decpt *= 10;
75                         }
76                 *b = 0;
77                 }
78         else if (decpt <= 0) {
79                 *b++ = '.';
80                 for(; decpt < 0; decpt++)
81                         *b++ = '0';
82                 while((*b++ = *s++)) {}
83                 }
84         else {
85                 while((*b = *s++)) {
86                         b++;
87                         if (--decpt == 0 && *s)
88                                 *b++ = '.';
89                         }
90                 for(; decpt > 0; decpt--)
91                         *b++ = '0';
92                 *b = 0;
93                 }
94  done0:
95         freedtoa(s0);
96 #ifdef IGNORE_ZERO_SIGN
97  done:
98 #endif
99         return b0;
100         }
101
102 }  // namespace dmg_fp