Adding unit test and refactoring
Added unit test for errormanager and refactored to simplify use of error_check() method.
This commit is contained in:
@@ -93,7 +93,7 @@ int EXPORT_PY_API proj_gettitle(Handle ph, char *line1, char *line2, char *line3
|
||||
int EXPORT_PY_API proj_settitle(Handle ph, const char *line1, const char *line2, const char *line3)
|
||||
{
|
||||
handle_t *pr = (handle_t *)ph;
|
||||
return error_set(pr->error, EN_settitle(pr->project, line1, line2, line3));
|
||||
return error_set(pr->error, EN_settitle(pr->project, (char *)line1, (char *)line2, (char *)line3));
|
||||
}
|
||||
|
||||
int EXPORT_PY_API proj_getcount(Handle ph, EN_CountType code, int *count)
|
||||
@@ -761,28 +761,13 @@ void EXPORT_PY_API err_clear(Handle ph)
|
||||
|
||||
int EXPORT_PY_API err_check(Handle ph, char** msg_buffer)
|
||||
//
|
||||
// Purpose: Returns the error message or NULL.
|
||||
// Purpose: Returns the error code and message or 0 and NULL respectively.
|
||||
//
|
||||
// Note: Caller must free memory allocated by EN_check_error
|
||||
//
|
||||
{
|
||||
int errorcode = 0;
|
||||
char *temp = NULL;
|
||||
|
||||
handle_t *pr = (handle_t *)ph;
|
||||
|
||||
|
||||
if (pr == NULL) return -1;
|
||||
else
|
||||
{
|
||||
errorcode = pr->error->error_status;
|
||||
if (errorcode)
|
||||
temp = error_check(pr->error);
|
||||
|
||||
*msg_buffer = temp;
|
||||
}
|
||||
|
||||
return errorcode;
|
||||
return error_check(pr->error, msg_buffer);
|
||||
}
|
||||
|
||||
int EXPORT_PY_API toolkit_getversion(int *version)
|
||||
|
||||
@@ -9,16 +9,25 @@
|
||||
// Author: Michael E. Tryby
|
||||
// US EPA - ORD/NRMRL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//#ifdef _WIN32
|
||||
//#define _CRTDBG_MAP_ALLOC
|
||||
//#include <stdlib.h>
|
||||
//#include <crtdbg.h>
|
||||
//#else
|
||||
#include <stdlib.h>
|
||||
//#endif
|
||||
#include <string.h>
|
||||
|
||||
#include "errormanager.h"
|
||||
|
||||
error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int))
|
||||
|
||||
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_t *error_handle;
|
||||
error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t));
|
||||
|
||||
error_handle->p_msg_lookup = p_error_message;
|
||||
@@ -26,7 +35,7 @@ error_handle_t* error_new_manager(void (*p_error_message)(int, char*, int))
|
||||
return error_handle;
|
||||
}
|
||||
|
||||
void error_dst_manager(error_handle_t* error_handle)
|
||||
void error_dst_manager(error_handle_t *error_handle)
|
||||
//
|
||||
// Purpose: Destroys the error handle.
|
||||
//
|
||||
@@ -34,38 +43,39 @@ void error_dst_manager(error_handle_t* error_handle)
|
||||
free(error_handle);
|
||||
}
|
||||
|
||||
int error_set(error_handle_t* error_handle, int errorcode)
|
||||
int error_set(error_handle_t *error_handle, int error_code)
|
||||
//
|
||||
// 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;
|
||||
if (error_code)
|
||||
error_handle->error_status = error_code;
|
||||
|
||||
return errorcode;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
char* error_check(error_handle_t* error_handle)
|
||||
int error_check(error_handle_t *error_handle, char **error_message)
|
||||
//
|
||||
// Purpose: Returns the error message or NULL.
|
||||
//
|
||||
// Note: Caller must free memory allocated by check_error
|
||||
//
|
||||
{
|
||||
char* temp = NULL;
|
||||
{ int error_code = error_handle->error_status;
|
||||
char *temp = NULL;
|
||||
|
||||
if (error_handle->error_status != 0) {
|
||||
temp = (char*) calloc(ERR_MAXMSG, sizeof(char));
|
||||
if (error_code != 0) {
|
||||
temp = (char*) calloc(ERR_MAXMSG + 1, sizeof(char));
|
||||
|
||||
if (temp)
|
||||
error_handle->p_msg_lookup(error_handle->error_status, temp, ERR_MAXMSG);
|
||||
error_handle->p_msg_lookup(error_code, temp, ERR_MAXMSG);
|
||||
}
|
||||
return temp;
|
||||
*error_message = temp;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
void error_clear(error_handle_t* error_handle)
|
||||
void error_clear(error_handle_t *error_handle)
|
||||
//
|
||||
// Purpose: Clears the error from the handle.
|
||||
//
|
||||
|
||||
@@ -12,16 +12,27 @@
|
||||
|
||||
#define ERR_MAXMSG 256
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct error_s {
|
||||
int error_status;
|
||||
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);
|
||||
int error_set(error_handle_t* error_handle, int error_code);
|
||||
int error_check(error_handle_t* error_handle, char **error_message);
|
||||
void error_clear(error_handle_t* error_handle);
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ERRORMANAGER_H_ */
|
||||
|
||||
Reference in New Issue
Block a user