forked from libfann/fann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fann_error.c
227 lines (205 loc) · 6.59 KB
/
fann_error.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2016 Steffen Nissen ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include "config.h"
#include "fann.h"
#ifdef _MSC_VER
#define vsnprintf _vsnprintf
#define snprintf _snprintf
#endif
/* define path max if not defined */
#if defined(_WIN32) && !defined(__MINGW32__)
#define PATH_MAX _MAX_PATH
#endif
#ifndef PATH_MAX
#ifdef _POSIX_PATH_MAX
#define PATH_MAX _POSIX_PATH_MAX
#else
#define PATH_MAX 4096
#endif
#endif
FANN_EXTERNAL FILE * FANN_API fann_default_error_log = (FILE *)-1;
/* resets the last error number
*/
FANN_EXTERNAL void FANN_API fann_reset_errno(struct fann_error *errdat)
{
errdat->errno_f = FANN_E_NO_ERROR;
}
/* resets the last errstr
*/
FANN_EXTERNAL void FANN_API fann_reset_errstr(struct fann_error *errdat)
{
if(errdat->errstr != NULL)
free(errdat->errstr);
errdat->errstr = NULL;
}
/* returns the last error number
*/
FANN_EXTERNAL enum fann_errno_enum FANN_API fann_get_errno(struct fann_error *errdat)
{
return errdat->errno_f;
}
/* returns the last errstr
*/
FANN_EXTERNAL char *FANN_API fann_get_errstr(struct fann_error *errdat)
{
char *errstr = errdat->errstr;
fann_reset_errno(errdat);
fann_reset_errstr(errdat);
return errstr;
}
/* change where errors are logged to
*/
FANN_EXTERNAL void FANN_API fann_set_error_log(struct fann_error *errdat, FILE * log_file)
{
if(errdat == NULL)
fann_default_error_log = log_file;
else
errdat->error_log = log_file;
}
/* prints the last error to stderr
*/
FANN_EXTERNAL void FANN_API fann_print_error(struct fann_error *errdat)
{
if(errdat->errno_f != FANN_E_NO_ERROR && errdat->errstr != NULL)
{
fprintf(stderr, "FANN Error %d: %s", errdat->errno_f, errdat->errstr);
}
}
/* INTERNAL FUNCTION
Populate the error information
*/
void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, ...)
{
va_list ap;
size_t errstr_max = FANN_ERRSTR_MAX + PATH_MAX - 1;
char errstr[FANN_ERRSTR_MAX + PATH_MAX];
FILE * error_log = fann_default_error_log;
if(errdat != NULL)
errdat->errno_f = errno_f;
va_start(ap, errno_f);
switch (errno_f)
{
case FANN_E_NO_ERROR:
return;
case FANN_E_CANT_OPEN_CONFIG_R:
vsnprintf(errstr, errstr_max, "Unable to open configuration file \"%s\" for reading.\n", ap);
break;
case FANN_E_CANT_OPEN_CONFIG_W:
vsnprintf(errstr, errstr_max, "Unable to open configuration file \"%s\" for writing.\n", ap);
break;
case FANN_E_WRONG_CONFIG_VERSION:
vsnprintf(errstr, errstr_max,
"Wrong version of configuration file, aborting read of configuration file \"%s\".\n",
ap);
break;
case FANN_E_CANT_READ_CONFIG:
vsnprintf(errstr, errstr_max, "Error reading \"%s\" from configuration file \"%s\".\n", ap);
break;
case FANN_E_CANT_READ_NEURON:
vsnprintf(errstr, errstr_max, "Error reading neuron info from configuration file \"%s\".\n", ap);
break;
case FANN_E_CANT_READ_CONNECTIONS:
vsnprintf(errstr, errstr_max, "Error reading connections from configuration file \"%s\".\n", ap);
break;
case FANN_E_WRONG_NUM_CONNECTIONS:
vsnprintf(errstr, errstr_max, "ERROR connections_so_far=%d, total_connections=%d\n", ap);
break;
case FANN_E_CANT_OPEN_TD_W:
vsnprintf(errstr, errstr_max, "Unable to open train data file \"%s\" for writing.\n", ap);
break;
case FANN_E_CANT_OPEN_TD_R:
vsnprintf(errstr, errstr_max, "Unable to open train data file \"%s\" for writing.\n", ap);
break;
case FANN_E_CANT_READ_TD:
vsnprintf(errstr, errstr_max, "Error reading info from train data file \"%s\", line: %d.\n", ap);
break;
case FANN_E_CANT_ALLOCATE_MEM:
strcpy(errstr, "Unable to allocate memory.\n");
break;
case FANN_E_CANT_TRAIN_ACTIVATION:
strcpy(errstr, "Unable to train with the selected activation function.\n");
break;
case FANN_E_CANT_USE_ACTIVATION:
strcpy(errstr, "Unable to use the selected activation function.\n");
break;
case FANN_E_TRAIN_DATA_MISMATCH:
strcpy(errstr, "Training data must be of equivalent structure.\n");
break;
case FANN_E_CANT_USE_TRAIN_ALG:
strcpy(errstr, "Unable to use the selected training algorithm.\n");
break;
case FANN_E_TRAIN_DATA_SUBSET:
vsnprintf(errstr, errstr_max, "Subset from %d of length %d not valid in training set of length %d.\n", ap);
break;
case FANN_E_INDEX_OUT_OF_BOUND:
vsnprintf(errstr, errstr_max, "Index %d is out of bound.\n", ap);
break;
case FANN_E_SCALE_NOT_PRESENT:
strcpy(errstr, "Scaling parameters not present.\n");
break;
case FANN_E_INPUT_NO_MATCH:
vsnprintf(errstr, errstr_max, "The number of input neurons in the ann (%d) and data (%d) don't match\n", ap);
break;
case FANN_E_OUTPUT_NO_MATCH:
vsnprintf(errstr, errstr_max, "The number of output neurons in the ann (%d) and data (%d) don't match\n", ap);
break;
case FANN_E_WRONG_PARAMETERS_FOR_CREATE:
strcpy(errstr, "The parameters for create_standard are wrong, either too few parameters provided or a negative/very high value provided.\n");
break;
}
va_end(ap);
if(errdat != NULL)
{
if(errdat->errstr == NULL)
{
errdat->errstr = (char*)malloc(strlen(errstr) + 1);
}
else if(strlen(errdat->errstr) < strlen(errstr))
{
errdat->errstr = (char*)realloc(errdat->errstr, strlen(errstr) + 1);
}
/* allocation failed */
if(errdat->errstr == NULL)
{
fprintf(stderr, "Unable to allocate memory.\n");
return;
}
strcpy(errdat->errstr, errstr);
error_log = errdat->error_log;
}
if(error_log == (FILE *)-1) /* This is the default behavior and will give stderr */
{
fprintf(stderr, "FANN Error %d: %s", errno_f, errstr);
}
else if(error_log != NULL)
{
fprintf(error_log, "FANN Error %d: %s", errno_f, errstr);
}
}
/* INTERNAL FUNCTION
Initialize an error data strcuture
*/
void fann_init_error_data(struct fann_error *errdat)
{
errdat->errstr = NULL;
errdat->errno_f = FANN_E_NO_ERROR;
errdat->error_log = fann_default_error_log;
}