From 692955e3d2c990c75d132a2e99ec57d8d3032a1e Mon Sep 17 00:00:00 2001 From: Lew Rossman Date: Tue, 5 Mar 2019 09:23:49 -0500 Subject: [PATCH] Give external apps access to binary output file (#407) --- src/epanet.c | 7 ++++++- src/project.c | 3 ++- src/report.c | 26 ++++++++++++++++---------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/epanet.c b/src/epanet.c index 626d65b..45e6e6c 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 02/08/2019 + Last Updated: 03/05/2019 ****************************************************************************** */ @@ -819,6 +819,11 @@ 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; + } return 0; } diff --git a/src/project.c b/src/project.c index 349fa13..b8e6119 100644 --- a/src/project.c +++ b/src/project.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 01/01/2019 + Last Updated: 03/05/2019 ****************************************************************************** */ @@ -95,6 +95,7 @@ int openhydfile(Project *pr) { if (pr->outfile.Hydflag == SCRATCH) return 0; fclose(pr->outfile.HydFile); + pr->outfile.HydFile = NULL; } // Use Hydflag to determine the type of hydraulics file to use. diff --git a/src/report.c b/src/report.c index 55387da..0032ee7 100644 --- a/src/report.c +++ b/src/report.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 11/27/2018 + Last Updated: 03/05/2019 ****************************************************************************** */ @@ -98,12 +98,7 @@ int copyreport(Project* pr, char *filename) // Copy contents of project's report file if (rpt->RptFile) { - c = fgetc(rpt->RptFile); - while (c != EOF) - { - fputc(c, tfile); - c = fgetc(rpt->RptFile); - } + while ((c = fgetc(rpt->RptFile)) != EOF) fputc(c, tfile); fclose(rpt->RptFile); } @@ -539,9 +534,6 @@ int writeresults(Project *pr) // at each reporting time. //----------------------------------------------------------- - // Return if no output file - if (outFile == NULL) return 106; - // Return if no nodes or links selected for reporting // or if no node or link report variables enabled if (!rpt->Nodeflag && !rpt->Linkflag) return errcode; @@ -552,6 +544,13 @@ int writeresults(Project *pr) for (j = LENGTH; j <= FRICTION; j++) nlv += rpt->Field[j].Enabled; if (nnv == 0 && nlv == 0) return errcode; + // Return if no output file + if (outFile == NULL) + { + outFile = fopen(pr->outfile.OutFname, "r+b"); + if (outFile == NULL) return 106; + } + // Allocate memory for output variables: // m = larger of # node variables & # link variables // n = larger of # nodes & # links @@ -592,6 +591,13 @@ int writeresults(Project *pr) } } + // Free output file + if (outFile != NULL) + { + fclose(outFile); + outFile = NULL; + } + // Free allocated memory for (j = 0; j < m; j++) free(x[j]); free(x);