2 * Definitions for value_string structures and routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
25 #ifndef __VALUE_STRING_H__
26 #define __VALUE_STRING_H__
30 #endif /* __cplusplus */
33 #include "ws_symbol_export.h"
35 /* VALUE TO STRING MATCHING */
37 typedef struct _value_string {
43 /* ----- VALUE_STRING "Helper" macros ----- */
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. */
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" )
56 VALUE_STRING_ENUM(foo); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
58 /*- gen value_string array -*/
60 VALUE_STRING_ARRAY(foo); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
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[]; */
67 #define bar_VALUE_STRING_LIST(XXX) \
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"}}; */
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)
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)
88 #define _VS_ENUM_XXX(array_name, macro) \
90 array_name##_VALUE_STRING_LIST(macro) \
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) \
99 #define _VS_ARRAY_SC_TYPE_NAME(array_name, sc) sc const value_string array_name[]
101 #define _VS_ENUM_ENTRY( name, value, string) name = value,
102 #define _VS_ARRAY_ENTRY(name, value, string) { value, string },
104 #define _VS_ENUM_ENTRY2( name, value) name = value,
105 #define _VS_ARRAY_ENTRY2(name, value) { value, #name },
110 val_to_str(const guint32 val, const value_string *vs, const char *fmt);
114 val_to_str_const(const guint32 val, const value_string *vs, const char *unknown_str);
118 try_val_to_str(const guint32 val, const value_string *vs);
122 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx);
124 /* 64-BIT VALUE TO STRING MATCHING */
126 typedef struct _val64_string {
133 val64_to_str(const guint64 val, const val64_string *vs, const char *fmt);
137 val64_to_str_const(const guint64 val, const val64_string *vs, const char *unknown_str);
141 try_val64_to_str(const guint64 val, const val64_string *vs);
145 try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx);
147 /* STRING TO VALUE MATCHING */
151 str_to_val(const gchar *val, const value_string *vs, const guint32 err_val);
155 str_to_val_idx(const gchar *val, const value_string *vs);
157 /* EXTENDED VALUE TO STRING MATCHING */
159 struct _value_string_ext;
160 typedef const value_string *(*_value_string_match2_t)(const guint32, const struct _value_string_ext *);
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) */
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
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 }
181 const value_string_ext *
182 value_string_ext_new(const value_string *vs, guint vs_tot_num_entries, const gchar *vs_name);
186 value_string_ext_free(const value_string_ext *vse);
190 val_to_str_ext(const guint32 val, const value_string_ext *vs, const char *fmt);
194 val_to_str_ext_const(const guint32 val, const value_string_ext *vs, const char *unknown_str);
198 try_val_to_str_ext(const guint32 val, const value_string_ext *vse);
202 try_val_to_str_idx_ext(const guint32 val, const value_string_ext *vse, gint *idx);
204 /* STRING TO STRING MATCHING */
206 typedef struct _string_string {
213 str_to_str(const gchar *val, const string_string *vs, const char *fmt);
217 try_str_to_str(const gchar *val, const string_string *vs);
221 try_str_to_str_idx(const gchar *val, const string_string *vs, gint *idx);
223 /* RANGE TO STRING MATCHING */
225 typedef struct _range_string {
233 rval_to_str(const guint32 val, const range_string *rs, const char *fmt);
237 rval_to_str_const(const guint32 val, const range_string *rs, const char *unknown_str);
241 try_rval_to_str(const guint32 val, const range_string *rs);
245 try_rval_to_str_idx(const guint32 val, const range_string *rs, gint *idx);
247 /* MISC (generally do not use) */
251 value_string_ext_validate(const value_string_ext *vse);
255 value_string_ext_match_type_str(const value_string_ext *vse);
259 #endif /* __cplusplus */
261 #endif /* __VALUE_STRING_H__ */
264 * Editor modelines - http://www.wireshark.org/tools/modelines.html
269 * indent-tabs-mode: nil
272 * vi: set shiftwidth=4 tabstop=8 expandtab:
273 * :indentSize=4:tabSize=8:noTabs=true: