558ad2e66075749a73c745af2d7845ce2e145180
[obnox/wireshark/wip.git] / tap-funnel.c
1 /*
2  *  tap-funnel.c
3  *  
4  */
5
6
7 #include <epan/funnel.h>
8 #include <stdio.h>
9 #include <epan/stat_cmd_args.h>
10
11
12 struct _funnel_text_window_t {
13     gchar* title;
14     GString* text;
15 };
16
17 static GPtrArray* text_windows = NULL;
18
19 static funnel_text_window_t* new_text_window(const gchar* title) {
20     funnel_text_window_t* tw = g_malloc(sizeof(funnel_text_window_t));
21     tw->title = g_strdup(title);
22     tw->text = g_string_new("");
23     
24     if (!text_windows)
25         text_windows = g_ptr_array_new();
26     
27     g_ptr_array_add(text_windows,tw);
28     
29     return tw;
30 }
31
32 static void text_window_clear(funnel_text_window_t*  tw) {
33     g_string_free(tw->text,TRUE);
34     tw->text = g_string_new("");    
35 }
36
37 static void text_window_append(funnel_text_window_t*  tw, const char *text ) {
38     g_string_append(tw->text,text);
39 }
40
41 static void text_window_set_text(funnel_text_window_t*  tw, const char* text) {
42     g_string_free(tw->text,TRUE);
43     tw->text = g_string_new(text);
44 }
45
46 static void text_window_prepend(funnel_text_window_t*  tw, const char *text) {
47     g_string_prepend(tw->text,text);    
48 }
49
50 static const gchar* text_window_get_text(funnel_text_window_t*  tw) {
51     return tw->text->str;
52 }
53
54 /* XXX: finish this */
55 static void funnel_logger(const gchar *log_domain _U_,
56                           GLogLevelFlags log_level _U_,
57                           const gchar *message,
58                           gpointer user_data _U_) {
59     fputs(message,stderr);
60 }
61
62
63
64 static const funnel_ops_t funnel_ops = {
65     new_text_window,
66     text_window_set_text,
67     text_window_append,
68     text_window_prepend,
69     text_window_clear,
70     text_window_get_text,
71     NULL,
72     NULL,
73     /*...,*/
74     NULL,
75     funnel_logger,
76         NULL
77 };
78
79
80 void initialize_funnel_ops(void) {
81     funnel_set_funnel_ops(&funnel_ops);
82 }
83
84
85 void funnel_dump_all_text_windows(void) {
86     guint i;
87     
88     if (!text_windows) return;
89     
90     for ( i = 0 ; i < text_windows->len; i++) {
91         funnel_text_window_t*  tw = g_ptr_array_index(text_windows,i);
92         printf("\n========================== %s "
93                "==========================\n%s\n",tw->title,tw->text->str);
94         
95         g_ptr_array_remove_index(text_windows,i);
96         g_free(tw->title);
97         g_string_free(tw->text,TRUE);
98         g_free(tw);        
99     }
100 }
101
102 #if 0
103
104 GHashTable* menus = NULL;;
105 typedef struct _menu_cb_t {
106     void (*callback)(gpointer);
107     void* callback_data;
108 } menu_cb_t;
109
110
111 static void  init_funnel_cmd(const char *optarg, void* data ) {
112     gchar** args = g_strsplit(optarg,",",0); 
113     gchar** arg;
114     menu_cb_t* mcb = data;
115     
116     for(arg = args; *arg ; arg++) {
117         g_strstrip(*arg);
118     }
119     
120     if (mcb->callback) {
121         mcb->callback(mcb->callback_data);
122     }
123     
124 }
125
126 static void register_menu_cb(const char *name,
127                              REGISTER_STAT_GROUP_E group _U_,
128                              void (*callback)(gpointer),
129                              gpointer callback_data,
130                              gboolean retap _U_) {
131     menu_cb_t* mcb = g_malloc(sizeof(menu_cb_t));
132     
133     mcb->callback = callback;
134     mcb->callback_data = callback_data;
135
136     if (!menus)
137         menus = g_hash_table_new(g_str_hash,g_str_equal);
138
139     g_hash_table_insert(menus,g_strdup(name),mcb);
140     
141     register_stat_cmd_arg(name,init_funnel_cmd,mcb);
142 }
143
144 void initialize_funnel_ops(void) {
145     funnel_set_funnel_ops(&funnel_ops);
146 }
147
148 #endif
149 void
150 register_tap_listener_gtkfunnel(void)
151 {
152 #if 0
153     funnel_register_all_menus(register_menu_cb);
154 #endif
155 }