Opened binary files for writing using "wb" in all cases

This commit is contained in:
Jim Monte 2019-12-16 14:53:40 -05:00 committed by Holger Vogt
parent de7d292501
commit d4b71500e2
2 changed files with 114 additions and 85 deletions

View File

@ -35,8 +35,7 @@ int raw_prec = -1; /* How many sigfigs to use, default 15 (max). */
/* Write a raw file. We write everything in the plot pointed to. */
void
raw_write(char *name, struct plot *pl, bool app, bool binary)
void raw_write(char *name, struct plot *pl, bool app, bool binary)
{
FILE *fp;
bool realflag = TRUE, writedims;
@ -58,12 +57,12 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
return;
}
if (raw_prec != -1)
if (raw_prec != -1) {
prec = raw_prec;
else
}
else {
prec = DEFPREC;
#if defined(__MINGW32__) || defined(_MSC_VER)
}
/* - Binary file binary write - hvogt 15.03.2000 ---------------------*/
if (binary) {
@ -72,7 +71,8 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
return;
}
fprintf(cp_out, "binary raw file \"%s\"\n", name);
} else {
}
else {
if ((fp = fopen(name, app ? "a" : "w")) == NULL) {
perror(name);
return;
@ -81,19 +81,11 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
}
/* --------------------------------------------------------------------*/
#else
if (!(fp = fopen(name, app ? "a" : "w"))) {
perror(name);
return;
}
#endif
numdims = nvars = length = 0;
for (v = pl->pl_dvecs; v; v = v->v_next) {
if (iscomplex(v))
if (iscomplex(v)) {
realflag = FALSE;
}
nvars++;
/* Find the length and dimensions of the longest vector
* in the plot.
@ -125,20 +117,24 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
fprintf(fp, "Dimensions: %s\n", buf);
}
for (wl = pl->pl_commands; wl; wl = wl->wl_next)
for (wl = pl->pl_commands; wl; wl = wl->wl_next) {
fprintf(fp, "Command: %s\n", wl->wl_word);
}
for (vv = pl->pl_env; vv; vv = vv->va_next) {
wl = cp_varwl(vv);
if (vv->va_type == CP_BOOL) {
fprintf(fp, "Option: %s\n", vv->va_name);
} else {
}
else {
fprintf(fp, "Option: %s = ", vv->va_name);
if (vv->va_type == CP_LIST)
if (vv->va_type == CP_LIST) {
fprintf(fp, "( ");
}
wl_print(wl, fp);
if (vv->va_type == CP_LIST)
if (vv->va_type == CP_LIST) {
fprintf(fp, " )");
}
(void) putc('\n', fp);
}
}
@ -146,8 +142,9 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
/* Before we write the stuff out, make sure that the scale is the first
* in the list.
*/
for (lv = NULL, v = pl->pl_dvecs; v != pl->pl_scale; v = v->v_next)
for (lv = NULL, v = pl->pl_dvecs; v != pl->pl_scale; v = v->v_next) {
lv = v;
}
if (lv) {
lv->v_next = v->v_next;
v->v_next = pl->pl_dvecs;
@ -163,26 +160,34 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
}
fprintf(fp, "\t%d\ti(%s)\t%s", i++, v->v_name, ft_typenames(v->v_type));
if (branch != NULL) *branch = '#';
} else if (v->v_type == SV_VOLTAGE) {
fprintf(fp, "\t%d\t%s\t%s", i++, v->v_name, ft_typenames(v->v_type));
} else {
}
else if (v->v_type == SV_VOLTAGE) {
fprintf(fp, "\t%d\t%s\t%s", i++, v->v_name, ft_typenames(v->v_type));
}
if (v->v_flags & VF_MINGIVEN)
else {
fprintf(fp, "\t%d\t%s\t%s", i++, v->v_name, ft_typenames(v->v_type));
}
if (v->v_flags & VF_MINGIVEN) {
fprintf(fp, " min=%e", v->v_minsignal);
if (v->v_flags & VF_MAXGIVEN)
}
if (v->v_flags & VF_MAXGIVEN) {
fprintf(fp, " max=%e", v->v_maxsignal);
if (v->v_defcolor)
}
if (v->v_defcolor) {
fprintf(fp, " color=%s", v->v_defcolor);
if (v->v_gridtype)
}
if (v->v_gridtype) {
fprintf(fp, " grid=%d", v->v_gridtype);
if (v->v_plottype)
}
if (v->v_plottype) {
fprintf(fp, " plot=%d", v->v_plottype);
}
/* Only write dims if they are different from default. */
writedims = FALSE;
if (v->v_numdims != numdims) {
writedims = TRUE;
} else {
}
else {
for (j = 0; j < numdims; j++)
if (dims[j] != v->v_dims[j])
writedims = TRUE;
@ -204,58 +209,70 @@ raw_write(char *name, struct plot *pl, bool app, bool binary)
dd = (isreal(v) ? v->v_realdata[i] :
realpart(v->v_compdata[i]));
(void) fwrite(&dd, sizeof(double), 1, fp);
} else if (isreal(v)) {
}
else if (isreal(v)) {
dd = v->v_realdata[i];
(void) fwrite(&dd, sizeof(double), 1, fp);
dd = 0.0;
(void) fwrite(&dd, sizeof(double), 1, fp);
} else {
}
else {
dd = realpart(v->v_compdata[i]);
(void) fwrite(&dd, sizeof(double), 1, fp);
dd = imagpart(v->v_compdata[i]);
(void) fwrite(&dd, sizeof(double), 1, fp);
}
} else if (raw_padding) {
}
else if (raw_padding) {
dd = 0.0;
if (realflag) {
(void) fwrite(&dd, sizeof(double), 1, fp);
} else {
}
else {
(void) fwrite(&dd, sizeof(double), 1, fp);
(void) fwrite(&dd, sizeof(double), 1, fp);
}
}
}
}
} else {
}
else {
fprintf(fp, "Values:\n");
for (i = 0; i < length; i++) {
fprintf(fp, " %d", i);
for (v = pl->pl_dvecs; v; v = v->v_next) {
if (i < v->v_length) {
if (realflag)
if (realflag) {
fprintf(fp, "\t%.*e\n", prec,
isreal(v) ? v->v_realdata[i] :
realpart(v->v_compdata[i]));
else if (isreal(v))
}
else if (isreal(v)) {
fprintf(fp, "\t%.*e,0.0\n", prec,
v->v_realdata[i]);
else
}
else {
fprintf(fp, "\t%.*e,%.*e\n", prec,
realpart(v->v_compdata[i]),
prec,
imagpart(v->v_compdata[i]));
} else if (raw_padding) {
if (realflag)
}
}
else if (raw_padding) {
if (realflag) {
fprintf(fp, "\t%.*e\n", prec, 0.0);
else
}
else {
fprintf(fp, "\t%.*e,%.*e\n", prec, 0.0, prec, 0.0);
}
}
}
(void) putc('\n', fp);
}
}
(void) fclose(fp);
}
} /* end of function raw_write */
/* Read a raw file. Returns a list of plot structures. This routine should be

View File

@ -187,14 +187,14 @@ com_pss(wordlist *wl)
#endif
static int
dosim(
char *what, /* in: command (pz,op,dc,ac,tf,tran,sens,disto,noise,run) */
wordlist *wl /* in: command option */
/* global variables in: ft_curckt, ft_circuits,
out: ft_setflag, ft_intrpt, rawfileFp, rawfileBinary,
last_used_rawfile
*/
static int dosim(
char *what, /* in: command
* (pz,op,dc,ac,tf,tran,sens,disto,noise,run) */
wordlist *wl /* in: command option */
/* global variables in: ft_curckt, ft_circuits,
* out: ft_setflag, ft_intrpt, rawfileFp, rawfileBinary,
* last_used_rawfile
*/
)
{
wordlist *ww = NULL;
@ -205,8 +205,9 @@ dosim(
/* set file type to binary or to what is given by environmental
variable SPICE_ASCIIRAWFILE in ivars.c */
bool ascii = AsciiRawFile;
if (eq(what, "run") && wl)
if (eq(what, "run") && wl) {
dofile = TRUE;
}
/* add "what" to beginning of wordlist wl, except "what" equals "run"
and a rawfile name is given (in wl) */
if (!dofile) {
@ -214,10 +215,12 @@ dosim(
}
/* reset output file type according to variable given in spinit */
if (cp_getvar("filetype", CP_STRING, buf, sizeof(buf))) {
if (eq(buf, "binary"))
if (eq(buf, "binary")) {
ascii = FALSE;
else if (eq(buf, "ascii"))
}
else if (eq(buf, "ascii")) {
ascii = TRUE;
}
else {
fprintf(cp_err,
"Warning: strange file type \"%s\" (using \"ascii\")\n", buf);
@ -228,17 +231,19 @@ dosim(
if (!ft_curckt) {
fprintf(cp_err, "Error: there aren't any circuits loaded.\n");
return 1;
} else if (ft_curckt->ci_ckt == NULL) { /* Set noparse? */
}
else if (ft_curckt->ci_ckt == NULL) { /* Set noparse? */
fprintf(cp_err, "Error: circuit not parsed.\n");
return 1;
}
for (ct = ft_circuits; ct; ct = ct->ci_next)
for (ct = ft_circuits; ct; ct = ct->ci_next) {
if (ct->ci_inprogress && (ct != ft_curckt)) {
fprintf(cp_err,
"Warning: losing old state for circuit '%s'\n",
ct->ci_name);
ct->ci_inprogress = FALSE;
}
}
/* "resume" will never occur in ngspice */
if (ft_curckt->ci_inprogress && eq(what, "resume")) {
ft_setflag = TRUE; /* don't allow abort upon interrupt during run */
@ -257,9 +262,10 @@ dosim(
ft_intrpt = FALSE;
/* command "run" is given with rawfile name in wl */
if (dofile) {
if (!*wl->wl_word)
if (!*wl->wl_word) {
rawfileFp = stdout;
#if defined(__MINGW32__) || defined(_MSC_VER)
}
/* ask if binary or ASCII, open file with wb or w */
else if (ascii) {
if ((rawfileFp = fopen(wl->wl_word, "w")) == NULL) {
@ -269,7 +275,7 @@ dosim(
}
fprintf(cp_out, "ASCII raw file \"%s\"\n", wl->wl_word);
}
else if (!ascii) {
else { /* binary */
if ((rawfileFp = fopen(wl->wl_word, "wb")) == NULL) {
perror(wl->wl_word);
ft_setflag = FALSE;
@ -277,26 +283,22 @@ dosim(
}
fprintf(cp_out, "binary raw file \"%s\"\n", wl->wl_word);
}
/*---------------------------------------------------------------------------*/
#else
else if (!(rawfileFp = fopen(wl->wl_word, "w"))) {
perror(wl->wl_word);
ft_setflag = FALSE;
return 1;
}
#endif /* __MINGW32__ */
rawfileBinary = !ascii;
} else {
}
else {
rawfileFp = NULL;
}
/*save rawfile name */
if (last_used_rawfile)
if (last_used_rawfile) {
tfree(last_used_rawfile);
if (rawfileFp)
}
if (rawfileFp) {
last_used_rawfile = copy(wl->wl_word);
else
}
else {
last_used_rawfile = NULL;
}
ft_curckt->ci_inprogress = TRUE;
cp_vset("sim_status", CP_NUM, &err);
@ -308,15 +310,18 @@ dosim(
#ifdef XSPICE
/* gtri - add - 12/12/90 - wbk - record error and return errchk */
g_ipc.run_error = IPC_TRUE;
if (g_ipc.enabled)
if (g_ipc.enabled) {
ipc_send_errchk();
}
/* gtri - end - 12/12/90 */
#endif
} else {
}
else {
ft_curckt->ci_inprogress = FALSE;
}
/* Do a run of the circuit */
} else {
}
else {
err = if_run(ft_curckt->ci_ckt, what, ww, ft_curckt->ci_symtab);
if (err == 1) {
/* The circuit was interrupted somewhere. */
@ -324,17 +329,20 @@ dosim(
#ifdef XSPICE
/* record error and return errchk */
g_ipc.run_error = IPC_TRUE;
if (g_ipc.enabled)
if (g_ipc.enabled) {
ipc_send_errchk();
}
/* gtri - end - 12/12/90 */
#endif
err = 0;
} else if (err == 2) {
}
else if (err == 2) {
fprintf(cp_err, "%s simulation(s) aborted\n", what);
ft_curckt->ci_inprogress = FALSE;
err = 1;
cp_vset("sim_status", CP_NUM, &err);
} else {
}
else {
ft_curckt->ci_inprogress = FALSE;
}
}
@ -342,9 +350,11 @@ dosim(
if (rawfileFp) {
if (ftell(rawfileFp) == 0) {
(void) fclose(rawfileFp);
if (wl)
if (wl) {
(void) unlink(wl->wl_word);
} else {
}
}
else {
(void) fclose(rawfileFp);
}
}
@ -353,22 +363,24 @@ dosim(
/* va: garbage collection: unlink first word (inserted here) and tfree it */
if (!dofile) {
tfree(ww->wl_word);
if (wl)
txfree(ww->wl_word);
if (wl) {
wl->wl_prev = NULL;
tfree(ww);
}
txfree(ww);
}
/* execute the .measure statements */
if (!err && ft_curckt->ci_last_an && ft_curckt->ci_meas)
if (!err && ft_curckt->ci_last_an && ft_curckt->ci_meas) {
do_measure(ft_curckt->ci_last_an, FALSE);
}
return err;
}
} /* end of function dosim */
/* Usage is run [filename] */
void com_run(wordlist *wl)
{
/* ft_getsaves(); */