From bdd9ce2f4b4fc04f66fe96da300cc10d8f4222ee Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 9 Jun 2010 15:56:12 +0200 Subject: [PATCH] graph_list: Implement better iterator functions. --- action_list_graphs.c | 40 +++++++++++------------ graph_list.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++---- graph_list.h | 36 ++++++++++++++++++++- 3 files changed, 136 insertions(+), 29 deletions(-) diff --git a/action_list_graphs.c b/action_list_graphs.c index 14a13bb..16d5cc4 100644 --- a/action_list_graphs.c +++ b/action_list_graphs.c @@ -43,33 +43,29 @@ static int print_graph_json (const graph_list_t *gl, void *user_data) /* {{{ */ return (0); } /* }}} int print_graph_json */ -static int print_graph_html (const graph_list_t *gl, - void __attribute__((unused)) *user_data) +static int print_graph_inst_html (__attribute__((unused)) graph_config_t *cfg, /* {{{ */ + graph_instance_t *inst, + __attribute__((unused)) void *user_data) { - if (gl == NULL) - return (EINVAL); + char buffer[1024]; - printf ("
  • "); + memset (buffer, 0, sizeof (buffer)); + gl_instance_get_ident (inst, buffer, sizeof (buffer)); - printf ("host, gl->plugin); - if (gl->plugin_instance != NULL) - printf ("plugin_instance=%s;", gl->plugin_instance); - printf ("type=%s;", gl->type); - if (gl->type_instance != NULL) - printf ("type_instance=%s;", gl->type_instance); - printf ("\">"); + printf ("
  • %s
  • \n", buffer); - printf ("%s/%s", gl->host, gl->plugin); - if (gl->plugin_instance != NULL) - printf ("-%s", gl->plugin_instance); - printf ("/%s", gl->type); - if (gl->type_instance != NULL) - printf ("-%s", gl->type_instance); - printf ("\n"); + return (0); +} /* }}} int print_graph_inst_html */ + +static int print_graph_html (graph_config_t *cfg, /* {{{ */ + __attribute__((unused)) void *user_data) +{ + printf ("
  • %p\n\n"); return (0); -} +} /* }}} int print_graph_html */ static int list_graphs_json (void) /* {{{ */ { @@ -89,7 +85,7 @@ static int list_graphs_html (void) /* {{{ */ printf ("Content-Type: text/html\n\n"); printf ("\n"); return (0); diff --git a/graph_list.c b/graph_list.c index 0e4428d..e998785 100644 --- a/graph_list.c +++ b/graph_list.c @@ -31,8 +31,6 @@ struct graph_ident_s /* {{{ */ char *type_instance; }; /* }}} struct graph_ident_s */ -struct graph_instance_s; -typedef struct graph_instance_s graph_instance_t; struct graph_instance_s /* {{{ */ { graph_ident_t select; @@ -43,8 +41,6 @@ struct graph_instance_s /* {{{ */ graph_instance_t *next; }; /* }}} struct graph_instance_s */ -struct graph_config_s; -typedef struct graph_config_s graph_config_t; struct graph_config_s /* {{{ */ { graph_ident_t select; @@ -69,6 +65,8 @@ static time_t gl_last_update = 0; /* * Private functions */ +/* FIXME: These "print_*" functions are used for debugging. They should be + * removed at some point. */ static int print_files (const graph_instance_t *inst) /* {{{ */ { size_t i; @@ -120,7 +118,6 @@ static int print_graphs (void) /* {{{ */ return (0); } /* }}} int print_graphs */ - /* "Safe" version of strcmp(3): Either or both pointers may be NULL. */ static int strcmp_s (const char *s1, const char *s2) /* {{{ */ { @@ -418,6 +415,7 @@ static int FIXME_graph_create_from_file (const graph_ident_t *file) /* {{{ */ return (0); } /* }}} int FIXME_graph_create_from_file */ +/* FIXME: Actually read the config file here. */ static int read_graph_config (void) /* {{{ */ { if (graph_config_head != NULL) @@ -676,13 +674,92 @@ static int callback_host (const char *host, void *user_data) /* {{{ */ /* * Global functions */ +int gl_instance_get_ident (graph_instance_t *inst, /* {{{ */ + char *buffer, size_t buffer_size) +{ + if ((inst == NULL) || (buffer == NULL) || (buffer_size < 1)) + return (EINVAL); + + snprintf (buffer, buffer_size, "%s/%s-%s/%s-%s", + inst->select.host, + inst->select.plugin, inst->select.plugin_instance, + inst->select.type, inst->select.type_instance); + buffer[buffer_size - 1] = 0; + + return (0); +} /* }}} int gl_instance_get_ident */ + +int gl_graph_get_all (gl_cfg_callback callback, /* {{{ */ + void *user_data) +{ + graph_config_t *cfg; + + if (callback == NULL) + return (EINVAL); + + for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next) + { + int status; + + status = (*callback) (cfg, user_data); + if (status != 0) + return (status); + } + + return (0); +} /* }}} int gl_graph_get_all */ + +int gl_graph_instance_get_all (graph_config_t *cfg, + gl_inst_callback callback, void *user_data) +{ + graph_instance_t *inst; + + if ((cfg == NULL) || (callback == NULL)) + return (EINVAL); + + for (inst = cfg->instances; inst != NULL; inst = inst->next) + { + int status; + + status = (*callback) (cfg, inst, user_data); + if (status != 0) + return (status); + } + + return (0); +} /* }}} int gl_graph_instance_get_all */ + +int gl_instance_get_all (gl_inst_callback callback, /* {{{ */ + void *user_data) +{ + graph_config_t *cfg; + + for (cfg = graph_config_head; cfg != NULL; cfg = cfg->next) + { + graph_instance_t *inst; + + for (inst = cfg->instances; inst != NULL; inst = inst->next) + { + int status; + + status = (*callback) (cfg, inst, user_data); + if (status != 0) + return (status); + } + } + + return (0); +} /* }}} int gl_instance_get_all */ + int gl_update (void) /* {{{ */ { time_t now; graph_ident_t gl; int status; + /* printf ("Content-Type: text/plain\n\n"); + */ read_graph_config (); @@ -702,7 +779,7 @@ int gl_update (void) /* {{{ */ status = foreach_host (callback_host, &gl); - print_graphs (); + /* print_graphs (); */ if (graph_list_length > 1) qsort (graph_list, graph_list_length, sizeof (*graph_list), gl_compare); diff --git a/graph_list.h b/graph_list.h index 3669e02..605571e 100644 --- a/graph_list.h +++ b/graph_list.h @@ -4,6 +4,39 @@ struct graph_ident_s; typedef struct graph_ident_s graph_ident_t; +struct graph_instance_s; +typedef struct graph_instance_s graph_instance_t; + +struct graph_config_s; +typedef struct graph_config_s graph_config_t; + +/* + * Callback types + */ +typedef int (*gl_cfg_callback) (graph_config_t *cfg, + void *user_data); + +typedef int (*gl_inst_callback) (graph_config_t *cfg, + graph_instance_t *inst, void *user_data); + +/* + * Functions + */ +int gl_graph_get_all (gl_cfg_callback callback, + void *user_data); + +int gl_graph_instance_get_all (graph_config_t *cfg, + gl_inst_callback callback, void *user_data); + +int gl_instance_get_all (gl_inst_callback callback, + void *user_data); + +int gl_instance_get_ident (graph_instance_t *inst, + char *buffer, size_t buffer_size); + + + + struct graph_list_s { char *host; @@ -14,7 +47,8 @@ struct graph_list_s }; typedef struct graph_list_s graph_list_t; -typedef int (*gl_callback) (const graph_list_t *, void *); +typedef int (*gl_callback) ( + const graph_list_t *, void *user_data); int gl_update (void); int gl_foreach (gl_callback callback, void *user_data); -- 2.11.0