np-conv.h

Sat, 19 Feb 2000 23:23:29 +0100

author
tuomov
date
Sat, 19 Feb 2000 23:23:29 +0100
changeset 1
6e704fc09528
parent 0
86b7f6f9c5c0
child 3
b1fbfab67908
permissions
-rw-r--r--

trunk: changeset 4
- Added include support in config file parser

- Added scatn()

- Fixed remalloczero()

- Fixed is_end() in numparser2.h -- EOF case was missing

/*
 * libtu/np-conv.h
 *
 * Copyright (c) Tuomo Valkonen 1999-2000.
 * 
 * This file is distributed under the terms of the "Artistic License".
 * See the included file LICENSE for details.
 */

#include <math.h>


#define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN)                          \
 static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \
 {                                                                   \
	if(num->exponent)                                                \
		return E_TOKZ_NOTINT;                                        \
	if(num->nmantissa>0)                                             \
		return E_TOKZ_RANGE;                                         \
                                                                     \
	if(!num->negative){                                              \
		*ret=num->mantissa[0];                                       \
		if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \
			return E_TOKZ_RANGE;                                     \
	}else{                                                           \
		*ret=-num->mantissa[0];                                      \
		if(num->mantissa[0]>-(ulong)MIN)                             \
			return E_TOKZ_RANGE;                                     \
	}                                                                \
	return 0;                                                        \
}

#define FN_NUM_TO_UNSIGNED(T, UMAX, MIN)                         \
 static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \
 {                                                               \
	if(num->exponent)                                            \
		return E_TOKZ_NOTINT;                                    \
	if(num->nmantissa>0)                                         \
		return E_TOKZ_RANGE;                                     \
                                                                 \
	if(!num->negative){                                          \
		*ret=num->mantissa[0];                                   \
		if(num->mantissa[0]>UMAX)                                \
			return E_TOKZ_RANGE;                                 \
	}else{                                                       \
		*ret=-num->mantissa[0];                                  \
		if(!allow_neg || num->mantissa[0]>(ulong)-MIN)           \
			return E_TOKZ_RANGE;                                 \
	}                                                            \
	return 0;                                                    \
}


#define FN_NUM_TO_FLOAT(T, POW)                  \
 static int num_to_##T(T *ret, const NPNum *num) \
 {                                               \
	T d=0;                                       \
	int i;                                       \
	                                             \
	for(i=num->nmantissa;i>=0;i--)               \
		d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \
                                                 \
	d*=POW(10, num->exponent);                   \
	*ret=d;                                      \
	                                             \
	return 0;                                    \
 }

FN_NUM_TO_SIGNED(long, ULONG_MAX, LONG_MAX, LONG_MIN)
FN_NUM_TO_SIGNED(char, UCHAR_MAX, CHAR_MAX, CHAR_MIN)
FN_NUM_TO_FLOAT(double, pow)

#undef NEG

mercurial