diff --git a/src/epanet.c b/src/epanet.c index 49c8795..e0abd48 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 03/05/2019 + Last Updated: 03/08/2019 ****************************************************************************** */ @@ -219,6 +219,13 @@ int DLLEXPORT EN_open(EN_Project p, const char *inpFile, const char *rptFile, // Read input data ERRCODE(getdata(p)); + // Close input file + if (p->parser.InFile != NULL) + { + fclose(p->parser.InFile); + p->parser.InFile = NULL; + } + // Free temporary linked lists used for Patterns & Curves freeTmplist(p->parser.Patlist); freeTmplist(p->parser.Curvelist); diff --git a/src/genmmd.c b/src/genmmd.c index 8f0cc73..932598c 100644 --- a/src/genmmd.c +++ b/src/genmmd.c @@ -1,6 +1,6 @@ /* MULTIPLE MINIMUM DEGREE ROW RE-ORDERING ALGORITHM * - * Modified to work with Fortran-style arrays. + * Modified to work with Fortran-style arrays and be thread-safe. * */ diff --git a/src/inpfile.c b/src/inpfile.c index bc110dd..59f09d0 100644 --- a/src/inpfile.c +++ b/src/inpfile.c @@ -7,7 +7,7 @@ Description: saves network data to an EPANET formatted text file Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE -Last Updated: 11/27/2018 +Last Updated: 03/08/2019 ****************************************************************************** */ @@ -38,7 +38,7 @@ extern char *TstatTxt[]; extern char *RptFlagTxt[]; extern char *SectTxt[]; -void saveauxdata(Parser *parser, FILE *f) +void saveauxdata(Project *pr, FILE *f) /* ------------------------------------------------------------ Writes auxilary data from original input file to new file. @@ -47,13 +47,21 @@ void saveauxdata(Parser *parser, FILE *f) { int sect, newsect; char *tok; + char write; char line[MAXLINE + 1]; char s[MAXLINE + 1]; - FILE *InFile = parser->InFile; + FILE *InFile = pr->parser.InFile; - sect = -1; - if (InFile == NULL) return; + // Re-open the input file + if (InFile == NULL) + { + InFile = fopen(pr->parser.InpFname, "rt"); + if (InFile == NULL) return; + } rewind(InFile); + sect = -1; + + // Read each line of the input file while (fgets(line, MAXLINE, InFile) != NULL) { strcpy(s, line); @@ -68,22 +76,43 @@ void saveauxdata(Parser *parser, FILE *f) { sect = newsect; if (sect == _END) break; + + // Write section heading to file + switch (sect) + { + case _VERTICES: + case _LABELS: + case _BACKDROP: + case _TAGS: + fprintf(f, "%s", line); + } } } // Write line of auxilary data to file - switch (sect) + else { + write = FALSE; + switch (sect) + { case _VERTICES: + if (findlink(&pr->network, tok) || *tok == ';') write = TRUE; break; + case _TAGS: + if (*tok == ';' || + (match("NODE", tok) && findnode(&pr->network, strtok(NULL, SEPSTR))) || + (match("LINK", tok) && findlink(&pr->network, strtok(NULL, SEPSTR)))) + write = TRUE; + break; case _LABELS: case _BACKDROP: - case _TAGS: - fprintf(f, "%s", line); - break; + write = TRUE; break; default: - break; + break; + } + if (write) fprintf(f, "%s", line); } } + fclose(InFile); } int saveinpfile(Project *pr, const char *fname) @@ -760,7 +789,7 @@ int saveinpfile(Project *pr, const char *fname) fprintf(f, "\n\n"); // Save auxilary data to new input file - saveauxdata(parser, f); + saveauxdata(pr, f); // Close the new input file fprintf(f, "\n");