(Trivial) Fix spellling in a comment.
[metze/wireshark/wip.git] / epan / value_string.h
1 /* value_string.h
2  * Definitions for value_string structures and routines
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #ifndef __VALUE_STRING_H__
26 #define __VALUE_STRING_H__
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */ 
31
32 #include <glib.h>
33 #include "ws_symbol_export.h"
34
35 /* VALUE TO STRING MATCHING */
36
37 typedef struct _value_string {
38     guint32      value;
39     const gchar *strptr;
40 } value_string;
41
42 #if 0
43   /* -----  VALUE_STRING "Helper" macros ----- */
44
45   /* Essentially: Provide the capability to define a list of value_strings once and
46      then to expand the list as an enum and/or as a value_string array. */
47
48   /* Usage: */
49
50   /*- define list of value strings -*/
51      #define foo_VALUE_STRING_LIST(XXX) \
52         XXX( FOO_A, 1, "aaa" ) \
53         XXX( FOO_B, 3, "bbb" )
54
55   /*- gen enum -*/
56      VALUE_STRING_ENUM(foo);      /* gen's 'enum {FOO_A=1, FOO_B=3};' */
57
58   /*- gen value_string array -*/
59      /* local */
60      VALUE_STRING_ARRAY(foo);     /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
61
62      /* global */
63      VALUE_STRING_ARRAY_GLOBAL_DEF(foo); /* gen's 'const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
64      VALUE_STRING_ARRAY_GLOBAL_DCL(foo); /* gen's 'const value_string foo[]; */
65
66   /* Alternatively: */
67      #define bar_VALUE_STRING_LIST(XXX) \
68         XXX( BAR_A, 1) \
69         XXX( BAR_B, 3)
70
71      VALUE_STRING_ENUM2(bar);     /* gen's 'enum {BAR_A=1, BAR_B=3};' */
72      VALUE_STRING_ARRAY2(bar);    /* gen's 'static const value_string bar[] = {{1,"BAR_A"}, {3,"BAR_B"}}; */
73      ...
74 #endif
75
76 /* -- Public -- */
77 #define VALUE_STRING_ENUM(             array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY)
78 #define VALUE_STRING_ARRAY(            array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY, static)
79 #define VALUE_STRING_ARRAY_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY, )
80 #define VALUE_STRING_ARRAY_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
81
82 #define VALUE_STRING_ENUM2(             array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY2)
83 #define VALUE_STRING_ARRAY2(            array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY2, static)
84 #define VALUE_STRING_ARRAY2_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY2, )
85 #define VALUE_STRING_ARRAY2_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
86
87 /* -- Private -- */
88 #define _VS_ENUM_XXX(array_name, macro) \
89 enum { \
90     array_name##_VALUE_STRING_LIST(macro) \
91 }
92
93 #define _VS_ARRAY_XXX(array_name, macro, sc)  \
94     _VS_ARRAY_SC_TYPE_NAME(array_name, sc) = {    \
95     array_name##_VALUE_STRING_LIST(macro) \
96     { 0, NULL } \
97 }
98
99 #define _VS_ARRAY_SC_TYPE_NAME(array_name, sc) sc const value_string array_name[]
100
101 #define _VS_ENUM_ENTRY( name, value, string) name = value,
102 #define _VS_ARRAY_ENTRY(name, value, string) { value, string },
103
104 #define _VS_ENUM_ENTRY2( name, value) name = value,
105 #define _VS_ARRAY_ENTRY2(name, value) { value, #name },
106 /* ----- ----- */
107
108 WS_DLL_PUBLIC
109 const gchar *
110 val_to_str(const guint32 val, const value_string *vs, const char *fmt);
111
112 WS_DLL_PUBLIC
113 const gchar *
114 val_to_str_const(const guint32 val, const value_string *vs, const char *unknown_str);
115
116 WS_DLL_PUBLIC
117 const gchar *
118 try_val_to_str(const guint32 val, const value_string *vs);
119
120 WS_DLL_PUBLIC
121 const gchar *
122 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx);
123
124 /* 64-BIT VALUE TO STRING MATCHING */
125
126 typedef struct _val64_string {
127     guint64      value;
128     const gchar *strptr;
129 } val64_string;
130
131 WS_DLL_PUBLIC
132 const gchar *
133 val64_to_str(const guint64 val, const val64_string *vs, const char *fmt);
134
135 WS_DLL_PUBLIC
136 const gchar *
137 val64_to_str_const(const guint64 val, const val64_string *vs, const char *unknown_str);
138
139 WS_DLL_PUBLIC
140 const gchar *
141 try_val64_to_str(const guint64 val, const val64_string *vs);
142
143 WS_DLL_PUBLIC
144 const gchar *
145 try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx);
146
147 /* STRING TO VALUE MATCHING */
148
149 WS_DLL_PUBLIC
150 guint32
151 str_to_val(const gchar *val, const value_string *vs, const guint32 err_val);
152
153 WS_DLL_PUBLIC
154 gint
155 str_to_val_idx(const gchar *val, const value_string *vs);
156
157 /* EXTENDED VALUE TO STRING MATCHING */
158
159 struct _value_string_ext;
160 typedef const value_string *(*_value_string_match2_t)(const guint32, const struct _value_string_ext *);
161
162 typedef struct _value_string_ext {
163     _value_string_match2_t _vs_match2;
164     guint32                _vs_first_value; /* first value of the value_string array       */
165     guint                  _vs_num_entries; /* number of entries in the value_string array */
166                                             /*  (excluding final {0, NULL})                */
167     const value_string    *_vs_p;           /* the value string array address              */
168     const gchar           *_vs_name;        /* vse "Name" (for error messages)             */
169 } value_string_ext;
170
171 #define VALUE_STRING_EXT_VS_P(x)           (x)->_vs_p
172 #define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
173 #define VALUE_STRING_EXT_VS_NAME(x)        (x)->_vs_name
174
175 WS_DLL_PUBLIC
176 const value_string *
177 _try_val_to_str_ext_init(const guint32 val, const value_string_ext *vse);
178 #define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
179
180 WS_DLL_PUBLIC
181 const value_string_ext *
182 value_string_ext_new(const value_string *vs, guint vs_tot_num_entries, const gchar *vs_name);
183
184 WS_DLL_PUBLIC
185 void
186 value_string_ext_free(const value_string_ext *vse);
187
188 WS_DLL_PUBLIC
189 const gchar *
190 val_to_str_ext(const guint32 val, const value_string_ext *vs, const char *fmt);
191
192 WS_DLL_PUBLIC
193 const gchar *
194 val_to_str_ext_const(const guint32 val, const value_string_ext *vs, const char *unknown_str);
195
196 WS_DLL_PUBLIC
197 const gchar *
198 try_val_to_str_ext(const guint32 val, const value_string_ext *vse);
199
200 WS_DLL_PUBLIC
201 const gchar *
202 try_val_to_str_idx_ext(const guint32 val, const value_string_ext *vse, gint *idx);
203
204 /* STRING TO STRING MATCHING */
205
206 typedef struct _string_string {
207     const gchar *value;
208     const gchar *strptr;
209 } string_string;
210
211 WS_DLL_PUBLIC
212 const gchar *
213 str_to_str(const gchar *val, const string_string *vs, const char *fmt);
214
215 WS_DLL_PUBLIC
216 const gchar *
217 try_str_to_str(const gchar *val, const string_string *vs);
218
219 WS_DLL_PUBLIC
220 const gchar *
221 try_str_to_str_idx(const gchar *val, const string_string *vs, gint *idx);
222
223 /* RANGE TO STRING MATCHING */
224
225 typedef struct _range_string {
226     guint32      value_min;
227     guint32      value_max;
228     const gchar *strptr;
229 } range_string;
230
231 WS_DLL_PUBLIC
232 const gchar *
233 rval_to_str(const guint32 val, const range_string *rs, const char *fmt);
234
235 WS_DLL_PUBLIC
236 const gchar *
237 rval_to_str_const(const guint32 val, const range_string *rs, const char *unknown_str);
238
239 WS_DLL_PUBLIC
240 const gchar *
241 try_rval_to_str(const guint32 val, const range_string *rs);
242
243 WS_DLL_PUBLIC
244 const gchar *
245 try_rval_to_str_idx(const guint32 val, const range_string *rs, gint *idx);
246
247 /* MISC (generally do not use) */
248
249 WS_DLL_LOCAL
250 gboolean
251 value_string_ext_validate(const value_string_ext *vse);
252
253 WS_DLL_LOCAL
254 const gchar *
255 value_string_ext_match_type_str(const value_string_ext *vse);
256
257 #ifdef __cplusplus
258 }
259 #endif /* __cplusplus */
260
261 #endif /* __VALUE_STRING_H__ */
262
263 /*
264  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
265  *
266  * Local variables:
267  * c-basic-offset: 4
268  * tab-width: 8
269  * indent-tabs-mode: nil
270  * End:
271  *
272  * vi: set shiftwidth=4 tabstop=8 expandtab:
273  * :indentSize=4:tabSize=8:noTabs=true:
274  */