| 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
|
|---|
| 30 | David M. Gay
|
|---|
| 31 | dmg@acm.org
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #include "gdtoaimp.h"
|
|---|
| 35 |
|
|---|
| 36 | #ifdef USE_LOCALE
|
|---|
| 37 | #include "locale.h"
|
|---|
| 38 | #endif
|
|---|
| 39 |
|
|---|
| 40 | char *
|
|---|
| 41 | #ifdef KR_headers
|
|---|
| 42 | g__fmt(b, s, se, decpt, sign) char *b; char *s; char *se; int decpt; ULong sign;
|
|---|
| 43 | #else
|
|---|
| 44 | g__fmt(char *b, char *s, char *se, int decpt, ULong sign)
|
|---|
| 45 | #endif
|
|---|
| 46 | {
|
|---|
| 47 | int i, j, k;
|
|---|
| 48 | char *s0 = s;
|
|---|
| 49 | #ifdef USE_LOCALE
|
|---|
| 50 | char decimalpoint = *localeconv()->decimal_point;
|
|---|
| 51 | #else
|
|---|
| 52 | #define decimalpoint '.'
|
|---|
| 53 | #endif
|
|---|
| 54 | if (sign)
|
|---|
| 55 | *b++ = '-';
|
|---|
| 56 | if (decpt <= -4 || decpt > se - s + 5) {
|
|---|
| 57 | *b++ = *s++;
|
|---|
| 58 | if (*s) {
|
|---|
| 59 | *b++ = decimalpoint;
|
|---|
| 60 | while((*b = *s++) !=0)
|
|---|
| 61 | b++;
|
|---|
| 62 | }
|
|---|
| 63 | *b++ = 'e';
|
|---|
| 64 | /* sprintf(b, "%+.2d", decpt - 1); */
|
|---|
| 65 | if (--decpt < 0) {
|
|---|
| 66 | *b++ = '-';
|
|---|
| 67 | decpt = -decpt;
|
|---|
| 68 | }
|
|---|
| 69 | else
|
|---|
| 70 | *b++ = '+';
|
|---|
| 71 | for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10){}
|
|---|
| 72 | for(;;) {
|
|---|
| 73 | i = decpt / k;
|
|---|
| 74 | *b++ = i + '0';
|
|---|
| 75 | if (--j <= 0)
|
|---|
| 76 | break;
|
|---|
| 77 | decpt -= i*k;
|
|---|
| 78 | decpt *= 10;
|
|---|
| 79 | }
|
|---|
| 80 | *b = 0;
|
|---|
| 81 | }
|
|---|
| 82 | else if (decpt <= 0) {
|
|---|
| 83 | *b++ = decimalpoint;
|
|---|
| 84 | for(; decpt < 0; decpt++)
|
|---|
| 85 | *b++ = '0';
|
|---|
| 86 | while((*b = *s++) !=0)
|
|---|
| 87 | b++;
|
|---|
| 88 | }
|
|---|
| 89 | else {
|
|---|
| 90 | while((*b = *s++) !=0) {
|
|---|
| 91 | b++;
|
|---|
| 92 | if (--decpt == 0 && *s)
|
|---|
| 93 | *b++ = decimalpoint;
|
|---|
| 94 | }
|
|---|
| 95 | for(; decpt > 0; decpt--)
|
|---|
| 96 | *b++ = '0';
|
|---|
| 97 | *b = 0;
|
|---|
| 98 | }
|
|---|
| 99 | freedtoa(s0);
|
|---|
| 100 | return b;
|
|---|
| 101 | }
|
|---|