enable output file selection by adding set_output_path() to every fopen with modes a or w

This commit is contained in:
h_vogt 2017-02-05 21:21:39 +01:00 committed by rlar
parent 02ca0c961a
commit d0607f5524
16 changed files with 202 additions and 79 deletions

View File

@ -8,6 +8,7 @@ Author: 1992 David A. Gates, U. C. Berkeley CAD Group
static char *LogFileName = "cider.log"; static char *LogFileName = "cider.log";
static int LogError = 0; static int LogError = 0;
extern char *set_output_path(char *filename);
void void
LOGmakeEntry(char *name, char *description) LOGmakeEntry(char *name, char *description)
@ -27,14 +28,17 @@ LOGmakeEntry(char *name, char *description)
#ifdef ultrix #ifdef ultrix
if ((fpLog = fopen(LogFileName, "A")) == NULL) { if ((fpLog = fopen(LogFileName, "A")) == NULL) {
#else #else
if ((fpLog = fopen(LogFileName, "a")) == NULL) { /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(LogFileName);
if ((fpLog = fopen(nname, "a")) == NULL) {
#endif #endif
if (!LogError) if (!LogError)
perror(LogFileName); perror(nname);
LogError = 1; LogError = 1;
} else { } else {
fprintf(fpLog, "<%05d> %s: %s\n", procStamp, name, description); fprintf(fpLog, "<%05d> %s: %s\n", procStamp, name, description);
fclose(fpLog); fclose(fpLog);
LogError = 0; LogError = 0;
} }
tfree(nname);
} }

View File

