2 * Routines for value_strings
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include "value_string.h"
35 /* Tries to match val against each element in the value_string array vs.
36 Returns the associated string ptr on a match.
37 Formats val with fmt, and returns the resulting string, on failure. */
39 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
42 g_assert(fmt != NULL);
44 ret = match_strval(val, vs);
48 return ep_strdup_printf(fmt, val);
51 /* Tries to match val against each element in the value_string array vs.
52 Returns the associated string ptr, and sets "*idx" to the index in
53 that table, on a match, and returns NULL, and sets "*idx" to -1,
56 match_strval_idx(guint32 val, const value_string *vs, gint *idx) {
60 while (vs[i].strptr) {
61 if (vs[i].value == val) {
73 /* Like match_strval_idx(), but doesn't return the index. */
75 match_strval(guint32 val, const value_string *vs) {
77 return match_strval_idx(val, vs, &ignore_me);
80 /* Generate a string describing an enumerated bitfield (an N-bit field
81 with various specific values having particular names). */
83 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
84 const value_string *tab, const char *fmt)
86 static char buf[1025];
89 p = decode_bitfield_value(buf, val, mask, width);
90 g_snprintf(p, 1024-(p-buf), fmt, val_to_str(val & mask, tab, "Unknown"));
95 /* Generate a string describing an enumerated bitfield (an N-bit field
96 with various specific values having particular names). */
98 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
99 const value_string *tab, const char *fmt)
101 static char buf[1025];
105 /* Compute the number of bits we have to shift the bitfield right
106 to extract its value. */
107 while ((mask & (1<<shift)) == 0)
110 p = decode_bitfield_value(buf, val, mask, width);
111 g_snprintf(p, 1024-(p-buf), fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));
116 /* FF: ranges aware versions */
118 /* Tries to match val against each range in the range_string array rs.
119 Returns the associated string ptr on a match.
120 Formats val with fmt, and returns the resulting string, on failure. */
121 const gchar *rval_to_str(guint32 val, const range_string *rs, const char *fmt)
123 const gchar *ret = NULL;
125 g_assert(fmt != NULL);
127 ret = match_strrval(val, rs);
131 return ep_strdup_printf(fmt, val);
134 /* Tries to match val against each range in the range_string array rs.
135 Returns the associated string ptr, and sets "*idx" to the index in
136 that table, on a match, and returns NULL, and sets "*idx" to -1,
138 const gchar *match_strrval_idx(guint32 val, const range_string *rs, gint *idx)
143 while(rs[i].strptr) {
144 if( (val >= rs[i].value_min) && (val <= rs[i].value_max) ) {
146 return (rs[i].strptr);
156 /* Like match_strrval_idx(), but doesn't return the index. */
157 const gchar *match_strrval(guint32 val, const range_string *rs)
160 return match_strrval_idx(val, rs, &ignore_me);