fixed indenting
[rrdtool.git] / src / rrd_info.c
index 4582e2a..f53a752 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.2.23  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3rc7  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_info  Get Information about the configuration of an RRD
  *****************************************************************************/
@@ -20,18 +20,14 @@ char     *sprintf_alloc(
     char *fmt,
     ...)
 {
-#ifdef HAVE_VSNPRINTF
-    int       maxlen = 50;
-#else
-    int       maxlen = 1000;
-#endif
+    int       maxlen = 1024 + strlen(fmt);
     char     *str = NULL;
     va_list   argp;
-    str = malloc(sizeof(char) * (strlen(fmt) + maxlen));
+    str = malloc(sizeof(char) * (maxlen + 1));
     if (str != NULL) {
         va_start(argp, fmt);
 #ifdef HAVE_VSNPRINTF
-        vsnprintf(str, maxlen - 1, fmt, argp);
+        vsnprintf(str, maxlen, fmt, argp);
 #else
         vsprintf(str, fmt, argp);
 #endif
@@ -71,6 +67,12 @@ info_t
         next->value.u_str = malloc(sizeof(char) * (strlen(value.u_str) + 1));
         strcpy(next->value.u_str, value.u_str);
         break;
+    case RD_I_BLO:
+        next->value.u_blo.size = value.u_blo.size;
+        next->value.u_blo.ptr =
+            malloc(sizeof(unsigned char) * value.u_blo.size);
+        memcpy(next->value.u_blo.ptr, value.u_blo.ptr, value.u_blo.size);
+        break;
     }
     return (next);
 }
@@ -99,17 +101,15 @@ info_t   *rrd_info_r(
 {
     unsigned int i, ii = 0;
     rrd_t     rrd;
-    info_t   *data, *cd;
+    info_t   *data = NULL, *cd;
     infoval   info;
     rrd_file_t *rrd_file;
     enum cf_en current_cf;
     enum dst_en current_ds;
 
     rrd_file = rrd_open(filename, &rrd, RRD_READONLY);
-    if (rrd_file == NULL) {
-        return (NULL);
-    }
-    close(rrd_file->fd);
+    if (rrd_file == NULL)
+        goto err_free;
 
     info.u_str = filename;
     cd = info_push(NULL, sprintf_alloc("filename"), RD_I_STR, info);
@@ -136,7 +136,7 @@ info_t   *rrd_info_r(
         {
             char     *buffer = NULL;
 
-            rpn_compact2str((rpn_cdefds_t *) & (rrd.ds_def[i].par[DS_cdef]),
+            rpn_compact2str((rpn_cdefds_t *) &(rrd.ds_def[i].par[DS_cdef]),
                             rrd.ds_def, &buffer);
             info.u_str = buffer;
             cd = info_push(cd,
@@ -188,12 +188,17 @@ info_t   *rrd_info_r(
         info.u_cnt = rrd.rra_def[i].row_cnt;
         cd = info_push(cd, sprintf_alloc("rra[%d].rows", i), RD_I_CNT, info);
 
+        info.u_cnt = rrd.rra_ptr[i].cur_row;
+        cd = info_push(cd, sprintf_alloc("rra[%d].cur_row", i), RD_I_CNT,
+                       info);
+
         info.u_cnt = rrd.rra_def[i].pdp_cnt;
         cd = info_push(cd, sprintf_alloc("rra[%d].pdp_per_row", i), RD_I_CNT,
                        info);
 
         switch (current_cf) {
         case CF_HWPREDICT:
+        case CF_MHWPREDICT:
             info.u_val = rrd.rra_def[i].par[RRA_hw_alpha].u_val;
             cd = info_push(cd, sprintf_alloc("rra[%d].alpha", i), RD_I_VAL,
                            info);
@@ -206,6 +211,13 @@ info_t   *rrd_info_r(
             info.u_val = rrd.rra_def[i].par[RRA_seasonal_gamma].u_val;
             cd = info_push(cd, sprintf_alloc("rra[%d].gamma", i), RD_I_VAL,
                            info);
+            if (atoi(rrd.stat_head->version) >= 4) {
+                info.u_val =
+                    rrd.rra_def[i].par[RRA_seasonal_smoothing_window].u_val;
+                cd = info_push(cd,
+                               sprintf_alloc("rra[%d].smoothing_window", i),
+                               RD_I_VAL, info);
+            }
             break;
         case CF_FAILURES:
             info.u_val = rrd.rra_def[i].par[RRA_delta_pos].u_val;
@@ -233,6 +245,7 @@ info_t   *rrd_info_r(
         for (ii = 0; ii < rrd.stat_head->ds_cnt; ii++) {
             switch (current_cf) {
             case CF_HWPREDICT:
+            case CF_MHWPREDICT:
                 info.u_val =
                     rrd.cdp_prep[i * rrd.stat_head->ds_cnt +
                                  ii].scratch[CDP_hw_intercept].u_val;
@@ -306,8 +319,62 @@ info_t   *rrd_info_r(
             }
         }
     }
-    rrd_free(&rrd);
+
     rrd_close(rrd_file);
+  err_free:
+    rrd_free(&rrd);
     return (data);
+}
 
+
+void info_print(
+    info_t *data)
+{
+    while (data) {
+        printf("%s = ", data->key);
+
+        switch (data->type) {
+        case RD_I_VAL:
+            if (isnan(data->value.u_val))
+                printf("NaN\n");
+            else
+                printf("%0.10e\n", data->value.u_val);
+            break;
+        case RD_I_CNT:
+            printf("%lu\n", data->value.u_cnt);
+            break;
+        case RD_I_INT:
+            printf("%d\n", data->value.u_int);
+            break;
+        case RD_I_STR:
+            printf("\"%s\"\n", data->value.u_str);
+            break;
+        case RD_I_BLO:
+            printf("BLOB_SIZE:%lu\n", data->value.u_blo.size);
+            fwrite(data->value.u_blo.ptr, data->value.u_blo.size, 1, stdout);
+            break;
+        }
+        data = data->next;
+    }
+}
+
+void info_free(
+    info_t *data)
+{
+    info_t   *save;
+
+    while (data) {
+        save = data;
+        if (data->key) {
+            if (data->type == RD_I_STR) {
+                free(data->value.u_str);
+            }
+            if (data->type == RD_I_BLO) {
+                free(data->value.u_blo.ptr);
+            }
+            free(data->key);
+        }
+        data = data->next;
+        free(save);
+    }
 }