@ -116,11 +116,14 @@ GL_NewViewport(GRAPH *graph)
{ {
hcopygraphid = graph->graphid; hcopygraphid = graph->graphid;
if ((plotfile = fopen((char*) graph->devdep, "w")) == NULL) { /* add user defined path (nname has to be freed after usage) */
perror((char*) graph->devdep); char *nname = set_output_path(graph->devdep);
graph->devdep = NULL; if ((plotfile = fopen((char*) nname, "w")) == NULL) {
perror(nname);
tfree(nname);
return (1); return (1);
} }
tfree(nname);
if (graph->absolute.width) { if (graph->absolute.width) {
/* hardcopying from the screen */ /* hardcopying from the screen */

View File

@ -678,7 +678,10 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile)
/* print out the expanded deck into debug-out2.txt */ /* print out the expanded deck into debug-out2.txt */
if (ft_ngdebug) { if (ft_ngdebug) {
/*debug: print into file*/ /*debug: print into file*/
FILE *fdo = fopen("debug-out2.txt", "w"); /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path("debug-out2.txt");
FILE *fdo = fopen(nname, "w");
tfree(nname);
if (!fdo) if (!fdo)
fprintf(cp_err, "Could not open file debug-out2.txt for writing debug info. \n"); fprintf(cp_err, "Could not open file debug-out2.txt for writing debug info. \n");
else { else {
@ -832,7 +835,10 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile)
/* print out the expanded deck into debug-out3.txt */ /* print out the expanded deck into debug-out3.txt */
if (ft_ngdebug) { if (ft_ngdebug) {
/*debug: print into file*/ /*debug: print into file*/
FILE *fdo = fopen("debug-out3.txt", "w"); /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path("debug-out3.txt");
FILE *fdo = fopen(nname, "w");
tfree(nname);
if (!fdo) if (!fdo)
fprintf(cp_err, "Could not open file debug-out3.txt for writing debug info. \n"); fprintf(cp_err, "Could not open file debug-out3.txt for writing debug info. \n");
else { else {

View File

@ -640,8 +640,11 @@ inp_readall(FILE *fp, char *dir_name, bool comfile, bool intfile, bool *expr_w_t
if (ft_ngdebug) { if (ft_ngdebug) {
/*debug: print into file*/ /*debug: print into file*/
FILE *fd = fopen("debug-out.txt", "w"); /* add user defined path (nname has to be freed after usage) */
if(!fd) char *nname = set_output_path("debug-out.txt");
FILE *fd = fopen(nname, "w");
tfree(nname);
if (!fd)
fprintf(cp_err, "Could not open file debug-out.txt for writing debug info. \n"); fprintf(cp_err, "Could not open file debug-out.txt for writing debug info. \n");
else { else {
struct line *t; struct line *t;
@ -5707,7 +5710,16 @@ tprint(struct line *t, int numb)
char *filename = tprintf("tprint-out%d.txt", numb); char *filename = tprintf("tprint-out%d.txt", numb);
/*debug: print into file*/ /*debug: print into file*/
FILE *fd = fopen(filename, "w"); /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(filename);
FILE *fd = fopen(nname, "w");
tfree(nname);
if (!fd) {
fprintf(cp_err, "Could not open file debug file %s for writing debug info. \n", filename);
tfree(filename);
return;
}
tfree(filename);
for (tmp = t; tmp; tmp = tmp->li_next) for (tmp = t; tmp; tmp = tmp->li_next)
if (*(tmp->li_line) != '*') { if (*(tmp->li_line) != '*') {
fprintf(fd, "%6d %6d %s", tmp->li_linenum_orig, tmp->li_linenum, tmp->li_line); fprintf(fd, "%6d %6d %s", tmp->li_linenum_orig, tmp->li_linenum, tmp->li_line);
@ -5729,7 +5741,6 @@ tprint(struct line *t, int numb)
if (*(tmp->li_line) != '*') if (*(tmp->li_line) != '*')
fprintf(fd, "%s\n",tmp->li_line); fprintf(fd, "%s\n",tmp->li_line);
fclose(fd); fclose(fd);
tfree(filename);
} }

View File

@ -426,8 +426,11 @@ putlogfile(char c, int num, char *t)
if (!logfileS) { if (!logfileS) {
char *fname = tprintf("logfile.%d", ++nblogS); char *fname = tprintf("logfile.%d", ++nblogS);
logfileS = fopen(fname, "w"); /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(fname);
logfileS = fopen(nname, "w");
tfree(fname); tfree(fname);
tfree(nname);
} }
if (logfileS) if (logfileS)

View File

@ -124,11 +124,15 @@ ft_gnuplot(double *xlims, double *ylims, char *filename, char *title, char *xlab
return; return;
} }
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(filename_plt);
/* Open the output gnuplot file. */ /* Open the output gnuplot file. */
if ((file = fopen(filename_plt, "w")) == NULL) { if ((file = fopen(nname, "w")) == NULL) {
perror(filename); perror(nname);
tfree(nname);
return; return;
} }
tfree(nname);
/* Set up the file header. */ /* Set up the file header. */
#if !defined(__MINGW32__) && !defined(_MSC_VER) #if !defined(__MINGW32__) && !defined(_MSC_VER)
@ -210,11 +214,15 @@ ft_gnuplot(double *xlims, double *ylims, char *filename, char *title, char *xlab
strcpy(plotstyle, "lines"); strcpy(plotstyle, "lines");
} }
/* add user defined path (nname has to be freed after usage) */
nname = set_output_path(filename_data);
/* Open the output gnuplot data file. */ /* Open the output gnuplot data file. */
if ((file_data = fopen(filename_data, "w")) == NULL) { if ((file_data = fopen(nname, "w")) == NULL) {
perror(filename); perror(nname);
tfree(nname);
return; return;
} }
tfree(nname);
fprintf(file, "set format y \"%%g\"\n"); fprintf(file, "set format y \"%%g\"\n");
fprintf(file, "set format x \"%%g\"\n"); fprintf(file, "set format x \"%%g\"\n");
fprintf(file, "plot "); fprintf(file, "plot ");
@ -357,11 +365,15 @@ ft_writesimple(double *xlims, double *ylims, char *filename, char *title, char *
maxlen = MAX(v->v_scale->v_length, maxlen); maxlen = MAX(v->v_scale->v_length, maxlen);
} }
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(filename);
/* Open the output data file. */ /* Open the output data file. */
if ((file_data = fopen(filename, appendwrite ? "a" : "w")) == NULL) { if ((file_data = fopen(nname, appendwrite ? "a" : "w")) == NULL) {
perror(filename); perror(nname);
tfree(nname);
return; return;
} }
tfree(nname);
/* If option numdgt is set, use it for printout precision. */ /* If option numdgt is set, use it for printout precision. */
if (cp_numdgt > 0) if (cp_numdgt > 0)

View File

@ -45,11 +45,15 @@ Plt5_Init(void)
int int
Plt5_NewViewport(GRAPH *graph) Plt5_NewViewport(GRAPH *graph)
{ {
if ((plotfile = fopen((char*) graph->devdep, "w")) == NULL) { /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path((char*) graph->devdep);
if ((plotfile = fopen(nname, "w")) == NULL) {
graph->devdep = NULL; graph->devdep = NULL;
perror((char*) graph->devdep); perror((char*) graph->devdep);
tfree(nname);
return (1); return (1);
} }
tfree(nname);
if (graph->absolute.width) { if (graph->absolute.width) {

View File

@ -13,6 +13,7 @@ Author: 1992 David A. Gates, U. C. Berkeley CAD Group
#include "ngspice/dvec.h" #include "ngspice/dvec.h"
#include "ngspice/fteparse.h" #include "ngspice/fteparse.h"
#include "xgraph.h" #include "xgraph.h"
#include "ngspice/fteext.h"
#define XG_MAXVECTORS 64 #define XG_MAXVECTORS 64
@ -80,10 +81,14 @@ ft_xgraph(double *xlims, double *ylims, char *filename, char *title, char *xlabe
} }
/* Open the output file. */ /* Open the output file. */
if ((file = fopen(filename, "w")) == NULL) { /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(filename);
if ((file = fopen(nname, "w")) == NULL) {
perror(filename); perror(filename);
tfree(nname);
return; return;
} }
tfree(nname);
/* Set up the file header. */ /* Set up the file header. */
if (title) { if (title) {

View File

@ -168,11 +168,15 @@ PS_NewViewport(GRAPH *graph)
int x1, x2, y1, y2; int x1, x2, y1, y2;
hcopygraphid = graph->graphid; hcopygraphid = graph->graphid;
/* devdep initially contains name of output file */ /* devdep initially contains name of output file */
if ((plotfile = fopen((char*)graph->devdep, "w")) == NULL) { /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path((char*)graph->devdep);
if ((plotfile = fopen(nname, "w")) == NULL) {
perror((char*)graph->devdep); perror((char*)graph->devdep);
graph->devdep = NULL; graph->devdep = NULL;
tfree(nname);
return (1); return (1);
} }
tfree(nname);
if (graph->absolute.width) { if (graph->absolute.width) {
/* hardcopying from the screen */ /* hardcopying from the screen */

View File

@ -18,6 +18,7 @@ Author: 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
#include "rawfile.h" #include "rawfile.h"
#include "variable.h" #include "variable.h"
#include "../misc/misc_time.h" #include "../misc/misc_time.h"
#include <sys/stat.h>
static void fixdims(struct dvec *v, char *s); static void fixdims(struct dvec *v, char *s);
@ -755,10 +756,15 @@ spar_write(char *name, struct plot *pl, double Rbaseval)
}*/ }*/
} }
if ((fp = fopen(name, "w")) == NULL) { /* add user defined path (nnmae has to be freed after usage) */
perror(name); char *nname = set_output_path(name);
if ((fp = fopen(nname, "w")) == NULL) {
perror(nname);
tfree(nname);
return; return;
} }
tfree(nname);
fprintf(fp, "!2-port S-parameter file\n"); fprintf(fp, "!2-port S-parameter file\n");
fprintf(fp, "!Title: %s\n", pl->pl_title); fprintf(fp, "!Title: %s\n", pl->pl_title);
@ -812,6 +818,9 @@ char *
set_output_path(char *filename) set_output_path(char *filename)
{ {
char varpath[BSIZE_SP]; char varpath[BSIZE_SP];
char *ret;
struct stat st;
char *fpath, *dirloc = NULL;
#if defined(__MINGW32__) || defined(_MSC_VER) #if defined(__MINGW32__) || defined(_MSC_VER)
/* If variable 'mingwpath' is set: convert mingw /d/... to d:/... */ /* If variable 'mingwpath' is set: convert mingw /d/... to d:/... */
@ -825,11 +834,32 @@ set_output_path(char *filename)
#endif #endif
if (is_absolute_pathname(filename)) if (is_absolute_pathname(filename))
return copy(filename); ret = copy(filename);
else if (cp_getvar("outputpath", CP_STRING, &varpath)) else if (cp_getvar("outputpath", CP_STRING, &varpath))
return tprintf("%s/%s", varpath, filename); ret = tprintf("%s%c%s", varpath, DIR_TERM, filename);
else if (Outp_Path) else if (Outp_Path)
return tprintf("%s/%s", Outp_Path, filename); ret = tprintf("%s%c%s", Outp_Path, DIR_TERM, filename);
else else
return copy(filename); ret = copy(filename);
/* get path string */
dirloc = strrchr(ret, DIR_TERM);
#if defined(__MINGW32__) || defined(_MSC_VER)
if(!dirloc)
dirloc = strrchr(ret, DIR_TERM_LINUX);
#endif
if (dirloc)
fpath = copy_substring(ret, dirloc);
else
fpath = copy(ret);
/* test if path exists */
if (stat(fpath, &st) == 0) {
tfree(fpath);
return ret;
}
else {
fprintf(cp_err, "Error: Output path %s does not exist\n", fpath);
tfree(ret);
tfree(fpath);
controlled_exit(1);
}
} }

View File

@ -261,33 +261,41 @@ dosim(
if (dofile) { if (dofile) {
if (!*wl->wl_word) if (!*wl->wl_word)
rawfileFp = stdout; rawfileFp = stdout;
#if defined(__MINGW32__) || defined(_MSC_VER)
/* ask if binary or ASCII, open file with wb or w */ /* ask if binary or ASCII, open file with wb or w */
else if (ascii) { else {
if ((rawfileFp = fopen(wl->wl_word, "w")) == NULL) { /* add user defined path (nname has to be freed after usage) */
perror(wl->wl_word); char *nname = set_output_path(wl->wl_word);
ft_setflag = FALSE; #if defined(__MINGW32__) || defined(_MSC_VER)
return 1; if (ascii) {
if ((rawfileFp = fopen(nname, "w")) == NULL) {
perror(wl->wl_word);
ft_setflag = FALSE;
tfree(nname);
return 1;
}
fprintf(cp_out, "ASCII raw file \"%s\"\n", nname);
} }
fprintf(cp_out, "ASCII raw file \"%s\"\n", wl->wl_word); else {
} if ((rawfileFp = fopen(nname, "wb")) == NULL) {
else if (!ascii) { perror(wl->wl_word);
if ((rawfileFp = fopen(wl->wl_word, "wb")) == NULL) { ft_setflag = FALSE;
perror(wl->wl_word); tfree(nname);
ft_setflag = FALSE; return 1;
return 1; }
fprintf(cp_out, "binary raw file \"%s\"\n", nname);
} }
fprintf(cp_out, "binary raw file \"%s\"\n", wl->wl_word);
}
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#else #else
else if (!(rawfileFp = fopen(wl->wl_word, "w"))) { if (!(rawfileFp = fopen(nname, "w"))) {
setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE); setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE);
perror(wl->wl_word); perror(wl->wl_word);
ft_setflag = FALSE; ft_setflag = FALSE;
return 1; tfree(nname);
} return 1;
}
#endif /* __MINGW32__ */ #endif /* __MINGW32__ */
tfree(nname);
}
rawfileBinary = !ascii; rawfileBinary = !ascii;
} else { } else {
rawfileFp = NULL; rawfileFp = NULL;

View File

@ -106,30 +106,41 @@ com_resume(wordlist *wl)
rawfileFp = stdout; rawfileFp = stdout;
#if defined(__MINGW32__) || defined(_MSC_VER) #if defined(__MINGW32__) || defined(_MSC_VER)
/* ask if binary or ASCII, open file with w or wb hvogt 15.3.2000 */ /* ask if binary or ASCII, open file with w or wb hvogt 15.3.2000 */
else if (ascii) { else {
if ((rawfileFp = fopen(last_used_rawfile, "a")) == NULL) { /* add user defined path (nname has to be freed after usage) */
setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE); char *nname = set_output_path(last_used_rawfile);
perror(last_used_rawfile); if (ascii) {
ft_setflag = FALSE; if ((rawfileFp = fopen(nname, "a")) == NULL) {
return; setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE);
perror(last_used_rawfile);
ft_setflag = FALSE;
tfree(nname);
return;
}
} }
} else if (!ascii) { else {
if ((rawfileFp = fopen(last_used_rawfile, "ab")) == NULL) { if ((rawfileFp = fopen(nname, "ab")) == NULL) {
setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE); setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE);
perror(last_used_rawfile); perror(last_used_rawfile);
ft_setflag = FALSE; ft_setflag = FALSE;
return; tfree(nname);
return;
}
} }
} /*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
#else #else
else if (!(rawfileFp = fopen(last_used_rawfile, "a"))) { else {
setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE); char *nname = set_output_path(last_used_rawfile);
perror(last_used_rawfile); if (!(rawfileFp = fopen(nname, "a"))) {
ft_setflag = FALSE; setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE);
return; perror(last_used_rawfile);
} ft_setflag = FALSE;
tfree(nname);
return;
}
#endif #endif
tfree(nname);
}
rawfileBinary = !ascii; rawfileBinary = !ascii;
} else { } else {
rawfileFp = NULL; rawfileFp = NULL;

View File

@ -1620,9 +1620,10 @@ void com_snsave(wordlist *wl)
fprintf(cp_err, "Only saving of tran analysis is implemented\n"); fprintf(cp_err, "Only saving of tran analysis is implemented\n");
return; return;
} }
/* add user defined path (nname has to be freed after usage) */
file = fopen(wl->wl_word, "wb"); char *nname = set_output_path(wl->wl_word);
file = fopen(nname, "wb");
tfree(nname);
if (!file) { if (!file) {
fprintf(cp_err, fprintf(cp_err,
"Error: Couldn't open \"%s\" for writing\n", wl->wl_word); "Error: Couldn't open \"%s\" for writing\n", wl->wl_word);

View File

@ -11,6 +11,7 @@
#include "quote.h" #include "quote.h"
#include "ngspice/cpextern.h" #include "ngspice/cpextern.h"
#include "streams.h" #include "streams.h"
#include "ngspice/fteext.h"
bool cp_debug = FALSE; bool cp_debug = FALSE;
@ -134,9 +135,11 @@ cp_redirect(wordlist *wl)
fprintf(stderr, "Error: %s: file exists\n", fname); fprintf(stderr, "Error: %s: file exists\n", fname);
goto error; goto error;
} }
/* add user defined path (nname has to be freed after usage) */
fp = fopen(fname, append ? "a" : "w+"); char *nname = set_output_path(fname);
fp = fopen(nname, append ? "a" : "w+");
tfree(fname); tfree(fname);
tfree(nname);
if (!fp) { if (!fp) {
perror(fname); perror(fname);

View File

@ -1039,7 +1039,10 @@ main(int argc, char **argv)
/* Open the log file */ /* Open the log file */
#ifdef HAS_WINGUI #ifdef HAS_WINGUI
/* flogp used by winmain's putc which writes to file 'log_file' */ /* flogp used by winmain's putc which writes to file 'log_file' */
flogp = fopen(log_file, "w"); /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(log_file);
flogp = fopen(nname, "w");
tfree(nname);
if (!flogp) { if (!flogp) {
perror(log_file); perror(log_file);
sp_shutdown(EXIT_BAD); sp_shutdown(EXIT_BAD);
@ -1060,8 +1063,11 @@ main(int argc, char **argv)
fprintf(stdout, "\nSOA warnings go to log-file: %s\n", soa_log_file); fprintf(stdout, "\nSOA warnings go to log-file: %s\n", soa_log_file);
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(soa_log_file);
/* Open the soa log file */ /* Open the soa log file */
slogp = fopen(soa_log_file, "w"); slogp = fopen(nname, "w");
tfree(nname);
if (!slogp) { if (!slogp) {
perror(soa_log_file); perror(soa_log_file);
sp_shutdown(EXIT_BAD); sp_shutdown(EXIT_BAD);
@ -1229,7 +1235,10 @@ main(int argc, char **argv)
if (tempfile == NULL) { if (tempfile == NULL) {
tpf = smktemp("sp"); tpf = smktemp("sp");
tempfile = fopen(tpf, "w+bTD"); /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(tpf);
tempfile = fopen(nname, "w+bTD");
tfree(nname);
if (tempfile == NULL) { if (tempfile == NULL) {
fprintf(stderr, "Could not open a temporary file to save and use optional arguments."); fprintf(stderr, "Could not open a temporary file to save and use optional arguments.");
sp_shutdown(EXIT_BAD); sp_shutdown(EXIT_BAD);

View File

@ -60,6 +60,7 @@ int Printer_Width = PRINTER_WIDTH;
#include "ngspice/wstdio.h" #include "ngspice/wstdio.h"
#endif #endif
extern char *set_output_path(char *filename);
#if DOCUMENTATION #if DOCUMENTATION
@ -449,8 +450,13 @@ spFileMatrix(MatrixPtr Matrix, char *File, char *Label, int Reordered,
assert( IS_SPARSE( Matrix ) ); assert( IS_SPARSE( Matrix ) );
/* Open file matrix file in write mode. */ /* Open file matrix file in write mode. */
if ((pMatrixFile = fopen(File, "w")) == NULL) /* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(File);
if ((pMatrixFile = fopen(nname, "w")) == NULL) {
SP_FREE(nname);
return 0; return 0;
}
SP_FREE(nname);
/* Output header. */ /* Output header. */
Size = Matrix->Size; Size = Matrix->Size;
@ -604,10 +610,13 @@ spFileVector(MatrixPtr Matrix, char *File, RealVector RHS, RealVector iRHS)
assert( IS_SPARSE( Matrix ) && RHS != NULL); assert( IS_SPARSE( Matrix ) && RHS != NULL);
if (File) { if (File) {
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(File);
/* Open File in append mode. */ /* Open File in append mode. */
pMatrixFile = fopen(File,"a"); pMatrixFile = fopen(nname,"a");
SP_FREE(nname);
if (pMatrixFile == NULL) if (pMatrixFile == NULL)
return 0; return 0;
} }
else else
pMatrixFile=stdout; pMatrixFile=stdout;