109 lines
3.6 KiB
C
109 lines
3.6 KiB
C
/*-
|
|
* Copyright (c) 1990 The Regents of the University of California.
|
|
* All rights reserved.
|
|
*
|
|
* This code is derived from software contributed to Berkeley by
|
|
* Chris Torek.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
* must display the following acknowledgement:
|
|
* This product includes software developed by the University of
|
|
* California, Berkeley and its contributors.
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
/*
|
|
* * Flags used during conversion.
|
|
* */
|
|
#define ALT 0x001 /* alternate form */
|
|
#define HEXPREFIX 0x002 /* add 0x or 0X prefix */
|
|
#define LADJUST 0x004 /* left adjustment */
|
|
#define LONGDBL 0x008 /* long double; unimplemented */
|
|
#define LONGINT 0x010 /* long integer */
|
|
#define QUADINT 0x020 /* quad integer */
|
|
#define SHORTINT 0x040 /* short integer */
|
|
#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
|
|
#define FPT 0x100 /* Floating point number */
|
|
|
|
void
|
|
setAlt(int flags)
|
|
{
|
|
flags |= ALT;
|
|
}
|
|
|
|
void
|
|
setFpt(int flags)
|
|
{
|
|
flags |= FPT;
|
|
}
|
|
|
|
extern void __dtoa(double value, int mode, int ndigits, int *decpt, int *dsgn, char **rve, char **buf, int fl_type);
|
|
|
|
void
|
|
bsd_cvt(value, ndigits, flags, sign, decpt, ch, length, buffer)
|
|
double value;
|
|
int ndigits, flags, *decpt, ch, *length;
|
|
char *sign, **buffer;
|
|
{
|
|
int mode, dsgn;
|
|
char *digits, *bp, *rve;
|
|
digits = *buffer;
|
|
|
|
if (ch == 'f') {
|
|
mode = 3; /* ndigits after the decimal point */
|
|
} else {
|
|
/* To obtain ndigits after the decimal point for the 'e'
|
|
* and 'E' formats, round to ndigits + 1 significant
|
|
* figures.
|
|
*/
|
|
if (ch == 'e' || ch == 'E') {
|
|
ndigits++;
|
|
}
|
|
mode = 2; /* ndigits significant digits */
|
|
}
|
|
|
|
if (value < 0) {
|
|
value = -value;
|
|
*sign = '-';
|
|
} else
|
|
*sign = '\000';
|
|
__dtoa(value, mode, ndigits, decpt, &dsgn, &rve, buffer, (ch == 'f' || ch =='F')? 1: 0);
|
|
if ((ch != 'g' && ch != 'G') || flags & ALT) { /* Print trailing zeros */
|
|
bp = digits + ndigits;
|
|
if (ch == 'f') {
|
|
if (*digits == '0' && value)
|
|
*decpt = -ndigits + 1;
|
|
bp += *decpt;
|
|
}
|
|
if (value == 0) /* kludge for __dtoa irregularity */
|
|
rve = bp;
|
|
while (rve < bp)
|
|
*rve++ = '0';
|
|
}
|
|
*length = (int)(rve - digits);
|
|
}
|