From b63f23288456beed16b7a36b19b394e72b8ff733 Mon Sep 17 00:00:00 2001 From: Michael Tryby Date: Fri, 30 Nov 2018 15:49:28 -0500 Subject: [PATCH] Initial commit interface for python wrap --- .gitignore | 3 +- CMakeLists.txt | 4 +-- include/epanet_py.h | 33 ++++++++++++++++++ src/epanet_py.c | 52 +++++++++++++++++++++++++++++ src/util/errormanager.c | 74 +++++++++++++++++++++++++++++++++++++++++ src/util/errormanager.h | 27 +++++++++++++++ 6 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 include/epanet_py.h create mode 100644 src/epanet_py.c create mode 100644 src/util/errormanager.c create mode 100644 src/util/errormanager.h diff --git a/.gitignore b/.gitignore index 628cb20..3c617e0 100755 --- a/.gitignore +++ b/.gitignore @@ -224,5 +224,6 @@ temp/ nrtestsuite/ tests/data/ -#Cmake generated export headers +#Cmake stuff +buildprod*/ *_export.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2756b71..268f55e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,8 +69,8 @@ ENDIF (MSVC) # configure file groups -file(GLOB EPANET_SOURCES src/*.c) -file(GLOB EPANET_LIB_ALL src/*.c src/*.h) +file(GLOB EPANET_SOURCES src/*.c src/util/*.c) +file(GLOB EPANET_LIB_ALL src/* src/util/*) source_group("Library" FILES ${EPANET_LIB_ALL}) diff --git a/include/epanet_py.h b/include/epanet_py.h new file mode 100644 index 0000000..dac4a2e --- /dev/null +++ b/include/epanet_py.h @@ -0,0 +1,33 @@ + + + + +#ifndef EPANET_PY_H +#define EPANET_PY_H + + +#include "epanet2_export.h" +#include "epanet2_enums.h" + + +#if defined(__cplusplus) +extern "C" { +#endif + +// Opaque pointer to project +typedef void *Handle; + + +int DLLEXPORT create_project(Handle *ph); + +int DLLEXPORT delete_project(Handle *ph); + +int DLLEXPORT run_project(Handle ph, const char *input_path, + const char *report_path, const char *output_path); + + +#if defined(__cplusplus) +} +#endif + +#endif //EPANET_PY_H diff --git a/src/epanet_py.c b/src/epanet_py.c new file mode 100644 index 0000000..abfedb9 --- /dev/null +++ b/src/epanet_py.c @@ -0,0 +1,52 @@ + + +#include + +#include "epanet_py.h" +#include "util/errormanager.h" +#include "epanet2_2.h" +#include "types.h" + + +typedef struct { + Project *project; + error_handle_t *error; +}handle_t; + + +int DLLEXPORT create_project(Handle *ph) +{ + handle_t *handle = (handle_t *)calloc(1, sizeof(handle_t)); + + if (handle != NULL) + { + EN_createproject(&handle->project); + handle->error = error_new_manager(&EN_geterror); + *ph = handle; + return 0; + } + return -1; +} + +int DLLEXPORT delete_project(Handle *ph) +{ + handle_t *handle = (handle_t *)*ph; + + if (handle == NULL) + return -1; + else + { + EN_deleteproject(&handle->project); + error_dst_manager(handle->error); + } + return 0; +} + +int DLLEXPORT run_project(Handle ph, const char *input_path, + const char *report_path, const char *output_path) +{ + handle_t *pr = (handle_t *)ph; + + return error_set(pr->error, + EN_runproject(pr->project, input_path, report_path, output_path, NULL)); +} diff --git a/src/util/errormanager.c b/src/util/errormanager.c new file mode 100644 index 0000000..3f4f47c --- /dev/null +++ b/src/util/errormanager.c @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------------- +// +// errormanager.c +// +// Purpose: Provides a simple interface for managing runtime error messages. +// +// Date: 08/25/2017 +// +// Author: Michael E. Tryby +// US EPA - ORD/NRMRL +//----------------------------------------------------------------------------- +#include +#include +#include "errormanager.h" + +error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)) +// +// Purpose: Constructs a new error handle. +// +{ + error_handle_t* error_handle; + error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t)); + + error_handle->p_msg_lookup = p_error_message; + + return error_handle; +} + +void error_dst_manager(error_handle_t* error_handle) +// +// Purpose: Destroys the error handle. +// +{ + free(error_handle); +} + +int error_set(error_handle_t* error_handle, int errorcode) +// +// Purpose: Sets an error code in the handle. +// +{ + // If the error code is 0 no action is taken and 0 is returned. + // This is a feature not a bug. + if (errorcode) + error_handle->error_status = errorcode; + + return errorcode; +} + +char* error_check(error_handle_t* error_handle) +// +// Purpose: Returns the error message or NULL. +// +// Note: Caller must free memory allocated by check_error +// +{ + char* temp = NULL; + + if (error_handle->error_status != 0) { + temp = (char*) calloc(ERR_MAXMSG, sizeof(char)); + + if (temp) + error_handle->p_msg_lookup(error_handle->error_status, temp, ERR_MAXMSG); + } + return temp; +} + +void error_clear(error_handle_t* error_handle) +// +// Purpose: Clears the error from the handle. +// +{ + error_handle->error_status = 0; +} diff --git a/src/util/errormanager.h b/src/util/errormanager.h new file mode 100644 index 0000000..2a8099f --- /dev/null +++ b/src/util/errormanager.h @@ -0,0 +1,27 @@ +/* + * errormanager.h + * + * Created on: Aug 25, 2017 + * + * Author: Michael E. Tryby + * US EPA - ORD/NRMRL + */ + +#ifndef ERRORMANAGER_H_ +#define ERRORMANAGER_H_ + +#define ERR_MAXMSG 256 + +typedef struct error_s { + int error_status; + void (*p_msg_lookup)(int, char*, int); +} error_handle_t; + +error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int)); +void error_dst_manager(error_handle_t* error_handle); + +int error_set(error_handle_t* error_handle, int errorcode); +char* error_check(error_handle_t* error_handle); +void error_clear(error_handle_t* error_handle); + +#endif /* ERRORMANAGER_H_ */