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("\n", - strlen("\n"), user); - cb("\n", - strlen("\n"), user); - cb("\n", - strlen("\n"), user); - cb("\n", - strlen("\n"), user); + CB_PUTS("\n"); + CB_PUTS("\n"); + CB_PUTS("\n"); + CB_PUTS("\n"); } else if (opt_header == 2) { - cb("\n", - strlen("\n"), user); - cb("\n", - strlen("\n"), user); - cb("\n", - strlen("\txsi:schemaLocation=\"http://oss.oetiker.ch/rrdtool/rrdtool-dump.xml http://oss.oetiker.ch/rrdtool/rrdtool-dump.xsd\">\n"), user); + CB_PUTS("\n"); + CB_PUTS("\n"); + CB_PUTS("\n"); } else { - cb("\n", - strlen("\n"), user); - cb("\n", - strlen("\n"), user); + CB_PUTS("\n"); + CB_PUTS("\n"); } if (atoi(rrd.stat_head->version) <= 3) { - snprintf(somestring, 255, "\t%s\n", RRD_VERSION3); + CB_FMTS("\t%s\n", RRD_VERSION3); } else { - snprintf(somestring, 255, "\t%s\n", RRD_VERSION); + CB_FMTS("\t%s\n", RRD_VERSION); } - cb(somestring, strlen(somestring), user); - snprintf(somestring, 255, "\t%lu \n", + CB_FMTS("\t%lu \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%lld \n\n", + CB_FMTS("\t%lld \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\n", - strlen("\t\n"), user); + CB_PUTS("\t\n"); - snprintf(somestring_buf, 255, "\t\t %s \n", rrd.ds_def[i].ds_nam); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t %s \n", rrd.ds_def[i].ds_nam); - snprintf(somestring_buf, 255, "\t\t %s \n", rrd.ds_def[i].dst); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t %s \n", rrd.ds_def[i].dst); if (dst_conv(rrd.ds_def[i].dst) != DST_CDEF) { - snprintf(somestring_buf, 255, "\t\t%lu\n", + CB_FMTS("\t\t%lu\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\tNaN\n"); + CB_PUTS("\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, "\t\t%0.10e\n", + CB_FMTS("\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, "\t\t%0.10e\n", + CB_FMTS("\t\t%0.10e\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 ", strlen("\t\t"), user); - cb(str, strlen(str), user); - cb(" \n", strlen("\n"), user); + CB_PUTS("\t\t "); + CB_PUTS(str); + CB_PUTS(" \n"); free(str); } - cb("\n\t\t\n", - strlen("\n\t\t\n"), user); - snprintf(somestring_buf, 255, "\t\t%s\n", + CB_PUTS("\n\t\t\n"); + CB_FMTS("\t\t%s\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\tNaN\n"); + CB_PUTS("\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, "\t\t%0.10e\n", + CB_FMTS("\t\t%0.10e\n", rrd.pdp_prep[i].scratch[PDP_val].u_val); } - cb(somestring_buf, strlen(somestring_buf), user); - snprintf(somestring_buf, 255, "\t\t %lu \n", + CB_FMTS("\t\t %lu \n", rrd.pdp_prep[i].scratch[PDP_unkn_sec_cnt].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); - cb("\t\n\n", - strlen("\t\n\n"), user); + CB_PUTS("\t\n\n"); } - cb("\t\n", - strlen("\t\n"), user); + CB_PUTS("\t\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\n", - strlen("\t\n"), user); + CB_PUTS("\t\n"); - snprintf(somestring_buf, 255, - "\t\t%s\n", rrd.rra_def[i].cf_nam); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t%s\n", rrd.rra_def[i].cf_nam); - snprintf(somestring_buf, 255, - "\t\t%lu \n\n", + CB_FMTS("\t\t%lu \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\n", - strlen("\t\t\n"), user); + CB_PUTS("\t\t\n"); switch (cf_conv(rrd.rra_def[i].cf_nam)) { case CF_HWPREDICT: case CF_MHWPREDICT: - snprintf(somestring_buf, 255, "\t\t%0.10e\n", - rrd.rra_def[i].par[RRA_hw_alpha].u_val); - cb(somestring_buf, strlen(somestring_buf), user); - - snprintf(somestring_buf, 255, "\t\t%0.10e\n", - rrd.rra_def[i].par[RRA_hw_beta].u_val); - cb(somestring_buf, strlen(somestring_buf), user); - - snprintf(somestring_buf, 255, - "\t\t%lu\n", - rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t%0.10e\n", + rrd.rra_def[i].par[RRA_hw_alpha].u_val); + + CB_FMTS("\t\t%0.10e\n", + rrd.rra_def[i].par[RRA_hw_beta].u_val); + + CB_FMTS("\t\t%lu\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%0.10e\n", - rrd.rra_def[i].par[RRA_seasonal_gamma].u_val); - cb(somestring_buf, strlen(somestring_buf), user); - - snprintf(somestring_buf, 255, - "\t\t%lu\n", - rrd.rra_def[i].par[RRA_seasonal_smooth_idx].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t%0.10e\n", + rrd.rra_def[i].par[RRA_seasonal_gamma].u_val); + + CB_FMTS("\t\t%lu\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%0.10e\n", + CB_FMTS("\t\t%0.10e\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%lu\n", - rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); + + CB_FMTS("\t\t%lu\n", + rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt); break; case CF_FAILURES: - snprintf(somestring_buf, 255, "\t\t%0.10e\n", - rrd.rra_def[i].par[RRA_delta_pos].u_val); - cb(somestring_buf, strlen(somestring_buf), user); - - snprintf(somestring_buf, 255, "\t\t%0.10e\n", - rrd.rra_def[i].par[RRA_delta_neg].u_val); - cb(somestring_buf, strlen(somestring_buf), user); - - snprintf(somestring_buf, 255, "\t\t%lu\n", - rrd.rra_def[i].par[RRA_window_len].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); - - snprintf(somestring_buf, 255, - "\t\t%lu\n", - rrd.rra_def[i].par[RRA_failure_threshold].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t%0.10e\n", + rrd.rra_def[i].par[RRA_delta_pos].u_val); + + CB_FMTS("\t\t%0.10e\n", + rrd.rra_def[i].par[RRA_delta_neg].u_val); + + CB_FMTS("\t\t%lu\n", + rrd.rra_def[i].par[RRA_window_len].u_cnt); + + CB_FMTS("\t\t%lu\n", + rrd.rra_def[i].par[RRA_failure_threshold].u_cnt); /* fall thru */ case CF_DEVPREDICT: - snprintf(somestring_buf, 255, - "\t\t%lu\n", - rrd.rra_def[i].par[RRA_dependent_rra_idx].u_cnt); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t%lu\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%0.10e\n", - rrd.rra_def[i].par[RRA_cdp_xff_val].u_val); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t%0.10e\n", + rrd.rra_def[i].par[RRA_cdp_xff_val].u_val); break; } - cb("\t\t\n", - strlen("\t\t\n"), user); - cb("\t\t\n", - strlen("\t\t\n"), user); + CB_PUTS("\t\t\n"); + CB_PUTS("\t\t\n"); for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) { unsigned long ivalue; - cb("\t\t\t\n", - strlen("\t\t\t\n"), user); + CB_PUTS("\t\t\t\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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%lu\n", ivalue); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t\t%lu\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%lu\n", ivalue); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t\t%lu\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\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%lu\n", ivalue); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t\t%lu\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 ", - strlen("\t\t\t "), user); + char *violations_array = (char *) ((void *) + rrd.cdp_prep[i * rrd.stat_head->ds_cnt + ii].scratch); + CB_PUTS("\t\t\t"); 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(" \n", - strlen(" \n"), user); + CB_PUTS("\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\tNaN\n"); + CB_PUTS("\t\t\tNaN\n"); } else { - snprintf(somestring_buf, 255, - "\t\t\t%0.10e\n", value); + CB_FMTS("\t\t\t%0.10e\n", value); } - cb(somestring_buf, strlen(somestring_buf), user); - snprintf(somestring_buf, 255, - "\t\t\t%lu\n", + CB_FMTS("\t\t\t%lu\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\n", - strlen("\t\t\t\n"), user); + CB_PUTS("\t\t\t\n"); } - cb("\t\t\n", - strlen("\t\t\n"), user); + CB_PUTS("\t\t\n"); - cb("\t\t\n", - strlen("\t\t\n"), user); + CB_PUTS("\t\t\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 ", somestring, (long long) now); - cb(somestring_buf, strlen(somestring_buf), user); + CB_FMTS("\t\t\t ", 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, "NaN"); + CB_PUTS("NaN"); } else { - snprintf(somestring_buf, 255, "%0.10e", my_cdp); + CB_FMTS("%0.10e", my_cdp); } - cb(somestring_buf, strlen(somestring_buf), user); } - cb("\n", - strlen("\n"), user); + CB_PUTS("\n"); } - cb("\t\t\n\t\n", - strlen("\t\t\n\t\n"), user); + CB_PUTS("\t\t\n\t\n"); } - cb("\n", - strlen("\n"), user); + CB_PUTS("\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(