Code

Make extract_value more generic:
authorThomas Guyot-Sionnest <dermoth@aei.ca>
Mon, 26 Jan 2009 07:05:21 +0000 (02:05 -0500)
committerThomas Guyot-Sionnest <dermoth@aei.ca>
Mon, 26 Jan 2009 07:05:21 +0000 (02:05 -0500)
Add a separator argument passed using defines.

lib/tests/test_utils.c
lib/utils_base.c
lib/utils_base.h
plugins/check_ntp_peer.c

index 97343afa49d48e17015623f0a22cc1f0295404c6..3a45944a2a59ffd6008292416fba9b7235ef86d8 100644 (file)
@@ -172,83 +172,83 @@ main (int argc, char **argv)
        test = np_escaped_string("everything");
        ok( strcmp(test, "everything") == 0, "everything okay");
 
-       /* np_extract_value tests (23) */
-       test=np_extract_value("foo=bar, bar=foo, foobar=barfoo\n", "foo");
+       /* np_extract_ntpvar tests (23) */
+       test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
        ok(test && !strcmp(test, "bar"), "1st test as expected");
        free(test);
 
-       test=np_extract_value("foo=bar,bar=foo,foobar=barfoo\n", "bar");
+       test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
        ok(test && !strcmp(test, "foo"), "2nd test as expected");
        free(test);
 
-       test=np_extract_value("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
+       test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
        ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
        free(test);
 
-       test=np_extract_value("foo=bar\n", "foo");
+       test=np_extract_ntpvar("foo=bar\n", "foo");
        ok(test && !strcmp(test, "bar"), "Single test as expected");
        free(test);
 
-       test=np_extract_value("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
+       test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
        ok(!test, "Key not found 1");
 
-       test=np_extract_value("foo=bar\n", "abcd");
+       test=np_extract_ntpvar("foo=bar\n", "abcd");
        ok(!test, "Key not found 2");
 
-       test=np_extract_value("foo=bar=foobar", "foo");
+       test=np_extract_ntpvar("foo=bar=foobar", "foo");
        ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
        free(test);
 
-       test=np_extract_value("foo", "foo");
+       test=np_extract_ntpvar("foo", "foo");
        ok(!test, "Malformed string 1");
 
-       test=np_extract_value("foo,", "foo");
+       test=np_extract_ntpvar("foo,", "foo");
        ok(!test, "Malformed string 2");
 
-       test=np_extract_value("foo=", "foo");
+       test=np_extract_ntpvar("foo=", "foo");
        ok(!test, "Malformed string 3");
 
-       test=np_extract_value("foo=,bar=foo", "foo");
+       test=np_extract_ntpvar("foo=,bar=foo", "foo");
        ok(!test, "Malformed string 4");
 
-       test=np_extract_value(",foo", "foo");
+       test=np_extract_ntpvar(",foo", "foo");
        ok(!test, "Malformed string 5");
 
-       test=np_extract_value("=foo", "foo");
+       test=np_extract_ntpvar("=foo", "foo");
        ok(!test, "Malformed string 6");
 
-       test=np_extract_value("=foo,", "foo");
+       test=np_extract_ntpvar("=foo,", "foo");
        ok(!test, "Malformed string 7");
 
-       test=np_extract_value(",,,", "foo");
+       test=np_extract_ntpvar(",,,", "foo");
        ok(!test, "Malformed string 8");
 
-       test=np_extract_value("===", "foo");
+       test=np_extract_ntpvar("===", "foo");
        ok(!test, "Malformed string 9");
 
-       test=np_extract_value(",=,=,", "foo");
+       test=np_extract_ntpvar(",=,=,", "foo");
        ok(!test, "Malformed string 10");
 
-       test=np_extract_value("=,=,=", "foo");
+       test=np_extract_ntpvar("=,=,=", "foo");
        ok(!test, "Malformed string 11");
 
-       test=np_extract_value("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foo");
+       test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foo");
        ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
        free(test);
 
-       test=np_extract_value("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "bar");
+       test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "bar");
        ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
        free(test);
 
-       test=np_extract_value("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foobar");
+       test=np_extract_ntpvar("  foo=bar  ,\n bar=foo\n , foobar=barfoo  \n  ", "foobar");
        ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
        free(test);
 
-       test=np_extract_value("  foo=bar  ,\n bar\n \n= \n foo\n , foobar=barfoo  \n  ", "bar");
+       test=np_extract_ntpvar("  foo=bar  ,\n bar\n \n= \n foo\n , foobar=barfoo  \n  ", "bar");
        ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
        free(test);
 
-       test=np_extract_value("", "foo");
+       test=np_extract_ntpvar("", "foo");
        ok(!test, "Empty string return NULL");
 
        return exit_status();
index a34cc5cc8430758fa9e54f0c6b6c5947511df73c..77700f5ba7e46079d5abb68c042c0694c79a77eb 100644 (file)
@@ -258,7 +258,7 @@ int np_warn_if_not_root(void) {
  * This function can be used to parse NTP control packet data and performance
  * data strings.
  */
-char *np_extract_value(const char *varlist, const char *name) {
+char *np_extract_value(const char *varlist, const char *name, char sep) {
        char *tmp=NULL, *value=NULL;
        int i;
 
@@ -277,7 +277,7 @@ char *np_extract_value(const char *varlist, const char *name) {
                                /* strip leading spaces */
                                for (varlist; isspace(varlist[0]); varlist++);
 
-                               if (tmp = index(varlist, ',')) {
+                               if (tmp = index(varlist, sep)) {
                                        /* Value is delimited by a comma */
                                        if (tmp-varlist == 0) continue;
                                        value = (char *)malloc(tmp-varlist+1);
@@ -293,7 +293,7 @@ char *np_extract_value(const char *varlist, const char *name) {
                                break;
                        }
                }
-               if (tmp = index(varlist, ',')) {
+               if (tmp = index(varlist, sep)) {
                        /* More keys, keep going... */
                        varlist = tmp + 1;
                } else {
index c34f0445c9f515b314711eee37fb857ea50ea1a5..f40fdb0f9e2fc77004667f919ccc92dc9d2bc64e 100644 (file)
@@ -56,6 +56,12 @@ int np_warn_if_not_root(void);
  * This function can be used to parse NTP control packet data and performance
  * data strings.
  */
-char *np_extract_value(const char*, const char*);
+char *np_extract_value(const char*, const char*, char);
+
+/*
+ * Same as np_extract_value with separator suitable for NTP control packet
+ * payloads (comma)
+ */
+#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
 
 #endif /* _UTILS_BASE_ */
index b40dbfcb789cb1fbaa2ec87deb5a32c00642fa53..e8325bc82392c0d39e55ca4fe1b7102aff741512 100644 (file)
@@ -333,7 +333,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
                        if(verbose)
                                printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
 
-                       value = np_extract_value(data, "offset");
+                       value = np_extract_ntpvar(data, "offset");
                        nptr=NULL;
                        /* Convert the value if we have one */
                        if(value != NULL)
@@ -357,7 +357,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
                                if(verbose) {
                                        printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
                                }
-                               value = np_extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
+                               value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
                                nptr=NULL;
                                /* Convert the value if we have one */
                                if(value != NULL)
@@ -376,7 +376,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
                                if(verbose) {
                                        printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
                                }
-                               value = np_extract_value(data, "stratum");
+                               value = np_extract_ntpvar(data, "stratum");
                                nptr=NULL;
                                /* Convert the value if we have one */
                                if(value != NULL)