luis.ontanon@gmail.com => luis@ontanon.org
[obnox/wireshark/wip.git] / epan / ex-opt.c
1 /*
2  *  ex-opt.c
3  *  
4  * Extension command line options
5  *
6  * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <glib.h>
34 #include "ex-opt.h"
35
36 static GHashTable* ex_opts = NULL;
37
38 gboolean ex_opt_add(const gchar* optarg) {
39     gchar** splitted;
40     
41     if (!ex_opts) 
42         ex_opts = g_hash_table_new(g_str_hash,g_str_equal);
43     
44     splitted = g_strsplit(optarg,":",2);
45     
46     if (splitted[0] && splitted[1]) {
47         GPtrArray* this_opts = g_hash_table_lookup(ex_opts,splitted[0]);
48         
49         if (this_opts) {
50             g_ptr_array_add(this_opts,splitted[1]);
51             g_free(splitted[0]);
52         } else {
53             this_opts = g_ptr_array_new();
54             g_ptr_array_add(this_opts,splitted[1]);
55             g_hash_table_insert(ex_opts,splitted[0],this_opts);
56         }
57         
58         g_free(splitted);
59         
60         return TRUE;
61     } else {
62         g_strfreev(splitted);
63         return FALSE;
64     }
65 }
66
67 gint ex_opt_count(const gchar* key) {
68     GPtrArray* this_opts;
69     
70     if (! ex_opts)
71         return 0;
72     
73     this_opts = g_hash_table_lookup(ex_opts,key);
74     
75     if (this_opts) {
76         return this_opts->len;
77     } else {
78         return 0;
79     }
80 }
81
82 const gchar* ex_opt_get_nth(const gchar* key, guint index) {
83     GPtrArray* this_opts;
84     
85     if (! ex_opts)
86         return 0;
87     
88     this_opts = g_hash_table_lookup(ex_opts,key);
89     
90     if (this_opts) {
91         if (this_opts->len > index) {
92             return g_ptr_array_index(this_opts,index);
93         } else {
94             /* XXX: assert? */
95             return NULL;
96         }
97     } else {
98         return NULL;
99     }
100     
101 }
102
103 extern const gchar* ex_opt_get_next(const gchar* key) {
104     GPtrArray* this_opts;
105     
106     if (! ex_opts)
107         return 0;
108     
109     this_opts = g_hash_table_lookup(ex_opts,key);
110     
111     if (this_opts) {
112         if (this_opts->len)
113             return g_ptr_array_remove_index(this_opts,0);
114         else 
115             return NULL;
116     } else {
117         return NULL;
118     }
119 }
120