summaryrefslogtreecommitdiff
path: root/src/shared/conf-parser.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-04-14 22:29:43 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-04-17 00:09:15 -0400
commiteb3491d9ab2f2a3a28d9a6749b2c4b8abff173c6 (patch)
treea6eca66707b2c6aaf8e56c52a251b30cb66d5735 /src/shared/conf-parser.c
parent6a58bf4135faa98f1600672179a2bf364d455f7e (diff)
conf-parser: generate 7 parsing functions from a macro
Those functions were identical, apart from typos. Log message is modified to contain the type of destination var (int, double, ...). I think this might make it easier to understand why a value was rejected (e.g. a minus from an unsigned type).
Diffstat (limited to 'src/shared/conf-parser.c')
-rw-r--r--src/shared/conf-parser.c216
1 files changed, 33 insertions, 183 deletions
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index fea2e5624e..a98805e215 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -341,140 +341,40 @@ int config_parse(
return 0;
}
-int config_parse_int(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- int *i = data;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = safe_atoi(rvalue, i);
- if (r < 0) {
- log_error("[%s:%u] Failed to parse numeric value, ingoring: %s", filename, line, rvalue);
- return 0;
- }
-
- return 0;
-}
-
-int config_parse_long(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- long *i = data;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = safe_atoli(rvalue, i);
- if (r < 0) {
- log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
- return 0;
- }
-
- return 0;
-}
-
-int config_parse_uint64(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- uint64_t *u = data;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = safe_atou64(rvalue, u);
- if (r < 0) {
- log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
- return 0;
- }
-
- return 0;
-}
-
-int config_parse_unsigned(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- unsigned *u = data;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = safe_atou(rvalue, u);
- if (r < 0) {
- log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
- return r;
- }
-
- return 0;
-}
-
-int config_parse_double(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
+#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, \
+ void *userdata) { \
+ \
+ vartype *i = data; \
+ int r; \
+ \
+ assert(filename); \
+ assert(lvalue); \
+ assert(rvalue); \
+ assert(data); \
+ \
+ r = conv_func(rvalue, i); \
+ if (r < 0) \
+ log_error("[%s:%u] Failed to parse %s value, ignoring: %s", \
+ filename, line, #vartype, rvalue); \
+ \
+ return 0; \
+ }
+
+DEFINE_PARSER(int, int, safe_atoi)
+DEFINE_PARSER(long, long, safe_atoli)
+DEFINE_PARSER(uint64, uint64_t, safe_atou64)
+DEFINE_PARSER(unsigned, unsigned, safe_atou)
+DEFINE_PARSER(double, double, safe_atod)
+DEFINE_PARSER(nsec, nsec_t, parse_nsec)
+DEFINE_PARSER(sec, usec_t, parse_sec)
- double *d = data;
- int r;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- r = safe_atod(rvalue, d);
- if (r < 0) {
- log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
- return r;
- }
-
- return 0;
-}
int config_parse_bytes_size(
const char *filename,
@@ -774,56 +674,6 @@ int config_parse_path_strv(
return 0;
}
-int config_parse_sec(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- usec_t *usec = data;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- if (parse_sec(rvalue, usec) < 0) {
- log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
- return 0;
- }
-
- return 0;
-}
-
-int config_parse_nsec(
- const char *filename,
- unsigned line,
- const char *section,
- const char *lvalue,
- int ltype,
- const char *rvalue,
- void *data,
- void *userdata) {
-
- nsec_t *nsec = data;
-
- assert(filename);
- assert(lvalue);
- assert(rvalue);
- assert(data);
-
- if (parse_nsec(rvalue, nsec) < 0) {
- log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
- return 0;
- }
-
- return 0;
-}
-
int config_parse_mode(
const char *filename,
unsigned line,