Hi,

Am 17.08.2009 11:44, schrieb Florian Forster:
> What about this:
> -- 8< --
>  #define CBPUTS(cb_func, user, str) \
>    (*(cb_func)) (str, strlen (str), (user))
>  #define CBPRINTF(cb_func, user, ...) do { \
>    char buffer[256]; \
>    snprintf (buffer, sizeof (buffer), __VA_ARGS__); \
>    buffer[sizeof (buffer) - 1] = 0; \
>    CBPUTS (cb_func, user, buffer); \
>  } while (0)
> -- >8 --
I did a minor modification to those macros as the callback name is
always cb and it always gets the user parameter so I can leave this from
the macro definition.

Integrating those two macros into the source leads to the following
patch (on top of the second patch from me)
> If you follow up with something like
>  $ sed -i -e 's/fprintf\s*(out_file,/CBPRINTF (cb, user,/'
> I bet 80% of the work is done.
Not that easy, But since manual work does the job quite well and even
allows for some optimizations, skipping sed on this helps with getting
real nice source.
> The code will then look somewhat like this:
> -- 8< --
>  CBPRINTF (cb, user, "\t<step> %lu </step> <!-- Seconds -->\n",
>      rrd.stat_head->pdp_step);
> -- >8 --
Well, that's
-- 8< --
 CB_FMTS ("\t<step> %lu </step> <!-- Seconds -->\n",
     rrd.stat_head->pdp_step);
-- >8 --
with my attached patch ...
> Regards,
> -octo
>   
Regards,
BenBE.

P.S.: Please dos2unix the patch first if necessary ;-)

diff U3 a/src/rrd_dump.c b/src/rrd_dump.c
--- a/src/rrd_dump.c    Mon Aug 17 19:11:07 2009
+++ b/src/rrd_dump.c    Mon Aug 17 19:08:13 2009
@@ -69,7 +69,6 @@
     unsigned int i, ii, ix, iii = 0;
     time_t    now;
     char      somestring[255];
-    char      somestring_buf[255];
     rrd_value_t my_cdp;
     off_t     rra_base, rra_start, rra_next;
     rrd_file_t *rrd_file;
@@ -78,6 +77,17 @@
     struct tm tm;
     char *old_locale = "";
 
