Replaced main() for runnable version of the library
This commit is contained in:
143
run/main.c
143
run/main.c
@@ -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"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
// 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[])
|
||||
/*--------------------------------------------------------------
|
||||
** Input: argc = number of command line arguments
|
||||
** *argv[] = array of command line arguments
|
||||
** Output: none
|
||||
** Purpose: main program segment
|
||||
** Purpose: main program stub for command line EPANET
|
||||
**
|
||||
** Command line for stand-alone operation is:
|
||||
** progname f1 f2 f3
|
||||
@@ -38,70 +31,56 @@ int main(int argc, char *argv[])
|
||||
**--------------------------------------------------------------
|
||||
*/
|
||||
{
|
||||
char *f1,*f2,*f3;
|
||||
char blank[] = "";
|
||||
char errmsg[MAXMSG+1]="";
|
||||
int errcode;
|
||||
int version;
|
||||
int major;
|
||||
int minor;
|
||||
int patch;
|
||||
char *f1,*f2,*f3;
|
||||
char blank[] = "";
|
||||
int errcode, version, major, minor, patch;
|
||||
EN_ProjectHandle ph;
|
||||
|
||||
// Check for proper number of command line arguments
|
||||
if (argc < 2)
|
||||
{
|
||||
printf(
|
||||
"\nUsage:\n %s <input_filename> <report_filename> [<binary_filename>]\n",
|
||||
argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* get version from DLL and trasform in Major.Minor.Patch format
|
||||
instead of hardcoded version */
|
||||
ENgetversion(&version);
|
||||
major= version/10000;
|
||||
minor= (version%10000)/100;
|
||||
patch= version%100;
|
||||
printf(FMT01, major, minor, patch);
|
||||
// Get version number and display in Major.Minor.Patch format
|
||||
ENgetversion(&version);
|
||||
major = version/10000;
|
||||
minor = (version%10000)/100;
|
||||
patch = version%100;
|
||||
printf("\n... Running EPANET Version %d.%d.%d\n", major, minor, patch);
|
||||
|
||||
/* Check for proper number of command line arguments */
|
||||
if (argc < 2) {
|
||||
printf(FMT03, argv[0]);
|
||||
return(1);
|
||||
}
|
||||
// Assign pointers to file names
|
||||
f1 = argv[1];
|
||||
if (argc > 2) f2 = argv[2]; // set rptfile name
|
||||
else f2 = blank; // use stdout for rptfile
|
||||
if (argc > 3) f3 = argv[3]; // set binary output file name
|
||||
else f3 = blank; // no binary output file
|
||||
|
||||
/* set inputfile name */
|
||||
f1 = argv[1];
|
||||
if (argc > 2) {
|
||||
/* set rptfile name */
|
||||
f2 = argv[2];
|
||||
}
|
||||
else {
|
||||
/* use stdout for rptfile */
|
||||
f2 = blank;
|
||||
}
|
||||
if (argc > 3) {
|
||||
/* set binary output file name */
|
||||
f3 = argv[3];
|
||||
}
|
||||
else {
|
||||
/* NO binary output*/
|
||||
f3 = blank;
|
||||
}
|
||||
// Create a project, run it, and delete it
|
||||
EN_createproject(&ph);
|
||||
errcode = EN_runproject(ph, f1, f2, f3, &writeConsole);
|
||||
EN_deleteproject(&ph);
|
||||
|
||||
/* Call the main control function */
|
||||
errcode = ENepanet(f1,f2,f3,NULL);
|
||||
// Blank out the last progress message
|
||||
printf("\r ");
|
||||
|
||||
/* Error/Warning check */
|
||||
if (errcode == 0) {
|
||||
/* no errors */
|
||||
printf(FMT09);
|
||||
return(0);
|
||||
}
|
||||
else {
|
||||
if (errcode > MAXWARNCODE) printf("\n Fatal Error: ");
|
||||
ENgeterror(errcode, errmsg, MAXMSG);
|
||||
printf("%s\n", errmsg);
|
||||
if (errcode > MAXWARNCODE) {
|
||||
// error //
|
||||
printf(FMT11);
|
||||
return(errcode);
|
||||
}
|
||||
else {
|
||||
// warning //
|
||||
printf(FMT10);
|
||||
return(0);
|
||||
}
|
||||
}
|
||||
} /* End of main */
|
||||
// Report run's status
|
||||
if (errcode == 0)
|
||||
{
|
||||
printf("\n... EPANET ran successfully.\n");
|
||||
}
|
||||
else if (errcode < 100)
|
||||
{
|
||||
printf(
|
||||
"\n... EPANET ran with warnings - see the Status Report.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(
|
||||
"\n... EPANET failed with ERROR %d - see the Status Report\n", errcode);
|
||||
}
|
||||
return errcode;
|
||||
}
|
||||
|
||||
99
src/epanet.c
99
src/epanet.c
@@ -143,44 +143,7 @@ void errorLookup(int errcode, char *errmsg, int len);
|
||||
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
|
||||
** 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 errcode = 0;
|
||||
int warncode = 0;
|
||||
EN_Project *p = NULL;
|
||||
|
||||
ERRCODE(EN_createproject(&_defaultModel));
|
||||
|
||||
ERRCODE(EN_runproject(_defaultModel, f1, f2, f3, pviewprog));
|
||||
if (errcode < 100) warncode = errcode;
|
||||
|
||||
ERRCODE(EN_deleteproject(&_defaultModel));
|
||||
|
||||
if (warncode) errcode = MAX(errcode, warncode);
|
||||
return (errcode);
|
||||
}
|
||||
|
||||
@@ -682,7 +648,8 @@ int DLLEXPORT EN_runproject(EN_ProjectHandle ph, const char *f1, const char *f2,
|
||||
ERRCODE(EN_report(ph));
|
||||
|
||||
EN_close(ph);
|
||||
|
||||
|
||||
if (p->Warnflag) errcode = MAX(errcode, p->Warnflag);
|
||||
return errcode;
|
||||
}
|
||||
|
||||
@@ -792,8 +759,7 @@ int DLLEXPORT EN_open(EN_ProjectHandle ph, const char *f1, const char *f2, const
|
||||
writelogo(p);
|
||||
|
||||
/* Find network size & allocate memory for data */
|
||||
//// writecon(FMT02);
|
||||
//// writewin(p->viewprog, FMT100);
|
||||
writewin(p->viewprog, FMT100);
|
||||
ERRCODE(netsize(p));
|
||||
ERRCODE(allocdata(p));
|
||||
|
||||
@@ -920,19 +886,14 @@ int DLLEXPORT EN_solveH(EN_ProjectHandle ph)
|
||||
if (!errcode) {
|
||||
/* Initialize hydraulics */
|
||||
errcode = EN_initH(ph, EN_SAVE);
|
||||
//// writecon(FMT14);
|
||||
|
||||
/* Analyze each hydraulic period */
|
||||
if (!errcode)
|
||||
do {
|
||||
|
||||
/* Display progress message */
|
||||
|
||||
/*** Updated 6/24/02 ***/
|
||||
sprintf(p->Msg, "%-10s",
|
||||
clocktime(p->report.Atime, p->time_options.Htime));
|
||||
|
||||
//// writecon(p->Msg);
|
||||
sprintf(p->Msg, FMT101, p->report.Atime);
|
||||
writewin(p->viewprog, p->Msg);
|
||||
|
||||
@@ -940,16 +901,10 @@ int DLLEXPORT EN_solveH(EN_ProjectHandle ph)
|
||||
tstep = 0;
|
||||
ERRCODE(EN_runH(ph, &t));
|
||||
ERRCODE(EN_nextH(ph, &tstep));
|
||||
/*** Updated 6/24/02 ***/
|
||||
//// writecon("\b\b\b\b\b\b\b\b\b\b");
|
||||
} while (tstep > 0);
|
||||
}
|
||||
|
||||
/* Close hydraulics solver */
|
||||
|
||||
/*** Updated 6/24/02 ***/
|
||||
//// writecon("\b\b\b\b\b\b\b\b ");
|
||||
|
||||
EN_closeH(ph);
|
||||
errcode = MAX(errcode, p->Warnflag);
|
||||
|
||||
@@ -1190,24 +1145,15 @@ int DLLEXPORT EN_solveQ(EN_ProjectHandle ph) {
|
||||
if (!errcode) {
|
||||
/* Initialize WQ */
|
||||
errcode = EN_initQ(ph, EN_SAVE);
|
||||
//// if (p->quality.Qualflag)
|
||||
//// writecon(FMT15);
|
||||
//// else {
|
||||
//// writecon(FMT16);
|
||||
if (!p->quality.Qualflag) writewin(p->viewprog, FMT103);
|
||||
//// }
|
||||
if (!p->quality.Qualflag) writewin(p->viewprog, FMT106);
|
||||
|
||||
/* Analyze each hydraulic period */
|
||||
if (!errcode)
|
||||
do {
|
||||
|
||||
/* Display progress message */
|
||||
|
||||
/*** Updated 6/24/02 ***/
|
||||
sprintf(p->Msg, "%-10s",
|
||||
clocktime(p->report.Atime, p->time_options.Htime));
|
||||
|
||||
//// writecon(p->Msg);
|
||||
if (p->quality.Qualflag) {
|
||||
sprintf(p->Msg, FMT102, p->report.Atime);
|
||||
writewin(p->viewprog, p->Msg);
|
||||
@@ -1217,17 +1163,10 @@ int DLLEXPORT EN_solveQ(EN_ProjectHandle ph) {
|
||||
tstep = 0;
|
||||
ERRCODE(EN_runQ(ph, &t));
|
||||
ERRCODE(EN_nextQ(ph, &tstep));
|
||||
|
||||
/*** Updated 6/24/02 ***/
|
||||
//// writecon("\b\b\b\b\b\b\b\b\b\b");
|
||||
|
||||
} while (tstep > 0);
|
||||
}
|
||||
|
||||
/* Close WQ solver */
|
||||
|
||||
/*** Updated 6/24/02 ***/
|
||||
//// writecon("\b\b\b\b\b\b\b\b ");
|
||||
EN_closeQ(ph);
|
||||
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 */
|
||||
if (!p->save_options.SaveQflag)
|
||||
return set_error(p->error_handle, 106);
|
||||
writewin(p->viewprog, FMT103);
|
||||
errcode = writereport(p);
|
||||
if (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 */
|
||||
if (strcomp(f1, f2) || strcomp(f1, f3) ||
|
||||
(strcomp(f2, f3) && (strlen(f2) > 0 || strlen(f3) > 0))) {
|
||||
//// writecon(FMT04);
|
||||
return 301;
|
||||
}
|
||||
|
||||
/* Attempt to open input and report files */
|
||||
if ((par->InFile = fopen(f1, "rt")) == NULL) {
|
||||
//// writecon(FMT05);
|
||||
//// writecon(f1);
|
||||
return 302;
|
||||
}
|
||||
if (strlen(f2) == 0)
|
||||
rep->RptFile = stdout;
|
||||
else if ((rep->RptFile = fopen(f2, "wt")) == NULL) {
|
||||
//// writecon(FMT06);
|
||||
return 303;
|
||||
}
|
||||
|
||||
@@ -4208,7 +4144,6 @@ int openoutfile(EN_Project *p)
|
||||
if (out->Outflag == SAVE)
|
||||
{
|
||||
if ((out->OutFile = fopen(out->OutFname, "w+b")) == NULL) {
|
||||
//// writecon(FMT07);
|
||||
errcode = 304;
|
||||
}
|
||||
}
|
||||
@@ -4218,7 +4153,6 @@ int openoutfile(EN_Project *p)
|
||||
getTmpName(p, out->OutFname);
|
||||
if ((out->OutFile = fopen(out->OutFname, "w+b")) == NULL)
|
||||
{
|
||||
//// writecon(FMT08);
|
||||
errcode = 304;
|
||||
}
|
||||
}
|
||||
@@ -4715,6 +4649,7 @@ char *geterrmsg(int errcode, char *msg)
|
||||
{
|
||||
switch (errcode) { /* Warnings */
|
||||
#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"
|
||||
#undef DAT
|
||||
default:
|
||||
@@ -4733,26 +4668,12 @@ void errmsg(EN_Project *p, int errcode)
|
||||
{
|
||||
if (errcode == 309) /* Report file write error - */
|
||||
{ /* Do not write msg to file. */
|
||||
//// writecon("\n ");
|
||||
//// writecon(geterrmsg(errcode,p->Msg));
|
||||
|
||||
} else if (p->report.RptFile != NULL && p->report.Messageflag) {
|
||||
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)
|
||||
/*----------------------------------------------------------------
|
||||
** Input: text string
|
||||
|
||||
@@ -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)
|
||||
char *geterrmsg(int errcode, char *msg); /* Gets text of error message */
|
||||
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 */
|
||||
|
||||
/* ------- INPUT1.C --------------------*/
|
||||
|
||||
@@ -82,8 +82,6 @@ int writereport(EN_Project *pr)
|
||||
/* write formatted output to primary report file. */
|
||||
rep->Fprinterr = FALSE;
|
||||
if (rep->Rptflag && strlen(rep->Rpt2Fname) == 0 && rep->RptFile != NULL) {
|
||||
//// writecon(FMT17);
|
||||
//// writecon(rep->Rpt1Fname);
|
||||
if (rep->Energyflag)
|
||||
writeenergy(pr);
|
||||
errcode = writeresults(pr);
|
||||
@@ -95,8 +93,6 @@ int writereport(EN_Project *pr)
|
||||
/* If secondary report file has same name as either input */
|
||||
/* or primary report file then use primary report file. */
|
||||
if (strcomp(rep->Rpt2Fname, par->InpFname) || strcomp(rep->Rpt2Fname, rep->Rpt1Fname)) {
|
||||
//// writecon(FMT17);
|
||||
//// writecon(rep->Rpt1Fname);
|
||||
if (rep->Energyflag)
|
||||
writeenergy(pr);
|
||||
errcode = writeresults(pr);
|
||||
@@ -117,8 +113,6 @@ int writereport(EN_Project *pr)
|
||||
/* Write full formatted report to file */
|
||||
else {
|
||||
rep->Rptflag = 1;
|
||||
//// writecon(FMT17);
|
||||
//// writecon(rep->Rpt2Fname);
|
||||
writelogo(pr);
|
||||
if (rep->Summaryflag)
|
||||
writesummary(pr);
|
||||
|
||||
14
src/text.h
14
src/text.h
@@ -449,19 +449,15 @@ AUTHOR: L. Rossman
|
||||
#define FMT82 "\n\f\n Page %-d %60.60s\n"
|
||||
|
||||
/* ------------------- Progress Messages ---------------------- */
|
||||
#define FMT100 "Retrieving network data..."
|
||||
#define FMT101 "Computing hydraulics at hour %s"
|
||||
#define FMT102 "Computing water quality at hour %s"
|
||||
#define FMT103 "Saving results to file..."
|
||||
#define FMT100 " Retrieving network data ... "
|
||||
#define FMT101 " Computing hydraulics at hour %-10s "
|
||||
#define FMT102 " Computing water quality at hour %-10s "
|
||||
#define FMT103 " Writing output report ... "
|
||||
#define FMT106 " Transferring results to file ... "
|
||||
#define FMT104 "Analysis begun %s"
|
||||
#define FMT105 "Analysis ended %s"
|
||||
|
||||
/*------------------- Error Messages --------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#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_ERR203 "Input Error 203: undefined node in following line of "
|
||||
|
||||
Reference in New Issue
Block a user