Replaced main() for runnable version of the library

This commit is contained in:
Lew Rossman
2018-10-27 11:00:00 -04:00
parent 9d0b738e77
commit 320dec3ff7
5 changed files with 76 additions and 187 deletions

View File

@@ -1,33 +1,26 @@
#include <stdio.h>
#include <string.h>
#include "epanet2.h"
#define MAXMSG 255 /* Max. # characters in message text */
#define MAXWARNCODE 99
/* text copied here, no more need of include "text.h" */
#define FMT01 "\nEPANET Version %d.%d.%d\n"
#define FMT03 "\nUsage:\n %s <input_filename> <report_filename> [<binary_filename>]\n"
#define FMT09 "\n\nEPANET completed.\n"
#define FMT10 "\nEPANET completed. There are warnings.\n"
#define FMT11 "\nEPANET completed. There are errors.\n"
void writeConsole(char *s);
/* /*
---------------------------------------------------------------- ----------------------------------------------------------------
Entry point used to compile a stand-alone executable. Command line executable for the EPANET water distribution system
analysis program using the EPANET API library.
---------------------------------------------------------------- ----------------------------------------------------------------
*/ */
#include <stdio.h>
#include "epanet2.h"
// Function for writing progress messages to the console
void writeConsole(char *s)
{
fprintf(stdout, "\r%s", s);
fflush(stdout);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
/*-------------------------------------------------------------- /*--------------------------------------------------------------
** Input: argc = number of command line arguments ** Input: argc = number of command line arguments
** *argv[] = array of command line arguments ** *argv[] = array of command line arguments
** Output: none ** Output: none
** Purpose: main program segment ** Purpose: main program stub for command line EPANET
** **
** Command line for stand-alone operation is: ** Command line for stand-alone operation is:
** progname f1 f2 f3 ** progname f1 f2 f3
@@ -40,68 +33,54 @@ int main(int argc, char *argv[])
{ {
char *f1,*f2,*f3; char *f1,*f2,*f3;
char blank[] = ""; char blank[] = "";
char errmsg[MAXMSG+1]=""; int errcode, version, major, minor, patch;
int errcode; EN_ProjectHandle ph;
int version;
int major;
int minor;
int patch;
/* get version from DLL and trasform in Major.Minor.Patch format // Check for proper number of command line arguments
instead of hardcoded version */ if (argc < 2)
{
printf(
"\nUsage:\n %s <input_filename> <report_filename> [<binary_filename>]\n",
argv[0]);
return 0;
}
// Get version number and display in Major.Minor.Patch format
ENgetversion(&version); ENgetversion(&version);
major= version/10000; major = version/10000;
minor= (version%10000)/100; minor = (version%10000)/100;
patch= version%100; patch = version%100;
printf(FMT01, major, minor, patch); printf("\n... Running EPANET Version %d.%d.%d\n", major, minor, patch);
/* Check for proper number of command line arguments */ // Assign pointers to file names
if (argc < 2) {
printf(FMT03, argv[0]);
return(1);
}
/* set inputfile name */
f1 = argv[1]; f1 = argv[1];
if (argc > 2) { if (argc > 2) f2 = argv[2]; // set rptfile name
/* set rptfile name */ else f2 = blank; // use stdout for rptfile
f2 = argv[2]; if (argc > 3) f3 = argv[3]; // set binary output file name
} else f3 = blank; // no binary output file
else {
/* use stdout for rptfile */
f2 = blank;
}
if (argc > 3) {
/* set binary output file name */
f3 = argv[3];
}
else {
/* NO binary output*/
f3 = blank;
}
/* Call the main control function */ // Create a project, run it, and delete it
errcode = ENepanet(f1,f2,f3,NULL); EN_createproject(&ph);
errcode = EN_runproject(ph, f1, f2, f3, &writeConsole);
EN_deleteproject(&ph);
/* Error/Warning check */ // Blank out the last progress message
if (errcode == 0) { printf("\r ");
/* no errors */
printf(FMT09); // Report run's status
return(0); if (errcode == 0)
{
printf("\n... EPANET ran successfully.\n");
} }
else { else if (errcode < 100)
if (errcode > MAXWARNCODE) printf("\n Fatal Error: "); {
ENgeterror(errcode, errmsg, MAXMSG); printf(
printf("%s\n", errmsg); "\n... EPANET ran with warnings - see the Status Report.\n");
if (errcode > MAXWARNCODE) {
// error //
printf(FMT11);
return(errcode);
} }
else { else
// warning // {
printf(FMT10); printf(
return(0); "\n... EPANET failed with ERROR %d - see the Status Report\n", errcode);
} }
} return errcode;
} /* End of main */ }

