-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRerror.c
167 lines (148 loc) · 4.2 KB
/
LRerror.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*!
\file LRerror.c
\brief LibRan error codes and messages
The LibRan error routines are patterned after the Standard C perror()/strerror()
routines and the like.
The object creation routines like `LR_new` and `LR_bin_new` will return
a NULL value if they fail. No error value is returned otherwise.
Many of the LibRan routines return a random variate or
distribution function value. When these result in an error these functions
return NAN (Not-A-Number).
The `LR_obj` and `LR_bin` objects have an \e errno attribute which can
be queried.
An example code fragment:
\code
#include <math.h>
#include "libran.h"
...
LR_obj *o = LR_new(piece, LR_double);
if (!*o) { // handle error }
...
if (isnan(x = LRd_RAN(o)) {
// handle error
LRperror("MyApp",o->errno);
}
...
\endcode
However, the other routines, especially those
that return an integer value are returning a LibRan error value if non-zero.
\code
#include "libran.h"
...
LR_bin *b = LR_bin_new(100);
if (!*b) { // handle error }
...
if (!LR_bin_set(b,2.0)) {
// handle error
LRperror("MyApp",b->errno);
}
...
\endcode
Use these routines to identify the specific LibRan error.
*/
/*
* Copyright 2019 R.K. Owen, Ph.D.
* License see lgpl.md (Gnu Lesser General Public License)
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include "libran.h"
/*!
@brief LRstrerror(int LRerrno) - return some explanatory text regarding
the given LibRan errno.
@param LRerrno LibRan error number
@return char * pointer to static constant string.
*/
char *LRstrerror(int LRerrno) {
switch (LRerrno) {
case LRerr_OK:
return "LibRan - No Error";
case LRerr_Unspecified:
return "LibRan - Unspecified Error";
case LRerr_BadDataType:
return "LibRan - Bad Data Type given";
case LRerr_BadLRType:
return "LibRan - Bad Random Variate Type given";
case LRerr_NoAuxiliaryObject:
return "LibRan - Required Auxiliary Object not found";
case LRerr_NoAuxNormalizeDone:
return "LibRan - Auxiliary Object requires normalization";
case LRerr_BadAuxSetup:
return "LibRan - Auxiliary Object incorrectly set-up";
case LRerr_BinGeneric:
return "LibRan - Binning Object Unspecified Error";
case LRerr_TooManyValues:
return "LibRan - Too Many Values given";
case LRerr_InvalidInputValue:
return "LibRan - Invalid Input Value Error";
case LRerr_InvalidRange:
return "LibRan - Invalid Range Value Error";
case LRerr_UnmetPreconditions:
return "LibRan - Preconditions Not Performed";
case LRerr_SuspiciousValues:
return "LibRan - Suspicious Value - Normalization Error?";
case LRerr_AllocFail:
return "LibRan - Memory Allocation Error";
}
return "LibRan - Invalid errno";
}
/*!
@brief LRstrerrno(int LRerrno) - return the errno code string for the
given LibRan errno.
@param LRerrno LibRan error number
@return char * pointer to static constant string.
*/
char *LRstrerrno(int LRerrno) {
switch (LRerrno) {
case LRerr_OK:
return "LRerr_OK";
case LRerr_Unspecified:
return "LRerr_Unspecified";
case LRerr_BadDataType:
return "LRerr_BadDataType";
case LRerr_BadLRType:
return "LRerr_BadLRType";
case LRerr_NoAuxiliaryObject:
return "LRerr_NoAuxiliaryObject";
case LRerr_NoAuxNormalizeDone:
return "LRerr_NoAuxNormalizeDone";
case LRerr_BadAuxSetup:
return "LRerr_BadAuxSetup";
case LRerr_BinGeneric:
return "LRerr_BinGeneric";
case LRerr_TooManyValues:
return "LRerr_TooManyValues";
case LRerr_InvalidInputValue:
return "LRerr_InvalidInputValue";
case LRerr_InvalidRange:
return "LRerr_InvalidRange";
case LRerr_UnmetPreconditions:
return "LRerr_UnmetPreconditions";
case LRerr_SuspiciousValues:
return "LRerr_SuspiciousValues";
case LRerr_AllocFail:
return "LRerr_AllocFail";
}
return "";
}
/*!
@brief LRperror(char *str, int LRerrno) - print out explanatory error
message to stderr.
If the char `str` string is not NULL then prepend this string to the LibRan
error message with a " : " delimiter and sent to \e stderr .
@param str String to prepend to error message
@param LRerrno LibRan error number
@return void - but outputs an error message to stderr
*/
void LRperror(char *str, int LRerrno) {
if (str) {
fprintf(stderr,"%s : %s\n", str, LRstrerror(LRerrno));
} else {
fprintf(stderr,"%s\n", LRstrerror(LRerrno));
}
}
#ifdef __cplusplus
}
#endif