c5cf79effdfcb7569ea32812ff6e043b477baa27
[rrdtool.git] / bindings / ruby / main.c
1 /* $Id$
2  * Substantial penalty for early withdrawal.
3  */
4
5 #include <unistd.h>
6 #include <ruby.h>
7 #include "../../src/rrd_tool.h"
8
9 typedef struct string_arr_t {
10     int       len;
11     char    **strings;
12 } string_arr;
13
14 VALUE     mRRD;
15 VALUE     rb_eRRDError;
16
17 typedef int (
18     *RRDFUNC) (
19     int argc,
20     char **argv);
21
22 #define RRD_CHECK_ERROR  \
23     if (rrd_test_error()) \
24       rb_raise(rb_eRRDError, rrd_get_error()); \
25     rrd_clear_error();
26
27 string_arr string_arr_new(
28     VALUE rb_strings)
29 {
30     string_arr a;
31     char      buf[64];
32     int       i;
33
34     Check_Type(rb_strings, T_ARRAY);
35     a.len = RARRAY(rb_strings)->len + 1;
36
37     a.strings = malloc(a.len * sizeof(char *));
38     a.strings[0] = "dummy"; /* first element is a dummy element */
39
40     for (i = 0; i < a.len - 1; i++) {
41         VALUE     v = rb_ary_entry(rb_strings, i);
42
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     }
58
59     return a;
60 }
61
62 void string_arr_delete(
63     string_arr a)
64 {
65     int       i;
66
67     /* skip dummy first entry */
68     for (i = 1; i < a.len; i++) {
69         free(a.strings[i]);
70     }
71
72     free(a.strings);
73 }
74
75 void reset_rrd_state(
76     )
77 {
78     optind = 0;
79     opterr = 0;
80     rrd_clear_error();
81 }
82
83 VALUE rrd_call(
84     RRDFUNC func,
85     VALUE args)
86 {
87     string_arr a;
88
89     a = string_arr_new(args);
90     reset_rrd_state();
91     func(a.len, a.strings);
92     string_arr_delete(a);
93
94     RRD_CHECK_ERROR return Qnil;
95 }
96
97 VALUE rb_rrd_create(
98     VALUE self,
99     VALUE args)
100 {
101     return rrd_call(rrd_create, args);
102 }
103
104 VALUE rb_rrd_dump(
105     VALUE self,
106     VALUE args)
107 {
108     return rrd_call(rrd_dump, args);
109 }
110
111 VALUE rb_rrd_fetch(
112     VALUE self,
113     VALUE args)
114 {
115     string_arr a;
116     unsigned long i, j, k, step, ds_cnt;
117     rrd_value_t *raw_data;
118     char    **raw_names;
119     VALUE     data, names, result;
120     time_t    start, end;
121
122     a = string_arr_new(args);
123     reset_rrd_state();
124     rrd_fetch(a.len, a.strings, &start, &end, &step, &ds_cnt, &raw_names,
125               &raw_data);
126     string_arr_delete(a);
127
128     RRD_CHECK_ERROR names = rb_ary_new();
129
130     for (i = 0; i < ds_cnt; i++) {
131         rb_ary_push(names, rb_str_new2(raw_names[i]));
132         free(raw_names[i]);
133     }
134     free(raw_names);
135
136     k = 0;
137     data = rb_ary_new();
138     for (i = start; i <= end; i += step) {
139         VALUE     line = rb_ary_new2(ds_cnt);
140
141         for (j = 0; j < ds_cnt; j++) {
142             rb_ary_store(line, j, rb_float_new(raw_data[k]));
143             k++;
144         }
145         rb_ary_push(data, line);
146     }
147     free(raw_data);
148
149     result = rb_ary_new2(4);
150     rb_ary_store(result, 0, INT2FIX(start));
151     rb_ary_store(result, 1, INT2FIX(end));
152     rb_ary_store(result, 2, names);
153     rb_ary_store(result, 3, data);
154     return result;
155 }
156
157 VALUE rb_rrd_graph(
158     VALUE self,
159     VALUE args)
160 {
161     string_arr a;
162     char    **calcpr, **p;
163     VALUE     result, print_results;
164     int       xsize, ysize;
165     double    ymin, ymax;
166
167     a = string_arr_new(args);
168     reset_rrd_state();
169     rrd_graph(a.len, a.strings, &calcpr, &xsize, &ysize, NULL, &ymin, &ymax);
170     string_arr_delete(a);
171
172     RRD_CHECK_ERROR result = rb_ary_new2(3);
173
174     print_results = rb_ary_new();
175     p = calcpr;
176     for (p = calcpr; p && *p; p++) {
177         rb_ary_push(print_results, rb_str_new2(*p));
178         free(*p);
179     }
180     free(calcpr);
181     rb_ary_store(result, 0, print_results);
182     rb_ary_store(result, 1, INT2FIX(xsize));
183     rb_ary_store(result, 2, INT2FIX(ysize));
184     return result;
185 }
186
187 VALUE rb_rrd_info(
188     VALUE self,
189     VALUE args)
190 {
191     string_arr a;
192     info_t   *p, *data;
193     VALUE     result;
194
195     a = string_arr_new(args);
196     data = rrd_info(a.len, a.strings);
197     string_arr_delete(a);
198
199     RRD_CHECK_ERROR result = rb_hash_new();
200     while (data) {
201         VALUE     key = rb_str_new2(data->key);
202
203         switch (data->type) {
204         case RD_I_VAL:
205             if (isnan(data->value.u_val)) {
206                 rb_hash_aset(result, key, Qnil);
207             } else {
208                 rb_hash_aset(result, key, rb_float_new(data->value.u_val));
209             }
210             break;
211         case RD_I_CNT:
212             rb_hash_aset(result, key, INT2FIX(data->value.u_cnt));
213             break;
214         case RD_I_STR:
215             rb_hash_aset(result, key, rb_str_new2(data->value.u_str));
216             free(data->value.u_str);
217             break;
218         }
219         p = data;
220         data = data->next;
221         free(p);
222     }
223     return result;
224 }
225
226 VALUE rb_rrd_last(
227     VALUE self,
228     VALUE args)
229 {
230     string_arr a;
231     time_t    last;
232
233     a = string_arr_new(args);
234     reset_rrd_state();
235     last = rrd_last(a.len, a.strings);
236     string_arr_delete(a);
237
238     RRD_CHECK_ERROR
239         return rb_funcall(rb_cTime, rb_intern("at"), 1, INT2FIX(last));
240 }
241
242 VALUE rb_rrd_resize(
243     VALUE self,
244     VALUE args)
245 {
246     return rrd_call(rrd_resize, args);
247 }
248
249 VALUE rb_rrd_restore(
250     VALUE self,
251     VALUE args)
252 {
253     return rrd_call(rrd_restore, args);
254 }
255
256 VALUE rb_rrd_tune(
257     VALUE self,
258     VALUE args)
259 {
260     return rrd_call(rrd_tune, args);
261 }
262
263 VALUE rb_rrd_update(
264     VALUE self,
265     VALUE args)
266 {
267     return rrd_call(rrd_update, args);
268 }
269
270 void Init_RRD(
271     )
272 {
273     mRRD = rb_define_module("RRD");
274     rb_eRRDError = rb_define_class("RRDError", rb_eStandardError);
275
276     rb_define_module_function(mRRD, "create", rb_rrd_create, -2);
277     rb_define_module_function(mRRD, "dump", rb_rrd_dump, -2);
278     rb_define_module_function(mRRD, "fetch", rb_rrd_fetch, -2);
279     rb_define_module_function(mRRD, "graph", rb_rrd_graph, -2);
280     rb_define_module_function(mRRD, "last", rb_rrd_last, -2);
281     rb_define_module_function(mRRD, "resize", rb_rrd_resize, -2);
282     rb_define_module_function(mRRD, "restore", rb_rrd_restore, -2);
283     rb_define_module_function(mRRD, "tune", rb_rrd_tune, -2);
284     rb_define_module_function(mRRD, "update", rb_rrd_update, -2);
285     rb_define_module_function(mRRD, "info", rb_rrd_info, -2);
286 }