Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
g__fmt.c
Go to the documentation of this file.
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;
41 char* s;
42 const char* se;
43 int decpt;
44 ULong sign;
45 #else
46 g__fmt(char *b, char *s, const char *se, int decpt, ULong sign)
47 #endif
48 {
49  char* s0 = s;
50 #ifdef USE_LOCALE
52 #else
53 #define decimalpoint '.'
54 #endif
55  if(sign)
56  {
57  {
58  *b++ = '-';
59  }
60  }
61  if(decpt <= -4 || decpt > se - s + 5)
62  {
63  int j, k;
64  *b++ = *s++;
65  if(*s)
66  {
67  *b++ = decimalpoint;
68  while((*b = *s++) != 0)
69  {
70  b++;
71  }
72  }
73  *b++ = 'e';
74  /* sprintf(b, "%+.2d", decpt - 1); */
75  if(--decpt < 0)
76  {
77  *b++ = '-';
78  decpt = -decpt;
79  }
80  else
81  {
82  {
83  *b++ = '+';
84  }
85  }
86  for(j = 2, k = 10; 10 * k <= decpt; j++, k *= 10)
87  {
88  }
89  for(;;)
90  {
91  int i = decpt / k;
92  *b++ = (char)i + '0';
93  if(--j <= 0)
94  {
95  break;
96  }
97  decpt -= i * k;
98  decpt *= 10;
99  }
100  *b = 0;
101  }
102  else if(decpt <= 0)
103  {
104  *b++ = decimalpoint;
105  for(; decpt < 0; decpt++)
106  {
107  {
108  *b++ = '0';
109  }
110  }
111  while((*b = *s++) != 0)
112  {
113  {
114  b++;
115  }
116  }
117  }
118  else
119  {
120  while((*b = *s++) != 0)
121  {
122  b++;
123  if(--decpt == 0 && *s)
124  {
125  {
126  *b++ = decimalpoint;
127  }
128  }
129  }
130  for(; decpt > 0; decpt--)
131  {
132  {
133  *b++ = '0';
134  }
135  }
136  *b = 0;
137  }
138  freedtoa(s0);
139  return b;
140 }
char * g__fmt(char *b, char *s, const char *se, int decpt, ULong sign)
Definition: g__fmt.c:46
void freedtoa(char *s)
Definition: dmisc.c:99
#define decimalpoint
struct lconv * localeconv(void)
unsigned Long ULong
Definition: gdtoa.h:41
char * decimal_point
Definition: locale.h:20