Modified printnum function to avoid printing bugs and memory leaks.

This commit is contained in:
pnenzi 2001-12-02 10:03:11 +00:00
parent 8fa2d2f361
commit 65392b77ff
6 changed files with 57 additions and 36 deletions

View File

@ -1,6 +1,7 @@
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
Modified: 2001 Paolo Nenzi (printnum)
**********/
/*
@ -22,6 +23,7 @@ static bool
nameeq(char *n1, char *n2)
{
char buf1[BSIZE_SP], buf2[BSIZE_SP];
int i;
if (eq(n1, n2))
@ -51,8 +53,9 @@ com_diff(wordlist *wl)
struct dvec *v1, *v2;
double d1, d2;
complex c1, c2, c3;
register int i, j;
int i, j;
wordlist *tw;
char numbuf[BSIZE_SP],numbuf2[BSIZE_SP] ,numbuf3[BSIZE_SP], numbuf4[BSIZE_SP]; /* For printnum */
if (!cp_getvar("diff_vntol", VT_REAL, (char *) &vntol))
vntol = 1.0e-6;
@ -204,14 +207,16 @@ com_diff(wordlist *wl)
d2 = v2->v_realdata[i];
if (MAX(fabs(d1), fabs(d2)) * reltol +
tol < fabs(d1 - d2)) {
printnum(numbuf, d1);
fprintf(cp_out,
"%s.%s[%d] = %-15s ",
p1->pl_typename, v1->v_name, i,
printnum(d1));
numbuf);
printnum(numbuf, d2);
fprintf(cp_out,
"%s.%s[%d] = %s\n",
p2->pl_typename, v2->v_name, i,
printnum(d2));
numbuf);
}
} else {
c1 = v1->v_compdata[i];
@ -224,14 +229,20 @@ com_diff(wordlist *wl)
cmax = MAX(cm1, cm2);
if (cmax * reltol +
tol < cmag(&c3)) {
printnum(numbuf, realpart(&c1));
printnum(numbuf2, imagpart(&c1));
printnum(numbuf3, realpart(&c2));
printnum(numbuf4, imagpart(&c2));
fprintf(cp_out,
"%s.%s[%d] = %-10s, %-10s %s.%s[%d] = %-10s, %s\n",
p1->pl_typename, v1->v_name, i,
copy(printnum(realpart(&c1))),
copy(printnum(imagpart(&c1))),
numbuf,
numbuf2,
p2->pl_typename, v2->v_name, i,
copy(printnum(realpart(&c2))),
copy(printnum(imagpart(&c2))));
numbuf3,
numbuf4);
}
}
}

View File

@ -167,6 +167,7 @@ ft_cktcoms(bool terse)
static wordlist twl = { "col", NULL, NULL } ;
struct plot *pl;
int i, found;
char numbuf[BSIZE_SP]; /* For printnum*/
all.wl_next = NULL;
all.wl_word = "all";
@ -207,16 +208,18 @@ ft_cktcoms(bool terse)
v->v_name);
continue;
}
if ((v->v_type == SV_VOLTAGE) && (*(v->v_name)!='@'))
fprintf(cp_out, "\t%-30s%15s\n", v->v_name,
printnum(v->v_realdata[0]));
if ((v->v_type == SV_VOLTAGE) && (*(v->v_name)!='@')) {
printnum(numbuf, v->v_realdata[0]);
fprintf(cp_out, "\t%-30s%15s\n", v->v_name, numbuf);
}
}
fprintf(cp_out, "\n\tSource\tCurrent\n");
fprintf(cp_out, "\t------\t-------\n\n");
for (v = plot_cur->pl_dvecs; v; v = v->v_next)
if (v->v_type == SV_CURRENT)
fprintf(cp_out, "\t%-30s%15s\n", v->v_name,
printnum(v->v_realdata[0]));
if (v->v_type == SV_CURRENT) {
printnum(numbuf, v->v_realdata[0]);
fprintf(cp_out, "\t%-30s%15s\n", v->v_name, numbuf);
}
fprintf(cp_out, "\n");
if (!ft_nomod)
@ -378,6 +381,7 @@ static void
fixdotplot(wordlist *wl)
{
char buf[BSIZE_SP], *s;
char numbuf[128]; /* Printnum Fix */
double *d, d1, d2;
while (wl) {
@ -408,13 +412,14 @@ fixdotplot(wordlist *wl)
wl->wl_next = alloc(struct wordlist);
wl->wl_next->wl_prev = wl;
wl = wl->wl_next;
(void) strcpy(buf, printnum(d1));
wl->wl_word = copy(buf);
printnum(numbuf, d1);
wl->wl_word = copy(numbuf);
wl->wl_next = alloc(struct wordlist);
wl->wl_next->wl_prev = wl;
wl = wl->wl_next;
(void) strcpy(buf, printnum(d2));
wl->wl_word = copy(buf);
printnum(numbuf, d2);
wl->wl_word = copy(numbuf);
}
wl = wl->wl_next;
}

