Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party Iniparser
提交
5d6e9045
T
Third Party Iniparser
项目概览
OpenHarmony
/
Third Party Iniparser
9 个月 前同步成功
通知
0
Star
24
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party Iniparser
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5d6e9045
编写于
8月 30, 2016
作者:
E
Emmanuel Leblond
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'custom_errback'
上级
5720a4cd
ac05815e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
94 addition
and
10 deletion
+94
-10
src/iniparser.c
src/iniparser.c
+47
-10
src/iniparser.h
src/iniparser.h
+12
-0
test/test_iniparser.c
test/test_iniparser.c
+35
-0
未找到文件。
src/iniparser.c
浏览文件 @
5d6e9045
...
@@ -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
;
}
}
}
}
...
...
src/iniparser.h
浏览文件 @
5d6e9045
...
@@ -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
...
...
test/test_iniparser.c
浏览文件 @
5d6e9045
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录