http://bugs.ethereal.com/bugzilla/show_bug.cgi?id=377
[obnox/wireshark/wip.git] / tap-stats_tree.c
index 2c85abcd92ed3382e13ff0f2b3aa319d4094d436..c7996fd80d9f968a7d5f39d009c1fe603e2c0e14 100644 (file)
@@ -2,7 +2,7 @@
  * tethereal's tap implememntation of stats_tree
  * 2005, Luis E. G. Ontanon
  *
- * $Id$
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
 #include <stdio.h>
 #include <glib.h>
 #include <epan/stats_tree_priv.h>
+#include <epan/stat_cmd_args.h>
+#include <epan/report_err.h>
 
 /* actually unused */
 struct _st_node_pres {
        void* dummy;
 };
 
-
 struct _tree_pres {
+       void* dummy;
+};
+
+struct _tree_cfg_pres {
        guint8* init_string;    
 };
 
@@ -49,13 +54,13 @@ static void draw_stats_tree(void *psp) {
        stat_node* child;
        
        s = g_string_new("\n===================================================================\n");
-       fmt = g_strdup_printf(" %%s%%-%us%%12s\t%%12s\t%%12s\n",stats_branch_max_name_len(&st->root,0));
-       g_string_sprintfa(s,fmt,"",st->name,"value","rate","percent");
+       fmt = g_strdup_printf(" %%s%%-%us%%12s\t%%12s\t%%12s\n",stats_tree_branch_max_namelen(&st->root,0));
+       g_string_sprintfa(s,fmt,"",st->cfg->name,"value","rate","percent");
        g_free(fmt);
        g_string_sprintfa(s,"-------------------------------------------------------------------\n");
        
        for (child = st->root.children; child; child = child->next ) {
-               stat_branch_to_str(child,s,0);
+               stats_tree_branch_to_str(child,s,0);
        }
        
        s = g_string_append(s,"\n===================================================================\n");
@@ -64,60 +69,68 @@ static void draw_stats_tree(void *psp) {
        
 }
 
-static void  init_stats_tree(char *optarg) {
-       guint8* abbr = get_st_abbr(optarg);
+static void  init_stats_tree(const char *optarg) {
+       guint8* abbr = stats_tree_get_abbr(optarg);
        GString *error_string;
-       stats_tree *st = NULL;
-
+       stats_tree_cfg *cfg = NULL;
+       stats_tree* st = NULL;
+       
        if (abbr) {
-               st = get_stats_tree_by_abbr(abbr);
-               
-               if (st != NULL) {
-                       if (strncmp (optarg, st->pr->init_string, strlen(st->pr->init_string)) == 0){
-                               st->filter=((guint8*)optarg)+strlen(st->pr->init_string);
+               cfg = stats_tree_get_cfg_by_abbr(abbr);
+
+               if (cfg != NULL) {
+                       if (strncmp (optarg, cfg->pr->init_string, strlen(cfg->pr->init_string)) == 0){
+                               st = stats_tree_new(cfg,NULL,((guint8*)optarg)+strlen(cfg->pr->init_string));
                        } else {
                                st->filter=NULL;
                        }
                } else {
-                       g_error("no such stats_tree (%s) found in stats_tree registry",abbr);
+                       report_failure("no such stats_tree (%s) found in stats_tree registry",abbr);
+                       return;
                }
                
                g_free(abbr);
                
        } else {
-               g_error("could not obtain stats_tree abbr (%s) from optarg '%s'",abbr,optarg);          
+               report_failure("could not obtain stats_tree abbr (%s) from optarg '%s'",abbr,optarg);           
+               return;
        }
        
-       error_string = register_tap_listener( st->tapname,
+       error_string = register_tap_listener( st->cfg->tapname,
                                                                                  st,
                                                                                  st->filter,
-                                                                                 reset_stats_tree,
+                                                                                 stats_tree_reset,
                                                                                  stats_tree_packet,
                                                                                  draw_stats_tree);
        
        if (error_string) {
-               g_error("stats_tree for: %s failed to attach to the tap: %s",st->name,error_string->str);
+               report_failure("stats_tree for: %s failed to attach to the tap: %s",cfg->name,error_string->str);
+               return;
        }
 
-       if (st->init) st->init(st);
+       if (cfg->init) cfg->init(st);
 
 }
 
 void register_stats_tree_tap (gpointer k _U_, gpointer v, gpointer p _U_) {
-       stats_tree* st = v;
+       stats_tree_cfg* cfg = v;
        
-       st->pr = g_malloc(sizeof(tree_pres));
-       st->pr->init_string = g_strdup_printf("%s,tree",st->abbr);
+       cfg->pr = g_malloc(sizeof(tree_cfg_pres));
+       cfg->pr->init_string = g_strdup_printf("%s,tree",cfg->abbr);
 
-       register_ethereal_tap(st->pr->init_string, init_stats_tree);
+       register_stat_cmd_arg(cfg->pr->init_string, init_stats_tree);
        
 }
 
+static void free_tree_presentation(stats_tree* st) {
+       g_free(st->pr);
+}
+
 
 void
 register_tap_listener_stats_tree_stat(void)
 {
        stats_tree_presentation(register_stats_tree_tap,
                                                        NULL, NULL, NULL, NULL, NULL,
-                                                       NULL, NULL, NULL, NULL);
+                                                       free_tree_presentation, NULL, NULL, NULL);
 }