2 * Routines for value_strings
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #ifdef NEED_SNPRINTF_H
33 # include "snprintf.h"
36 #include "value_string.h"
38 /* Tries to match val against each element in the value_string array vs.
39 Returns the associated string ptr on a match.
40 Formats val with fmt, and returns the resulting string, on failure. */
42 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
44 static gchar str[3][64];
47 ret = match_strval(val, vs);
50 if (cur == &str[0][0]) {
52 } else if (cur == &str[1][0]) {
57 snprintf(cur, 64, fmt, val);
61 /* Tries to match val against each element in the value_string array vs.
62 Returns the associated string ptr on a match, or NULL on failure. */
64 match_strval(guint32 val, const value_string *vs) {
67 while (vs[i].strptr) {
68 if (vs[i].value == val)
76 /* Generate a string describing an enumerated bitfield (an N-bit field
77 with various specific values having particular names). */
79 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
80 const value_string *tab, const char *fmt)
82 static char buf[1025];
85 p = decode_bitfield_value(buf, val, mask, width);
86 sprintf(p, fmt, val_to_str(val & mask, tab, "Unknown"));
91 /* Generate a string describing an enumerated bitfield (an N-bit field
92 with various specific values having particular names). */
94 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
95 const value_string *tab, const char *fmt)
97 static char buf[1025];
101 /* Compute the number of bits we have to shift the bitfield right
102 to extract its value. */
103 while ((mask & (1<<shift)) == 0)
106 p = decode_bitfield_value(buf, val, mask, width);
107 sprintf(p, fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));