Skip to content

Commit

Permalink
Optimize fann_error function
Browse files Browse the repository at this point in the history
  • Loading branch information
bukka committed Dec 11, 2013
1 parent 588c1a7 commit 20f3967
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/fann_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,17 @@ FANN_EXTERNAL void FANN_API fann_print_error(struct fann_error *errdat)
void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, ...)
{
va_list ap;
char *errstr;
int errfree = 0;
char errstr[FANN_ERRSTR_MAX + PATH_MAX];
FILE * error_log = fann_default_error_log;

if(errdat != NULL)
errdat->errno_f = errno_f;

if(errdat != NULL && errdat->errstr != NULL)
{
errstr = errdat->errstr;
}
else
{
errstr = (char *) malloc(FANN_ERRSTR_MAX + PATH_MAX);
if(errstr == NULL)
{
fprintf(stderr, "Unable to allocate memory.\n");
return;
}
errfree = 1;
}

va_start(ap, errno_f);
switch (errno_f)
{
case FANN_E_NO_ERROR:
break;
return;
case FANN_E_CANT_OPEN_CONFIG_R:
vsprintf(errstr, "Unable to open configuration file \"%s\" for reading.\n", ap);
break;
Expand Down Expand Up @@ -204,11 +188,21 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .

if(errdat != NULL)
{
if (errdat->errstr == NULL)
if(errdat->errstr == NULL)
{
errdat->errstr = malloc(strlen(errstr) + 1);
}
else if(strlen(errdat->errstr) < strlen(errstr))
{
errdat->errstr = errstr;
errfree = 0;
errdat->errstr = 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;
}

Expand All @@ -220,10 +214,6 @@ void fann_error(struct fann_error *errdat, const enum fann_errno_enum errno_f, .
{
fprintf(error_log, "FANN Error %d: %s", errno_f, errstr);
}
if (errfree)
{
free(errstr);
}
}

/* INTERNAL FUNCTION
Expand Down

0 comments on commit 20f3967

Please sign in to comment.