From ea08fb9da12e7a2dbb1726472ad98fc6f59942b2 Mon Sep 17 00:00:00 2001 From: mauriziocingi Date: Fri, 20 Nov 2015 13:35:40 +0100 Subject: [PATCH 1/5] undone Warnflag declaration due to cl.exe incompatibilities , now Warnflag is retuned with errorcode --- build/MinGW/Makefile | 2 +- include/epanet2.h | 5 --- run/main.c | 75 +++++++++++++++++++++++++++++++++----------- src/epanet.c | 10 +++--- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/build/MinGW/Makefile b/build/MinGW/Makefile index a5c3178..eae49e2 100644 --- a/build/MinGW/Makefile +++ b/build/MinGW/Makefile @@ -52,7 +52,7 @@ epanet_heads=enumstxt.h funcs.h hash.h mempool.h text.h types.h vars.h epanet2.h # Epanet main program epanet_main=main # Epanet main program header files -epanet_main_heads=epanet2.h text.h +epanet_main_heads=epanet2.h .PHONY: all all: $(libname) $(exename) diff --git a/include/epanet2.h b/include/epanet2.h index 75488c1..5908ec9 100755 --- a/include/epanet2.h +++ b/include/epanet2.h @@ -36,7 +36,6 @@ // --- define DLLEXPORT #ifndef DLLEXPORT #ifdef WINDOWS - #define DLLDATA __declspec(dllexport) #ifdef __cplusplus #define DLLEXPORT extern "C" __declspec(dllexport) #else @@ -54,9 +53,6 @@ #define DLLEXPORT #endif #endif -#ifndef DLLDATA - #define DLLDATA -#endif // --- Define the EPANET toolkit constants @@ -287,7 +283,6 @@ extern "C" { int DLLEXPORT ENsetcurvevalue(int index, int pnt, EN_API_FLOAT_TYPE x, EN_API_FLOAT_TYPE y); int DLLEXPORT ENsetcurve(int index, EN_API_FLOAT_TYPE *x, EN_API_FLOAT_TYPE *y, int len); int DLLEXPORT ENaddcurve(char *id); - char DLLDATA Warnflag; #if defined(__cplusplus) } diff --git a/run/main.c b/run/main.c index 83949a7..d36ad6f 100644 --- a/run/main.c +++ b/run/main.c @@ -1,10 +1,17 @@ #include +#include #include "epanet2.h" -#include "text.h" + +/* text copied here, no more need of include "text.h" */ +#define FMT01 "\n... EPANET Version %d.%d.%d\n" +#define FMT03 "\n Correct syntax is:\n epanet \n" +#define FMT09 "\n... EPANET completed.\n" +#define FMT10 "\n... EPANET completed. There are warnings." +#define FMT11 "\n... EPANET completed. There are errors." + void writeConsole(char *s); -extern char Warnflag; /* ---------------------------------------------------------------- @@ -23,40 +30,70 @@ int main(int argc, char *argv[]) ** Command line for stand-alone operation is: ** progname f1 f2 f3 ** where progname = name of executable this code was compiled to, - ** f1 = name of input file, f2 = name of report file, and - ** f3 = name of binary output file (optional). + ** f1 = name of input file, + ** f2 = name of report file (optional, stdout if left blank) + ** f3 = name of binary output file (optional, nullfile if left blank). **-------------------------------------------------------------- */ { char *f1,*f2,*f3; char blank[] = ""; + char errmsg[40]; int errcode; + int version; + char s[25]; + /* Check for proper number of command line arguments */ - if (argc < 3) { + if (argc < 2) { writeConsole(FMT03); } else { /* Call the main control function */ f1 = argv[1]; - f2 = argv[2]; + if (argc > 2) { + f2 = argv[2]; + } + else { + f2 = blank; + } if (argc > 3) { f3 = argv[3]; } else { f3 = blank; } - writeConsole(FMT01); - errcode = ENepanet(f1,f2,f3,NULL); - if (errcode > 0) { - writeConsole(FMT11); - } - else if (Warnflag > 0) { - writeConsole(FMT10); - } - else { - writeConsole(FMT09); - } + } + + /* get version from DLL and trasform in Major.Minor.Patch format + instead of hardcoded version */ + ENgetversion(&version); + int major= version/10000; + int minor= (version%10000)/100; + int patch= version%100; + sprintf(s,FMT01, major , minor, patch); + writeConsole(s); + + if (strlen(f2)> 0) { + /* use stdout for progress messages */ + errcode = ENepanet(f1,f2,f3,writeConsole); + } + else { + /* use stdout for reporting, no progress messages */ + errcode = ENepanet(f1,f2,f3,NULL); + } + + /* Error/Warning check */ + if (errcode == 0) writeConsole(FMT09); + else { + if (errcode > 100) { + writeConsole(FMT11); + } + else { + writeConsole(FMT10); + } + ENgeterror(errcode, errmsg, 40); + writeConsole(errmsg); } return(0); } /* End of main */ @@ -70,6 +107,8 @@ void writeConsole(char *s) **---------------------------------------------------------------- */ { - fprintf(stdout,"%s",s); + fprintf(stdout,"%s\n",s); fflush(stdout); } + + diff --git a/src/epanet.c b/src/epanet.c index 571bdc5..18ef1c6 100755 --- a/src/epanet.c +++ b/src/epanet.c @@ -180,7 +180,7 @@ int DLLEXPORT ENepanet(char *f1, char *f2, char *f3, void (*pviewprog) (char *)) ERRCODE(ENsolveQ()); ERRCODE(ENreport()); ENclose(); - return(errcode); + return(MAX(errcode, Warnflag) ); } @@ -290,7 +290,7 @@ int DLLEXPORT ENclose() } //(2.00.12 - LR) if (InFile != NULL) { fclose(InFile); InFile=NULL; } - if (RptFile != NULL) { fclose(RptFile); RptFile=NULL; } + if (RptFile != NULL && RptFile != stdout) { fclose(RptFile); RptFile=NULL; } if (HydFile != NULL) { fclose(HydFile); HydFile=NULL; } if (OutFile != NULL) { fclose(OutFile); OutFile=NULL; } @@ -3499,8 +3499,8 @@ void writecon(char *s) */ { //(2.00.11 - LR) - fprintf(stdout,s); - fflush(stdout); + //fprintf(stdout,s); + //fflush(stdout); } @@ -3514,14 +3514,12 @@ void writewin(char *s) **---------------------------------------------------------------- */ { -#ifdef DLL char progmsg[MAXMSG+1]; if (viewprog != NULL) { strncpy(progmsg,s,MAXMSG); viewprog(progmsg); } -#endif } From 08937fc838a27504c10cf793554d33e6d00e1441 Mon Sep 17 00:00:00 2001 From: mauriziocingi Date: Mon, 23 Nov 2015 11:16:11 +0100 Subject: [PATCH 2/5] resolved no_args bug --- run/main.c | 85 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/run/main.c b/run/main.c index d36ad6f..a96c979 100644 --- a/run/main.c +++ b/run/main.c @@ -2,12 +2,14 @@ #include #include "epanet2.h" +#define MAXMSG 79 /* Max. # characters in message text */ +#define MAXWARNCODE 99 /* text copied here, no more need of include "text.h" */ -#define FMT01 "\n... EPANET Version %d.%d.%d\n" +#define FMT01 "\nEPANET Version %d.%d.%d\n" #define FMT03 "\n Correct syntax is:\n epanet \n" -#define FMT09 "\n... EPANET completed.\n" -#define FMT10 "\n... EPANET completed. There are warnings." -#define FMT11 "\n... EPANET completed. There are errors." +#define FMT09 "\nEPANET completed.\n" +#define FMT10 "\nEPANET completed. There are warnings." +#define FMT11 "\nEPANET completed. There are errors." void writeConsole(char *s); @@ -38,42 +40,50 @@ int main(int argc, char *argv[]) { char *f1,*f2,*f3; char blank[] = ""; - char errmsg[40]; + char errmsg[MAXMSG+1]=""; int errcode; int version; char s[25]; + int major; + int minor; + int patch; + + /* 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; + sprintf(s,FMT01, major , minor, patch); + writeConsole(s); /* Check for proper number of command line arguments */ if (argc < 2) { writeConsole(FMT03); + return(1); + } + + /* set inputfile name */ + f1 = argv[1]; + if (argc > 2) { + /* set rptfile name */ + f2 = argv[2]; } else { - /* Call the main control function */ - f1 = argv[1]; - if (argc > 2) { - f2 = argv[2]; - } - else { - f2 = blank; - } - if (argc > 3) { - f3 = argv[3]; - } - else { - f3 = blank; - } + /* use stdout for rptfile */ + f2 = blank; + } + if (argc > 3) { + /* set binary output file name */ + f3 = argv[3]; + } + else { + /* NO binary output*/ + f3 = blank; } - /* get version from DLL and trasform in Major.Minor.Patch format - instead of hardcoded version */ - ENgetversion(&version); - int major= version/10000; - int minor= (version%10000)/100; - int patch= version%100; - sprintf(s,FMT01, major , minor, patch); - writeConsole(s); - + /* Call the main control function */ if (strlen(f2)> 0) { /* use stdout for progress messages */ errcode = ENepanet(f1,f2,f3,writeConsole); @@ -84,18 +94,27 @@ int main(int argc, char *argv[]) } /* Error/Warning check */ - if (errcode == 0) writeConsole(FMT09); + if (errcode == 0) { + /* no errors */ + writeConsole(FMT09); + return(0); + } else { - if (errcode > 100) { + ENgeterror(errcode, errmsg, MAXMSG); + writeConsole(errmsg); + if (errcode > MAXWARNCODE) { + /* error */ writeConsole(FMT11); + return(errcode); } else { + /* warning */ writeConsole(FMT10); + return(0); } - ENgeterror(errcode, errmsg, 40); - writeConsole(errmsg); } - return(0); + + } /* End of main */ From f89515febdcfe06ea7e806dc9b7365e93539879d Mon Sep 17 00:00:00 2001 From: Maurizio Cingi Date: Tue, 24 Nov 2015 19:49:13 +0100 Subject: [PATCH 3/5] added TmpOutFile=NULL; after closing TmpOutFile In case of [TIMES] Statistic not equal NONE (see page 174 of Epanet2 users manual) ENclose() function tries to close TmpOutFile again giving a "double free or corruption" error --- src/output.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/output.c b/src/output.c index 0e18bb8..ced8c73 100755 --- a/src/output.c +++ b/src/output.c @@ -503,6 +503,7 @@ int savefinaloutput() ERRCODE(savetimestat(x,LINKHDR)); if (!errcode) Nperiods = 1; fclose(TmpOutFile); + TmpOutFile=NULL; free(x); } From 1905c1011c55155916f0224d97423bc0e3e49946 Mon Sep 17 00:00:00 2001 From: Maurizio Cingi Date: Wed, 25 Nov 2015 12:01:51 +0100 Subject: [PATCH 4/5] adapted Linux/Makefile and created runepanet.sh.template --- build/Linux/runepanet.sh.template | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 build/Linux/runepanet.sh.template diff --git a/build/Linux/runepanet.sh.template b/build/Linux/runepanet.sh.template new file mode 100644 index 0000000..253dbc8 --- /dev/null +++ b/build/Linux/runepanet.sh.template @@ -0,0 +1,3 @@ +#!/bin/sh +export LD_LIBRARY_PATH=libdir:$LD_LIBRARY_PATH +exec exename $* From 60dc2870f5eda8850f5130988a652d5c05d174be Mon Sep 17 00:00:00 2001 From: Maurizio Cingi Date: Wed, 25 Nov 2015 12:03:51 +0100 Subject: [PATCH 5/5] adapted Linux/Makefile and created runepanet.sh.template --- build/Linux/Makefile | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/build/Linux/Makefile b/build/Linux/Makefile index 01e80cf..584c1ad 100755 --- a/build/Linux/Makefile +++ b/build/Linux/Makefile @@ -41,10 +41,13 @@ runcmdname = runepanet2.sh epanetincludedir = ../../include # Search path for sources epanetsrcdir = ../../src -VPATH = $(epanetsrcdir):$(epanetincludedir) +epanetrundir = ../../run +VPATH = $(epanetsrcdir):$(epanetincludedir):$(epanetrundir) # Install directories -prefix = ~ +#prefix = /usr/local # GNU standard -- requires superuser rights +prefix =~ +# exec_prefix = $(prefix) libdir = $(exec_prefix)/lib bindir = $(exec_prefix)/bin @@ -66,11 +69,11 @@ INSTALL_DATA = $(INSTALL) -m 644 # Files for the shared object library epanet_objs=hash.o hydraul.o inpfile.o input1.o input2.o \ input3.o mempool.o output.o quality.o report.o \ - rules.o smatrix.o + rules.o smatrix.o epanet.o # Epanet header files -epanet_heads=enumstxt.h funcs.h hash.h mempool.h text.h toolkit.h types.h vars.h +epanet_heads=enumstxt.h funcs.h hash.h mempool.h text.h types.h vars.h # Epanet main program -epanet_main=epanet +epanet_main=main # Epanet main program header files epanet_main_heads=epanet2.h @@ -78,11 +81,9 @@ epanet_main_heads=epanet2.h all: $(libname) $(exename) $(libname): $(epanet_objs) - $(CC) $(CFLAGS) $(CPPFLAGS) -D SOL -c $(epanetsrcdir)/$(epanet_main).c - $(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $(epanet_main).o $^ + $(CC) $(CFLAGS) $(LDFLAGS) -shared -o $@ $^ -$(exename): $(libname) $(epanet_main_heads) - $(CC) $(CFLAGS) $(CPPFLAGS) -D CLE -c $(epanetsrcdir)/$(epanet_main).c +$(exename): $(epanet_main).o $(epanet_main_heads) $(CC) $(CFLAGS) -o $@ $(epanet_main).o -l$(epanetrootname) $(LDFLAGS) $(epanet_objs): $(epanet_heads) @@ -95,7 +96,7 @@ install: $(INSTALL_PROGRAM) -D $(exename) $(bindir)/$(exename) $(INSTALL_PROGRAM) -D $(libname) $(libdir)/$(libname) $(INSTALL_DATA) -D $(epanetincludedir)/epanet2.h $(includedir)/epanet2.h - $(INSTALL_PROGRAM) -D $(runcmdname) $(bindir)/$(runcmdname) + $(INSTALL_PROGRAM) -D $(runcmdname) ~/$(runcmdname) .PHONY: uninstall uninstall: