From aad346f2e4d52fe1bf167f49af84240bd93ac25b Mon Sep 17 00:00:00 2001 From: ByungSoo Kim Date: Tue, 13 Jun 2017 11:34:22 +0900 Subject: [PATCH] common: provide parsing multiple data types MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit add define_parser macro in order to support parsing multiple data types. It came from systemd after changing arguments. It can be moved to support these apis in libsystem. Change-Id: Iee4599293b486baf59fc1a499cf27e1ab6472794 Signed-off-by: ByungSoo Kim Signed-off-by: Paweł Szewczyk --- src/common/config-parser.c | 42 +++++++++++++++++++++++------------------- src/common/config-parser.h | 2 ++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/common/config-parser.c b/src/common/config-parser.c index 5b851eb..e1b4aed 100644 --- a/src/common/config-parser.c +++ b/src/common/config-parser.c @@ -385,26 +385,30 @@ int config_parse_bool(const char *filename, return 0; } +#define DEFINE_PARSER(type, vartype, conv_func) \ + int config_parse_##type( \ + const char *filename, \ + unsigned line, \ + const char *section, \ + const char *lvalue, \ + int ltype, \ + const char *rvalue, \ + void *data) { \ + \ + vartype *i = data; \ + \ + assert(filename); \ + assert(lvalue); \ + assert(rvalue); \ + assert(data); \ + \ + *i = conv_func(rvalue); \ + return 0; \ + } -int config_parse_int(const char *filename, - unsigned line, - const char *section, - const char *lvalue, - int ltype, - const char *rvalue, - void *data) -{ - int *i = data; - - assert(filename); - assert(lvalue); - assert(rvalue); - assert(data); - - *i = atoi(rvalue); - - return 0; -} +DEFINE_PARSER(int, int, atoi) +DEFINE_PARSER(float, float, atof) +DEFINE_PARSER(long, long, atol) int config_parse_string(const char *filename, unsigned line, diff --git a/src/common/config-parser.h b/src/common/config-parser.h index a7bb920..e425c5a 100644 --- a/src/common/config-parser.h +++ b/src/common/config-parser.h @@ -76,5 +76,7 @@ int config_parse_bool(const char *filename, unsigned line, const char *section, int config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data); int config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data); int config_parse_bytes(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data); +int config_parse_float(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data); +int config_parse_long(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data); int config_parse_strv(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data); #endif -- 2.7.4