From: Florian Forster Date: Fri, 9 Sep 2016 14:50:06 +0000 (+0200) Subject: src/daemon/common.[ch]: Implement parse_value_file(). X-Git-Tag: collectd-5.7.0~98^2~6 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=95ef899242f7f2cf552162601608239d6d8eaa09;p=collectd.git src/daemon/common.[ch]: Implement parse_value_file(). Fixes: #1914 --- diff --git a/src/daemon/common.c b/src/daemon/common.c index 05b11990..7806c716 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -1199,6 +1199,18 @@ int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds) return (0); } /* int parse_values */ +int parse_value_file (char const *path, value_t *ret_value, int ds_type) +{ + char buffer[256]; + + if (read_file_contents (path, buffer, sizeof (buffer)) < 0) + return errno; + + strstripnewline (buffer); + + return parse_value (buffer, ret_value, ds_type); +} /* int parse_value_file */ + #if !HAVE_GETPWNAM_R int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp) diff --git a/src/daemon/common.h b/src/daemon/common.h index 720e5f1b..9e252932 100644 --- a/src/daemon/common.h +++ b/src/daemon/common.h @@ -322,6 +322,11 @@ int parse_identifier_vl (const char *str, value_list_t *vl); int parse_value (const char *value, value_t *ret_value, int ds_type); int parse_values (char *buffer, value_list_t *vl, const data_set_t *ds); +/* parse_value_file reads "path" and parses its content as an integer or + * floating point, depending on "ds_type". On success, the value is stored in + * "ret_value" and zero is returned. On failure, a non-zero value is returned. */ +int parse_value_file (char const *path, value_t *ret_value, int ds_type); + #if !HAVE_GETPWNAM_R int getpwnam_r (const char *name, struct passwd *pwbuf, char *buf, size_t buflen, struct passwd **pwbufp);