2 * Definitions for value_string structures and routines
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef __VALUE_STRING_H__
24 #define __VALUE_STRING_H__
28 #endif /* __cplusplus */
31 #include "ws_symbol_export.h"
32 #include "wmem/wmem.h"
34 /* VALUE TO STRING MATCHING */
36 typedef struct _value_string {
42 /* ----- VALUE_STRING "Helper" macros ----- */
44 /* Essentially: Provide the capability to define a list of value_strings once and
45 then to expand the list as an enum and/or as a value_string array. */
49 /*- define list of value strings -*/
50 #define foo_VALUE_STRING_LIST(XXX) \
51 XXX( FOO_A, 1, "aaa" ) \
52 XXX( FOO_B, 3, "bbb" )
55 VALUE_STRING_ENUM(foo); /* gen's 'enum {FOO_A=1, FOO_B=3};' */
57 /*- gen value_string array -*/
59 VALUE_STRING_ARRAY(foo); /* gen's 'static const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
62 VALUE_STRING_ARRAY_GLOBAL_DEF(foo); /* gen's 'const value_string foo[] = {{1,"aaa"}, {3,"bbb"}}; */
63 VALUE_STRING_ARRAY_GLOBAL_DCL(foo); /* gen's 'const value_string foo[]; */
66 #define bar_VALUE_STRING_LIST(XXX) \
70 VALUE_STRING_ENUM2(bar); /* gen's 'enum {BAR_A=1, BAR_B=3};' */
71 VALUE_STRING_ARRAY2(bar); /* gen's 'static const value_string bar[] = {{1,"BAR_A"}, {3,"BAR_B"}}; */
76 #define VALUE_STRING_ENUM( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY)
77 #define VALUE_STRING_ARRAY( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY, static)
78 #define VALUE_STRING_ARRAY_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY)
79 #define VALUE_STRING_ARRAY_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
81 #define VALUE_STRING_ENUM2( array_name) _VS_ENUM_XXX( array_name, _VS_ENUM_ENTRY2)
82 #define VALUE_STRING_ARRAY2( array_name) _VS_ARRAY_SC_XXX(array_name, _VS_ARRAY_ENTRY2, static)
83 #define VALUE_STRING_ARRAY2_GLOBAL_DEF( array_name) _VS_ARRAY_XXX(array_name, _VS_ARRAY_ENTRY2)
84 #define VALUE_STRING_ARRAY2_GLOBAL_DCL( array_name) _VS_ARRAY_SC_TYPE_NAME(array_name, extern)
87 #define _VS_ENUM_XXX(array_name, macro) \
89 array_name##_VALUE_STRING_LIST(macro) \
90 _##array_name##_ENUM_DUMMY = 0 \
93 #define _VS_ARRAY_SC_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_XXX(array_name, macro) \
100 _VS_ARRAY_TYPE_NAME(array_name) = { \
101 array_name##_VALUE_STRING_LIST(macro) \
105 #define _VS_ARRAY_SC_TYPE_NAME(array_name, sc) sc const value_string array_name[]
106 #define _VS_ARRAY_TYPE_NAME(array_name) const value_string array_name[]
108 #define _VS_ENUM_ENTRY( name, value, string) name = value,
109 #define _VS_ARRAY_ENTRY(name, value, string) { value, string },
111 #define _VS_ENUM_ENTRY2( name, value) name = value,
112 #define _VS_ARRAY_ENTRY2(name, value) { value, #name },
117 val_to_str(const guint32 val, const value_string *vs, const char *fmt)
122 val_to_str_wmem(wmem_allocator_t *scope, const guint32 val, const value_string *vs, const char *fmt)
127 val_to_str_const(const guint32 val, const value_string *vs, const char *unknown_str);
131 try_val_to_str(const guint32 val, const value_string *vs);
135 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx);
137 /* 64-BIT VALUE TO STRING MATCHING */
139 typedef struct _val64_string {
146 val64_to_str(const guint64 val, const val64_string *vs, const char *fmt)
151 val64_to_str_const(const guint64 val, const val64_string *vs, const char *unknown_str);
155 try_val64_to_str(const guint64 val, const val64_string *vs);
159 try_val64_to_str_idx(const guint64 val, const val64_string *vs, gint *idx);
161 /* STRING TO VALUE MATCHING */
165 str_to_val(const gchar *val, const value_string *vs, const guint32 err_val);
169 str_to_val_idx(const gchar *val, const value_string *vs);
171 /* EXTENDED VALUE TO STRING MATCHING */
173 typedef struct _value_string_ext value_string_ext;
174 typedef const value_string *(*_value_string_match2_t)(const guint32, value_string_ext*);
176 struct _value_string_ext {
177 _value_string_match2_t _vs_match2;
178 guint32 _vs_first_value; /* first value of the value_string array */
179 guint _vs_num_entries; /* number of entries in the value_string array */
180 /* (excluding final {0, NULL}) */
181 const value_string *_vs_p; /* the value string array address */
182 const gchar *_vs_name; /* vse "Name" (for error messages) */
185 #define VALUE_STRING_EXT_VS_P(x) (x)->_vs_p
186 #define VALUE_STRING_EXT_VS_NUM_ENTRIES(x) (x)->_vs_num_entries
187 #define VALUE_STRING_EXT_VS_NAME(x) (x)->_vs_name
191 _try_val_to_str_ext_init(const guint32 val, value_string_ext *vse);
192 #define VALUE_STRING_EXT_INIT(x) { _try_val_to_str_ext_init, 0, G_N_ELEMENTS(x)-1, x, #x }
196 value_string_ext_new(const value_string *vs, guint vs_tot_num_entries, const gchar *vs_name);
200 value_string_ext_free(value_string_ext *vse);
204 val_to_str_ext(const guint32 val, value_string_ext *vse, const char *fmt)
209 val_to_str_ext_wmem(wmem_allocator_t *scope, const guint32 val, value_string_ext *vse, const char *fmt)
214 val_to_str_ext_const(const guint32 val, value_string_ext *vs, const char *unknown_str);
218 try_val_to_str_ext(const guint32 val, value_string_ext *vse);
222 try_val_to_str_idx_ext(const guint32 val, value_string_ext *vse, gint *idx);
224 /* STRING TO STRING MATCHING */
226 typedef struct _string_string {
233 str_to_str(const gchar *val, const string_string *vs, const char *fmt)
238 try_str_to_str(const gchar *val, const string_string *vs);
242 try_str_to_str_idx(const gchar *val, const string_string *vs, gint *idx);
244 /* RANGE TO STRING MATCHING */
246 typedef struct _range_string {
254 rval_to_str(const guint32 val, const range_string *rs, const char *fmt)
259 rval_to_str_const(const guint32 val, const range_string *rs, const char *unknown_str);
263 try_rval_to_str(const guint32 val, const range_string *rs);
267 try_rval_to_str_idx(const guint32 val, const range_string *rs, gint *idx);
271 try_rval64_to_str(const guint64 val, const range_string *rs);
275 try_rval64_to_str_idx(const guint64 val, const range_string *rs, gint *idx);
277 /* BYTES TO STRING MATCHING */
279 typedef struct _bytes_string {
281 const size_t value_length;
287 bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs, const char *fmt)
292 try_bytesval_to_str(const guint8 *val, const size_t val_len, const bytes_string *bs);
296 bytesprefix_to_str(const guint8 *haystack, const size_t haystack_len, const bytes_string *bs, const char *fmt)
301 try_bytesprefix_to_str(const guint8 *haystack, const size_t haystack_len, const bytes_string *bs);
303 /* MISC (generally do not use) */
307 value_string_ext_validate(const value_string_ext *vse);
311 value_string_ext_match_type_str(const value_string_ext *vse);
315 #endif /* __cplusplus */
317 #endif /* __VALUE_STRING_H__ */
320 * Editor modelines - http://www.wireshark.org/tools/modelines.html
325 * indent-tabs-mode: nil
328 * vi: set shiftwidth=4 tabstop=8 expandtab:
329 * :indentSize=4:tabSize=8:noTabs=true: