Refactoring how the binary output is closed

This change implements the fix suggested by @gonccalo.
This commit is contained in:
Lew Rossman
2019-03-06 11:14:03 -05:00
parent 3ebb1831d7
commit 6e13d7e3aa
3 changed files with 31 additions and 19 deletions

View File

@@ -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;
}

View File

@@ -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 *);

View File

@@ -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