/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
/*
* AT&T Research
* Glenn Fowler & Phong Vo
*
* common header and implementation for
*
* strtof strtod strtold _sfdscan
* strntof strntod strntold
*
* define these macros to instantiate an implementation:
*
* S2F_function the function name
* S2F_static <0:export =0:extern >0:static
* S2F_type 0:float 1:double 2:long.double
* S2F_qualifier 1 for optional [fFlL] qualifier suffix
* S2F_size 1 for interface with size_t second arg
* S2F_scan 1 for alternate interface with these arguments:
* void* handle
* int (*getchar)(void* handle, int flag)
* exactly one extra (*getchar)() is done, i.e.,
* the caller must do the pushback
* flag==0 get next char
* flag==1 no number seen
* return 0 on error or EOF
*/
#include "sfhdr.h"
/*
* the default is _sfdscan for standalone sfio compatibility
*/
#if !defined(S2F_function)
#ifndef elementsof
#define elementsof(a) (sizeof(a)/sizeof(a[0]))
#endif
#endif
#endif
#if S2F_type == 0
#define S2F_number float
#endif
#if S2F_type == 1
#define S2F_number double
#endif
#if S2F_type == 2
#define S2F_number long double
#endif
#if -S2F_exp_10_min < S2F_exp_10_max
#else
#endif
#if S2F_scan
#define ERR(e)
#define PUT(p)
#define REV(p,t,b)
#define SET(p,t,b)
#else
#define NON(p)
#if S2F_size
#else
#define GET(p) (*p++)
#define REV(p,t,b) (p=t)
#define SET(p,t,b) (t=p)
#endif
#endif
typedef struct S2F_part_s
{
int digits;
} S2F_part_t;
#if !defined(ERANGE)
#endif
#if S2F_static > 0
static
#else
#if S2F_static < 0 || !defined(S2F_static)
#if defined(__EXPORT__)
#define extern __EXPORT__
#endif
extern
#undef extern
#endif
#endif
#if S2F_scan
#if __STD_C
#else
#endif
#else
#if S2F_size
#if __STD_C
#else
#endif
#else
#if __STD_C
#else
#endif
#endif
#endif
{
#if !S2F_scan
register unsigned char* s = (unsigned char*)str;
#if S2F_size
register unsigned char* z = s + size;
int b;
#endif
unsigned char* t;
#endif
register S2F_batch n;
register int c;
register int digits;
register int m;
register unsigned char* cv;
int negative;
int enegative;
int fraction;
int decimal = 0;
int thousand = 0;
int part = 0;
int back_part;
S2F_number v;
S2F_number p;
/*
* radix char and thousands separator are locale specific
*/
SFCVINIT();
/*
* skip initial blanks
*/
SET(s, t, b);
/*
* get the sign
*/
c = GET(s);
/*
* drop leading 0's
*/
digits = 0;
fraction = -1;
if (c == '0')
{
c = GET(s);
if (c == 'x' || c == 'X')
{
/*
* hex floating point -- easy
*/
v = 0;
for (;;)
{
c = GET(s);
{
digits++;
v *= 16;
v += part;
}
else if (c == decimal)
{
decimal = -1;
}
else
break;
}
m = 0;
if (c == 'p' || c == 'P')
{
c = GET(s);
c = GET(s);
while (c >= '0' && c <= '9')
{
m = (m << 3) + (m << 1) + (c - '0');
c = GET(s);
}
if (enegative)
m = -m;
}
#if S2F_qualifier
/*
* consume the optional suffix
*/
switch (c)
{
case 'f':
case 'F':
case 'l':
case 'L':
c = GET(s);
break;
}
#endif
PUT(s);
if (v == 0)
return negative ? -v : v;
if (fraction >= 0)
if (m < S2F_exp_2_min)
{
if ((m -= S2F_exp_2_min) < S2F_exp_2_min)
{
return 0;
}
v = S2F_ldexp(v, S2F_exp_2_min);
}
else if (m > S2F_exp_2_max)
{
}
v = S2F_ldexp(v, m);
goto check;
}
while (c == '0')
c = GET(s);
}
else if (c == decimal)
{
decimal = -1;
fraction = 0;
for (;;)
{
c = GET(s);
if (c != '0')
break;
digits++;
}
}
else if (c == 'i' || c == 'I')
{
{
REV(s, t, b);
PUT(s);
return 0;
}
c = GET(s);
SET(s, t, b);
if (((c) == 'i' || c == 'I') &&
{
c = GET(s);
SET(s, t, b);
}
REV(s, t, b);
PUT(s);
}
else if (c == 'n' || c == 'N')
{
{
REV(s, t, b);
PUT(s);
return 0;
}
PUT(s);
}
else if (c < '1' || c > '9')
{
REV(s, t, b);
PUT(s);
NON(s);
return 0;
}
/*
* consume the integral and fractional parts
*/
n = 0;
m = 0;
for (;;)
{
if (c >= '0' && c <= '9')
{
digits++;
n = (n << 3) + (n << 1) + (c - '0');
{
n = 0;
part++;
}
}
else if (m && (digits - m) != 3)
break;
else if (c == decimal)
{
decimal = -1;
thousand = -1;
m = 0;
}
else if (c != thousand)
break;
else if (!(m = digits))
{
SET(s, t, b);
break;
}
else
{
SET(s, t, b);
back_n = n;
}
c = GET(s);
}
if (m && (digits - m) != 3)
{
REV(s, t, b);
n = back_n;
}
/*
* don't forget the last part
*/
{
part++;
}
/*
* consume the exponent
*/
if (fraction >= 0)
if (c == 'e' || c == 'E')
{
c = GET(s);
c = GET(s);
n = 0;
while (c >= '0' && c <= '9')
{
n = (n << 3) + (n << 1) + (c - '0');
c = GET(s);
}
if (enegative)
digits -= n;
else
digits += n;
}
#if S2F_qualifier
/*
* consume the optional suffix
*/
switch (c)
{
case 'f':
case 'F':
case 'l':
case 'L':
c = GET(s);
break;
}
#endif
PUT(s);
/*
* adjust for at most one multiply per part
* and at most one divide overall
*/
v = 0;
if (!part)
return negative ? -v : v;
digits += m;
else
m = 0;
/*
* combine the parts
*/
while (part--)
{
if (c > S2F_exp_10_max)
{
}
if (c > 0)
{
{
}
#endif
p *= S2F_pow10[c];
}
v += p;
}
if (m)
{
while (m > S2F_exp_10_max)
{
m -= S2F_exp_10_max;
v /= S2F_pow10[S2F_exp_10_max];
}
{
}
#endif
v /= S2F_pow10[m];
}
/*
* check the range
*/
if (v < S2F_min)
{
v = 0;
}
else if (v > S2F_max)
{
v = S2F_inf;
}
/*
* done
*/
return negative ? -v : v;
}