From 58e330bf869b5c9c17949a778a581e6b2aae713c Mon Sep 17 00:00:00 2001 From: Caleb Date: Fri, 16 Feb 2024 01:22:30 -0500 Subject: [PATCH] Expand API to return error code and support unit testing #162 --- src/solver/consts.h | 2 +- src/solver/include/swmm5.h | 2 +- src/solver/project.c | 2 +- src/solver/swmm5.c | 34 +++++++++++++++++++--------------- 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/solver/consts.h b/src/solver/consts.h index 8208c1466..205a3c1d5 100644 --- a/src/solver/consts.h +++ b/src/solver/consts.h @@ -23,7 +23,7 @@ // General Constants //------------------ -#define VERSION 52004 +#define VERSION 53000 #define MAGICNUMBER 516114522 #define EOFMARK 0x1A // Use 0x04 for UNIX systems #define MAXTITLE 3 // Max. # title lines diff --git a/src/solver/include/swmm5.h b/src/solver/include/swmm5.h index 99498adfb..365d7ab00 100644 --- a/src/solver/include/swmm5.h +++ b/src/solver/include/swmm5.h @@ -194,7 +194,7 @@ int DLLEXPORT swmm_getErrorFromCode(int error_code, char **outErrMsg); int DLLEXPORT swmm_getWarnings(void); int DLLEXPORT swmm_getCount(int objType); -void DLLEXPORT swmm_getName(int objType, int index, char *name, int size); +int DLLEXPORT swmm_getName(int objType, int index, char *name, int size); int DLLEXPORT swmm_getIndex(int objType, const char *name); double DLLEXPORT swmm_getValue(int property, int index); int DLLEXPORT swmm_setValue(int property, int index, double value); diff --git a/src/solver/project.c b/src/solver/project.c index 1f2b5e585..13d3c0715 100644 --- a/src/solver/project.c +++ b/src/solver/project.c @@ -390,7 +390,7 @@ char *project_findID(int type, char *id) //============================================================================= -double ** project_createMatrix(int nrows, int ncols) +double **project_createMatrix(int nrows, int ncols) // // Input: nrows = number of rows (0-based) // ncols = number of columns (0-based) diff --git a/src/solver/swmm5.c b/src/solver/swmm5.c index b34c0f583..736925699 100644 --- a/src/solver/swmm5.c +++ b/src/solver/swmm5.c @@ -553,7 +553,6 @@ int DLLEXPORT swmm_useHotStart(const char* hotStartFile) FhotstartInput.mode = USE_FILE; sstrncpy(FhotstartInput.name, addAbsolutePath(fname), MAXFNAME); - return error_code; } @@ -574,7 +573,6 @@ int DLLEXPORT swmm_saveHotStart(const char* hotStartFile) else if (!IsStartedFlag) return (ErrorCode = ERR_API_NOT_STARTED); - error_code = hotstart_save_to_file(hotStartFile); return error_code; @@ -863,42 +861,42 @@ int DLLEXPORT swmm_getErrorFromCode(int error_code, char **outErrMsg) return 0; } - //============================================================================= int DLLEXPORT swmm_getCount(int objType) // // Input: objType = a type of SWMM object -// Output: returns the number of objects; +// Output: returns the number of objects or error code; // Purpose: retrieves the number of objects of a specific type. { if (!IsOpenFlag) - return 0; + return ERR_API_NOT_OPEN; if (objType < swmm_GAGE || objType > swmm_LINK) - return 0; + return ERR_API_OBJECT_TYPE; return Nobjects[objType]; } //============================================================================= -void DLLEXPORT swmm_getName(int objType, int index, char *name, int size) +int DLLEXPORT swmm_getName(int objType, int index, char *name, int size) // // Input: objType = a type of SWMM object // index = the object's index in the array of objects // name = a character array // size = size of the name array -// Output: name = the object's ID name; +// Output: name = the object's ID name; +// error code // Purpose: retrieves the ID name of an object. { char *idName = NULL; name[0] = '\0'; if (!IsOpenFlag) - return; + return ERR_API_NOT_OPEN; if (objType < swmm_GAGE || objType > swmm_LINK) - return; + return ERR_API_OBJECT_TYPE; if (index < 0 || index >= Nobjects[objType]) - return; + return ERR_API_OBJECT_INDEX; switch (objType) { case GAGE: idName = Gage[index].ID; break; @@ -908,6 +906,8 @@ void DLLEXPORT swmm_getName(int objType, int index, char *name, int size) } if (idName) sstrncpy(name, idName, size); + + return 0; } //============================================================================= @@ -916,13 +916,13 @@ int DLLEXPORT swmm_getIndex(int objType, const char *name) // // Input: objType = a type of SWMM object // name = the object's ID name -// Output: returns the object's position in the array of like objects; +// Output: returns the object's position in the array of like objects or error code; // Purpose: retrieves the index of a named object. { if (!IsOpenFlag) - return -1; + return ERR_API_NOT_OPEN; if (objType < swmm_GAGE || objType > swmm_LINK) - return -1; + return ERR_API_OBJECT_TYPE; return project_findObject(objType, name); } @@ -1273,6 +1273,10 @@ double getSystemValue(int property) return SkipSteadyState; case swmm_IGNORERAINFALL: return IgnoreRainfall; + case swmm_RULESTEP: + return RuleStep; + case swmm_SWEEPSTART: + return SweepStart; default: return ERR_API_PROPERTY_TYPE; } @@ -1519,7 +1523,7 @@ int setSystemValue(int property, double value) // // Input: property = a system property code // value = the property's new value -// Output: none +// Output: returns an error code // Purpose: sets the value of a system property. { int y, m, d, h, min, s;