+//These two macros are local defines to clean up visible code from its 
redndancy
+//and make it easier to read.
+#define CB_PUTS(str)                                            \
+    cb((str), strlen((str)), user)
+#define CB_FMTS(...) do {                                       \
+    char buffer[256];                                           \
+    snprintf (buffer, sizeof(buffer), __VA_ARGS__);             \
+    CB_PUTS (buffer);                                           \
+    } while (0)
+//These macros are to be undefined at the end of this function
+
     //Check if we got a (valid) callback method
     if (!cb) {
         return (-1);
@@ -96,40 +106,30 @@
 #endif
 
     if (opt_header == 1) {
-        cb("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", 
-            strlen("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"), user);
-        cb("<!DOCTYPE rrd SYSTEM 
\"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\";>\n",
-            strlen("<!DOCTYPE rrd SYSTEM 
\"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\";>\n"), user);
-        cb("<!-- Round Robin Database Dump -->\n", 
-            strlen("<!-- Round Robin Database Dump -->\n"), user);
-        cb("<rrd>\n", 
-            strlen("<rrd>\n"), user);
+        CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
+        CB_PUTS("<!DOCTYPE rrd SYSTEM 
\"http://oss.oetiker.ch/rrdtool/rrdtool.dtd\";>\n");
+        CB_PUTS("<!-- Round Robin Database Dump -->\n");
+        CB_PUTS("<rrd>\n");
     } else if (opt_header == 2) {
-        cb("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n", 
-            strlen("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"), user);
-        cb("<!-- Round Robin Database Dump -->\n", 
-            strlen("<!-- Round Robin Database Dump -->\n"), user);
-        cb("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";, 
-            strlen("<rrd 
xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";), user);
-        
cb("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml 
http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\";>\n", 
-            
strlen("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml 
http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\";>\n"), user);
+        CB_PUTS("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
+        CB_PUTS("<!-- Round Robin Database Dump -->\n");
+        CB_PUTS("<rrd xmlns=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml\"; 
"
+                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";);
+        
CB_PUTS("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml "
+                "http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\";>\n");
     } else {
-        cb("<!-- Round Robin Database Dump -->\n", 
-            strlen("<!-- Round Robin Database Dump -->\n"), user);
-        cb("<rrd>\n", 
-            strlen("<rrd>\n"), user);
+        CB_PUTS("<!-- Round Robin Database Dump -->\n");
+        CB_PUTS("<rrd>\n");
     }
 
     if (atoi(rrd.stat_head->version) <= 3) {
-        snprintf(somestring, 255, "\t<version>%s</version>\n", RRD_VERSION3);
+        CB_FMTS("\t<version>%s</version>\n", RRD_VERSION3);
     } else {
-        snprintf(somestring, 255, "\t<version>%s</version>\n", RRD_VERSION);
+        CB_FMTS("\t<version>%s</version>\n", RRD_VERSION);
     }
-    cb(somestring, strlen(somestring), user);
     
-    snprintf(somestring, 255, "\t<step>%lu</step> <!-- Seconds -->\n",
+    CB_FMTS("\t<step>%lu</step> <!-- Seconds -->\n",
         rrd.stat_head->pdp_step);
-    cb(somestring, strlen(somestring), user);
 
 #ifdef HAVE_STRFTIME
     localtime_r(&rrd.live_head->last_up, &tm);
@@ -137,39 +137,32 @@
 #else
 # error "Need strftime"
 #endif
-    snprintf(somestring_buf, 255, "\t<lastupdate>%lld</lastupdate> <!-- %s 
-->\n\n",
+    CB_FMTS("\t<lastupdate>%lld</lastupdate> <!-- %s -->\n\n",
         (long long) rrd.live_head->last_up, somestring);
-    cb(somestring_buf, strlen(somestring_buf), user);
     for (i = 0; i < rrd.stat_head->ds_cnt; i++) {
-        cb("\t<ds>\n", 
-            strlen("\t<ds>\n"), user);
+        CB_PUTS("\t<ds>\n");
 
-        snprintf(somestring_buf, 255, "\t\t<name> %s </name>\n", 
rrd.ds_def[i].ds_nam);
-        cb(somestring_buf, strlen(somestring_buf), user);
+        CB_FMTS("\t\t<name> %s </name>\n", rrd.ds_def[i].ds_nam);
 
-        snprintf(somestring_buf, 255, "\t\t<type> %s </type>\n", 
rrd.ds_def[i].dst);
-        cb(somestring_buf, strlen(somestring_buf), user);
+        CB_FMTS("\t\t<type> %s </type>\n", rrd.ds_def[i].dst);
 
         if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) {
-            snprintf(somestring_buf, 255, 
"\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
+            CB_FMTS("\t\t<minimal_heartbeat>%lu</minimal_heartbeat>\n",
                     rrd.ds_def[i].par[DS_mrhb_cnt].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
 
             if (isnan(rrd.ds_def[i].par[DS_min_val].u_val)) {
-                snprintf(somestring_buf, 255, "\t\t<min>NaN</min>\n");
+                CB_PUTS("\t\t<min>NaN</min>\n");
             } else {
-                snprintf(somestring_buf, 255, "\t\t<min>%0.10e</min>\n",
+                CB_FMTS("\t\t<min>%0.10e</min>\n",
                     rrd.ds_def[i].par[DS_min_val].u_val);
             }
-            cb(somestring_buf, strlen(somestring_buf), user);
             
             if (isnan(rrd.ds_def[i].par[DS_max_val].u_val)) {
-                snprintf(somestring_buf, 255, "\t\t<max>NaN</max>\n");
+                CB_PUTS("\t\t<max>NaN</max>\n");
             } else {
-                snprintf(somestring_buf, 255, "\t\t<max>%0.10e</max>\n",
+                CB_FMTS("\t\t<max>%0.10e</max>\n",
                     rrd.ds_def[i].par[DS_max_val].u_val);
             }
-            cb(somestring_buf, strlen(somestring_buf), user);
         } else {        /* DST_CDEF */
             char     *str = NULL;
 
@@ -178,37 +171,31 @@
 
             //Splitting into 3 writes to avoid allocating memory
             //This is better compared to snprintf as str may be of arbitrary 
size
-            cb("\t\t<cdef> ", strlen("\t\t<cdef>"), user);
-            cb(str, strlen(str), user);
-            cb(" </cdef>\n", strlen("</cdef>\n"), user);
+            CB_PUTS("\t\t<cdef> ");
+            CB_PUTS(str);
+            CB_PUTS(" </cdef>\n");
 
             free(str);
         }
 
-        cb("\n\t\t<!-- PDP Status -->\n",
-            strlen("\n\t\t<!-- PDP Status -->\n"), user);
-        snprintf(somestring_buf, 255, "\t\t<last_ds>%s</last_ds>\n",
+        CB_PUTS("\n\t\t<!-- PDP Status -->\n");
+        CB_FMTS("\t\t<last_ds>%s</last_ds>\n",
             rrd.pdp_prep[i].last_ds);
-        cb(somestring_buf, strlen(somestring_buf), user);
 
         if (isnan(rrd.pdp_prep[i].scratch[PDP_val].u_val)) {
-            snprintf(somestring_buf, 255, "\t\t<value>NaN</value>\n");
+            CB_PUTS("\t\t<value>NaN</value>\n");
         } else {
-            snprintf(somestring_buf, 255, "\t\t<value>%0.10e</value>\n",
+            CB_FMTS("\t\t<value>%0.10e</value>\n",
                 rrd.pdp_prep[i].scratch[PDP_val].u_val);
         }
-        cb(somestring_buf, strlen(somestring_buf), user);
 
-        snprintf(somestring_buf, 255, "\t\t<unknown_sec> %lu </unknown_sec>\n",
+        CB_FMTS("\t\t<unknown_sec> %lu </unknown_sec>\n",
             rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt);
-        cb(somestring_buf, strlen(somestring_buf), user);
 
-        cb("\t</ds>\n\n",
-            strlen("\t</ds>\n\n"), user);
+        CB_PUTS("\t</ds>\n\n");
     }
 
-    cb("\t<!-- Round Robin Archives -->\n",
-        strlen("\t<!-- Round Robin Archives -->\n"), user);
+    CB_PUTS("\t<!-- Round Robin Archives -->\n");
 
     rra_base = rrd_file->header_len;
     rra_next = rra_base;
@@ -221,108 +208,80 @@
         rra_next += (rrd.stat_head->ds_cnt
                      * rrd.rra_def[i].row_cnt * sizeof(rrd_value_t));
 
-        cb("\t<rra>\n",
-            strlen("\t<rra>\n"), user);
+        CB_PUTS("\t<rra>\n");
 
-        snprintf(somestring_buf, 255, 
-            "\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
-        cb(somestring_buf, strlen(somestring_buf), user);
+        CB_FMTS("\t\t<cf>%s</cf>\n", rrd.rra_def[i].cf_nam);
 
-        snprintf(somestring_buf, 255, 
-            "\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
+        CB_FMTS("\t\t<pdp_per_row>%lu</pdp_per_row> <!-- %lu seconds -->\n\n",
             rrd.rra_def[i].pdp_cnt, 
             rrd.rra_def[i].pdp_cnt * rrd.stat_head->pdp_step);
-        cb(somestring_buf, strlen(somestring_buf), user);
 
         /* support for RRA parameters */
-        cb("\t\t<params>\n", 
-            strlen("\t\t<params>\n"), user);
+        CB_PUTS("\t\t<params>\n");
 
         switch (cf_conv(rrd.rra_def[i].cf_nam)) {
         case CF_HWPREDICT:
         case CF_MHWPREDICT:
-            snprintf(somestring_buf, 255, "\t\t<hw_alpha>%0.10e</hw_alpha>\n",
-                    rrd.rra_def[i].par[RRA_hw_alpha].u_val);
-            cb(somestring_buf, strlen(somestring_buf), user);
-
-            snprintf(somestring_buf, 255, "\t\t<hw_beta>%0.10e</hw_beta>\n",
-                    rrd.rra_def[i].par[RRA_hw_beta].u_val);
-            cb(somestring_buf, strlen(somestring_buf), user);
-
-            snprintf(somestring_buf, 255, 
-                    "\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
-                    rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
+            CB_FMTS("\t\t<hw_alpha>%0.10e</hw_alpha>\n",
+                rrd.rra_def[i].par[RRA_hw_alpha].u_val);
+
+            CB_FMTS("\t\t<hw_beta>%0.10e</hw_beta>\n",
+                rrd.rra_def[i].par[RRA_hw_beta].u_val);
+
+            CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
+                rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
             break;
         case CF_SEASONAL:
         case CF_DEVSEASONAL:
-            snprintf(somestring_buf, 255, 
-                    "\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
-                    rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
-            cb(somestring_buf, strlen(somestring_buf), user);
-
-            snprintf(somestring_buf, 255, 
-                    "\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
-                    rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
+            CB_FMTS("\t\t<seasonal_gamma>%0.10e</seasonal_gamma>\n",
+                rrd.rra_def[i].par[RRA_seasonal_gamma].u_val);
+
+            CB_FMTS("\t\t<seasonal_smooth_idx>%lu</seasonal_smooth_idx>\n",
+                rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt);
 
             if (atoi(rrd.stat_head->version) >= 4) {
-                snprintf(somestring_buf, 255, 
-                    "\t\t<smoothing_window>%0.10e</smoothing_window>\n",
+                CB_FMTS("\t\t<smoothing_window>%0.10e</smoothing_window>\n",
                     rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val);
-                cb(somestring_buf, strlen(somestring_buf), user);
             }
-            snprintf(somestring_buf, 255, 
-                    "\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
-                    rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
+


+            CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
+                rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
             break;
         case CF_FAILURES:
-            snprintf(somestring_buf, 255, 
"\t\t<delta_pos>%0.10e</delta_pos>\n",
-                    rrd.rra_def[i].par[RRA_delta_pos].u_val);
-            cb(somestring_buf, strlen(somestring_buf), user);
-
-            snprintf(somestring_buf, 255, 
"\t\t<delta_neg>%0.10e</delta_neg>\n",
-                    rrd.rra_def[i].par[RRA_delta_neg].u_val);
-            cb(somestring_buf, strlen(somestring_buf), user);
-
-            snprintf(somestring_buf, 255, "\t\t<window_len>%lu</window_len>\n",
-                    rrd.rra_def[i].par[RRA_window_len].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
-
-            snprintf(somestring_buf, 255, 
-                    "\t\t<failure_threshold>%lu</failure_threshold>\n",
-                    rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
+            CB_FMTS("\t\t<delta_pos>%0.10e</delta_pos>\n",
+                rrd.rra_def[i].par[RRA_delta_pos].u_val);
+
+            CB_FMTS("\t\t<delta_neg>%0.10e</delta_neg>\n",
+                rrd.rra_def[i].par[RRA_delta_neg].u_val);
+
+            CB_FMTS("\t\t<window_len>%lu</window_len>\n",
+                rrd.rra_def[i].par[RRA_window_len].u_cnt);
+
+            CB_FMTS("\t\t<failure_threshold>%lu</failure_threshold>\n",
+                rrd.rra_def[i].par[RRA_failure_threshold].u_cnt);
 
             /* fall thru */
         case CF_DEVPREDICT:
-            snprintf(somestring_buf, 255, 
-                    "\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
-                    rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
-            cb(somestring_buf, strlen(somestring_buf), user);
+            CB_FMTS("\t\t<dependent_rra_idx>%lu</dependent_rra_idx>\n",
+                rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt);
             break;
         case CF_AVERAGE:
         case CF_MAXIMUM:
         case CF_MINIMUM:
         case CF_LAST:
         default:
-            snprintf(somestring_buf, 255, "\t\t<xff>%0.10e</xff>\n",
-                    rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
-            cb(somestring_buf, strlen(somestring_buf), user);
+            CB_FMTS("\t\t<xff>%0.10e</xff>\n",
+                rrd.rra_def[i].par[RRA_cdp_xff_val].u_val);
             break;
         }
 
-        cb("\t\t</params>\n",
-            strlen("\t\t</params>\n"), user);
-        cb("\t\t<cdp_prep>\n",
-            strlen("\t\t<cdp_prep>\n"), user);
+        CB_PUTS("\t\t</params>\n");
+        CB_PUTS("\t\t<cdp_prep>\n");
 
         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
             unsigned long ivalue;
 
-            cb("\t\t\t<ds>\n",
-                strlen("\t\t\t<ds>\n"), user);
+            CB_PUTS("\t\t\t<ds>\n");
             /* support for exporting all CDP parameters */
             /* parameters common to all CFs */
             /* primary_val and secondary_val do not need to be saved between 
updates
@@ -331,24 +290,18 @@
             value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                 scratch[CDP_primary_val].u_val;
             if (isnan(value)) {
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<primary_value>NaN</primary_value>\n");
+                CB_PUTS("\t\t\t<primary_value>NaN</primary_value>\n");
             } else {
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<primary_value>%0.10e</primary_value>\n", value);
+                CB_FMTS("\t\t\t<primary_value>%0.10e</primary_value>\n", 
value);
             }
-            cb(somestring_buf, strlen(somestring_buf), user);
 
             value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                 scratch[CDP_secondary_val].u_val;
             if (isnan(value)) {
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<secondary_value>NaN</secondary_value>\n");
+                CB_PUTS("\t\t\t<secondary_value>NaN</secondary_value>\n");
             } else {
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<secondary_value>%0.10e</secondary_value>\n", 
value);
+                CB_FMTS("\t\t\t<secondary_value>%0.10e</secondary_value>\n", 
value);
             }
-            cb(somestring_buf, strlen(somestring_buf), user);
 
             switch (cf_conv(rrd.rra_def[i].cf_nam)) {
             case CF_HWPREDICT:
@@ -356,111 +309,79 @@
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_hw_intercept].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<intercept>NaN</intercept>\n");
+                    CB_PUTS("\t\t\t<intercept>NaN</intercept>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<intercept>%0.10e</intercept>\n", value);
+                    CB_FMTS("\t\t\t<intercept>%0.10e</intercept>\n", value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_hw_last_intercept].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<last_intercept>NaN</last_intercept>\n");
+                    CB_PUTS("\t\t\t<last_intercept>NaN</last_intercept>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<last_intercept>%0.10e</last_intercept>\n", 
value);
+                    CB_FMTS("\t\t\t<last_intercept>%0.10e</last_intercept>\n", 
value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_hw_slope].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<slope>NaN</slope>\n");
+                    CB_PUTS("\t\t\t<slope>NaN</slope>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<slope>%0.10e</slope>\n", value);
+                    CB_FMTS("\t\t\t<slope>%0.10e</slope>\n", value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_hw_last_slope].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<last_slope>NaN</last_slope>\n");
+                    CB_PUTS("\t\t\t<last_slope>NaN</last_slope>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<last_slope>%0.10e</last_slope>\n", value);
+                    CB_FMTS("\t\t\t<last_slope>%0.10e</last_slope>\n", value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
                 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_null_count].u_cnt;
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
-                cb(somestring_buf, strlen(somestring_buf), user);
+                CB_FMTS("\t\t\t<nan_count>%lu</nan_count>\n", ivalue);
 
                 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_last_null_count].u_cnt;
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<last_nan_count>%lu</last_nan_count>\n", ivalue);
-                cb(somestring_buf, strlen(somestring_buf), user);
+                CB_FMTS("\t\t\t<last_nan_count>%lu</last_nan_count>\n", 
ivalue);
                 break;
             case CF_SEASONAL:
             case CF_DEVSEASONAL:
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_hw_seasonal].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<seasonal>NaN</seasonal>\n");
+                    CB_PUTS("\t\t\t<seasonal>NaN</seasonal>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<seasonal>%0.10e</seasonal>\n", value);
+                    CB_FMTS("\t\t\t<seasonal>%0.10e</seasonal>\n", value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_hw_last_seasonal].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<last_seasonal>NaN</last_seasonal>\n");
+                    CB_PUTS("\t\t\t<last_seasonal>NaN</last_seasonal>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", 
value);
+                    CB_FMTS("\t\t\t<last_seasonal>%0.10e</last_seasonal>\n", 
value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
                 ivalue = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                     scratch[CDP_init_seasonal].u_cnt;
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
-                cb(somestring_buf, strlen(somestring_buf), user);
+                CB_FMTS("\t\t\t<init_flag>%lu</init_flag>\n", ivalue);
                 break;
             case CF_DEVPREDICT:
                 break;
             case CF_FAILURES:
             {
                 unsigned short vidx;
-                char     *violations_array = (char *) ((void *)
-                                                       rrd.cdp_prep[i *
-                                                                    rrd.
-                                                                    stat_head->
-                                                                    ds_cnt +
-                                                                    ii].
-                                                       scratch);
-                cb("\t\t\t<history> ", 
-                    strlen("\t\t\t<history> "), user);
+                char *violations_array = (char *) ((void *)
+                    rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch);
+                CB_PUTS("\t\t\t<history>");
                 for (vidx = 0;
                     vidx < rrd.rra_def[i].par[RRA_window_len].u_cnt;
                     ++vidx) {
-                    snprintf(somestring_buf, 255, "%d", 
violations_array[vidx]);
-                    cb(somestring_buf, strlen(somestring_buf), user);
+                    CB_FMTS("%d", violations_array[vidx]);
                 }
-                cb(" </history>\n",
-                    strlen(" </history>\n"), user);
+                CB_PUTS("</history>\n");
             }
                 break;
             case CF_AVERAGE:
@@ -470,29 +391,21 @@
             default:
                 value = rrd.cdp_prep[i * rrd.stat_head->ds_cnt + 
ii].scratch[CDP_val].u_val;
                 if (isnan(value)) {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<value>NaN</value>\n");
+                    CB_PUTS("\t\t\t<value>NaN</value>\n");
                 } else {
-                    snprintf(somestring_buf, 255, 
-                        "\t\t\t<value>%0.10e</value>\n", value);
+                    CB_FMTS("\t\t\t<value>%0.10e</value>\n", value);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
 
-                snprintf(somestring_buf, 255, 
-                    "\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
+                CB_FMTS("\t\t\t<unknown_datapoints>%lu</unknown_datapoints>\n",
                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].
                         scratch[CDP_unkn_pdp_cnt].u_cnt);
-                cb(somestring_buf, strlen(somestring_buf), user);
                 break;
             }
-            cb("\t\t\t</ds>\n",
-                strlen("\t\t\t</ds>\n"), user);
+            CB_PUTS("\t\t\t</ds>\n");
         }
-        cb("\t\t</cdp_prep>\n",
-            strlen("\t\t</cdp_prep>\n"), user);
+        CB_PUTS("\t\t</cdp_prep>\n");
 
-        cb("\t\t<database>\n",
-            strlen("\t\t<database>\n"), user);
+        CB_PUTS("\t\t<database>\n");
         rrd_seek(rrd_file, (rra_start + (rrd.rra_ptr[i].cur_row + 1)
                             * rrd.stat_head->ds_cnt
                             * sizeof(rrd_value_t)), SEEK_SET);
@@ -516,27 +429,21 @@
 #else
 # error "Need strftime"
 #endif
-            snprintf(somestring_buf, 255, 
-                "\t\t\t<!-- %s / %lld --> <row>",  somestring, (long long) 
now);
-            cb(somestring_buf, strlen(somestring_buf), user);
+            CB_FMTS("\t\t\t<!-- %s / %lld --> <row>",  somestring, (long long) 
now);
             for (iii = 0; iii < rrd.stat_head->ds_cnt; iii++) {
                 rrd_read(rrd_file, &my_cdp, sizeof(rrd_value_t) * 1);
                 if (isnan(my_cdp)) {
-                    snprintf(somestring_buf, 255, "<v>NaN</v>");
+                    CB_PUTS("<v>NaN</v>");
                 } else {
-                    snprintf(somestring_buf, 255, "<v>%0.10e</v>", my_cdp);
+                    CB_FMTS("<v>%0.10e</v>", my_cdp);
                 }
-                cb(somestring_buf, strlen(somestring_buf), user);
             }
-            cb("</row>\n",
-                strlen("</row>\n"), user);
+            CB_PUTS("</row>\n");
         }
-        cb("\t\t</database>\n\t</rra>\n",
-            strlen("\t\t</database>\n\t</rra>\n"), user);
+        CB_PUTS("\t\t</database>\n\t</rra>\n");
     }
 
-    cb("</rrd>\n",
-        strlen("</rrd>\n"), user);
+    CB_PUTS("</rrd>\n");
 
     rrd_free(&rrd);
 
@@ -545,6 +452,13 @@
 #endif
 
     return rrd_close(rrd_file);
+
+//Undefining the previously defined shortcuts
+//See start of this function
+#undef CB_PUTS
+#undef CB_FMTS
+//End of macro undefining
+
 }
 
 size_t rrd_dump_opt_cb_fileout(

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
rrd-developers mailing list
[email protected]
https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers

Reply via email to