From 6e13d7e3aa8ec200dbe5a0745fc5a8afcd1a65d7 Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Wed, 6 Mar 2019 11:14:03 -0500 Subject: [PATCH] Refactoring how the binary output is closed This change implements the fix suggested by @gonccalo. --- src/epanet.c | 8 +------- src/funcs.h | 1 + src/project.c | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/epanet.c b/src/epanet.c index d8ba333..e39c752 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -819,13 +819,7 @@ int DLLEXPORT EN_closeQ(EN_Project p) if (!p->Openflag) return 102; closequal(p); p->quality.OpenQflag = FALSE; - - if (p->outfile.OutFile != NULL) - { - fclose(p->outfile.OutFile); - p->outfile.OutFile = NULL; - } - + closeoutfile(p); return 0; } diff --git a/src/funcs.h b/src/funcs.h index dde13c4..154dcfc 100755 --- a/src/funcs.h +++ b/src/funcs.h @@ -24,6 +24,7 @@ void freedata(Project *); int openfiles(Project *, const char *, const char *,const char *); int openhydfile(Project *); int openoutfile(Project *); +void closeoutfile(Project *); int buildadjlists(Network *); void freeadjlists(Network *); diff --git a/src/project.c b/src/project.c index 4d1621f..aceffa8 100644 --- a/src/project.c +++ b/src/project.c @@ -173,18 +173,8 @@ int openoutfile(Project *pr) int errcode = 0; // Close output file if already opened - if (pr->outfile.TmpOutFile != pr->outfile.OutFile && - pr->outfile.TmpOutFile != NULL) - { - fclose(pr->outfile.TmpOutFile); - pr->outfile.TmpOutFile = NULL; - } - if (pr->outfile.OutFile != NULL) - { - fclose(pr->outfile.OutFile); - pr->outfile.OutFile = NULL; - } - + closeoutfile(pr); + // If output file name was supplied, then attempt to // open it. Otherwise open a temporary output file. pr->outfile.OutFile = fopen(pr->outfile.OutFname, "w+b"); @@ -209,6 +199,33 @@ int openoutfile(Project *pr) return errcode; } +void closeoutfile(Project *pr) +/*---------------------------------------------------------------- +** Input: none +** Output: none +** Purpose: closes binary output file. +**---------------------------------------------------------------- +*/ +{ + if (pr->outfile.TmpOutFile != pr->outfile.OutFile) + { + if (pr->outfile.TmpOutFile != NULL) + { + fclose(pr->outfile.TmpOutFile); + pr->outfile.TmpOutFile = NULL; + } + } + if (pr->outfile.OutFile != NULL) + { + if (pr->outfile.OutFile == pr->outfile.TmpOutFile) + { + pr->outfile.TmpOutFile = NULL; + } + fclose(pr->outfile.OutFile); + pr->outfile.OutFile = NULL; + } +} + void initpointers(Project *pr) /*---------------------------------------------------------------- ** Input: none