1 /*
2  *  ex-opt.c
3  *
4  * Extension command line options
5  *
6  * (c) 2006, Luis E. Garcia Ontanon <luis@ontanon.org>
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * SPDX-License-Identifier: GPL-2.0-or-later
13  */
14 
15 #include "config.h"
16 
17 #include <glib.h>
18 #include "ex-opt.h"
19 
20 static GHashTable* ex_opts = NULL;
21 
ex_opt_add(const gchar * ws_optarg)22 gboolean ex_opt_add(const gchar* ws_optarg) {
23     gchar** splitted;
24 
25     if (!ex_opts)
26         ex_opts = g_hash_table_new(g_str_hash,g_str_equal);
27 
28     splitted = g_strsplit(ws_optarg,":",2);
29 
30     if (splitted[0] && splitted[1]) {
31         GPtrArray* this_opts = (GPtrArray *)g_hash_table_lookup(ex_opts,splitted[0]);
32 
33         if (this_opts) {
34             g_ptr_array_add(this_opts,splitted[1]);
35             g_free(splitted[0]);
36         } else {
37             this_opts = g_ptr_array_new();
38             g_ptr_array_add(this_opts,splitted[1]);
39             g_hash_table_insert(ex_opts,splitted[0],this_opts);
40         }
41 
42         g_free(splitted);
43 
44         return TRUE;
45     } else {
46         g_strfreev(splitted);
47         return FALSE;
48     }
49 }
50 
ex_opt_count(const gchar * key)51 gint ex_opt_count(const gchar* key) {
52     GPtrArray* this_opts;
53 
54     if (! ex_opts)
55         return 0;
56 
57     this_opts = (GPtrArray *)g_hash_table_lookup(ex_opts,key);
58 
59     if (this_opts) {
60         return this_opts->len;
61     } else {
62         return 0;
63     }
64 }
65 
ex_opt_get_nth(const gchar * key,guint key_index)66 const gchar* ex_opt_get_nth(const gchar* key, guint key_index) {
67     GPtrArray* this_opts;
68 
69     if (! ex_opts)
70         return 0;
71 
72     this_opts = (GPtrArray *)g_hash_table_lookup(ex_opts,key);
73 
74     if (this_opts) {
75         if (this_opts->len > key_index) {
76             return (const gchar *)g_ptr_array_index(this_opts,key_index);
77         } else {
78             /* XXX: assert? */
79             return NULL;
80         }
81     } else {
82         return NULL;
83     }
84 
85 }
86 
ex_opt_get_next(const gchar * key)87 extern const gchar* ex_opt_get_next(const gchar* key) {
88     GPtrArray* this_opts;
89 
90     if (! ex_opts)
91         return 0;
92 
93     this_opts = (GPtrArray *)g_hash_table_lookup(ex_opts,key);
94 
95     if (this_opts) {
96         if (this_opts->len)
97             return (const gchar *)g_ptr_array_remove_index(this_opts,0);
98         else
99             return NULL;
100     } else {
101         return NULL;
102     }
103 }
104 
105 /*
106  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
107  *
108  * Local variables:
109  * c-basic-offset: 4
110  * tab-width: 8
111  * indent-tabs-mode: nil
112  * End:
113  *
114  * vi: set shiftwidth=4 tabstop=8 expandtab:
115  * :indentSize=4:tabSize=8:noTabs=true:
116  */
117