View File

@@ -143,44 +143,7 @@ void errorLookup(int errcode, char *errmsg, int len);
LEGACY (v <= 2.1) API: uses global project variable LEGACY (v <= 2.1) API: uses global project variable
*****************************************************************/ *****************************************************************/
/*
int runconcurrent(EN_ProjectHandle ph, const char *inputfile, const char *reportfile,
const char *outputfile, void(*pviewprog)(char *))
{
long t, tstep_h, tstep_q;
int errcode = 0;
EN_Project *p = NULL;
ERRCODE(EN_open(ph, inputfile, reportfile, outputfile));
p = (EN_Project*)(ph);
p->viewprog = pviewprog;
ERRCODE(EN_openH(ph));
ERRCODE(EN_initH(ph, EN_SAVE));
ERRCODE(EN_openQ(ph));
ERRCODE(EN_initQ(ph, EN_SAVE));
do {
ERRCODE(EN_runH(ph, &t));
ERRCODE(EN_runQ(ph, &t));
ERRCODE(EN_nextH(ph, &tstep_h));
ERRCODE(EN_nextQ(ph, &tstep_q));
} while (tstep_h > 0);
ERRCODE(EN_closeH(ph));
ERRCODE(EN_closeQ(ph));
ERRCODE(EN_report(ph));
ERRCODE(EN_close(ph));
return errcode;
}
*/
/*------------------------------------------------------------------------ /*------------------------------------------------------------------------
** Input: f1 = pointer to name of input file ** Input: f1 = pointer to name of input file
** f2 = pointer to name of report file ** f2 = pointer to name of report file
@@ -201,14 +164,17 @@ int runconcurrent(EN_ProjectHandle ph, const char *inputfile, const char *report
int DLLEXPORT ENepanet(const char *f1, const char *f2, const char *f3, void (*pviewprog)(char *)) int DLLEXPORT ENepanet(const char *f1, const char *f2, const char *f3, void (*pviewprog)(char *))
{ {
int errcode = 0; int errcode = 0;
int warncode = 0;
EN_Project *p = NULL; EN_Project *p = NULL;
ERRCODE(EN_createproject(&_defaultModel)); ERRCODE(EN_createproject(&_defaultModel));
ERRCODE(EN_runproject(_defaultModel, f1, f2, f3, pviewprog)); ERRCODE(EN_runproject(_defaultModel, f1, f2, f3, pviewprog));
if (errcode < 100) warncode = errcode;
ERRCODE(EN_deleteproject(&_defaultModel)); ERRCODE(EN_deleteproject(&_defaultModel));
if (warncode) errcode = MAX(errcode, warncode);
return (errcode); return (errcode);
} }
@@ -683,6 +649,7 @@ int DLLEXPORT EN_runproject(EN_ProjectHandle ph, const char *f1, const char *f2,
EN_close(ph); EN_close(ph);
if (p->Warnflag) errcode = MAX(errcode, p->Warnflag);
return errcode; return errcode;
} }
@@ -792,8 +759,7 @@ int DLLEXPORT EN_open(EN_ProjectHandle ph, const char *f1, const char *f2, const
writelogo(p); writelogo(p);
/* Find network size & allocate memory for data */ /* Find network size & allocate memory for data */
//// writecon(FMT02); writewin(p->viewprog, FMT100);
//// writewin(p->viewprog, FMT100);
ERRCODE(netsize(p)); ERRCODE(netsize(p));
ERRCODE(allocdata(p)); ERRCODE(allocdata(p));
@@ -920,19 +886,14 @@ int DLLEXPORT EN_solveH(EN_ProjectHandle ph)
if (!errcode) { if (!errcode) {
/* Initialize hydraulics */ /* Initialize hydraulics */
errcode = EN_initH(ph, EN_SAVE); errcode = EN_initH(ph, EN_SAVE);
//// writecon(FMT14);
/* Analyze each hydraulic period */ /* Analyze each hydraulic period */
if (!errcode) if (!errcode)
do { do {
/* Display progress message */ /* Display progress message */
/*** Updated 6/24/02 ***/
sprintf(p->Msg, "%-10s", sprintf(p->Msg, "%-10s",
clocktime(p->report.Atime, p->time_options.Htime)); clocktime(p->report.Atime, p->time_options.Htime));
//// writecon(p->Msg);
sprintf(p->Msg, FMT101, p->report.Atime); sprintf(p->Msg, FMT101, p->report.Atime);
writewin(p->viewprog, p->Msg); writewin(p->viewprog, p->Msg);
@@ -940,16 +901,10 @@ int DLLEXPORT EN_solveH(EN_ProjectHandle ph)
tstep = 0; tstep = 0;
ERRCODE(EN_runH(ph, &t)); ERRCODE(EN_runH(ph, &t));
ERRCODE(EN_nextH(ph, &tstep)); ERRCODE(EN_nextH(ph, &tstep));
/*** Updated 6/24/02 ***/
//// writecon("\b\b\b\b\b\b\b\b\b\b");
} while (tstep > 0); } while (tstep > 0);
} }
/* Close hydraulics solver */ /* Close hydraulics solver */
/*** Updated 6/24/02 ***/
//// writecon("\b\b\b\b\b\b\b\b ");
EN_closeH(ph); EN_closeH(ph);
errcode = MAX(errcode, p->Warnflag); errcode = MAX(errcode, p->Warnflag);
@@ -1190,24 +1145,15 @@ int DLLEXPORT EN_solveQ(EN_ProjectHandle ph) {
if (!errcode) { if (!errcode) {
/* Initialize WQ */ /* Initialize WQ */
errcode = EN_initQ(ph, EN_SAVE); errcode = EN_initQ(ph, EN_SAVE);
//// if (p->quality.Qualflag) if (!p->quality.Qualflag) writewin(p->viewprog, FMT106);
//// writecon(FMT15);
//// else {
//// writecon(FMT16);
if (!p->quality.Qualflag) writewin(p->viewprog, FMT103);
//// }
/* Analyze each hydraulic period */ /* Analyze each hydraulic period */
if (!errcode) if (!errcode)
do { do {
/* Display progress message */ /* Display progress message */
/*** Updated 6/24/02 ***/
sprintf(p->Msg, "%-10s", sprintf(p->Msg, "%-10s",
clocktime(p->report.Atime, p->time_options.Htime)); clocktime(p->report.Atime, p->time_options.Htime));
//// writecon(p->Msg);
if (p->quality.Qualflag) { if (p->quality.Qualflag) {
sprintf(p->Msg, FMT102, p->report.Atime); sprintf(p->Msg, FMT102, p->report.Atime);
writewin(p->viewprog, p->Msg); writewin(p->viewprog, p->Msg);
@@ -1217,17 +1163,10 @@ int DLLEXPORT EN_solveQ(EN_ProjectHandle ph) {
tstep = 0; tstep = 0;
ERRCODE(EN_runQ(ph, &t)); ERRCODE(EN_runQ(ph, &t));
ERRCODE(EN_nextQ(ph, &tstep)); ERRCODE(EN_nextQ(ph, &tstep));
/*** Updated 6/24/02 ***/
//// writecon("\b\b\b\b\b\b\b\b\b\b");
} while (tstep > 0); } while (tstep > 0);
} }
/* Close WQ solver */ /* Close WQ solver */
/*** Updated 6/24/02 ***/
//// writecon("\b\b\b\b\b\b\b\b ");
EN_closeQ(ph); EN_closeQ(ph);
return set_error(p->error_handle, errcode); return set_error(p->error_handle, errcode);
} }
@@ -1355,6 +1294,7 @@ int DLLEXPORT EN_report(EN_ProjectHandle ph) {
/* Check if results saved to binary output file */ /* Check if results saved to binary output file */
if (!p->save_options.SaveQflag) if (!p->save_options.SaveQflag)
return set_error(p->error_handle, 106); return set_error(p->error_handle, 106);
writewin(p->viewprog, FMT103);
errcode = writereport(p); errcode = writereport(p);
if (errcode) if (errcode)
errmsg(p, errcode); errmsg(p, errcode);
@@ -4064,20 +4004,16 @@ int openfiles(EN_Project *p, const char *f1, const char *f2, const char *f3)
/* Check that file names are not identical */ /* Check that file names are not identical */
if (strcomp(f1, f2) || strcomp(f1, f3) || if (strcomp(f1, f2) || strcomp(f1, f3) ||
(strcomp(f2, f3) && (strlen(f2) > 0 || strlen(f3) > 0))) { (strcomp(f2, f3) && (strlen(f2) > 0 || strlen(f3) > 0))) {
//// writecon(FMT04);
return 301; return 301;
} }
/* Attempt to open input and report files */ /* Attempt to open input and report files */
if ((par->InFile = fopen(f1, "rt")) == NULL) { if ((par->InFile = fopen(f1, "rt")) == NULL) {
//// writecon(FMT05);
//// writecon(f1);
return 302; return 302;
} }
if (strlen(f2) == 0) if (strlen(f2) == 0)
rep->RptFile = stdout; rep->RptFile = stdout;
else if ((rep->RptFile = fopen(f2, "wt")) == NULL) { else if ((rep->RptFile = fopen(f2, "wt")) == NULL) {
//// writecon(FMT06);
return 303; return 303;
} }
@@ -4208,7 +4144,6 @@ int openoutfile(EN_Project *p)
if (out->Outflag == SAVE) if (out->Outflag == SAVE)
{ {
if ((out->OutFile = fopen(out->OutFname, "w+b")) == NULL) { if ((out->OutFile = fopen(out->OutFname, "w+b")) == NULL) {
//// writecon(FMT07);
errcode = 304; errcode = 304;
} }
} }
@@ -4218,7 +4153,6 @@ int openoutfile(EN_Project *p)
getTmpName(p, out->OutFname); getTmpName(p, out->OutFname);
if ((out->OutFile = fopen(out->OutFname, "w+b")) == NULL) if ((out->OutFile = fopen(out->OutFname, "w+b")) == NULL)
{ {
//// writecon(FMT08);
errcode = 304; errcode = 304;
} }
} }
@@ -4715,6 +4649,7 @@ char *geterrmsg(int errcode, char *msg)
{ {
switch (errcode) { /* Warnings */ switch (errcode) { /* Warnings */
#define DAT(code,enumer,string) case code: strcpy(msg, string); break; #define DAT(code,enumer,string) case code: strcpy(msg, string); break;
//#define DAT(code,enumer,string) case code: sprintf(msg, "Error %d: %s", code, string); break;
#include "errors.dat" #include "errors.dat"
#undef DAT #undef DAT
default: default:
@@ -4733,26 +4668,12 @@ void errmsg(EN_Project *p, int errcode)
{ {
if (errcode == 309) /* Report file write error - */ if (errcode == 309) /* Report file write error - */
{ /* Do not write msg to file. */ { /* Do not write msg to file. */
//// writecon("\n ");
//// writecon(geterrmsg(errcode,p->Msg));
} else if (p->report.RptFile != NULL && p->report.Messageflag) { } else if (p->report.RptFile != NULL && p->report.Messageflag) {
writeline(p, geterrmsg(errcode,p->Msg)); writeline(p, geterrmsg(errcode,p->Msg));
} }
} }
//void writecon(const char *s)
/*----------------------------------------------------------------
** Input: text string
** Output: none
** Purpose: writes string of characters to console
**----------------------------------------------------------------
*/
//{
//
// fprintf(stdout, "%s", s);
// fflush(stdout);
//}
void writewin(void (*vp)(char *), char *s) void writewin(void (*vp)(char *), char *s)
/*---------------------------------------------------------------- /*----------------------------------------------------------------
** Input: text string ** Input: text string

View File

@@ -52,7 +52,6 @@ int findvalve(EN_Network *n, int); /* Find valve index from node
int findpump(EN_Network *n, int); /* Find pump index from node index */ // (AH) int findpump(EN_Network *n, int); /* Find pump index from node index */ // (AH)
char *geterrmsg(int errcode, char *msg); /* Gets text of error message */ char *geterrmsg(int errcode, char *msg); /* Gets text of error message */
void errmsg(EN_Project *p, int); /* Reports program error */ void errmsg(EN_Project *p, int); /* Reports program error */
////void writecon(const char *); /* Writes text to console */
void writewin(void (*vp)(char *), char *); /* Passes text to calling app */ void writewin(void (*vp)(char *), char *); /* Passes text to calling app */
/* ------- INPUT1.C --------------------*/ /* ------- INPUT1.C --------------------*/

View File

@@ -82,8 +82,6 @@ int writereport(EN_Project *pr)
/* write formatted output to primary report file. */ /* write formatted output to primary report file. */
rep->Fprinterr = FALSE; rep->Fprinterr = FALSE;
if (rep->Rptflag && strlen(rep->Rpt2Fname) == 0 && rep->RptFile != NULL) { if (rep->Rptflag && strlen(rep->Rpt2Fname) == 0 && rep->RptFile != NULL) {
//// writecon(FMT17);
//// writecon(rep->Rpt1Fname);
if (rep->Energyflag) if (rep->Energyflag)
writeenergy(pr); writeenergy(pr);
errcode = writeresults(pr); errcode = writeresults(pr);
@@ -95,8 +93,6 @@ int writereport(EN_Project *pr)
/* If secondary report file has same name as either input */ /* If secondary report file has same name as either input */
/* or primary report file then use primary report file. */ /* or primary report file then use primary report file. */
if (strcomp(rep->Rpt2Fname, par->InpFname) || strcomp(rep->Rpt2Fname, rep->Rpt1Fname)) { if (strcomp(rep->Rpt2Fname, par->InpFname) || strcomp(rep->Rpt2Fname, rep->Rpt1Fname)) {
//// writecon(FMT17);
//// writecon(rep->Rpt1Fname);
if (rep->Energyflag) if (rep->Energyflag)
writeenergy(pr); writeenergy(pr);
errcode = writeresults(pr); errcode = writeresults(pr);
@@ -117,8 +113,6 @@ int writereport(EN_Project *pr)
/* Write full formatted report to file */ /* Write full formatted report to file */
else { else {
rep->Rptflag = 1; rep->Rptflag = 1;
//// writecon(FMT17);
//// writecon(rep->Rpt2Fname);
writelogo(pr); writelogo(pr);
if (rep->Summaryflag) if (rep->Summaryflag)
writesummary(pr); writesummary(pr);

View File

@@ -449,19 +449,15 @@ AUTHOR: L. Rossman
#define FMT82 "\n\f\n Page %-d %60.60s\n" #define FMT82 "\n\f\n Page %-d %60.60s\n"
/* ------------------- Progress Messages ---------------------- */ /* ------------------- Progress Messages ---------------------- */
#define FMT100 "Retrieving network data..." #define FMT100 " Retrieving network data ... "
#define FMT101 "Computing hydraulics at hour %s" #define FMT101 " Computing hydraulics at hour %-10s "
#define FMT102 "Computing water quality at hour %s" #define FMT102 " Computing water quality at hour %-10s "
#define FMT103 "Saving results to file..." #define FMT103 " Writing output report ... "
#define FMT106 " Transferring results to file ... "
#define FMT104 "Analysis begun %s" #define FMT104 "Analysis begun %s"
#define FMT105 "Analysis ended %s" #define FMT105 "Analysis ended %s"
/*------------------- Error Messages --------------------*/ /*------------------- Error Messages --------------------*/
#define R_ERR201 "Input Error 201: syntax error in following line of " #define R_ERR201 "Input Error 201: syntax error in following line of "
#define R_ERR202 "Input Error 202: illegal numeric value in following line of " #define R_ERR202 "Input Error 202: illegal numeric value in following line of "
#define R_ERR203 "Input Error 203: undefined node in following line of " #define R_ERR203 "Input Error 203: undefined node in following line of "