From: ellson Date: Fri, 21 Dec 2007 01:48:33 +0000 (+0000) Subject: No value in making gvprintnum() generally accessible. Merge into gvdevice.c X-Git-Tag: LAST_LIBGRAPH~32^2~4963 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5fac5b8dfca1a80d28bc86473eb27559f0199f3a;p=graphviz No value in making gvprintnum() generally accessible. Merge into gvdevice.c and make static. --- diff --git a/lib/gvc/gvcproc.h b/lib/gvc/gvcproc.h index b8c93f6e4..3599d1b67 100644 --- a/lib/gvc/gvcproc.h +++ b/lib/gvc/gvcproc.h @@ -77,7 +77,6 @@ extern "C" { extern void gvdevice_format(GVJ_t * job); extern void gvdevice_finalize(GVJ_t * job); - extern unsigned char * gvprintnum(int *len, double num); extern void gvdevice_printnum(GVJ_t * job, double num); extern void gvdevice_printpointf(GVJ_t * job, pointf p); extern void gvdevice_printpointflist(GVJ_t * job, pointf *p, int n); diff --git a/lib/gvc/gvdevice.c b/lib/gvc/gvdevice.c index 1cf0d98cc..fe2caf787 100644 --- a/lib/gvc/gvdevice.c +++ b/lib/gvc/gvdevice.c @@ -104,6 +104,104 @@ void gvdevice_printf(GVJ_t * job, const char *format, ...) gvdevice_write(job, buf, len); } + +/* Test with: + * cc -DGVPRINTNUM_TEST gvprintnum.c -o gvprintnum + */ + +#define DECPLACES 2 +#define DECPLACES_SCALE 100 + +/* use macro so maxnegnum is stated just once for both double and string versions */ +#define val_str(n, x) static double n = x; static unsigned char n##str[] = #x; +val_str(maxnegnum, -999999999999999.99) + +/* Note. Returned string is only good until the next call to gvprintnum */ +static unsigned char * gvprintnum (int *len, double number) +{ + static unsigned char tmpbuf[sizeof(maxnegnumstr)]; /* buffer big enough for worst case */ + unsigned char *result = tmpbuf+sizeof(maxnegnumstr); /* init result to end of tmpbuf */ + long int N; + bool showzeros, negative; + int digit, i; + + /* + number limited to a working range: maxnegnum >= n >= -maxnegnum + N = number * DECPLACES_SCALE rounded towards zero, + printing to buffer in reverse direction, + printing "." after DECPLACES + suppressing trailing "0" and "." + */ + + if (number < maxnegnum) { /* -ve limit */ + *len = sizeof(maxnegnumstr)-1; /* len doesn't include terminator */ + return maxnegnumstr;; + } + if (number > -maxnegnum) { /* +ve limit */ + *len = sizeof(maxnegnumstr)-2; /* len doesn't include terminator or sign */ + return maxnegnumstr+1; /* +1 to skip the '-' sign */ + } + number *= DECPLACES_SCALE; /* scale by DECPLACES_SCALE */ + if (number < 0.0) /* round towards zero */ + N = number - 0.5; + else + N = number + 0.5; + if (N == 0) { /* special case for exactly 0 */ + *len = 1; + return (unsigned char *)"0"; + } + if ((negative = (N < 0))) /* avoid "-0" by testing rounded int */ + N = -N; /* make number +ve */ + *--result = '\0'; /* terminate the result string */ + showzeros = false; /* don't print trailing zeros */ + for (i = DECPLACES; N || i > 0; i--) { /* non zero remainder, + or still in fractional part */ + digit = N % 10; /* next least-significant digit */ + N /= 10; + if (digit || showzeros) { /* if digit is non-zero, + or if we are printing zeros */ + *--result = digit | '0'; /* convert digit to ascii */ + showzeros = true; /* from now on we must print zeros */ + } + if (i == 1) { /* if completed fractional part */ + if (showzeros) /* if there was a non-zero fraction */ + *--result = '.'; /* print decimal point */ + showzeros = true; /* print all digits in int part */ + } + } + if (negative) /* print "-" if needed */ + *--result = '-'; + *len = tmpbuf+sizeof(maxnegnumstr)-1 - result; + return result; +} + + +#ifdef GVPRINTNUM_TEST +int main (int argc, char *argv[]) +{ + unsigned char *buf; + int len; + + double test[] = { + -maxnegnum*1.1, -maxnegnum*.9, + 1e8, 10.008, 10, 1, .1, .01, + .006, .005, .004, .001, 1e-8, + 0, -0, + -1e-8, -.001, -.004, -.005, -.006, + -.01, -.1, -1, -10, -10.008, -1e8, + maxnegnum*.9, maxnegnum*1.1 + }; + int i = sizeof(test) / sizeof(test[0]); + + while (i--) { + buf = gvprintnum(&len, test[i]); + fprintf (stdout, "%g = %s %d\n", test[i], buf, len); + } + + return 0; +} +#endif + void gvdevice_printnum(GVJ_t * job, double num) { unsigned char *buf; diff --git a/lib/gvc/gvprintnum.c b/lib/gvc/gvprintnum.c deleted file mode 100644 index 4087fa0c9..000000000 --- a/lib/gvc/gvprintnum.c +++ /dev/null @@ -1,116 +0,0 @@ -/* $Id$ $Revision$ */ -/* vim:set shiftwidth=4 ts=8: */ - -/*********************************************************** - * This software is part of the graphviz package * - * http://www.graphviz.org/ * - * * - * Copyright (c) 1994-2004 AT&T Corp. * - * and is licensed under the * - * Common Public License, Version 1.0 * - * by AT&T Corp. * - * * - * Information and Software Systems Research * - * AT&T Research, Florham Park NJ * - ***********************************************************/ - -/* Test with: - * cc -DGVPRINTNUM_TEST gvprintnum.c -o gvprintnum - */ - -#include -#include -#include - -#define DECPLACES 2 -#define DECPLACES_SCALE 100 - -/* use macro so maxnegnum is stated just once for both double and string versions */ -#define val_str(n, x) static double n = x; static unsigned char n##str[] = #x; -val_str(maxnegnum, -999999999999999.99) - -/* Note. Returned string is only good until the next call to gvprintnum */ -unsigned char * gvprintnum (int *len, double number) -{ - static unsigned char tmpbuf[sizeof(maxnegnumstr)]; /* buffer big enough for worst case */ - unsigned char *result = tmpbuf+sizeof(maxnegnumstr); /* init result to end of tmpbuf */ - long int N; - bool showzeros, negative; - int digit, i; - - /* - number limited to a working range: maxnegnum >= n >= -maxnegnum - N = number * DECPLACES_SCALE rounded towards zero, - printing to buffer in reverse direction, - printing "." after DECPLACES - suppressing trailing "0" and "." - */ - - if (number < maxnegnum) { /* -ve limit */ - *len = sizeof(maxnegnumstr)-1; /* len doesn't include terminator */ - return maxnegnumstr;; - } - if (number > -maxnegnum) { /* +ve limit */ - *len = sizeof(maxnegnumstr)-2; /* len doesn't include terminator or sign */ - return maxnegnumstr+1; /* +1 to skip the '-' sign */ - } - number *= DECPLACES_SCALE; /* scale by DECPLACES_SCALE */ - if (number < 0.0) /* round towards zero */ - N = number - 0.5; - else - N = number + 0.5; - if (N == 0) { /* special case for exactly 0 */ - *len = 1; - return (unsigned char *)"0"; - } - if ((negative = (N < 0))) /* avoid "-0" by testing rounded int */ - N = -N; /* make number +ve */ - *--result = '\0'; /* terminate the result string */ - showzeros = false; /* don't print trailing zeros */ - for (i = DECPLACES; N || i > 0; i--) { /* non zero remainder, - or still in fractional part */ - digit = N % 10; /* next least-significant digit */ - N /= 10; - if (digit || showzeros) { /* if digit is non-zero, - or if we are printing zeros */ - *--result = digit | '0'; /* convert digit to ascii */ - showzeros = true; /* from now on we must print zeros */ - } - if (i == 1) { /* if completed fractional part */ - if (showzeros) /* if there was a non-zero fraction */ - *--result = '.'; /* print decimal point */ - showzeros = true; /* print all digits in int part */ - } - } - if (negative) /* print "-" if needed */ - *--result = '-'; - *len = tmpbuf+sizeof(maxnegnumstr)-1 - result; - return result; -} - - -#ifdef GVPRINTNUM_TEST -int main (int argc, char *argv[]) -{ - unsigned char *buf; - int len; - - double test[] = { - -maxnegnum*1.1, -maxnegnum*.9, - 1e8, 10.008, 10, 1, .1, .01, - .006, .005, .004, .001, 1e-8, - 0, -0, - -1e-8, -.001, -.004, -.005, -.006, - -.01, -.1, -1, -10, -10.008, -1e8, - maxnegnum*.9, maxnegnum*1.1 - }; - int i = sizeof(test) / sizeof(test[0]); - - while (i--) { - buf = gvprintnum(&len, test[i]); - fprintf (stdout, "%g = %s %d\n", test[i], buf, len); - } - - return 0; -} -#endif