diff --git a/src/cfg.c b/src/cfg.c index 31489d9..90c2550 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -326,23 +326,7 @@ cfg_set_server_client_key(const char *client_key, const char **errstrp) int cfg_set_server_reconnect_attempts(const char *num_str, const char **errstrp) { - const char *errstr; - unsigned int num; - - if (!num_str || !num_str[0]) { - if (errstrp) - *errstrp = "empty"; - return (-1); - } - - num = strtonum(num_str, 0, UINT_MAX, &errstr); - if (errstr) { - if (errstrp) - *errstrp = errstr; - return (-1); - } - cfg.server.reconnect_attempts = num; - + SET_UINTNUM(cfg.server.reconnect_attempts, num_str, errstrp); return (0); } @@ -525,23 +509,7 @@ cfg_set_metadata_format_str(const char *format_str, const char **errstrp) int cfg_set_metadata_refresh_interval(const char *num_str, const char **errstrp) { - const char *errstr; - unsigned int num; - - if (!num_str || !num_str[0]) { - if (errstrp) - *errstrp = "empty"; - return (-1); - } - - num = strtonum(num_str, 0, UINT_MAX, &errstr); - if (errstr) { - if (errstrp) - *errstrp = errstr; - return (-1); - } - cfg.metadata.refresh_interval = num; - + SET_UINTNUM(cfg.metadata.refresh_interval, num_str, errstrp); return (0); } diff --git a/src/cfg_private.h b/src/cfg_private.h index 02748e8..e0e1e41 100644 --- a/src/cfg_private.h +++ b/src/cfg_private.h @@ -126,6 +126,25 @@ struct cfg { (t) = val; \ } while (0) +#define SET_UINTNUM(t, s, e) do { \ + const char *errstr; \ + unsigned int num; \ + \ + if (!(s) || !(s)[0]) { \ + if ((e)) \ + *(e) = "empty"; \ + return (-1); \ + } \ + \ + num = strtonum((s), 0, UINT_MAX, &errstr); \ + if (errstr) { \ + if ((e)) \ + *(e) = errstr; \ + return (-1); \ + } \ + (t) = num; \ +} while (0) + #define CHECKPH_PROHIBITED(s, p) do { \ if (NULL != strstr((s), (p))) { \ if (errstrp) \