Initial commit interface for python wrap

This commit is contained in:
Michael Tryby
2018-11-30 15:49:28 -05:00
parent 9ed3462dbf
commit b63f232884
6 changed files with 190 additions and 3 deletions

3
.gitignore vendored
View File

@@ -224,5 +224,6 @@ temp/
nrtestsuite/ nrtestsuite/
tests/data/ tests/data/
#Cmake generated export headers #Cmake stuff
buildprod*/
*_export.h *_export.h

View File

@@ -69,8 +69,8 @@ ENDIF (MSVC)
# configure file groups # configure file groups
file(GLOB EPANET_SOURCES src/*.c) file(GLOB EPANET_SOURCES src/*.c src/util/*.c)
file(GLOB EPANET_LIB_ALL src/*.c src/*.h) file(GLOB EPANET_LIB_ALL src/* src/util/*)
source_group("Library" FILES ${EPANET_LIB_ALL}) source_group("Library" FILES ${EPANET_LIB_ALL})

33
include/epanet_py.h Normal file
View File

@@ -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

52
src/epanet_py.c Normal file
View File

@@ -0,0 +1,52 @@
#include <stdlib.h>
#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));
}

74
src/util/errormanager.c Normal file
View File

@@ -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 <stdlib.h>
#include <string.h>
#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;
}

27
src/util/errormanager.h Normal file
View File

@@ -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_ */