提交 5d6e9045 编写于 作者: E Emmanuel Leblond

Merge branch 'custom_errback'

...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
/*---------------------------- Includes ------------------------------------*/ /*---------------------------- Includes ------------------------------------*/
#include <ctype.h> #include <ctype.h>
#include <stdarg.h>
#include "iniparser.h" #include "iniparser.h"
/*---------------------------- Defines -------------------------------------*/ /*---------------------------- Defines -------------------------------------*/
...@@ -107,6 +108,41 @@ unsigned strstrip(char * s) ...@@ -107,6 +108,41 @@ unsigned strstrip(char * s)
return last - s; return last - s;
} }
/*-------------------------------------------------------------------------*/
/**
@brief Default error callback for iniparser: wraps `fprintf(stderr, ...)`.
*/
/*--------------------------------------------------------------------------*/
static int default_error_callback(const char *format, ...)
{
int ret;
va_list argptr;
va_start(argptr, format);
ret = vfprintf(stderr, format, argptr);
va_end(argptr);
return ret;
}
static int (*iniparser_error_callback)(const char*, ...) = default_error_callback;
/*-------------------------------------------------------------------------*/
/**
@brief Configure a function to receive the error messages.
@param errback Function to call.
By default, the error will be printed on stderr. If a null pointer is passed
as errback the error callback will be switched back to default.
*/
/*--------------------------------------------------------------------------*/
void iniparser_set_error_callback(int (*errback)(const char *, ...))
{
if (errback) {
iniparser_error_callback = errback;
} else {
iniparser_error_callback = default_error_callback;
}
}
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
/** /**
@brief Get number of sections in a dictionary @brief Get number of sections in a dictionary
...@@ -694,7 +730,7 @@ dictionary * iniparser_load(const char * ininame) ...@@ -694,7 +730,7 @@ dictionary * iniparser_load(const char * ininame)
dictionary * dict ; dictionary * dict ;
if ((in=fopen(ininame, "r"))==NULL) { if ((in=fopen(ininame, "r"))==NULL) {
fprintf(stderr, "iniparser: cannot open %s\n", ininame); iniparser_error_callback("iniparser: cannot open %s\n", ininame);
return NULL ; return NULL ;
} }
...@@ -717,10 +753,10 @@ dictionary * iniparser_load(const char * ininame) ...@@ -717,10 +753,10 @@ dictionary * iniparser_load(const char * ininame)
continue; continue;
/* Safety check against buffer overflows */ /* Safety check against buffer overflows */
if (line[len]!='\n' && !feof(in)) { if (line[len]!='\n' && !feof(in)) {
fprintf(stderr, iniparser_error_callback(
"iniparser: input line too long in %s (%d)\n", "iniparser: input line too long in %s (%d)\n",
ininame, ininame,
lineno); lineno);
dictionary_del(dict); dictionary_del(dict);
fclose(in); fclose(in);
return NULL ; return NULL ;
...@@ -757,10 +793,11 @@ dictionary * iniparser_load(const char * ininame) ...@@ -757,10 +793,11 @@ dictionary * iniparser_load(const char * ininame)
break ; break ;
case LINE_ERROR: case LINE_ERROR:
fprintf(stderr, "iniparser: syntax error in %s (%d):\n", iniparser_error_callback(
ininame, "iniparser: syntax error in %s (%d):\n-> %s\n",
lineno); ininame,
fprintf(stderr, "-> %s\n", line); lineno,
line);
errs++ ; errs++ ;
break; break;
...@@ -770,7 +807,7 @@ dictionary * iniparser_load(const char * ininame) ...@@ -770,7 +807,7 @@ dictionary * iniparser_load(const char * ininame)
memset(line, 0, ASCIILINESZ); memset(line, 0, ASCIILINESZ);
last=0; last=0;
if (mem_err<0) { if (mem_err<0) {
fprintf(stderr, "iniparser: memory allocation failure\n"); iniparser_error_callback("iniparser: memory allocation failure\n");
break ; break ;
} }
} }
......
...@@ -31,6 +31,18 @@ ...@@ -31,6 +31,18 @@
extern "C" { extern "C" {
#endif #endif
/*-------------------------------------------------------------------------*/
/**
@brief Configure a function to receive the error messages.
@param errback Function to call.
By default, the error will be printed on stderr. If a null pointer is passed
as errback the error callback will be switched back to default.
*/
/*--------------------------------------------------------------------------*/
void iniparser_set_error_callback(int (*errback)(const char *, ...));
/*-------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/
/** /**
@brief Get number of sections in a dictionary @brief Get number of sections in a dictionary
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdarg.h>
#include "CuTest.h" #include "CuTest.h"
#include "dictionary.h" #include "dictionary.h"
...@@ -661,3 +662,37 @@ void Test_dictionary_wrapper(CuTest *tc) ...@@ -661,3 +662,37 @@ void Test_dictionary_wrapper(CuTest *tc)
iniparser_freedict(dic); iniparser_freedict(dic);
} }
static char _last_error[1024];
static int _error_callback(const char *format, ...)
{
int ret;
va_list argptr;
va_start(argptr, format);
ret = vsprintf(_last_error, format, argptr);
va_end(argptr);
return ret;
}
void Test_iniparser_error_callback(CuTest *tc)
{
dictionary *dic;
/* Specify our custom error_callback */
iniparser_set_error_callback(_error_callback);
/* Trigger an error and check it was written on the right output */
dic = iniparser_load("/path/to/nowhere.ini");
CuAssertPtrEquals(tc, NULL, dic);
CuAssertStrEquals(tc, "iniparser: cannot open /path/to/nowhere.ini\n", _last_error);
/* Reset erro_callback */
_last_error[0] = '\0';
iniparser_set_error_callback(NULL);
/* Make sure custom callback is no more called */
dic = iniparser_load("/path/to/nowhere.ini");
CuAssertPtrEquals(tc, NULL, dic);
CuAssertStrEquals(tc, "", _last_error);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册