lixiaojun
2025-01-21 f589894c84d47b9671eef6d3a8337b6b51b32edb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 ******************************************************************************
 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.
//
{
    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 delete_error_manager(error_handle_t *error_handle)
//
// Purpose: Destroys the error handle.
//
{
    free(error_handle);
}
 
int set_error(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 (error_code)
        error_handle->error_status = error_code;
 
    return error_code;
}
 
int check_error(error_handle_t *error_handle, char **error_message)
//
// Purpose: Returns the error message or NULL.
//
// Note: Caller must free memory allocated by check_error
//
{   int error_code = error_handle->error_status;
    char *temp = NULL;
 
    if (error_code != 0) {
        temp = (char*) calloc(ERR_MAXMSG + 1, sizeof(char));
 
        if (temp)
            error_handle->p_msg_lookup(error_code, temp, ERR_MAXMSG);
    }
    *error_message = temp;
    return error_code;
}
 
void clear_error(error_handle_t *error_handle)
//
// Purpose: Clears the error from the handle.
//
{
    error_handle->error_status = 0;
}