From: Florian Forster Date: Wed, 8 Sep 2010 08:44:54 +0000 (+0200) Subject: src/graph_def.[ch]: Implement "def_to_json". X-Git-Tag: v4.0.0~46 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=860cbdf33b4e6a928c0f3153ed57539a2750e9d6;p=collection4.git src/graph_def.[ch]: Implement "def_to_json". --- diff --git a/src/graph_def.c b/src/graph_def.c index b358905..7ac8dac 100644 --- a/src/graph_def.c +++ b/src/graph_def.c @@ -148,6 +148,42 @@ static graph_def_t *def_config_get_obj (graph_config_t *cfg, /* {{{ */ return (def); } /* }}} graph_def_t *def_config_get_obj */ +static int def_to_json_recursive (const graph_def_t *def, /* {{{ */ + yajl_gen handler) +{ + char color[16]; + + if (def == NULL) + return (0); + + snprintf (color, sizeof (color), "%06"PRIx32, def->color); + color[sizeof (color) - 1] = 0; + + yajl_gen_map_open (handler); + +#define yajl_gen_string_cast(h,p,l) \ + yajl_gen_string (h, (unsigned char *) p, (unsigned int) l) + + yajl_gen_string_cast (handler, "select", strlen ("select")); + ident_to_json (def->select, handler); + yajl_gen_string_cast (handler, "ds_name", strlen ("ds_name")); + yajl_gen_string_cast (handler, def->ds_name, strlen (def->ds_name)); + yajl_gen_string_cast (handler, "legend", strlen ("legend")); + yajl_gen_string_cast (handler, def->legend, strlen (def->legend)); + yajl_gen_string_cast (handler, "color", strlen ("color")); + yajl_gen_string_cast (handler, color, strlen (color)); + yajl_gen_string_cast (handler, "stack", strlen ("stack")); + yajl_gen_bool (handler, def->stack); + yajl_gen_string_cast (handler, "area", strlen ("area")); + yajl_gen_bool (handler, def->area); + yajl_gen_string_cast (handler, "format", strlen ("format")); + yajl_gen_string_cast (handler, def->format, strlen (def->format)); + + yajl_gen_map_close (handler); + + return (def_to_json_recursive (def->next, handler)); +} /* }}} int def_to_json_recursive */ + /* * Public functions */ @@ -421,4 +457,17 @@ int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */ return (0); } /* }}} int def_get_rrdargs */ +int def_to_json (const graph_def_t *def, /* {{{ */ + yajl_gen handler) +{ + if (handler == NULL) + return (EINVAL); + + yajl_gen_array_open (handler); + def_to_json_recursive (def, handler); + yajl_gen_array_close (handler); + + return (0); +} /* }}} int def_to_json */ + /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/graph_def.h b/src/graph_def.h index 7c3e9fe..ed9a24c 100644 --- a/src/graph_def.h +++ b/src/graph_def.h @@ -24,6 +24,8 @@ #ifndef GRAPH_DEF_H #define GRAPH_DEF_H 1 +#include + #include "graph_types.h" #include "utils_array.h" #include "oconfig.h" @@ -48,5 +50,7 @@ int def_foreach (graph_def_t *def, def_callback_t callback, void *user_data); int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, rrd_args_t *args); +int def_to_json (const graph_def_t *def, yajl_gen handler); + /* vim: set sw=2 sts=2 et fdm=marker : */ #endif