From: octo Date: Fri, 14 Apr 2006 07:41:40 +0000 (+0000) Subject: Substituting colons with commas, to get comma seperated value (CSV) files. X-Git-Tag: liboping-0.1.0~95^2~1 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3d57efd9b77ae9a5bd67efc4c8e4e5396ee0f600;p=collectd.git Substituting colons with commas, to get comma seperated value (CSV) files. --- diff --git a/src/common.c b/src/common.c index 0b603fa8..56ae326a 100644 --- a/src/common.c +++ b/src/common.c @@ -155,6 +155,27 @@ int strjoin (char *dst, size_t dst_len, return (strlen (dst)); } +int strsubstitute (char *str, char c_from, char c_to) +{ + int ret; + + if (str == NULL) + return (-1); + + ret = 0; + while (*str != '\0') + { + if (*str == c_from) + { + *str = c_to; + ret++; + } + str++; + } + + return (ret); +} + int escape_slashes (char *buf, int buf_len) { int i; @@ -300,12 +321,13 @@ static int log_create_file (char *filename, char **ds_def, int ds_num) return (-1); } + fprintf (log, "epoch"); for (i = 0; i < ds_num; i++) { char *name; char *tmp; - name = index (ds_def[i], ':'); + name = strchr (ds_def[i], ':'); if (name == NULL) { syslog (LOG_WARNING, "Invalid DS definition '%s' for %s", @@ -316,7 +338,7 @@ static int log_create_file (char *filename, char **ds_def, int ds_num) } name += 1; - tmp = index(name, ':'); + tmp = strchr (name, ':'); if (tmp == NULL) { syslog (LOG_WARNING, "Invalid DS definition '%s' for %s", @@ -326,9 +348,9 @@ static int log_create_file (char *filename, char **ds_def, int ds_num) return (-1); } - if (i != 0) - fprintf (log, ":"); - fprintf(log, "%.*s", (tmp - name), name); + /* The `%.*s' is needed because there is no null-byte behind + * the name. */ + fprintf(log, ",%.*s", (tmp - name), name); } fprintf(log, "\n"); fclose(log); @@ -344,6 +366,9 @@ static int log_update_file (char *host, char *file, char *values, struct stat statbuf; char full_file[1024]; + /* Cook the values a bit: Substitute colons with commas */ + strsubstitute (values, ':', ','); + /* host == NULL => local mode */ if (host != NULL) { @@ -367,6 +392,8 @@ static int log_update_file (char *host, char *file, char *values, time_t now; struct tm *tm; + /* TODO: Find a way to minimize the calls to `localtime', since + * they are pretty expensive.. */ now = time (NULL); tm = localtime (&now);