Merge branch 'dev' of https://github.com/OpenWaterAnalytics/EPANET into dev-unittesting

This commit is contained in:
Michael Tryby
2019-04-03 11:10:52 -04:00
45 changed files with 832 additions and 379 deletions

View File

@@ -28,7 +28,7 @@ env:
- TEST_HOME=nrtestsuite
before_install:
- sudo apt-get -qq update
# - sudo apt-get -qq update
- eval "${MATRIX_EVAL}"
#install:

View File

@@ -20,7 +20,6 @@ environment:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
GENERATOR: "Visual Studio 15 2017"
GROUP: "SUPPORTED"
BUILD_CONFIG: "Release"
BOOST_ROOT: "C:/Libraries/boost_1_67_0"
PLATFORM: "win32"
REF_BUILD_ID: "220dev5"
@@ -28,15 +27,6 @@ environment:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
GENERATOR: "Visual Studio 15 2017 Win64"
GROUP: "EXPERIMENTAL"
BUILD_CONFIG: "Release"
BOOST_ROOT: "C:/Libraries/boost_1_67_0"
PLATFORM: "win64"
REF_BUILD_ID: "381_2"
# adding debug configuration so we can monitor memory leaks
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
GENERATOR: "Visual Studio 15 2017 Win64"
GROUP: "EXPERIMENTAL"
BUILD_CONFIG: "Debug"
BOOST_ROOT: "C:/Libraries/boost_1_67_0"
PLATFORM: "win64"
REF_BUILD_ID: "381_2"
@@ -69,7 +59,7 @@ before_build:
# run custom build script
build_script:
- cmake --build . --config %BUILD_CONFIG%
- cmake --build . --config Release
before_test:
- cd %EPANET_HOME%
@@ -79,22 +69,17 @@ before_test:
test_script:
# run unit tests
- cd %BUILD_HOME%\tests
- ctest -C %BUILD_CONFIG% --output-on-failure
- ctest -C Release --output-on-failure
# run regression tests
- cd %EPANET_HOME%
- IF "%BUILD_CONFIG%" == "Release" (
tools\run-nrtest.cmd %REF_BUILD_ID% %SUT_BUILD_ID%
)
- tools\run-nrtest.cmd %REF_BUILD_ID% %SUT_BUILD_ID%
on_success:
- cd %TEST_HOME%\benchmark
- IF "%BUILD_CONFIG%" == "Release" (
appveyor PushArtifact receipt.json
)
- appveyor PushArtifact receipt.json
on_failure:
- cd %TEST_HOME%\benchmark
# zip up the SUT benchmarks
- IF "%BUILD_CONFIG%" == "Release" (
7z a benchmark-%PLATFORM%-%SUT_BUILD_ID%.zip .\epanet-%SUT_BUILD_ID% &
appveyor PushArtifact benchmark-%PLATFORM%-%SUT_BUILD_ID%.zip
)
- 7z a benchmark-%PLATFORM%-%SUT_BUILD_ID%.zip .\epanet-%SUT_BUILD_ID%
- appveyor PushArtifact benchmark-%PLATFORM%-%SUT_BUILD_ID%.zip

View File

@@ -343,7 +343,7 @@ extern "C" {
EN_API_FLOAT_TYPE x, EN_API_FLOAT_TYPE y);
int DLLEXPORT ENgetcurve(int index, char* id, int *nPoints,
EN_API_FLOAT_TYPE **xValues, EN_API_FLOAT_TYPE **yValues);
EN_API_FLOAT_TYPE *xValues, EN_API_FLOAT_TYPE *yValues);
int DLLEXPORT ENsetcurve(int index, EN_API_FLOAT_TYPE *xValues,
EN_API_FLOAT_TYPE *yValues, int nPoints);

View File

@@ -1422,7 +1422,7 @@ typedef struct Project *EN_Project;
@ref EN_MAXID characters.
*/
int DLLEXPORT EN_getcurve(EN_Project ph, int index, char* id, int *nPoints,
double **xValues, double **yValues);
double *xValues, double *yValues);
/**
@brief assigns a set of data points to a curve.

View File

@@ -139,7 +139,7 @@ int EXPORT_PY_API curv_getlength(Handle ph, int index, int *len);
int EXPORT_PY_API curv_gettype(Handle ph, int curveIndex, int *outType);
int EXPORT_PY_API curv_getvalue(Handle ph, int curveIndex, int pointIndex, double *x, double *y);
int EXPORT_PY_API curv_setvalue(Handle ph, int curveIndex, int pointIndex, double x, double y);
int EXPORT_PY_API curv_get(Handle ph, int curveIndex, char* id, int *nValues, double **xValues, double **yValues);
int EXPORT_PY_API curv_get(Handle ph, int curveIndex, char* id, int *nValues, double *xValues, double *yValues);
int EXPORT_PY_API curv_set(Handle ph, int index, double *x, double *y, int len);
int EXPORT_PY_API scntl_add(Handle ph, int type, int linkIndex, double setting, int nodeIndex, double level, int *index);

View File

@@ -12,7 +12,6 @@
*/
#include <stdio.h>
#include "epanet2.h"
void writeConsole(char *s)

View File

