As suggested by Anders: back out 37112.
[obnox/wireshark/wip.git] / epan / stats_tree.c
index 57747dea68ff42a35f22a196c377c79c232749cc..7344cf6091ab18664e64f7d11874f67004a11216 100644 (file)
@@ -7,17 +7,17 @@
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
@@ -34,9 +34,9 @@
 #include "stats_tree.h"
 
 /*
-TODO: 
+TODO:
    - sort out the sorting issue
+
  */
 
 /* used to contain the registered stat trees */
@@ -48,9 +48,9 @@ extern void
 stats_tree_get_strs_from_node(const stat_node *node, gchar *value, gchar *rate, gchar *percent)
 {
        float f;
-       
+
        if (value) g_snprintf(value,NUM_BUF_SIZE,"%u",node->counter);
-       
+
        if (rate) {
                *rate = '\0';
                if (node->st->elapsed > 0.0) {
@@ -58,7 +58,7 @@ stats_tree_get_strs_from_node(const stat_node *node, gchar *value, gchar *rate,
                        g_snprintf(rate,NUM_BUF_SIZE,"%f",f);
                }
        }
-       
+
        if (percent) {
                *percent = '\0';
                if (node->parent->counter > 0) {
@@ -88,19 +88,19 @@ stats_tree_branch_max_namelen(const stat_node *node, guint indent)
        stat_node *child;
        guint maxlen = 0;
        guint len;
-       
+
        indent = indent > INDENT_MAX ? INDENT_MAX : indent;
 
        if (node->children) {
                for (child = node->children; child; child = child->next ) {
-                       len = stats_tree_branch_max_namelen(child,indent+1); 
+                       len = stats_tree_branch_max_namelen(child,indent+1);
                        maxlen = len > maxlen ? len : maxlen;
                }
        }
-       
-       len = strlen(node->name) + indent;
+
+       len = (guint) strlen(node->name) + indent;
        maxlen = len > maxlen ? len : maxlen;
-       
+
        return maxlen;
 }
 
@@ -116,34 +116,34 @@ stats_tree_branch_to_str(const stat_node *node, GString *s, guint indent)
        static gchar value[NUM_BUF_SIZE];
        static gchar rate[NUM_BUF_SIZE];
        static gchar percent[NUM_BUF_SIZE];
-       
+
        guint i = 0;
-       
+
        if (indent == 0) {
                format = g_strdup_printf(" %%s%%-%us%%12s   %%12s    %%12s\n",stats_tree_branch_max_namelen(node,0));
        }
-       
+
        stats_tree_get_strs_from_node(node, value, rate, percent);
-       
+
        indent = indent > INDENT_MAX ? INDENT_MAX : indent;
-       
+
        /* fill indentation with indent spaces */
        if (indent > 0) {
                while(i<indent)
                        indentation[i++] = ' ';
        }
-       
-       indentation[i++] = '\0';
-       
+
+       indentation[i] = '\0';
+
        g_string_append_printf(s,format,
                                          indentation,node->name,value,rate,percent);
-               
+
        if (node->children) {
                for (child = node->children; child; child = child->next ) {
                        stats_tree_branch_to_str(child,s,indent+1);
                }
        }
-       
+
        if (indent == 0) {
                g_free(format);
        }
@@ -156,7 +156,7 @@ free_stat_node(stat_node *node)
 {
        stat_node *child;
        stat_node *next;
-       
+
        if (node->children) {
         for (child = node->children; child; child = next ) {
             /* child->next will be gone after free_stat_node, so cache it here */
@@ -164,15 +164,13 @@ free_stat_node(stat_node *node)
                        free_stat_node(child);
         }
        }
-       
+
        if(node->st->cfg->free_node_pr) node->st->cfg->free_node_pr(node);
-       
+
        if (node->hash) g_hash_table_destroy(node->hash);
 
-       if (node->rng) g_free(node->rng);
-       
-       if (node->name) g_free(node->name);
-       
+       g_free(node->rng);
+       g_free(node->name);
        g_free(node);
 }
 
@@ -182,23 +180,23 @@ stats_tree_free(stats_tree *st)
 {
        stat_node *child;
        stat_node *next;
-       
+
        g_free(st->filter);
        g_hash_table_destroy(st->names);
        g_ptr_array_free(st->parents,TRUE);
-       
+
        for (child = st->root.children; child; child = next ) {
                /* child->next will be gone after free_stat_node, so cache it here */
                next = child->next;
                free_stat_node(child);
        }
-       
+
        if (st->cfg->free_tree_pr)
                st->cfg->free_tree_pr(st);
-       
+
        if (st->cfg->cleanup)
                st->cfg->cleanup(st);
-       
+
        g_free(st);
 }
 
@@ -208,18 +206,18 @@ static void
 reset_stat_node(stat_node *node)
 {
        stat_node *child;
-       
+
        if (node->children) {
-               for (child = node->children; child; child = child->next ) 
+               for (child = node->children; child; child = child->next )
                        reset_stat_node(child);
        }
-       
+
        node->counter = 0;
-       
+
        if(node->st->cfg->reset_node) {
                node->st->cfg->reset_node(node);
        }
-       
+
 }
 
 /* reset the whole stats_tree */
@@ -227,12 +225,12 @@ extern void
 stats_tree_reset(void *p)
 {
        stats_tree *st = p;
-    
+
        st->start = -1.0;
        st->elapsed = 0.0;
-    
+
        reset_stat_node(&st->root);
-       
+
        if (st->cfg->reset_tree) {
                st->cfg->reset_tree(st);
        }
@@ -244,16 +242,16 @@ stats_tree_reinit(void *p)
        stats_tree *st = p;
        stat_node *child;
        stat_node *next;
-       
+
        for (child = st->root.children; child; child = next) {
         /* child->next will be gone after free_stat_node, so cache it here */
         next = child->next;
                free_stat_node(child);
        }
-       
+
        st->root.children = NULL;
        st->root.counter = 0;
-       
+
        if (st->cfg->init) {
                st->cfg->init(st);
        }
@@ -261,24 +259,28 @@ stats_tree_reinit(void *p)
 
 /* register a new stats_tree */
 extern void
-stats_tree_register(const char *tapname, const char *abbr, const char *name,
+stats_tree_register_with_group(const char *tapname, const char *abbr, const char *name,
+                   guint flags,
                    stat_tree_packet_cb packet, stat_tree_init_cb init,
-                   stat_tree_cleanup_cb cleanup)
+                   stat_tree_cleanup_cb cleanup, register_stat_group_t stat_group)
 {
-       
+
        stats_tree_cfg *cfg = g_malloc( sizeof(stats_tree_cfg) );
 
-       /* at the very least the abbrev and the packet function should be given */ 
+       /* at the very least the abbrev and the packet function should be given */
        g_assert( tapname && abbr && packet );
 
        cfg->tapname = g_strdup(tapname);
        cfg->abbr = g_strdup(abbr);
        cfg->name = name ? g_strdup(name) : g_strdup(abbr);
-       
+       cfg->stat_group = stat_group;
+
        cfg->packet = packet;
        cfg->init = init;
        cfg->cleanup = cleanup;
-       
+
+       cfg->flags = flags;
+
        /* these have to be filled in by implementations */
        cfg->setup_node_pr = NULL;
        cfg->new_tree_pr = NULL;
@@ -292,11 +294,24 @@ stats_tree_register(const char *tapname, const char *abbr, const char *name,
        if (!registry) registry = g_hash_table_new(g_str_hash,g_str_equal);
 
        g_hash_table_insert(registry,cfg->abbr,cfg);
-       
+
+}
+
+/* register a new stats_tree with default group REGISTER_STAT_GROUP_UNSORTED */
+extern void
+stats_tree_register(const char *tapname, const char *abbr, const char *name,
+                   guint flags,
+                   stat_tree_packet_cb packet, stat_tree_init_cb init,
+                   stat_tree_cleanup_cb cleanup)
+{
+       stats_tree_register_with_group(tapname, abbr, name,
+                   flags,
+                   packet, init,
+                   cleanup, REGISTER_STAT_GROUP_UNSORTED);
 }
 
 extern stats_tree*
-stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, char *filter)
+stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, const char *filter)
 {
        stats_tree *st = g_malloc(sizeof(stats_tree));
 
@@ -306,7 +321,7 @@ stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, char *filter)
        st->names = g_hash_table_new(g_str_hash,g_str_equal);
        st->parents = g_ptr_array_new();
        st->filter = g_strdup(filter);
-       
+
        st->start = -1.0;
        st->elapsed = 0.0;
 
@@ -318,11 +333,11 @@ stats_tree_new(stats_tree_cfg *cfg, tree_pres *pr, char *filter)
        st->root.next = NULL;
        st->root.hash = NULL;
        st->root.pr = NULL;
-       
+
        g_ptr_array_add(st->parents,&st->root);
-       
+
        return st;
-}      
+}
 
 /* will be the tap packet cb */
 extern int
@@ -330,11 +345,11 @@ stats_tree_packet(void *p, packet_info *pinfo, epan_dissect_t *edt, const void *
 {
        stats_tree *st = p;
        double now = nstime_to_msec(&pinfo->fd->rel_ts);
-       
+
        if (st->start < 0.0) st->start = now;
-       
+
        st->elapsed = now - st->start;
-       
+
        if (st->cfg->packet)
                return st->cfg->packet(st,pinfo,edt,pri);
        else
@@ -364,7 +379,7 @@ setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
 {
        stats_tree_cfg *cfg = v;
        struct _stats_tree_pres_cbs *d = p;
-       
+
        cfg->in_use = FALSE;
        cfg->setup_node_pr = d->setup_node_pr;
        cfg->new_tree_pr = d->new_tree_pr;
@@ -374,7 +389,7 @@ setup_tree_presentation(gpointer k _U_, gpointer v, gpointer p)
        cfg->draw_tree = d->draw_tree;
        cfg->reset_node = d->reset_node;
        cfg->reset_tree = d->reset_tree;
-       
+
 }
 
 extern void
@@ -390,7 +405,7 @@ stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
                        void *data)
 {
        static struct _stats_tree_pres_cbs d;
-       
+
        d.setup_node_pr = setup_node_pr;
        d.new_tree_pr = new_tree_pr;
        d.free_node_pr = free_node_pr;
@@ -399,12 +414,12 @@ stats_tree_presentation(void (*registry_iterator)(gpointer,gpointer,gpointer),
        d.draw_tree = draw_tree;
        d.reset_node = reset_node;
        d.reset_tree = reset_tree;
-       
+
        if (registry) g_hash_table_foreach(registry,setup_tree_presentation,&d);
-       
+
        if (registry_iterator && registry)
                g_hash_table_foreach(registry,registry_iterator,data);
-       
+
 }
 
 
@@ -421,7 +436,7 @@ new_stat_node(stats_tree *st, const gchar *name, int parent_id,
 
        stat_node *node = g_malloc (sizeof(stat_node));
        stat_node *last_chld = NULL;
-       
+
        node->counter = 0;
        node->name = g_strdup(name);
        node->children = NULL;
@@ -435,45 +450,45 @@ new_stat_node(stats_tree *st, const gchar *name, int parent_id,
                g_hash_table_insert(st->names,
                                                        node->name,
                                                        node);
-               
+
                g_ptr_array_add(st->parents,node);
-               
+
                node->id = st->parents->len - 1;
        } else {
                node->id = -1;
        }
-       
+
        if (parent_id >= 0 && parent_id < (int) st->parents->len ) {
                node->parent = g_ptr_array_index(st->parents,parent_id);
        } else {
                /* ??? should we set the parent to be root ??? */
                g_assert_not_reached();
        }
-       
+
        if (node->parent->children) {
                /* insert as last child */
-               
+
                for (last_chld = node->parent->children;
                         last_chld->next;
                         last_chld = last_chld->next ) ;
-               
+
                last_chld->next = node;
-               
+
        } else {
                /* insert as first child */
                node->parent->children = node;
        }
-       
+
        if(node->parent->hash) {
                g_hash_table_insert(node->parent->hash,node->name,node);
        }
-       
+
        if (st->cfg->setup_node_pr) {
                st->cfg->setup_node_pr(node);
        } else {
                node->pr = NULL;
        }
-       
+
        return node;
 }
 /***/
@@ -482,8 +497,8 @@ extern int
 stats_tree_create_node(stats_tree *st, const gchar *name, int parent_id, gboolean with_hash)
 {
        stat_node *node = new_stat_node(st,name,parent_id,with_hash,TRUE);
-       
-       if (node) 
+
+       if (node)
                return node->id;
        else
                return 0;
@@ -511,26 +526,26 @@ stats_tree_manip_node(manip_node_mode mode, stats_tree *st, const char *name,
 {
        stat_node *node = NULL;
        stat_node *parent = NULL;
-       
+
        g_assert( parent_id >= 0 && parent_id < (int) st->parents->len );
-       
+
        parent = g_ptr_array_index(st->parents,parent_id);
-       
+
        if( parent->hash ) {
                node = g_hash_table_lookup(parent->hash,name);
        } else {
                node = g_hash_table_lookup(st->names,name);
        }
-       
-       if ( node == NULL ) 
+
+       if ( node == NULL )
                node = new_stat_node(st,name,parent_id,with_hash,with_hash);
-       
+
        switch (mode) {
                case MN_INCREASE: node->counter += value; break;
                case MN_SET: node->counter = value; break;
        }
-       
-       if (node) 
+
+       if (node)
                return node->id;
        else
                return -1;
@@ -545,9 +560,9 @@ stats_tree_get_abbr(const char *optarg)
        /* XXX: this fails when tshark is given any options
           after the -z */
        g_assert(optarg != NULL);
-       
+
        for (i=0; optarg[i] && optarg[i] != ','; i++);
-       
+
        if (optarg[i] == ',') {
                return g_strndup(optarg,i);
        } else {
@@ -586,7 +601,7 @@ get_range(char *rngstr)
           return NULL;
         }
 
-        /* means we have a non empty string 
+        /* means we have a non empty string
          * which does not contain a delimiter */
         if (split[1] == NULL) {
           g_strfreev(split);
@@ -638,7 +653,7 @@ extern int
 stats_tree_parent_id_by_name(stats_tree *st, const gchar *parent_name)
 {
        stat_node *node = g_hash_table_lookup(st->names,parent_name);
-       
+
        if (node)
                return node->id;
        else
@@ -662,46 +677,46 @@ stats_tree_range_node_with_pname(stats_tree *st, const gchar *name,
                range_node->rng = get_range(curr_range);
        }
        va_end( list );
-       
+
        return rng_root->id;
-}      
+}
 
 
 extern int
 stats_tree_tick_range(stats_tree *st, const gchar *name, int parent_id,
                      int value_in_range)
 {
-       
+
        stat_node *node = NULL;
        stat_node *parent = NULL;
        stat_node *child = NULL;
        gint floor, ceil;
-       
+
        if (parent_id >= 0 && parent_id < (int) st->parents->len) {
                parent = g_ptr_array_index(st->parents,parent_id);
        } else {
                g_assert_not_reached();
        }
-       
+
        if( parent->hash ) {
                node = g_hash_table_lookup(parent->hash,name);
        } else {
                node = g_hash_table_lookup(st->names,name);
        }
-       
-       if ( node == NULL ) 
+
+       if ( node == NULL )
                g_assert_not_reached();
-       
+
        for ( child = node->children; child; child = child->next) {
                floor =  child->rng->floor;
                ceil = child->rng->ceil;
-               
+
                if ( value_in_range >= floor && value_in_range <= ceil ) {
                        child->counter++;
                        return node->id;
                }
        }
-       
+
        return node->id;
 }
 
@@ -709,8 +724,8 @@ extern int
 stats_tree_create_pivot(stats_tree *st, const gchar *name, int parent_id)
 {
        stat_node *node = new_stat_node(st,name,parent_id,TRUE,TRUE);
-       
-       if (node) 
+
+       if (node)
                return node->id;
        else
                return 0;
@@ -722,10 +737,10 @@ stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
 {
        int parent_id = stats_tree_parent_id_by_name(st,parent_name);
        stat_node *node;
-       
+
        node = new_stat_node(st,name,parent_id,TRUE,TRUE);
-       
-       if (node) 
+
+       if (node)
                return node->id;
        else
                return 0;
@@ -734,12 +749,12 @@ stats_tree_create_pivot_by_pname(stats_tree *st, const gchar *name,
 extern int
 stats_tree_tick_pivot(stats_tree *st, int pivot_id, const gchar *pivot_value)
 {
-       
+
        stat_node *parent = g_ptr_array_index(st->parents,pivot_id);
-       
+
        parent->counter++;
        stats_tree_manip_node( MN_INCREASE, st, pivot_value, pivot_id, FALSE, 1);
-       
+
        return pivot_id;
 }