We always HAVE_CONFIG_H so don't bother checking whether we have it or not.
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
27  */
28
29 #include "config.h"
30
31 #include <glib.h>
32 #include "ex-opt.h"
33
34 static GHashTable* ex_opts = NULL;
35
36 gboolean ex_opt_add(const gchar* optarg) {
37     gchar** splitted;
38     
39     if (!ex_opts) 
40         ex_opts = g_hash_table_new(g_str_hash,g_str_equal);
41     
42     splitted = g_strsplit(optarg,":",2);
43     
44     if (splitted[0] && splitted[1]) {
45         GPtrArray* this_opts = g_hash_table_lookup(ex_opts,splitted[0]);
46         
47         if (this_opts) {
48             g_ptr_array_add(this_opts,splitted[1]);
49             g_free(splitted[0]);
50         } else {
51             this_opts = g_ptr_array_new();
52             g_ptr_array_add(this_opts,splitted[1]);
53             g_hash_table_insert(ex_opts,splitted[0],this_opts);
54         }
55         
56         g_free(splitted);
57         
58         return TRUE;
59     } else {
60         g_strfreev(splitted);
61         return FALSE;
62     }
63 }
64
65 gint ex_opt_count(const gchar* key) {
66     GPtrArray* this_opts;
67     
68     if (! ex_opts)
69         return 0;
70     
71     this_opts = g_hash_table_lookup(ex_opts,key);
72     
73     if (this_opts) {
74         return this_opts->len;
75     } else {
76         return 0;
77     }
78 }
79
80 const gchar* ex_opt_get_nth(const gchar* key, guint index) {
81     GPtrArray* this_opts;
82     
83     if (! ex_opts)
84         return 0;
85     
86     this_opts = g_hash_table_lookup(ex_opts,key);
87     
88     if (this_opts) {
89         if (this_opts->len > index) {
90             return g_ptr_array_index(this_opts,index);
91         } else {
92             /* XXX: assert? */
93             return NULL;
94         }
95     } else {
96         return NULL;
97     }
98     
99 }
100
101 extern const gchar* ex_opt_get_next(const gchar* key) {
102     GPtrArray* this_opts;
103     
104     if (! ex_opts)
105         return 0;
106     
107     this_opts = g_hash_table_lookup(ex_opts,key);
108     
109     if (this_opts) {
110         if (this_opts->len)
111             return g_ptr_array_remove_index(this_opts,0);
112         else 
113             return NULL;
114     } else {
115         return NULL;
116     }
117 }
118