@@ -7,21 +7,16 @@
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 03/17/2019
Last Updated: 04/02/2019
******************************************************************************
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#include <float.h>
#include <math.h>
@@ -221,24 +216,26 @@ int DLLEXPORT EN_open(EN_Project p, const char *inpFile, const char *rptFile,
ERRCODE(netsize(p));
ERRCODE(allocdata(p));
if (!errcode) {
// Read input data
ERRCODE(getdata(p));
// Read input data
ERRCODE(getdata(p));
// Close input file
if (p->parser.InFile != NULL) {
// 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);
// Free temporary linked lists used for Patterns & Curves
freeTmplist(p->parser.Patlist);
freeTmplist(p->parser.Curvelist);
// If using previously saved hydraulics file then open it
if (p->outfile.Hydflag == USE) ERRCODE(openhydfile(p));
// If using previously saved hydraulics file then open it
if (p->outfile.Hydflag == USE) ERRCODE(openhydfile(p));
// Write input summary to report file
// Write input summary to report file
if (!errcode)
{
if (p->report.Summaryflag) writesummary(p);
writetime(p, FMT104);
p->Openflag = TRUE;
@@ -282,7 +279,7 @@ int DLLEXPORT EN_getcomment(EN_Project p, int object, int index, char *comment)
/*----------------------------------------------------------------
** Input: object = a type of object (see EN_ObjectType)
** index = the object's index
** Output: comment = the object's descriptive comment
** Output: comment = the object's descriptive comment
** Returns: error code
** Purpose: Retrieves an object's descriptive comment
**----------------------------------------------------------------
@@ -843,7 +840,7 @@ int DLLEXPORT EN_closeQ(EN_Project p)
if (!p->Openflag) return 102;
closequal(p);
p->quality.OpenQflag = FALSE;
closeoutfile(p);
closeoutfile(p);
return 0;
}
@@ -1592,13 +1589,20 @@ int DLLEXPORT EN_setqualtype(EN_Project p, int qualType, char *chemName,
Quality *qual = &p->quality;
double *Ucf = p->Ucf;
int i;
int i, oldQualFlag, traceNodeIndex;
double ccf = 1.0;
if (!p->Openflag) return 102;
if (qual->OpenQflag) return 262;
if (qualType < EN_NONE || qualType > EN_TRACE) return 251;
qual->Qualflag = (char)qualType;
if (qualType < NONE || qualType > TRACE) return 251;
if (qualType == TRACE)
{
traceNodeIndex = findnode(net, traceNode);
if (traceNodeIndex == 0) return 212;
}
oldQualFlag = qual->Qualflag;
qual->Qualflag = qualType;
qual->Ctol *= Ucf[QUALITY];
if (qual->Qualflag == CHEM) // Chemical analysis
{
@@ -1626,14 +1630,14 @@ int DLLEXPORT EN_setqualtype(EN_Project p, int qualType, char *chemName,
// When changing from CHEM to AGE or TRACE, nodes initial quality
// values must be returned to their original ones
if ((qual->Qualflag == AGE || qual->Qualflag == TRACE) & (Ucf[QUALITY] != 1))
if ((qual->Qualflag == AGE || qual->Qualflag == TRACE) && oldQualFlag == CHEM)
{
for (i = 1; i <= p->network.Nnodes; i++)
{
p->network.Node[i].C0 *= Ucf[QUALITY];
}
}
Ucf[QUALITY] = ccf;
Ucf[LINKQUAL] = ccf;
Ucf[REACTRATE] = ccf;
@@ -4381,7 +4385,7 @@ int DLLEXPORT EN_setcurvevalue(EN_Project p, int curveIndex, int pointIndex,
}
int DLLEXPORT EN_getcurve(EN_Project p, int index, char *id, int *nPoints,
double **xValues, double **yValues)
double *xValues, double *yValues)
/*----------------------------------------------------------------
** Input: index = data curve index
** Output: id = ID name of data curve
@@ -4406,8 +4410,8 @@ int DLLEXPORT EN_getcurve(EN_Project p, int index, char *id, int *nPoints,
*nPoints = curve->Npts;
for (i = 0; i < curve->Npts; i++)
{
*xValues[i] = curve->X[i];
*yValues[i] = curve->Y[i];
xValues[i] = curve->X[i];
yValues[i] = curve->Y[i];
}
return 0;
}

View File

@@ -10,15 +10,11 @@
Last Updated: 03/17/2019
******************************************************************************
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#include <stdlib.h>
#endif
#include <string.h>
#include "types.h"
@@ -649,7 +645,7 @@ int DLLEXPORT ENsetcurvevalue(int curveIndex, int pointIndex, EN_API_FLOAT_TYPE
}
int DLLEXPORT ENgetcurve(int index, char *id, int *nPoints,
EN_API_FLOAT_TYPE **xValues, EN_API_FLOAT_TYPE **yValues)
EN_API_FLOAT_TYPE *xValues, EN_API_FLOAT_TYPE *yValues)
{
int i;
Network *net = &_defaultProject->network;
@@ -661,8 +657,8 @@ int DLLEXPORT ENgetcurve(int index, char *id, int *nPoints,
*nPoints = curve->Npts;
for (i = 0; i < curve->Npts; i++)
{
*xValues[i] = (EN_API_FLOAT_TYPE)curve->X[i];
*yValues[i] = (EN_API_FLOAT_TYPE)curve->Y[i];
xValues[i] = (EN_API_FLOAT_TYPE)curve->X[i];
yValues[i] = (EN_API_FLOAT_TYPE)curve->Y[i];
}
return 0;
}

View File

@@ -26,7 +26,6 @@ typedef struct {
Project *project;
error_handle_t *error;
}handle_t;
// Extern functions
extern char *geterrmsg(int, char *);
// Local functions
@@ -625,7 +624,7 @@ int EXPORT_PY_API curv_setvalue(Handle ph, int curveIndex, int pointIndex, doubl
return set_error(pr->error, EN_setcurvevalue(pr->project, curveIndex, pointIndex, x, y));
}
int EXPORT_PY_API curv_get(Handle ph, int curveIndex, char* id, int *nValues, double **xValues, double **yValues)
int EXPORT_PY_API curv_get(Handle ph, int curveIndex, char* id, int *nValues, double *xValues, double *yValues)
{
handle_t *pr = (handle_t *)ph;
return set_error(pr->error, EN_getcurve(pr->project, curveIndex, id, nValues, xValues, yValues));
@@ -787,29 +786,25 @@ void error_lookup(int errcode, char *dest_msg, int dest_len)
{
char *msg = NULL;
switch (errcode) {
case 1:
msg = WARN1;
break;
case 2:
msg = WARN2;
break;
case 3:
msg = WARN3;
break;
case 4:
msg = WARN4;
break;
case 5:
msg = WARN5;
break;
case 6:
msg = WARN6;
break;
default: {
char new_msg[MAXMSG + 1];
msg = geterrmsg(errcode, new_msg);
}
switch (errcode)
{
case 1: msg = WARN1;
break;
case 2: msg = WARN2;
break;
case 3: msg = WARN3;
break;
case 4: msg = WARN4;
break;
case 5: msg = WARN5;
break;
case 6: msg = WARN6;
break;
default:
{
char new_msg[MAXMSG + 1];
msg = geterrmsg(errcode, new_msg);
}
}
strncpy(dest_msg, msg, dest_len);
}

View File

@@ -11,9 +11,12 @@
******************************************************************************
*/
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <string.h>
#include "hash.h"
#define HASHTABLEMAXSIZE 128000

View File

@@ -11,9 +11,13 @@
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "types.h"

View File

@@ -11,9 +11,13 @@
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "types.h"

View File

@@ -12,9 +12,13 @@
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "types.h"

View File

@@ -12,7 +12,6 @@ Last Updated: 11/27/2018
*/
#include <stdio.h>
#include "types.h"
#include "funcs.h"

View File

@@ -7,13 +7,17 @@ Description: saves network data to an EPANET formatted text file
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 03/17/2019
Last Updated: 04/02/2019
******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "types.h"
@@ -538,7 +542,7 @@ int saveinpfile(Project *pr, const char *fname)
if (qual->Climit > 0.0)
{
fprintf(f, "\n LIMITING POTENTIAL %-.6f", qual->Climit);
fprintf(f, "\n LIMITING POTENTIAL %-.6f", qual->Climit * pr->Ucf[QUALITY]);
}
if (qual->Rfactor != MISSING && qual->Rfactor != 0.0)
{
@@ -793,8 +797,7 @@ int saveinpfile(Project *pr, const char *fname)
saveauxdata(pr, f);
// Close the new input file
fprintf(f, "\n");
fprintf(f, s_END);
fprintf(f, "\n%s\n", s_END);
fclose(f);
return 0;
}

View File

@@ -3,30 +3,26 @@
Project: OWA EPANET
Version: 2.2
Module: input1.c
Description: retrieves network data from an EPANET input file
Description: retrieves network data from an EPANET input file
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 03/17/2019
******************************************************************************
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#include "types.h"
#include "funcs.h"
#include "hash.h"
#include "text.h"
#include <math.h>
// Default values
#define MAXITER 200 // Default max. # hydraulic iterations
@@ -60,7 +56,7 @@ int getdata(Project *pr)
int errcode = 0;
// Assign default data values & reporting options
setdefaults(pr);
setdefaults(pr);
initreport(&pr->report);
// Read in network data
@@ -106,7 +102,7 @@ void setdefaults(Project *pr)
parser->Pressflag = PSI; // Pressure units are psi
out->Hydflag = SCRATCH; // No external hydraulics file
rpt->Tstatflag = SERIES; // Generate time series output
hyd->Formflag = HW; // Use Hazen-Williams formula
hyd->Htol = HTOL; // Default head tolerance
hyd->Qtol = QTOL; // Default flow tolerance
@@ -357,7 +353,7 @@ int inittanks(Project *pr)
*/
{
Network *net = &pr->network;
int i, j, n = 0;
double a;
int errcode = 0, levelerr;
@@ -550,7 +546,7 @@ void convertunits(Project *pr)
Slink *link;
Spump *pump;
Scontrol *control;
// Convert nodal elevations & initial WQ
// (WQ source units are converted in QUALITY.C
for (i = 1; i <= net->Nnodes; i++)
@@ -648,7 +644,7 @@ void convertunits(Project *pr)
pump->H0 /= pr->Ucf[HEAD];
pump->R *= (pow(pr->Ucf[FLOW], pump->N) / pr->Ucf[HEAD]);
}
// Convert flow range & max. head units
pump->Q0 /= pr->Ucf[FLOW];
pump->Qmax /= pr->Ucf[FLOW];
@@ -657,7 +653,7 @@ void convertunits(Project *pr)
}
else
{
// For flow control valves, convert flow setting
// For flow control valves, convert flow setting
// while for other valves convert pressure setting
link->Diam /= pr->Ucf[DIAM];
link->Km = 0.02517 * link->Km / SQR(link->Diam) / SQR(link->Diam);

View File

@@ -7,19 +7,16 @@ Description: reads and interprets network data from an EPANET input file
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 03/17/2019
Last Updated: 04/02/2019
******************************************************************************
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#include <math.h>
#include "types.h"
@@ -59,7 +56,7 @@ int netsize(Project *pr)
*/
{
Parser *parser = &pr->parser;
char line[MAXLINE + 1]; // Line from input data file
char *tok; // First token of line
int sect, newsect; // Input data sections
@@ -150,7 +147,7 @@ int readdata(Project *pr)
inperr, errsum; // Error code & total error count
// Allocate input buffer
parser->X = (double *)calloc(MAXTOKS + 1, sizeof(double));
parser->X = (double *)calloc(MAXTOKS, sizeof(double));
ERRCODE(MEMCHECK(parser->X));
if (errcode) return errcode;
@@ -178,7 +175,7 @@ int readdata(Project *pr)
while (fgets(line, MAXLINE, parser->InFile) != NULL)
{
// Make copy of line and scan for tokens
strncpy(wline, line, MAXLINE);
strcpy(wline, line);
parser->Ntokens = gettokens(wline, parser->Tok, MAXTOKS, parser->Comment);
// Skip blank lines and those filled with a comment
@@ -249,10 +246,10 @@ int readdata(Project *pr)
// Stop if reach end of file or max. error count
if (errsum == MAXERRS) break;
}
// Check for errors
if (errsum > 0) errcode = 200;
// Check for unlinked nodes
if (!errcode) errcode = unlinked(pr);
@@ -400,7 +397,7 @@ int updatepumpparams(Project *pr, int pumpindex)
curve->Type = PUMP_CURVE;
npts = curve->Npts;
// Generic power function curve
// Generic power function curve
if (npts == 1)
{
pump->Ptype = POWER_FUNC;
@@ -410,7 +407,7 @@ int updatepumpparams(Project *pr, int pumpindex)
q2 = 2.0 * q1;
h2 = 0.0;
}
// 3 point curve with shutoff head
else if (npts == 3 && curve->X[0] == 0.0)
{
@@ -421,7 +418,7 @@ int updatepumpparams(Project *pr, int pumpindex)
q2 = curve->X[2];
h2 = curve->Y[2];
}
// Custom pump curve
else
{
@@ -434,7 +431,7 @@ int updatepumpparams(Project *pr, int pumpindex)
pump->Q0 = (curve->X[0] + pump->Qmax) / 2.0;
pump->Hmax = curve->Y[0];
}
// Compute shape factors & limits of power function curves
if (pump->Ptype == POWER_FUNC)
{
@@ -464,7 +461,7 @@ int addnodeID(Network *net, int n, char *id)
**--------------------------------------------------------------
*/
{
if (findnode(net,id)) return 0;
if (findnode(net,id)) return 0;
strncpy(net->Node[n].ID, id, MAXID);
hashtable_insert(net->NodeHashTable, net->Node[n].ID, n);
return 1;
@@ -590,7 +587,7 @@ int unlinked(Project *pr)
Network *net = &pr->network;
int *marked;
int i, err, errcode;
errcode = 0;
err = 0;
@@ -599,19 +596,19 @@ int unlinked(Project *pr)
ERRCODE(MEMCHECK(marked));
if (errcode) return errcode;
memset(marked, 0, (net->Nnodes + 1) * sizeof(int));
// Mark end nodes of each link
for (i = 1; i <= net->Nlinks; i++)
{
marked[net->Link[i].N1]++;
marked[net->Link[i].N2]++;
}
// Check each junction
for (i = 1; i <= net->Njuncs; i++)
{
// If not marked then error
if (marked[i] == 0)
if (marked[i] == 0)
{
err++;
sprintf(pr->Msg, "Error 233: %s %s", geterrmsg(233, pr->Msg), net->Node[i].ID);
@@ -641,7 +638,7 @@ int getpatterns(Project *pr)
SFloatlist *f;
STmplist *tmppattern;
Spattern *pattern;
// Start at head of the list of patterns
tmppattern = parser->Patlist;
@@ -699,7 +696,7 @@ int getcurves(Project *pr)
{
Network *net = &pr->network;
Parser *parser = &pr->parser;
int i, j;
double x;
char errmsg[MAXMSG+1];
@@ -830,14 +827,14 @@ int gettokens(char *s, char** Tok, int maxToks, char *comment)
int m, n;
size_t len;
char *c, *c2;
// clear comment
comment[0] = '\0';
// Begin with no tokens
for (n=0; n<maxToks; n++) Tok[n] = NULL;
n = 0;
// Truncate s at start of comment
c = strchr(s,';');
if (c)
@@ -858,11 +855,17 @@ int gettokens(char *s, char** Tok, int maxToks, char *comment)
*c = '\0';
}
len = (int)strlen(s);
// Scan s for tokens until nothing left
while (len > 0 && n < MAXTOKS)
{
m = (int)strcspn(s,SEPSTR); // Find token length
if (m == len) // s is last token
{
Tok[n] = s;
n++;
break;
}
len -= m+1; // Update length of s
if (m == 0) s++; // No token found
else
@@ -871,7 +874,7 @@ int gettokens(char *s, char** Tok, int maxToks, char *comment)
{
s++; // Start token after quote
m = (int)strcspn(s,"\"\n\r"); // Find end quote (or EOL)
}
}
s[m] = '\0'; // Null-terminate the token
Tok[n] = s; // Save pointer to token
n++; // Update token count
@@ -879,7 +882,7 @@ int gettokens(char *s, char** Tok, int maxToks, char *comment)
}
}
return n;
}
}
double hour(char *time, char *units)
/*
@@ -917,7 +920,7 @@ double hour(char *time, char *units)
if (match(units, w_DAYS)) return (y[0] * 24.0);
}
// Convert hh:mm:ss format to decimal hours
// Convert hh:mm:ss format to decimal hours
if (n > 1) y[0] = y[0] + y[1] / 60.0 + y[2] / 3600.0;
// If am/pm attached then adjust hour accordingly
@@ -936,7 +939,7 @@ double hour(char *time, char *units)
else return (y[0] + 12.0);
}
return -1.0;
}
}
int getfloat(char *s, double *y)
/*
@@ -982,14 +985,14 @@ void inperrmsg(Project *pr, int err, int sect, char *line)
*/
{
Parser *parser = &pr->parser;
char errStr[MAXMSG + 1] = "";
char tok[MAXMSG + 1];
// Get token associated with input error
if (parser->ErrTok >= 0) strcpy(tok, parser->Tok[parser->ErrTok]);
else strcpy(tok, "");
// write error message to report file
sprintf(pr->Msg, "Error %d: %s %s in %s section:",
err, geterrmsg(err, errStr), tok, SectTxt[sect]);

View File

@@ -10,16 +10,13 @@ License: see LICENSE
Last Updated: 03/17/2019
******************************************************************************
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#include <math.h>
#include "types.h"

View File

@@ -15,7 +15,9 @@
*/
#include <stdlib.h>
#ifndef __APPLE__
#include <malloc.h>
#endif
#include "mempool.h"
/*

View File

@@ -21,7 +21,9 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON)
# configure file groups
set(EPANET_OUT_SOURCES src/epanet_output.c
../util/errormanager.c)
../util/errormanager.c
../util/filemanager.c
../util/cstr_helper.c)
# the binary output file API

View File

@@ -2,8 +2,9 @@
//
// epanet_output.c -- API for reading results from EPANET binary output file
//
// Version: 0.30
// Date 09/06/2017
// Version: 0.40
// Date 04/02/2019
// 09/06/2017
// 06/17/2016
// 08/05/2014
// 05/21/2014
@@ -40,18 +41,13 @@
#include <string.h>
#include "util/errormanager.h"
#include "util/filemanager.h"
#include "epanet_output.h"
#include "messages.h"
// NOTE: These depend on machine data model and may change when porting
// F_OFF Must be a 8 byte / 64 bit integer for large file support
#ifdef _WIN32 // Windows (32-bit and 64-bit)
#define F_OFF __int64
#else // Other platforms
#define F_OFF off_t
#endif
#define INT4 int // Must be a 4 byte / 32 bit integer type
#define REAL4 float // Must be a 4 byte / 32 bit real type
#define WORDSIZE 4 // Memory alignment 4 byte word size for both int and real
@@ -71,33 +67,28 @@
// Typedefs for opaque pointer
typedef struct data_s {
char name[MAXFNAME+1]; // file path/name
FILE* file; // FILE structure pointer
INT4 nodeCount, tankCount, linkCount, pumpCount, valveCount, nPeriods;
F_OFF outputStartPos; // starting file position of output data
F_OFF bytesPerPeriod; // bytes saved per simulation time period
error_handle_t* error_handle;
error_handle_t *error_handle;
file_handle_t *file_handle;
} data_t;
//-----------------------------------------------------------------------------
// Local functions
//-----------------------------------------------------------------------------
void errorLookup(int errcode, char* errmsg, int length);
int validateFile(ENR_Handle);
float getNodeValue(ENR_Handle, int, int, int);
float getLinkValue(ENR_Handle, int, int, int);
void errorLookup(int errcode, char* errmsg, int length);
int validateFile(ENR_Handle);
float getNodeValue(ENR_Handle, int, int, int);
float getLinkValue(ENR_Handle, int, int, int);
int _fopen(FILE **f, const char *name, const char *mode);
int _fseek(FILE* stream, F_OFF offset, int whence);
F_OFF _ftell(FILE* stream);
float* newFloatArray(int n);
int* newIntArray(int n);
char* newCharArray(int n);
float *newFloatArray(int n);
int *newIntArray(int n);
char *newCharArray(int n);
int EXPORT_OUT_API ENR_init(ENR_Handle* dp_handle)
int EXPORT_OUT_API ENR_init(ENR_Handle *dp_handle)
// Purpose: Initialized pointer for the opaque ENR_Handle.
//
// Returns: Error code 0 on success, -1 on failure
@@ -114,6 +105,7 @@ int EXPORT_OUT_API ENR_init(ENR_Handle* dp_handle)
if (p_data != NULL){
p_data->error_handle = create_error_manager(&errorLookup);
p_data->file_handle = create_file_manager();
*dp_handle = p_data;
}
else
@@ -123,7 +115,7 @@ int EXPORT_OUT_API ENR_init(ENR_Handle* dp_handle)
return errorcode;
}
int EXPORT_OUT_API ENR_close(ENR_Handle* p_handle)
int EXPORT_OUT_API ENR_close(ENR_Handle *p_handle)
/*------------------------------------------------------------------------
** Input: *p_handle = pointer to ENR_Handle struct
**
@@ -143,13 +135,16 @@ int EXPORT_OUT_API ENR_close(ENR_Handle* p_handle)
p_data = (data_t*)(*p_handle);
if (p_data == NULL || p_data->file == NULL)
if (p_data == NULL || p_data->file_handle == NULL)
errorcode = -1;
else
{
close_file(p_data->file_handle);
delete_error_manager(p_data->error_handle);
fclose(p_data->file);
delete_file_manager(p_data->file_handle);
free(p_data);
*p_handle = NULL;
@@ -178,23 +173,22 @@ int EXPORT_OUT_API ENR_open(ENR_Handle p_handle, const char* path)
if (p_data == NULL) return -1;
else
{
strncpy(p_data->name, path, MAXFNAME);
// Attempt to open binary output file for reading only
if ((_fopen(&(p_data->file), path, "rb")) != 0) errorcode = 434;
if ((open_file(p_data->file_handle, path, "rb")) != 0)
errorcode = 434;
// Perform checks to insure the file is valid
else if ((err = validateFile(p_data)) != 0) errorcode = err;
// If a warning is encountered read file header
if (errorcode < 400 ) {
// read network size
fseek(p_data->file, 2*WORDSIZE, SEEK_SET);
fread(&(p_data->nodeCount), WORDSIZE, 1, p_data->file);
fread(&(p_data->tankCount), WORDSIZE, 1, p_data->file);
fread(&(p_data->linkCount), WORDSIZE, 1, p_data->file);
fread(&(p_data->pumpCount), WORDSIZE, 1, p_data->file);
fread(&(p_data->valveCount), WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 2*WORDSIZE, SEEK_SET);
read_file(&(p_data->nodeCount), WORDSIZE, 1, p_data->file_handle);
read_file(&(p_data->tankCount), WORDSIZE, 1, p_data->file_handle);
read_file(&(p_data->linkCount), WORDSIZE, 1, p_data->file_handle);
read_file(&(p_data->pumpCount), WORDSIZE, 1, p_data->file_handle);
read_file(&(p_data->valveCount), WORDSIZE, 1, p_data->file_handle);
// Compute positions and offsets for retrieving data
// fixed portion of header + title section + filenames + chem names
@@ -240,8 +234,8 @@ int EXPORT_OUT_API ENR_getVersion(ENR_Handle p_handle, int* version)
if (p_data == NULL) return -1;
else
{
fseek(p_data->file, 1*WORDSIZE, SEEK_SET);
if (fread(version, WORDSIZE, 1, p_data->file) != 1)
seek_file(p_data->file_handle, 1*WORDSIZE, SEEK_SET);
if (read_file(version, WORDSIZE, 1, p_data->file_handle) != 1)
errorcode = 436;
}
@@ -319,26 +313,26 @@ int EXPORT_OUT_API ENR_getUnits(ENR_Handle p_handle, ENR_Units code, int* unitFl
switch (code)
{
case ENR_flowUnits:
_fseek(p_data->file, 9*WORDSIZE, SEEK_SET);
fread(unitFlag, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 9*WORDSIZE, SEEK_SET);
read_file(unitFlag, WORDSIZE, 1, p_data->file_handle);
break;
case ENR_pressUnits:
_fseek(p_data->file, 10*WORDSIZE, SEEK_SET);
fread(unitFlag, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 10*WORDSIZE, SEEK_SET);
read_file(unitFlag, WORDSIZE, 1, p_data->file_handle);
break;
case ENR_qualUnits:
offset = 7*WORDSIZE;
_fseek(p_data->file, offset, SEEK_SET);
fread(unitFlag, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(unitFlag, WORDSIZE, 1, p_data->file_handle);
if (*unitFlag == 0) *unitFlag = ENR_NONE;
else if (*unitFlag == 1) {
offset = 15*WORDSIZE + 3*MAXMSG_P1 + 2*(MAXFNAME+1) + MAXID_P1;
_fseek(p_data->file, offset, SEEK_SET);
fread(temp, MAXID_P1, 1, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(temp, MAXID_P1, 1, p_data->file_handle);
if (!strcmp(temp, "mg/L")) *unitFlag = ENR_MGL;
else *unitFlag = ENR_UGL;
@@ -379,18 +373,18 @@ int EXPORT_OUT_API ENR_getTimes(ENR_Handle p_handle, ENR_Time code, int* time)
switch (code)
{
case ENR_reportStart:
fseek(p_data->file, 12*WORDSIZE, SEEK_SET);
fread(time, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 12*WORDSIZE, SEEK_SET);
read_file(time, WORDSIZE, 1, p_data->file_handle);
break;
case ENR_reportStep:
fseek(p_data->file, 13*WORDSIZE, SEEK_SET);
fread(time, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 13*WORDSIZE, SEEK_SET);
read_file(time, WORDSIZE, 1, p_data->file_handle);
break;
case ENR_simDuration:
fseek(p_data->file, 14*WORDSIZE, SEEK_SET);
fread(time, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 14*WORDSIZE, SEEK_SET);
read_file(time, WORDSIZE, 1, p_data->file_handle);
break;
case ENR_numPeriods:
@@ -405,7 +399,6 @@ int EXPORT_OUT_API ENR_getTimes(ENR_Handle p_handle, ENR_Time code, int* time)
}
int EXPORT_OUT_API ENR_getChemData(ENR_Handle p_handle, char** name, int* length)
{
return 0;
}
@@ -459,8 +452,8 @@ int EXPORT_OUT_API ENR_getElementName(ENR_Handle p_handle, ENR_ElementType type,
if (!errorcode)
{
_fseek(p_data->file, offset, SEEK_SET);
fread(temp, 1, MAXID_P1, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(temp, 1, MAXID_P1, p_data->file_handle);
*name = temp;
*length = MAXID_P1;
@@ -505,9 +498,9 @@ int EXPORT_OUT_API ENR_getEnergyUsage(ENR_Handle p_handle, int pumpIndex,
offset += (pumpIndex - 1)*(WORDSIZE + 6*WORDSIZE);
// Power summary is 1 int and 6 floats for each pump
_fseek(p_data->file, offset, SEEK_SET);
fread(linkIndex, WORDSIZE, 1, p_data->file);
fread(temp, WORDSIZE, 6, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(linkIndex, WORDSIZE, 1, p_data->file_handle);
read_file(temp, WORDSIZE, 6, p_data->file_handle);
*outValues = temp;
*length = NENERGYRESULTS;
@@ -541,8 +534,8 @@ int EXPORT_OUT_API ENR_getNetReacts(ENR_Handle p_handle, float** outValues, int*
// Reaction summary is 4 floats located right before epilogue.
// This offset is relative to the end of the file.
offset = - 3*WORDSIZE - 4*WORDSIZE;
_fseek(p_data->file, offset, SEEK_END);
fread(temp, WORDSIZE, 4, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_END);
read_file(temp, WORDSIZE, 4, p_data->file_handle);
*outValues = temp;
*length = NREACTRESULTS;
@@ -670,8 +663,8 @@ int EXPORT_OUT_API ENR_getNodeAttribute(ENR_Handle p_handle, int periodIndex,
// add offset for node and attribute
offset += ((attr - 1)*p_data->nodeCount)*WORDSIZE;
_fseek(p_data->file, offset, SEEK_SET);
fread(temp, WORDSIZE, p_data->nodeCount, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(temp, WORDSIZE, p_data->nodeCount, p_data->file_handle);
*outValueArray = temp;
*length = p_data->nodeCount;
@@ -720,8 +713,8 @@ int EXPORT_OUT_API ENR_getLinkAttribute(ENR_Handle p_handle, int periodIndex,
// add offset for link and attribute
offset += ((attr - 1)*p_data->linkCount)*WORDSIZE;
_fseek(p_data->file, offset, SEEK_SET);
fread(temp, WORDSIZE, p_data->linkCount, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(temp, WORDSIZE, p_data->linkCount, p_data->file_handle);
*outValueArray = temp;
*length = p_data->linkCount;
@@ -852,16 +845,16 @@ int validateFile(ENR_Handle p_handle)
p_data = (data_t*)p_handle;
// Read magic number from beginning of file
fseek(p_data->file, 0L, SEEK_SET);
fread(&magic1, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, 0L, SEEK_SET);
read_file(&magic1, WORDSIZE, 1, p_data->file_handle);
// Fast forward to end and read file epilogue
fseek(p_data->file, -3*WORDSIZE, SEEK_END);
fread(&(p_data->nPeriods), WORDSIZE, 1, p_data->file);
fread(&hydcode, WORDSIZE, 1, p_data->file);
fread(&magic2, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, -3*WORDSIZE, SEEK_END);
read_file(&(p_data->nPeriods), WORDSIZE, 1, p_data->file_handle);
read_file(&hydcode, WORDSIZE, 1, p_data->file_handle);
read_file(&magic2, WORDSIZE, 1, p_data->file_handle);
filepos = _ftell(p_data->file);
filepos = tell_file(p_data->file_handle);
// Is the file an EPANET binary file?
if (magic1 != magic2) errorcode = 435;
@@ -891,8 +884,8 @@ float getNodeValue(ENR_Handle p_handle, int periodIndex, int nodeIndex,
// add byte position for attribute and node
offset += ((attr - 1)*p_data->nodeCount + (nodeIndex - 1))*WORDSIZE;
_fseek(p_data->file, offset, SEEK_SET);
fread(&y, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(&y, WORDSIZE, 1, p_data->file_handle);
return y;
}
@@ -915,56 +908,12 @@ float getLinkValue(ENR_Handle p_handle, int periodIndex, int linkIndex,
// add byte position for attribute and link
offset += ((attr - 1)*p_data->linkCount + (linkIndex - 1))*WORDSIZE;
_fseek(p_data->file, offset, SEEK_SET);
fread(&y, WORDSIZE, 1, p_data->file);
seek_file(p_data->file_handle, offset, SEEK_SET);
read_file(&y, WORDSIZE, 1, p_data->file_handle);
return y;
}
int _fopen(FILE **f, const char *name, const char *mode) {
//
// Purpose: Substitute for fopen_s on platforms where it doesn't exist
// Note: fopen_s is part of C++11 standard
//
int ret = 0;
#ifdef _WIN32
ret = (int)fopen_s(f, name, mode);
#else
*f = fopen(name, mode);
if (!*f)
ret = -1;
#endif
return ret;
}
int _fseek(FILE* stream, F_OFF offset, int whence)
//
// Purpose: Selects platform fseek() for large file support
//
{
#ifdef _WIN32 // Windows (32-bit and 64-bit)
#define FSEEK64 _fseeki64
#else // Other platforms
#define FSEEK64 fseeko
#endif
return FSEEK64(stream, offset, whence);
}
F_OFF _ftell(FILE* stream)
//
// Purpose: Selects platform ftell() for large file support
//
{
#ifdef _WIN32 // Windows (32-bit and 64-bit)
#define FTELL64 _ftelli64
#else // Other platforms
#define FTELL64 ftello
#endif
return FTELL64(stream);
}
float* newFloatArray(int n)
//
// Warning: Caller must free memory allocated by this function.

View File

@@ -11,9 +11,13 @@ Last Updated: 11/27/2018
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "types.h"

View File

@@ -11,18 +11,10 @@
******************************************************************************
*/
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#include <stdio.h>
#include <string.h>
//*** For the Windows SDK _tempnam function ***//
#ifdef _WIN32
#include <windows.h>
@@ -178,7 +170,7 @@ int openoutfile(Project *pr)
// Close output file if already opened
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");
@@ -288,7 +280,7 @@ void initpointers(Project *pr)
pr->hydraul.smatrix.XLNZ = NULL;
pr->hydraul.smatrix.NZSUB = NULL;
pr->hydraul.smatrix.LNZ = NULL;
initrules(pr);
}
@@ -1031,7 +1023,7 @@ char *xstrcpy(char **s1, const char *s2, const size_t n)
{
size_t n1 = 0, n2;
// Source string is empty -- free destination string
// Source string is empty -- free destination string
if (s2 == NULL || strlen(s2) == 0)
{
free(*s1);

View File

@@ -11,9 +11,13 @@ Last Updated: 11/27/2018
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "mempool.h"

View File

@@ -13,7 +13,6 @@ Last Updated: 11/27/2018
#include <stdio.h>
#include <math.h>
#include "types.h"
// Exported functions

View File

@@ -11,10 +11,13 @@ Last Updated: 11/27/2018
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include "mempool.h"
#include "types.h"

View File

@@ -11,10 +11,13 @@
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#ifdef _WIN32
#define snprintf _snprintf

View File

@@ -11,9 +11,13 @@
******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include "types.h"
#include "funcs.h"

View File

@@ -18,11 +18,16 @@
linsolve() -- called from netsolve() in HYDRAUL.C
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef __APPLE__
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include <math.h>
#include <limits.h>
#include <time.h> //For optional timer macros
#include "text.h"

View File

@@ -14,10 +14,8 @@
#ifndef TYPES_H
#define TYPES_H
#include <stdio.h>
#include "hash.h"
#include <stdio.h>
/*
-------------------------------------------

48
src/util/cstr_helper.c Normal file
View File

@@ -0,0 +1,48 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/cstr_helper.c
Description: Provides C string helper functions
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/02/2019
******************************************************************************
*/
#include <stdlib.h>
#include <string.h>
#include "cstr_helper.h"
int copy_cstr(const char *source, char **dest)
// Determines length, allocates memory, and returns a null terminated copy
// Be Aware: caller is responsible for freeing memory
{
size_t size;
size = 1 + strlen(source);
*dest = (char *) calloc(size, sizeof(char));
if (*dest == NULL)
return -1;
else {
#ifdef _MSC_VER
strncpy_s(*dest, size, source, size);
#else
strncpy(*dest, source, size);
#endif
}
return 0;
}
bool isnullterm_cstr(const char *source)
{
if (strchr(source, '\0'))
return true;
else
return false;
}

25
src/util/cstr_helper.h Normal file
View File

@@ -0,0 +1,25 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/cstr_helper.h
Description: Provides C string helper functions
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/02/2019
******************************************************************************
*/
#ifndef CSTR_HELPER_H_
#define CSTR_HELPER_H_
#include <stdbool.h>
int copy_cstr(const char *source, char **destination);
bool isnullterm_cstr(const char *source);
#endif /* CSTR_HELPER_H_ */

View File

@@ -1,21 +1,34 @@
//-----------------------------------------------------------------------------
//
// errormanager.c
//
// Purpose: Provides a simple interface for managing runtime error messages.
//
// Date: 08/25/2017
//
// Author: Michael E. Tryby
// US EPA - ORD/NRMRL
//-----------------------------------------------------------------------------
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/errormanager.c
Description: Provides a simple interface for managing errors
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/02/2019
******************************************************************************
*/
//#ifdef _WIN32
//#define _CRTDBG_MAP_ALLOC
//#include <stdlib.h>
//#include <crtdbg.h>
//#else
#include <stdlib.h>
//#endif
#include <string.h>
#include "errormanager.h"
typedef struct error_s {
int error_status;
void (*p_msg_lookup)(int, char*, int);
} error_handle_t;
error_handle_t *create_error_manager(void (*p_error_message)(int, char*, int))
//
// Purpose: Constructs a new error handle.

View File

@@ -1,11 +1,15 @@
/*
* errormanager.h
*
* Created on: Aug 25, 2017
*
* Author: Michael E. Tryby
* US EPA - ORD/NRMRL
*/
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/errormanager.h
Description: Provides a simple interface for managing errors
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/02/2019
******************************************************************************
*/
#ifndef ERRORMANAGER_H_
#define ERRORMANAGER_H_
@@ -17,11 +21,8 @@
extern "C" {
#endif
typedef struct error_s {
int error_status;
void (*p_msg_lookup)(int, char*, int);
} error_handle_t;
// Forward declaration
typedef struct error_s error_handle_t;
error_handle_t* create_error_manager(void (*p_error_message)(int, char*, int));
void delete_error_manager(error_handle_t* error_handle);

211
src/util/filemanager.c Normal file
View File

@@ -0,0 +1,211 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/filemanager.c
Description: Provides a simple interface for managing files
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/01/2019
******************************************************************************
*/
// MSVC ONLY
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
#include <stdarg.h>
#include "filemanager.h"
typedef struct file_s {
char *filename; // Assumes this is a null terminated string
FILE *file;
} file_handle_t;
// local (private) functions
int _fopen(FILE **f, const char *name, const char *mode);
int _get_temp_filename(char **tempname);
file_handle_t *create_file_manager() {
file_handle_t *file_handle;
file_handle = (file_handle_t *)calloc(1, sizeof(file_handle_t));
file_handle->filename = NULL;
file_handle->file = NULL;
return file_handle;
}
void delete_file_manager(file_handle_t *file_handle) {
if (file_handle->file != NULL)
close_file(file_handle);
free(file_handle->filename);
free(file_handle);
}
int get_filename(file_handle_t *file_handle, char **filename)
//
// BE AWARE: The memory allocated here must be freed by the caller
//
{
return copy_cstr(file_handle->filename, filename);
}
int open_file(file_handle_t *file_handle, const char *filename, const char *file_mode) {
int error = 0;
if (filename == NULL)
_get_temp_filename(&(file_handle->filename));
else
copy_cstr(filename, &(file_handle->filename));
if (file_mode == NULL)
error = -1;
else {
error = _fopen(&(file_handle->file), file_handle->filename, file_mode);
}
return error;
}
int seek_file(file_handle_t *file_handle, F_OFF offset, int whence)
{
#ifdef _MSC_VER // Windows (32-bit and 64-bit)
#define FSEEK64 _fseeki64
#else // Other platforms
#define FSEEK64 fseeko
#endif
return FSEEK64(file_handle->file, offset, whence);
}
F_OFF tell_file(file_handle_t *file_handle)
{
#ifdef _MSC_VER // Windows (32-bit and 64-bit)
#define FTELL64 _ftelli64
#else // Other platforms
#define FTELL64 ftello
#endif
return FTELL64(file_handle->file);
}
// Read and write to a binary file
size_t read_file(void *ptr, size_t size, size_t nmemb, file_handle_t *file_handle)
{
return fread(ptr, size, nmemb, file_handle->file);
}
size_t write_file(const void * ptr, size_t size, size_t count, file_handle_t *file_handle)
{
return fwrite(ptr, size, count, file_handle->file);
}
// print and get from a text file
int printf_file(file_handle_t *file_handle, const char *format, ... )
{
int error = 0;
va_list args;
va_start(args, format);
error = vfprintf(file_handle->file, format, args);
va_end(args);
return error;
}
int gets_file(char *str, int num, file_handle_t *file_handle)
{
fgets(str, num, file_handle->file);
return 0;
}
int close_file(file_handle_t *file_handle) {
int error = 0;
if (file_handle->file != NULL) {
error = fclose(file_handle->file);
file_handle->file = NULL;
}
return error;
}
int remove_file(file_handle_t *file_handle) {
return remove(file_handle->filename);
}
bool is_valid(file_handle_t *file_handle)
{
if ((file_handle->filename == NULL && file_handle->file == NULL) ||
(isnullterm_cstr(file_handle->filename) && file_handle != NULL))
return true;
else
return false;
}
int _fopen(FILE **f, const char *name, const char *mode)
//
// Purpose: Substitute for fopen_s on platforms where it doesn't exist
// Note: fopen_s is part of C++11 standard
//
{
int ret = 0;
#ifdef _MSC_VER
ret = (int)fopen_s(f, name, mode);
#else
*f = fopen(name, mode);
if (!*f)
ret = -1;
#endif
return ret;
}
int _get_temp_filename(char **tempname)
{
int error = 0;
#ifdef _MSC_VER
char *name = NULL;
// --- use Windows _tempnam function to get a pointer to an
// unused file name that begins with "en"
if ((name = _tempnam(name, "en")) == NULL) {
error = -1;
return error;
}
else
copy_cstr(name, tempname);
// --- free the pointer returned by _tempnam
if (name)
free(name);
// --- for non-Windows systems:
#else
// --- use system function mkstemp() to create a temporary file name
copy_cstr("enXXXXXX", tempname);
error = mkstemp(*tempname);
#endif
return error;
}

81
src/util/filemanager.h Normal file
View File

@@ -0,0 +1,81 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/filemanager.h
Description: Provides a simple interface for managing files
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/01/2019
******************************************************************************
*/
#ifndef FILEMANAGER_H_
#define FILEMANAGER_H_
#include <stdio.h>
#include <stdarg.h>
#include "cstr_helper.h"
// F_OFF Must be a 8 byte / 64 bit integer for large file support
#ifdef _MSC_VER // Windows (32-bit and 64-bit)
#define F_OFF __int64
#else // Other platforms
#define F_OFF off_t
#endif
#define FILE_MAXNAME 259
#if defined(__cplusplus)
extern "C" {
#endif
// Forward declariation of file_handle_t
typedef struct file_s file_handle_t;
file_handle_t *create_file_manager();
void delete_file_manager(file_handle_t *file_handle);
int get_filename(file_handle_t *file_handle, char **filename);
int open_file(file_handle_t *file_handle, const char *filename, const char *file_mode);
int seek_file(file_handle_t *file_handle, F_OFF offset, int whence);
F_OFF tell_file(file_handle_t *file_handle);
// Functions for working with binary files
size_t read_file(void *ptr, size_t size, size_t nmemb, file_handle_t *file_handle);
size_t write_file(const void *ptr, size_t size, size_t count, file_handle_t *file_handle);
// Functions for working with text files
int printf_file(file_handle_t *file_handle, const char *format, ... );
int gets_file(char *str, int num, file_handle_t *file_handle);
int close_file(file_handle_t *file_handle);
int remove_file(file_handle_t *file_handle);
bool is_valid(file_handle_t *file_handle);
#if defined(__cplusplus)
}
#endif
#endif /* FILEMANAGER_H_ */

View File

@@ -19,19 +19,18 @@ endif(UNIX)
set(toolkit_test_srcs
test_toolkit.cpp
test_project.cpp
# test_hydraulics.cpp
# test_quality.cpp
# test_report.cpp
# test_analysis.cpp
# test_node.cpp
# test_demand.cpp
# test_link.cpp
test_hydraulics.cpp
test_quality.cpp
test_report.cpp
test_analysis.cpp
test_node.cpp
test_demand.cpp
test_link.cpp
# test_pump.cpp
# test_pattern.cpp
# test_curve.cpp
# test_control.cpp
# test_net_builder.cpp
)
test_pattern.cpp
test_curve.cpp
test_control.cpp
test_net_builder.cpp)
add_executable(test_toolkit ${toolkit_test_srcs})
@@ -60,6 +59,10 @@ add_test(NAME test_reent
add_test(NAME test_errormanager
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_errormanager)
add_test(NAME test_filemanager
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_filemanager
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/outfile/data)
add_test(NAME test_output
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_output
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/outfile/data)

View File

@@ -11,6 +11,7 @@
******************************************************************************
*/
//#define BOOST_ALL_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "test_toolkit.hpp"

View File

@@ -35,6 +35,35 @@ BOOST_FIXTURE_TEST_CASE(test_curve_comments, FixtureOpenClose)
error = EN_getcomment(ph, EN_CURVE, index, comment);
BOOST_REQUIRE(error == 0);
BOOST_CHECK(check_string(comment, (char *)"Curve 1"));
// Test of EN_setcurve and EN_getcurve
int i;
char id1[] = "NewCurve";
int n1 = 5;
double X1[] = {16.88889, 19.5, 22.13889, 25.94445, 33.33334};
double Y1[] = {156.7, 146.5, 136.2, 117.9, 50.0};
int n2;
double X2[5], Y2[5];
char id2[EN_MAXID+1];
// Add data to a new curve
error = EN_addcurve(ph, id1);
BOOST_REQUIRE(error == 0);
error = EN_getcurveindex(ph, id1, &i);
BOOST_REQUIRE(error == 0);
error = EN_setcurve(ph, i, X1, Y1, n1);
BOOST_REQUIRE(error == 0);
// Retrieve data from curve
error = EN_getcurve(ph, i, id2, &n2, X2, Y2);
BOOST_REQUIRE(error == 0);
BOOST_CHECK(check_string(id2, id1));
BOOST_REQUIRE(n2 == n1);
for (i = 0; i < n1; i++)
{
BOOST_REQUIRE(X1[i] == X2[i]);
BOOST_REQUIRE(Y1[i] == Y2[i]);
}
}
BOOST_AUTO_TEST_SUITE_END()

View File

@@ -11,6 +11,8 @@
******************************************************************************
*/
#include <math.h>
#include <boost/test/unit_test.hpp>
#include "test_toolkit.hpp"

View File

@@ -58,47 +58,35 @@ BOOST_AUTO_TEST_CASE(test_save)
{
int error;
EN_Project ph = NULL;
EN_Project ph_save;
error = EN_createproject(&ph);
BOOST_REQUIRE(error == 0);
error = EN_open(ph, DATA_PATH_NET1, DATA_PATH_RPT, DATA_PATH_OUT);
EN_createproject(&ph_save);
error = EN_open(ph_save, DATA_PATH_NET1, DATA_PATH_RPT, DATA_PATH_OUT);
BOOST_REQUIRE(error == 0);
error = EN_saveinpfile(ph, DATA_PATH_TMP);
error = EN_saveinpfile(ph_save, "test_reopen.inp");
BOOST_REQUIRE(error == 0);
BOOST_CHECK(boost::filesystem::exists(DATA_PATH_TMP) == true);
BOOST_CHECK(boost::filesystem::exists("test_reopen.inp") == true);
error = EN_close(ph);
error = EN_close(ph_save);
BOOST_REQUIRE(error == 0);
error = EN_deleteproject(&ph);
BOOST_REQUIRE(error == 0);
BOOST_CHECK(ph == NULL);
EN_deleteproject(&ph_save);
}
BOOST_AUTO_TEST_CASE(test_reopen, * boost::unit_test::depends_on("test_project/test_save"))
{
int error;
EN_Project ph = NULL;
EN_Project ph_reopen;
error = EN_createproject(&ph);
BOOST_REQUIRE(error == 0);
EN_createproject(&ph_reopen);
error = EN_open(ph_reopen, "test_reopen.inp", DATA_PATH_RPT, DATA_PATH_OUT);
BOOST_REQUIRE(error == 0);
error = EN_open(ph, DATA_PATH_TMP, DATA_PATH_RPT, DATA_PATH_OUT);
BOOST_REQUIRE(error == 0);
error = EN_close(ph);
BOOST_REQUIRE(error == 0);
EN_deleteproject(&ph);
BOOST_REQUIRE(error == 0);
BOOST_CHECK(ph == NULL);
error = EN_close(ph_reopen);
BOOST_REQUIRE(error == 0);
EN_deleteproject(&ph_reopen);
}
BOOST_AUTO_TEST_CASE(test_run)

View File

@@ -14,12 +14,6 @@
#ifndef TEST_TOOLKIT_HPP
#define TEST_TOOLKIT_HPP
// MSVC ONLY
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include "epanet2_2.h"

View File

@@ -7,11 +7,14 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (test_source
./test_errormanager.cpp
../../src/util/errormanager.c
)
add_executable(test_errormanager ${test_source})
add_executable(test_errormanager ./test_errormanager.cpp
../../src/util/errormanager.c)
target_include_directories(test_errormanager PUBLIC ../../src/)
target_link_libraries(test_errormanager ${Boost_LIBRARIES})
add_executable(test_filemanager ./test_filemanager.cpp
../../src/util/filemanager.c
../../src/util/cstr_helper.c)
target_include_directories(test_filemanager PUBLIC ../../src/)
target_link_libraries(test_filemanager ${Boost_LIBRARIES})

View File

@@ -1,7 +1,7 @@
#define BOOST_TEST_MODULE errormanager
//#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "util/errormanager.h"
@@ -15,10 +15,10 @@ void mock_lookup(int errcode, char *errmsg, int len)
char *msg = NULL;
if (errcode == 100) {
msg = MESSAGE_STRING;
msg = (char *)MESSAGE_STRING;
}
else {
msg = "";
msg = (char *)"";
}
strncpy(errmsg, msg, len);
}

View File

@@ -0,0 +1,97 @@
/*
******************************************************************************
Project: OWA EPANET
Version: 2.2
Module: util/test_filemanager.cpp
Description: Tests for util/filemanager.c
Authors: see AUTHORS
Copyright: see AUTHORS
License: see LICENSE
Last Updated: 04/01/2019
******************************************************************************
*/
#define BOOST_TEST_MODULE filemanager
#include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp>
#include "util/filemanager.h"
#define DATA_PATH_OUTPUT "./example1.out"
boost::test_tools::predicate_result check_string(std::string test, std::string ref)
{
if (ref.compare(test) == 0)
return true;
else
return false;
}
BOOST_AUTO_TEST_SUITE(test_filemanager)
BOOST_AUTO_TEST_CASE (test_create_destroy)
{
file_handle_t *file_handle = NULL;
file_handle = create_file_manager();
BOOST_CHECK(file_handle != NULL);
BOOST_CHECK(is_valid(file_handle) == true);
delete_file_manager(file_handle);
}
BOOST_AUTO_TEST_CASE(test_open_close)
{
int error = 0;
file_handle_t *file_handle = NULL;
file_handle = create_file_manager();
BOOST_CHECK(file_handle != NULL);
error = open_file(file_handle, DATA_PATH_OUTPUT, "rb");
BOOST_REQUIRE(error == 0);
BOOST_CHECK(is_valid(file_handle) == true);
error = close_file(file_handle);
BOOST_REQUIRE(error == 0);
delete_file_manager(file_handle);
}
struct Fixture{
Fixture() {
error = 0;
file_handle = NULL;
file_handle = create_file_manager();
open_file(file_handle, NULL, "wt");
}
~Fixture() {
close_file(file_handle);
delete_file_manager(file_handle);
}
int error;
file_handle_t *file_handle;
};
BOOST_FIXTURE_TEST_CASE(test_temp_file, Fixture)
{
char *filename;
printf_file(file_handle, "%s", "This is a test.");
error = get_filename(file_handle, &filename);
BOOST_REQUIRE(error == 0);
BOOST_CHECK(is_valid(file_handle) == true);
BOOST_CHECK(boost::filesystem::exists(filename) == true);
free(filename);
}
BOOST_AUTO_TEST_SUITE_END()