common: provide parsing multiple data types 98/187498/4
authorByungSoo Kim <bs1770.kim@samsung.com>
Tue, 13 Jun 2017 02:34:22 +0000 (11:34 +0900)
committerPaweł Szewczyk <p.szewczyk@samsung.com>
Mon, 24 Sep 2018 12:58:33 +0000 (14:58 +0200)
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 <bs1770.kim@samsung.com>
Signed-off-by: Paweł Szewczyk <p.szewczyk@samsung.com>
src/common/config-parser.c
src/common/config-parser.h

index 5b851eb..e1b4aed 100644 (file)
@@ -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,
index a7bb920..e425c5a 100644 (file)
@@ -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