?RCS: $Id: d_gconvert.U 1 2006-08-24 12:32:52Z rmanfredi $ ?RCS: ?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi ?RCS: ?RCS: You may redistribute only under the terms of the Artistic Licence, ?RCS: as specified in the README file that comes with the distribution. ?RCS: You may reuse parts of this distribution only within the terms of ?RCS: that same Artistic Licence; a copy of which may be found at the root ?RCS: of the source tree for dist 4.0. ?RCS: ?RCS: Original Author: Andy Dougherty ?RCS: ?RCS: $Log: d_gconvert.U,v $ ?RCS: Revision 3.0.1.3 1997/02/28 15:33:38 ram ?RCS: patch61: integrated new unit from perl5 ?RCS: ?RCS: Revision 3.0.1.2 1995/07/25 13:55:59 ram ?RCS: patch56: improved comments about the Gconvert macro (ADO) ?RCS: patch56: force compile-link test since it may exist but be unusable (ADO) ?RCS: ?RCS: Revision 3.0.1.1 1994/10/29 16:12:51 ram ?RCS: patch36: created by ADO ?RCS: ?MAKE:d_Gconvert: cat cc ccflags ldflags libs Inlibc rm _o ?MAKE: -pick add $@ %< ?S:d_Gconvert: ?S: This variable holds what Gconvert is defined as to convert ?S: floating point numbers into strings. It could be 'gconvert' ?S: or a more complex macro emulating gconvert with gcvt() or sprintf. ?S:. ?C:Gconvert: ?C: This preprocessor macro is defined to convert a floating point ?C: number to a string without a trailing decimal point. This ?C: emulates the behavior of sprintf("%g"), but is sometimes much more ?C: efficient. If gconvert() is not available, but gcvt() drops the ?C: trailing decimal point, then gcvt() is used. If all else fails, ?C: a macro using sprintf("%g") is used. Arguments for the Gconvert ?C: macro are: value, number of digits, whether trailing zeros should ?C: be retained, and the output buffer. ?C: Possible values are: ?C: d_Gconvert='gconvert((x),(n),(t),(b))' ?C: d_Gconvert='gcvt((x),(n),(b))' ?C: d_Gconvert='sprintf((b),"%.*g",(n),(x))' ?C: The last two assume trailing zeros should not be kept. ?C:. ?H:#define Gconvert(x,n,t,b) $d_Gconvert ?H:. ?T: xxx_list xxx_convert ?F:!try ?X: : Check how to convert floats to strings. echo " " echo "Checking for an efficient way to convert floats to strings." ?X: We want to be sure to drop trailing decimal points (perl5 ?X: needs this). $cat >try.c <<'EOP' #ifdef TRY_gconvert #define Gconvert(x,n,t,b) gconvert((x),(n),(t),(b)) char *myname = "gconvert"; #endif #ifdef TRY_gcvt #define Gconvert(x,n,t,b) gcvt((x),(n),(b)) char *myname = "gcvt"; #endif #ifdef TRY_sprintf #define Gconvert(x,n,t,b) sprintf((b),"%.*g",(n),(x)) char *myname = "sprintf"; #endif #include int checkit(expect, got) char *expect; char *got; { if (strcmp(expect, got)) { printf("%s oddity: Expected %s, got %s\n", myname, expect, got); exit(1); } } int int main() { char buf[64]; buf[63] = '\0'; /* This must be 1st test on (which?) platform */ /* Alan Burlison */ Gconvert(0.1, 8, 0, buf); checkit("0.1", buf); Gconvert(1.0, 8, 0, buf); checkit("1", buf); Gconvert(0.0, 8, 0, buf); checkit("0", buf); Gconvert(-1.0, 8, 0, buf); checkit("-1", buf); /* Some Linux gcvt's give 1.e+5 here. */ Gconvert(100000.0, 8, 0, buf); checkit("100000", buf); /* Some Linux gcvt's give -1.e+5 here. */ Gconvert(-100000.0, 8, 0, buf); checkit("-100000", buf); exit(0); } EOP ?X: List of order in which to search for functions. ?X: Usual order of efficiency is gconvert gcvt sprintf ?X: Respect a previous or hinted value. case "$d_Gconvert" in gconvert*) xxx_list='gconvert gcvt sprintf' ;; gcvt*) xxx_list='gcvt gconvert sprintf' ;; sprintf*) xxx_list='sprintf gconvert gcvt' ;; *) xxx_list='gconvert gcvt sprintf' ;; esac for xxx_convert in $xxx_list; do echo "Trying $xxx_convert" $rm -f try try$_o if $cc $ccflags -DTRY_$xxx_convert $ldflags -o try \ try.c $libs > /dev/null 2>&1 ; then echo "$xxx_convert" found. >&4 if ./try; then echo "I'll use $xxx_convert to convert floats into a string." >&4 break; else echo "...But $xxx_convert didn't work as I expected." fi else echo "$xxx_convert NOT found." >&4 fi done case "$xxx_convert" in gconvert) d_Gconvert='gconvert((x),(n),(t),(b))' ;; gcvt) d_Gconvert='gcvt((x),(n),(b))' ;; *) d_Gconvert='sprintf((b),"%.*g",(n),(x))' ;; esac