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 int LogError = 0;
extern char *set_output_path(char *filename);
void
LOGmakeEntry(char *name, char *description)
@ -27,14 +28,17 @@ LOGmakeEntry(char *name, char *description)
#ifdef ultrix
if ((fpLog = fopen(LogFileName, "A")) == NULL) {
#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
if (!LogError)
perror(LogFileName);
perror(nname);
LogError = 1;
} else {
fprintf(fpLog, "<%05d> %s: %s\n", procStamp, name, description);
fclose(fpLog);
LogError = 0;
}
tfree(nname);
}

View File

@ -116,11 +116,14 @@ GL_NewViewport(GRAPH *graph)
{
hcopygraphid = graph->graphid;
if ((plotfile = fopen((char*) graph->devdep, "w")) == NULL) {
perror((char*) graph->devdep);
graph->devdep = NULL;
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(graph->devdep);
if ((plotfile = fopen((char*) nname, "w")) == NULL) {
perror(nname);
tfree(nname);
return (1);
}
tfree(nname);
if (graph->absolute.width) {
/* 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 */
if (ft_ngdebug) {
/*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)
fprintf(cp_err, "Could not open file debug-out2.txt for writing debug info. \n");
else {
@ -832,7 +835,10 @@ inp_spsource(FILE *fp, bool comfile, char *filename, bool intfile)
/* print out the expanded deck into debug-out3.txt */
if (ft_ngdebug) {
/*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)
fprintf(cp_err, "Could not open file debug-out3.txt for writing debug info. \n");
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) {
/*debug: print into file*/
FILE *fd = fopen("debug-out.txt", "w");
if(!fd)
/* add user defined path (nname has to be freed after usage) */
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");
else {
struct line *t;
@ -5707,7 +5710,16 @@ tprint(struct line *t, int numb)
char *filename = tprintf("tprint-out%d.txt", numb);
/*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)
if (*(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) != '*')
fprintf(fd, "%s\n",tmp->li_line);
fclose(fd);
tfree(filename);
}

View File

@ -426,8 +426,11 @@ putlogfile(char c, int num, char *t)
if (!logfileS) {
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(nname);
}
if (logfileS)

View File

@ -124,11 +124,15 @@ ft_gnuplot(double *xlims, double *ylims, char *filename, char *title, char *xlab
return;
}
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(filename_plt);
/* Open the output gnuplot file. */
if ((file = fopen(filename_plt, "w")) == NULL) {
perror(filename);
if ((file = fopen(nname, "w")) == NULL) {
perror(nname);
tfree(nname);
return;
}
tfree(nname);
/* Set up the file header. */
#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");
}
/* add user defined path (nname has to be freed after usage) */
nname = set_output_path(filename_data);
/* Open the output gnuplot data file. */
if ((file_data = fopen(filename_data, "w")) == NULL) {
perror(filename);
if ((file_data = fopen(nname, "w")) == NULL) {
perror(nname);
tfree(nname);
return;
}
tfree(nname);
fprintf(file, "set format y \"%%g\"\n");
fprintf(file, "set format x \"%%g\"\n");
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);
}
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(filename);
/* Open the output data file. */
if ((file_data = fopen(filename, appendwrite ? "a" : "w")) == NULL) {
perror(filename);
if ((file_data = fopen(nname, appendwrite ? "a" : "w")) == NULL) {
perror(nname);
tfree(nname);
return;
}
tfree(nname);
/* If option numdgt is set, use it for printout precision. */
if (cp_numdgt > 0)

View File

@ -45,11 +45,15 @@ Plt5_Init(void)
int
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;
perror((char*) graph->devdep);
tfree(nname);
return (1);
}
tfree(nname);
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/fteparse.h"
#include "xgraph.h"
#include "ngspice/fteext.h"
#define XG_MAXVECTORS 64
@ -80,10 +81,14 @@ ft_xgraph(double *xlims, double *ylims, char *filename, char *title, char *xlabe
}
/* 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);
tfree(nname);
return;
}
tfree(nname);
/* Set up the file header. */
if (title) {

View File

@ -168,11 +168,15 @@ PS_NewViewport(GRAPH *graph)
int x1, x2, y1, y2;
hcopygraphid = graph->graphid;
/* 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);
graph->devdep = NULL;
tfree(nname);
return (1);
}
tfree(nname);
if (graph->absolute.width) {
/* hardcopying from the screen */

View File

@ -18,6 +18,7 @@ Author: 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
#include "rawfile.h"
#include "variable.h"
#include "../misc/misc_time.h"
#include <sys/stat.h>
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) {
perror(name);
/* add user defined path (nnmae has to be freed after usage) */
char *nname = set_output_path(name);
if ((fp = fopen(nname, "w")) == NULL) {
perror(nname);
tfree(nname);
return;
}
tfree(nname);
fprintf(fp, "!2-port S-parameter file\n");
fprintf(fp, "!Title: %s\n", pl->pl_title);
@ -812,6 +818,9 @@ char *
set_output_path(char *filename)
{
char varpath[BSIZE_SP];
char *ret;
struct stat st;
char *fpath, *dirloc = NULL;
#if defined(__MINGW32__) || defined(_MSC_VER)
/* If variable 'mingwpath' is set: convert mingw /d/... to d:/... */
@ -825,11 +834,32 @@ set_output_path(char *filename)
#endif
if (is_absolute_pathname(filename))
return copy(filename);
ret = copy(filename);
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)
return tprintf("%s/%s", Outp_Path, filename);
ret = tprintf("%s%c%s", Outp_Path, DIR_TERM, filename);
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 (!*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) {
perror(wl->wl_word);
ft_setflag = FALSE;
return 1;
else {
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(wl->wl_word);
#if defined(__MINGW32__) || defined(_MSC_VER)
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 (!ascii) {
if ((rawfileFp = fopen(wl->wl_word, "wb")) == NULL) {
perror(wl->wl_word);
ft_setflag = FALSE;
return 1;
else {
if ((rawfileFp = fopen(nname, "wb")) == NULL) {
perror(wl->wl_word);
ft_setflag = FALSE;
tfree(nname);
return 1;
}
fprintf(cp_out, "binary raw file \"%s\"\n", nname);
}
fprintf(cp_out, "binary raw file \"%s\"\n", wl->wl_word);
}
/*---------------------------------------------------------------------------*/
#else
else if (!(rawfileFp = fopen(wl->wl_word, "w"))) {
setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE);
perror(wl->wl_word);
ft_setflag = FALSE;
return 1;
}
if (!(rawfileFp = fopen(nname, "w"))) {
setvbuf(rawfileFp, rawfileBuf, _IOFBF, RAWBUF_SIZE);
perror(wl->wl_word);
ft_setflag = FALSE;
tfree(nname);
return 1;
}
#endif /* __MINGW32__ */
tfree(nname);
}
rawfileBinary = !ascii;
} else {
rawfileFp = NULL;

View File

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

View File

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

View File

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

View File

@ -1039,7 +1039,10 @@ main(int argc, char **argv)
/* Open the log file */
#ifdef HAS_WINGUI
/* 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) {
perror(log_file);
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);
/* add user defined path (nname has to be freed after usage) */
char *nname = set_output_path(soa_log_file);
/* Open the soa log file */
slogp = fopen(soa_log_file, "w");
slogp = fopen(nname, "w");
tfree(nname);
if (!slogp) {
perror(soa_log_file);
sp_shutdown(EXIT_BAD);
@ -1229,7 +1235,10 @@ main(int argc, char **argv)
if (tempfile == NULL) {
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) {
fprintf(stderr, "Could not open a temporary file to save and use optional arguments.");
sp_shutdown(EXIT_BAD);

View File

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