View File

@ -74,6 +74,7 @@ com_print(wordlist *wl)
bool col = TRUE, nobreak = FALSE, noprintscale, plotnames = FALSE;
bool optgiven = FALSE;
char *s, buf[BSIZE_SP], buf2[BSIZE_SP];
char numbuf[BSIZE_SP], numbuf2[BSIZE_SP];
int ngood;
if (wl == NULL)
@ -140,17 +141,20 @@ com_print(wordlist *wl)
ll = 10;
if (v->v_length == 1) {
if (isreal(v)) {
out_printf("%s = %s\n", buf,
printnum(*v->v_realdata));
printnum(numbuf, *v->v_realdata);
out_printf("%s = %s\n", buf, numbuf);
} else {
/*DG: memory leak here copy of the string returned by printnum will never be freed
out_printf("%s = %s,%s\n", buf,
copy(printnum(realpart(v->v_compdata))),
copy(printnum(imagpart(v->v_compdata))));
*/
copy(printnum(imagpart(v->v_compdata)))); */
printnum(numbuf, realpart(v->v_compdata));
printnum(numbuf2, imagpart(v->v_compdata));
out_printf("%s = %s,%s\n", buf,
printnum(realpart(v->v_compdata)),
printnum(imagpart(v->v_compdata)));
numbuf,
numbuf2);
}
@ -158,8 +162,9 @@ com_print(wordlist *wl)
out_printf("%s = ( ", buf);
for (i = 0; i < v->v_length; i++)
if (isreal(v)) {
(void) strcpy(buf,
printnum(v->v_realdata[i]));
printnum(numbuf, v->v_realdata[i]);
(void) strcpy(buf, numbuf);
out_send(buf);
ll += strlen(buf);
ll = (ll + 7) / 8;
@ -171,9 +176,11 @@ com_print(wordlist *wl)
out_send("\t");
} else {
/*DG*/
printnum(numbuf, realpart(&v->v_compdata[i]));
printnum(numbuf2, imagpart(&v->v_compdata[i]));
(void) sprintf(buf, "%s,%s",
printnum(realpart(&v->v_compdata[i])),
printnum(imagpart(&v->v_compdata[i])));
numbuf,
numbuf2);
out_send(buf);
ll += strlen(buf);
ll = (ll + 7) / 8;

View File

@ -31,7 +31,7 @@ Author: 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
extern char *getusername();
extern char *gethome();
extern char *tildexpand();
extern char *printnum();
extern void printnum();
extern int cp_numdgt;
extern void fatal();

View File

@ -1,10 +1,11 @@
/**********
Copyright 1990 Regents of the University of California. All rights reserved.
Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
Modified: 2001 Paolo Nenzi
**********/
/* Print a number in a reasonable form. This is the sort of thing that
* %G does, but more appropriate for spice. Returns static data.
/* Paolo Nenzi 2001: printnum does not returns static data anymore.
* It is up to the caller to allocate space for strings.
*/
#include "ngspice.h"
@ -13,10 +14,8 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
int cp_numdgt = -1;
char *
printnum(double num)
void printnum(char *buf, double num)
{
static char buf[128];
int n;
if (cp_numdgt > 1)
@ -27,6 +26,5 @@ printnum(double num)
n--;
(void) sprintf(buf, "%.*e", n, num);
return (buf);
}

View File

@ -6,6 +6,6 @@
#ifndef PRINTNUM_H_INCLUDED
#define PRINTNUM_H_INCLUDED
char * printnum(double num);
void printnum(char * buf, double num);
#endif