Code

ruby binding fix: RRD graphv appears to not reset state properly - Sven Engelhardt
[rrdtool-all.git] / program / bindings / ruby / main.c
1 /* $Id$
2  * Substantial penalty for early withdrawal.
3  */
5 #include <unistd.h>
6 #include <ruby.h>
7 #include "../../src/rrd_tool.h"
9 typedef struct string_arr_t {
10     int       len;
11     char    **strings;
12 } string_arr;
14 VALUE     mRRD;
15 VALUE     rb_eRRDError;
17 typedef int (
18     *RRDFUNC) (
19     int argc,
20     char **argv);
22 #define RRD_CHECK_ERROR  \
23     if (rrd_test_error()) \
24       rb_raise(rb_eRRDError, rrd_get_error()); \
25     rrd_clear_error();
27 string_arr string_arr_new(
28     VALUE rb_strings)
29 {
30     string_arr a;
31     char      buf[64];
32     int       i;
34     Check_Type(rb_strings, T_ARRAY);
35     a.len = RARRAY_LEN(rb_strings) + 1;
37     a.strings = malloc(a.len * sizeof(char *));
38     a.strings[0] = "dummy"; /* first element is a dummy element */
40     for (i = 0; i < a.len - 1; i++) {
41         VALUE     v = rb_ary_entry(rb_strings, i);
43         switch (TYPE(v)) {
44         case T_STRING:
45             a.strings[i + 1] = strdup(STR2CSTR(v));
46             break;
47         case T_FIXNUM:
48             snprintf(buf, 63, "%d", FIX2INT(v));
49             a.strings[i + 1] = strdup(buf);
50             break;
51         default:
52             rb_raise(rb_eTypeError,
53                      "invalid argument - %s, expected T_STRING or T_FIXNUM on index %d",
54                      rb_class2name(CLASS_OF(v)), i);
55             break;
56         }
57     }
59     return a;
60 }
62 void string_arr_delete(
63     string_arr a)
64 {
65     int       i;
67     /* skip dummy first entry */
68     for (i = 1; i < a.len; i++) {
69         free(a.strings[i]);
70     }
72     free(a.strings);
73 }
75 void reset_rrd_state(
76     )
77 {
78     optind = 0;
79     opterr = 0;
80     rrd_clear_error();
81 }
83 /* Simple Calls */
85 VALUE rrd_call(
86     RRDFUNC func,
87     VALUE args)
88 {
89     string_arr a;
91     a = string_arr_new(args);
92     reset_rrd_state();
93     func(a.len, a.strings);
94     string_arr_delete(a);
96     RRD_CHECK_ERROR return Qnil;
97 }
99 VALUE rb_rrd_create(
100     VALUE self,
101     VALUE args)
103     return rrd_call(rrd_create, args);
106 VALUE rb_rrd_dump(
107     VALUE self,
108     VALUE args)
110     return rrd_call(rrd_dump, args);
113 VALUE rb_rrd_resize(
114     VALUE self,
115     VALUE args)
117     return rrd_call(rrd_resize, args);
120 VALUE rb_rrd_restore(
121     VALUE self,
122     VALUE args)
124     return rrd_call(rrd_restore, args);
127 VALUE rb_rrd_tune(
128     VALUE self,
129     VALUE args)
131     return rrd_call(rrd_tune, args);
134 VALUE rb_rrd_update(
135     VALUE self,
136     VALUE args)
138     return rrd_call(rrd_update, args);
142 /* Calls Returning Data via the Info Interface */
144 VALUE rb_rrd_infocall(
145     RRDFUNC func,
146     VALUE args)
148     string_arr a;
149     rrd_info_t *p, *data;
150     VALUE     result;
152     a = string_arr_new(args);
153     reset_rrd_state();
154     data = func(a.len, a.strings);
155     string_arr_delete(a);
157     RRD_CHECK_ERROR result = rb_hash_new();
159     p = data;
160     while (data) {
161         VALUE     key = rb_str_new2(data->key);
163         switch (data->type) {
164         case RD_I_VAL:
165             if (isnan(data->value.u_val)) {
166                 rb_hash_aset(result, key, Qnil);
167             } else {
168                 rb_hash_aset(result, key, rb_float_new(data->value.u_val));
169             }
170             break;
171         case RD_I_CNT:
172             rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
173             break;
174         case RD_I_STR:
175             rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
176             break;
177         case RD_I_BLO:
178             rb_hash_aset(result, key,
179                          rb_str_new(data->value.u_blo.ptr,
180                                     data->value.u_blo.size));
181             break;
182         }
183         data = data->next;
184     }
185     rrd_info_free(p);
186     return result;
189 VALUE rb_rrd_info(
190     VALUE self,
191     VALUE args)
193     return rb_rrd_infocall(rrd_info, args);
196 VALUE rb_rrd_updatev(
197     VALUE self,
198     VALUE args)
200     return rb_rrd_infocall(rrd_update_v, args);
203 VALUE rb_rrd_graphv(
204     VALUE self,
205     VALUE args)
207     return rb_rrd_infocall(rrd_graph_v, args);
211 /* Other Calls */
213 VALUE rb_rrd_fetch(
214     VALUE self,
215     VALUE args)
217     string_arr a;
218     unsigned long i, j, k, step, ds_cnt;
219     rrd_value_t *raw_data;
220     char    **raw_names;
221     VALUE     data, names, result;
222     time_t    start, end;
224     a = string_arr_new(args);
225     reset_rrd_state();
226     rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
227               &raw_data);
228     string_arr_delete(a);
230     RRD_CHECK_ERROR names = rb_ary_new();
232     for (i = 0; i < ds_cnt; i++) {
233         rb_ary_push(names, rb_str_new2(raw_names[i]));
234         rrd_freemem(raw_names[i]);
235     }
236     rrd_freemem(raw_names);
238     k = 0;
239     data = rb_ary_new();
240     for (i = start; i <= end; i += step) {
241         VALUE     line = rb_ary_new2(ds_cnt);
243         for (j = 0; j < ds_cnt; j++) {
244             rb_ary_store(line, j, rb_float_new(raw_data[k]));
245             k++;
246         }
247         rb_ary_push(data, line);
248     }
249     rrd_freemem(raw_data);
251     result = rb_ary_new2(5);
252     rb_ary_store(result, 0, INT2NUM(start));
253     rb_ary_store(result, 1, INT2NUM(end));
254     rb_ary_store(result, 2, names);
255     rb_ary_store(result, 3, data);
256     rb_ary_store(result, 4, INT2FIX(step));
257     return result;
260 VALUE rb_rrd_graph(
261     VALUE self,
262     VALUE args)
264     string_arr a;
265     char    **calcpr, **p;
266     VALUE     result, print_results;
267     int       xsize, ysize;
268     double    ymin, ymax;
270     a = string_arr_new(args);
271     reset_rrd_state();
272     rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
273     string_arr_delete(a);
275     RRD_CHECK_ERROR result = rb_ary_new2(3);
277     print_results = rb_ary_new();
278     p = calcpr;
279     for (p = calcpr; p && *p; p++) {
280         rb_ary_push(print_results, rb_str_new2(*p));
281         rrd_freemem(*p);
282     }
283     rrd_freemem(calcpr);
284     rb_ary_store(result, 0, print_results);
285     rb_ary_store(result, 1, INT2FIX(xsize));
286     rb_ary_store(result, 2, INT2FIX(ysize));
287     return result;
291 VALUE rb_rrd_last(
292     VALUE self,
293     VALUE args)
295     string_arr a;
296     time_t    last;
298     a = string_arr_new(args);
299     reset_rrd_state();
300     last = rrd_last(a.len, a.strings);
301     string_arr_delete(a);
303     RRD_CHECK_ERROR
304         return rb_funcall(rb_cTime, rb_intern("at"), 1, UINT2NUM(last));
307 void Init_RRD(
308     )
310     mRRD = rb_define_module("RRD");
311     rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
313     rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
314     rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
315     rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
316     rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
317     rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
318     rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
319     rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
320     rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
321     rb_define_module_function(mRRD, "update", rb_rrd_update, -2);
322     rb_define_module_function(mRRD, "info", rb_rrd_info, -2);
323     rb_define_module_function(mRRD, "updatev", rb_rrd_updatev, -2);
324     rb_define_module_function(mRRD, "graphv", rb_rrd_graphv, -2);