qse/ase/lsp/misc.c

598 lines
12 KiB
C
Raw Normal View History

2006-10-23 14:44:43 +00:00
/*
2007-02-24 14:32:44 +00:00
* $Id: misc.c,v 1.10 2007-02-24 14:32:11 bacon Exp $
2007-02-03 10:52:36 +00:00
*
* {License}
2006-10-23 14:44:43 +00:00
*/
2006-10-24 04:22:40 +00:00
#include <ase/lsp/lsp_i.h>
2006-10-23 14:44:43 +00:00
2006-10-24 04:22:40 +00:00
ase_long_t ase_lsp_strxtolong (
ase_lsp_t* lsp, const ase_char_t* str, ase_size_t len,
int base, const ase_char_t** endptr)
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
ase_long_t n = 0;
const ase_char_t* p;
const ase_char_t* end;
ase_size_t rem;
2006-10-23 14:44:43 +00:00
int digit, negative = 0;
2006-10-26 09:31:28 +00:00
ASE_LSP_ASSERT (lsp, base < 37);
2006-10-23 14:44:43 +00:00
p = str;
end = str + len;
/* strip off leading spaces */
2006-10-24 04:22:40 +00:00
/*while (ASE_LSP_ISSPACE(lsp,*p)) p++;*/
2006-10-23 14:44:43 +00:00
/* check for a sign */
2006-10-24 04:22:40 +00:00
/*while (*p != ASE_T('\0')) */
2006-10-23 14:44:43 +00:00
while (p < end)
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('-'))
2006-10-23 14:44:43 +00:00
{
negative = ~negative;
p++;
}
2006-10-24 04:22:40 +00:00
else if (*p == ASE_T('+')) p++;
2006-10-23 14:44:43 +00:00
else break;
}
/* check for a binary/octal/hexadecimal notation */
rem = end - p;
if (base == 0)
{
2006-10-24 04:22:40 +00:00
if (rem >= 1 && *p == ASE_T('0'))
2006-10-23 14:44:43 +00:00
{
p++;
if (rem == 1) base = 8;
2006-10-24 04:22:40 +00:00
else if (*p == ASE_T('x') || *p == ASE_T('X'))
2006-10-23 14:44:43 +00:00
{
p++; base = 16;
}
2006-10-24 04:22:40 +00:00
else if (*p == ASE_T('b') || *p == ASE_T('B'))
2006-10-23 14:44:43 +00:00
{
p++; base = 2;
}
else base = 8;
}
else base = 10;
}
else if (rem >= 2 && base == 16)
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('0') &&
(*(p+1) == ASE_T('x') || *(p+1) == ASE_T('X'))) p += 2;
2006-10-23 14:44:43 +00:00
}
else if (rem >= 2 && base == 2)
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('0') &&
(*(p+1) == ASE_T('b') || *(p+1) == ASE_T('B'))) p += 2;
2006-10-23 14:44:43 +00:00
}
/* process the digits */
2006-10-24 04:22:40 +00:00
/*while (*p != ASE_T('\0'))*/
2006-10-23 14:44:43 +00:00
while (p < end)
{
2006-10-24 04:22:40 +00:00
if (*p >= ASE_T('0') && *p <= ASE_T('9'))
digit = *p - ASE_T('0');
else if (*p >= ASE_T('A') && *p <= ASE_T('Z'))
digit = *p - ASE_T('A') + 10;
else if (*p >= ASE_T('a') && *p <= ASE_T('z'))
digit = *p - ASE_T('a') + 10;
2006-10-23 14:44:43 +00:00
else break;
if (digit >= base) break;
n = n * base + digit;
p++;
}
2006-10-24 04:22:40 +00:00
if (endptr != ASE_NULL) *endptr = p;
2006-10-23 14:44:43 +00:00
return (negative)? -n: n;
}
/*
2006-10-24 04:22:40 +00:00
* ase_lsp_strtoreal is almost a replica of strtod.
2006-10-23 14:44:43 +00:00
*
* strtod.c --
*
* Source code for the "strtod" library procedure.
*
* Copyright (c) 1988-1993 The Regents of the University of California.
* Copyright (c) 1994 Sun Microsystems, Inc.
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for any purpose and without
* fee is hereby granted, provided that the above copyright
* notice appear in all copies. The University of California
* makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without
* express or implied warranty.
*/
#define MAX_EXPONENT 511
2006-10-24 04:22:40 +00:00
ase_real_t ase_lsp_strtoreal (ase_lsp_t* lsp, const ase_char_t* str)
2006-10-23 14:44:43 +00:00
{
/*
* Table giving binary powers of 10. Entry is 10^2^i.
* Used to convert decimal exponents into floating-point numbers.
*/
2006-10-24 04:22:40 +00:00
static ase_real_t powers_of_10[] =
2006-10-23 14:44:43 +00:00
{
10., 100., 1.0e4, 1.0e8, 1.0e16,
1.0e32, 1.0e64, 1.0e128, 1.0e256
};
2006-10-24 04:22:40 +00:00
ase_real_t fraction, dbl_exp, * d;
const ase_char_t* p;
ase_cint_t c;
2006-10-23 14:44:43 +00:00
int exp = 0; /* Esseonent read from "EX" field */
/*
* Esseonent that derives from the fractional part. Under normal
* circumstatnces, it is the negative of the number of digits in F.
* However, if I is very long, the last digits of I get dropped
* (otherwise a long I with a large negative exponent could cause an
* unnecessary overflow on I alone). In this case, frac_exp is
* incremented one for each dropped digit.
*/
int frac_exp;
int mant_size; /* Number of digits in mantissa. */
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
2006-10-24 04:22:40 +00:00
const ase_char_t *pexp; /* Temporarily holds location of exponent in string */
2006-10-23 14:44:43 +00:00
int negative = 0, exp_negative = 0;
p = str;
/* strip off leading blanks */
2006-10-24 04:22:40 +00:00
/*while (ASE_LSP_ISSPACE(lsp,*p)) p++;*/
2006-10-23 14:44:43 +00:00
/* check for a sign */
2006-10-24 04:22:40 +00:00
while (*p != ASE_T('\0'))
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('-'))
2006-10-23 14:44:43 +00:00
{
negative = ~negative;
p++;
}
2006-10-24 04:22:40 +00:00
else if (*p == ASE_T('+')) p++;
2006-10-23 14:44:43 +00:00
else break;
}
/* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point. */
dec_pt = -1;
for (mant_size = 0; ; mant_size++)
{
c = *p;
2006-10-24 04:22:40 +00:00
if (!ASE_LSP_ISDIGIT (lsp, c))
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
if ((c != ASE_T('.')) || (dec_pt >= 0)) break;
2006-10-23 14:44:43 +00:00
dec_pt = mant_size;
}
p++;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pexp = p;
p -= mant_size;
if (dec_pt < 0)
{
dec_pt = mant_size;
}
else
{
mant_size--; /* One of the digits was the point */
}
if (mant_size > 18)
{
frac_exp = dec_pt - 18;
mant_size = 18;
}
else
{
frac_exp = dec_pt - mant_size;
}
if (mant_size == 0)
{
fraction = 0.0;
/*p = str;*/
p = pexp;
goto done;
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mant_size > 9; mant_size--)
{
c = *p;
p++;
2006-10-24 04:22:40 +00:00
if (c == ASE_T('.'))
2006-10-23 14:44:43 +00:00
{
c = *p;
p++;
}
2006-10-24 04:22:40 +00:00
frac1 = 10 * frac1 + (c - ASE_T('0'));
2006-10-23 14:44:43 +00:00
}
frac2 = 0;
for (; mant_size > 0; mant_size--) {
c = *p;
p++;
2006-10-24 04:22:40 +00:00
if (c == ASE_T('.'))
2006-10-23 14:44:43 +00:00
{
c = *p;
p++;
}
2006-10-24 04:22:40 +00:00
frac2 = 10*frac2 + (c - ASE_T('0'));
2006-10-23 14:44:43 +00:00
}
fraction = (1.0e9 * frac1) + frac2;
}
/* Skim off the exponent */
p = pexp;
2006-10-24 04:22:40 +00:00
if ((*p == ASE_T('E')) || (*p == ASE_T('e')))
2006-10-23 14:44:43 +00:00
{
p++;
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('-'))
2006-10-23 14:44:43 +00:00
{
exp_negative = 1;
p++;
}
else
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('+')) p++;
2006-10-23 14:44:43 +00:00
exp_negative = 0;
}
2006-10-24 04:22:40 +00:00
if (!ASE_LSP_ISDIGIT (lsp, *p))
2006-10-23 14:44:43 +00:00
{
/* p = pexp; */
/* goto done; */
goto no_exp;
}
2006-10-24 04:22:40 +00:00
while (ASE_LSP_ISDIGIT (lsp, *p))
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
exp = exp * 10 + (*p - ASE_T('0'));
2006-10-23 14:44:43 +00:00
p++;
}
}
no_exp:
if (exp_negative) exp = frac_exp - exp;
else exp = frac_exp + exp;
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0)
{
exp_negative = 1;
exp = -exp;
}
else exp_negative = 0;
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
dbl_exp = 1.0;
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
{
if (exp & 01) dbl_exp *= *d;
}
if (exp_negative) fraction /= dbl_exp;
else fraction *= dbl_exp;
done:
return (negative)? -fraction: fraction;
}
2006-10-24 04:22:40 +00:00
ase_real_t ase_lsp_strxtoreal (
ase_lsp_t* lsp, const ase_char_t* str, ase_size_t len,
const ase_char_t** endptr)
2006-10-23 14:44:43 +00:00
{
/*
* Table giving binary powers of 10. Entry is 10^2^i.
* Used to convert decimal exponents into floating-point numbers.
*/
2006-10-24 04:22:40 +00:00
static ase_real_t powers_of_10[] =
2006-10-23 14:44:43 +00:00
{
10., 100., 1.0e4, 1.0e8, 1.0e16,
1.0e32, 1.0e64, 1.0e128, 1.0e256
};
2006-10-24 04:22:40 +00:00
ase_real_t fraction, dbl_exp, * d;
const ase_char_t* p, * end;
ase_cint_t c;
2006-10-23 14:44:43 +00:00
int exp = 0; /* Esseonent read from "EX" field */
/*
* Esseonent that derives from the fractional part. Under normal
* circumstatnces, it is the negative of the number of digits in F.
* However, if I is very long, the last digits of I get dropped
* (otherwise a long I with a large negative exponent could cause an
* unnecessary overflow on I alone). In this case, frac_exp is
* incremented one for each dropped digit.
*/
int frac_exp;
int mant_size; /* Number of digits in mantissa. */
int dec_pt; /* Number of mantissa digits BEFORE decimal point */
2006-10-24 04:22:40 +00:00
const ase_char_t *pexp; /* Temporarily holds location of exponent in string */
2006-10-23 14:44:43 +00:00
int negative = 0, exp_negative = 0;
p = str;
end = str + len;
/* Strip off leading blanks and check for a sign */
2006-10-24 04:22:40 +00:00
/*while (ASE_LSP_ISSPACE(lsp,*p)) p++;*/
2006-10-23 14:44:43 +00:00
2006-10-24 04:22:40 +00:00
/*while (*p != ASE_T('\0')) */
2006-10-23 14:44:43 +00:00
while (p < end)
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('-'))
2006-10-23 14:44:43 +00:00
{
negative = ~negative;
p++;
}
2006-10-24 04:22:40 +00:00
else if (*p == ASE_T('+')) p++;
2006-10-23 14:44:43 +00:00
else break;
}
/* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point. */
dec_pt = -1;
/*for (mant_size = 0; ; mant_size++) */
for (mant_size = 0; p < end; mant_size++)
{
c = *p;
2006-10-24 04:22:40 +00:00
if (!ASE_LSP_ISDIGIT (lsp, c))
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
if (c != ASE_T('.') || dec_pt >= 0) break;
2006-10-23 14:44:43 +00:00
dec_pt = mant_size;
}
p++;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pexp = p;
p -= mant_size;
if (dec_pt < 0)
{
dec_pt = mant_size;
}
else
{
mant_size--; /* One of the digits was the point */
}
2006-10-24 04:22:40 +00:00
if (mant_size > 18) /* TODO: is 18 correct for ase_real_t??? */
2006-10-23 14:44:43 +00:00
{
frac_exp = dec_pt - 18;
mant_size = 18;
}
else
{
frac_exp = dec_pt - mant_size;
}
if (mant_size == 0)
{
fraction = 0.0;
/*p = str;*/
p = pexp;
goto done;
}
else
{
int frac1, frac2;
frac1 = 0;
for ( ; mant_size > 9; mant_size--)
{
c = *p;
p++;
2006-10-24 04:22:40 +00:00
if (c == ASE_T('.'))
2006-10-23 14:44:43 +00:00
{
c = *p;
p++;
}
2006-10-24 04:22:40 +00:00
frac1 = 10 * frac1 + (c - ASE_T('0'));
2006-10-23 14:44:43 +00:00
}
frac2 = 0;
for (; mant_size > 0; mant_size--) {
c = *p++;
2006-10-24 04:22:40 +00:00
if (c == ASE_T('.'))
2006-10-23 14:44:43 +00:00
{
c = *p;
p++;
}
2006-10-24 04:22:40 +00:00
frac2 = 10 * frac2 + (c - ASE_T('0'));
2006-10-23 14:44:43 +00:00
}
fraction = (1.0e9 * frac1) + frac2;
}
/* Skim off the exponent */
p = pexp;
2006-10-24 04:22:40 +00:00
if (p < end && (*p == ASE_T('E') || *p == ASE_T('e')))
2006-10-23 14:44:43 +00:00
{
p++;
if (p < end)
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('-'))
2006-10-23 14:44:43 +00:00
{
exp_negative = 1;
p++;
}
else
{
2006-10-24 04:22:40 +00:00
if (*p == ASE_T('+')) p++;
2006-10-23 14:44:43 +00:00
exp_negative = 0;
}
}
else exp_negative = 0;
2006-10-24 04:22:40 +00:00
if (!(p < end && ASE_LSP_ISDIGIT (lsp, *p)))
2006-10-23 14:44:43 +00:00
{
/*p = pexp;*/
/*goto done;*/
goto no_exp;
}
2006-10-24 04:22:40 +00:00
while (p < end && ASE_LSP_ISDIGIT (lsp, *p))
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
exp = exp * 10 + (*p - ASE_T('0'));
2006-10-23 14:44:43 +00:00
p++;
}
}
no_exp:
if (exp_negative) exp = frac_exp - exp;
else exp = frac_exp + exp;
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0)
{
exp_negative = 1;
exp = -exp;
}
else exp_negative = 0;
if (exp > MAX_EXPONENT) exp = MAX_EXPONENT;
dbl_exp = 1.0;
for (d = powers_of_10; exp != 0; exp >>= 1, d++)
{
if (exp & 01) dbl_exp *= *d;
}
if (exp_negative) fraction /= dbl_exp;
else fraction *= dbl_exp;
done:
2006-10-24 04:22:40 +00:00
if (endptr != ASE_NULL) *endptr = p;
2006-10-23 14:44:43 +00:00
return (negative)? -fraction: fraction;
}
2006-10-24 04:22:40 +00:00
ase_size_t ase_lsp_longtostr (
ase_long_t value, int radix, const ase_char_t* prefix,
ase_char_t* buf, ase_size_t size)
2006-10-23 14:44:43 +00:00
{
2006-10-24 04:22:40 +00:00
ase_long_t t, rem;
ase_size_t len, ret, i;
ase_size_t prefix_len;
2006-10-23 14:44:43 +00:00
2007-02-23 10:57:09 +00:00
prefix_len = (prefix != ASE_NULL)? ase_strlen(prefix): 0;
2006-10-23 14:44:43 +00:00
t = value;
if (t == 0)
{
/* zero */
2006-10-24 04:22:40 +00:00
if (buf == ASE_NULL) return prefix_len + 1;
2006-10-23 14:44:43 +00:00
if (size < prefix_len+1)
{
/* buffer too small */
2006-10-24 04:22:40 +00:00
return (ase_size_t)-1;
2006-10-23 14:44:43 +00:00
}
for (i = 0; i < prefix_len; i++) buf[i] = prefix[i];
2006-10-24 04:22:40 +00:00
buf[prefix_len] = ASE_T('0');
if (size > prefix_len+1) buf[prefix_len+1] = ASE_T('\0');
2006-10-23 14:44:43 +00:00
return 1;
}
/* non-zero values */
len = prefix_len;
if (t < 0) { t = -t; len++; }
while (t > 0) { len++; t /= radix; }
2006-10-24 04:22:40 +00:00
if (buf == ASE_NULL)
2006-10-23 14:44:43 +00:00
{
/* if buf is not given, return the number of bytes required */
return len;
}
2006-10-24 04:22:40 +00:00
if (size < len) return (ase_size_t)-1; /* buffer too small */
if (size > len) buf[len] = ASE_T('\0');
2006-10-23 14:44:43 +00:00
ret = len;
t = value;
if (t < 0) t = -t;
while (t > 0)
{
rem = t % radix;
if (rem >= 10)
2006-10-24 04:22:40 +00:00
buf[--len] = (ase_char_t)rem + ASE_T('a') - 10;
2006-10-23 14:44:43 +00:00
else
2006-10-24 04:22:40 +00:00
buf[--len] = (ase_char_t)rem + ASE_T('0');
2006-10-23 14:44:43 +00:00
t /= radix;
}
if (value < 0)
{
for (i = 1; i <= prefix_len; i++)
{
buf[i] = prefix[i-1];
len--;
}
2006-10-24 04:22:40 +00:00
buf[--len] = ASE_T('-');
2006-10-23 14:44:43 +00:00
}
else
{
for (i = 0; i < prefix_len; i++) buf[i] = prefix[i];
}
return ret;
}
2006-10-26 09:31:28 +00:00
int ase_lsp_assertfail (ase_lsp_t* lsp,
2006-10-30 14:31:37 +00:00
const ase_char_t* expr, const ase_char_t* desc,
const ase_char_t* file, int line)
2006-10-23 14:44:43 +00:00
{
2006-10-30 14:31:37 +00:00
if (desc == ASE_NULL)
{
2007-02-23 10:57:09 +00:00
lsp->prmfns.misc.aprintf (
2007-02-24 14:32:44 +00:00
lsp->prmfns.misc.custom_data,
2006-10-30 14:31:37 +00:00
ASE_T("ASSERTION FAILURE AT FILE %s LINE %d\n%s\n"),
file, line, expr);
}
else
{
2007-02-23 10:57:09 +00:00
lsp->prmfns.misc.aprintf (
2007-02-24 14:32:44 +00:00
lsp->prmfns.misc.custom_data,
2006-10-30 14:31:37 +00:00
ASE_T("ASSERTION FAILURE AT FILE %s LINE %d\n%s\n\nDESCRIPTION:\n%s\n"),
file, line, expr, desc);
}
2007-02-23 10:57:09 +00:00
lsp->prmfns.misc.abort (lsp->prmfns.misc.custom_data);
2006-10-23 14:44:43 +00:00
return 0;
}