Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / epan / value_string.c
1 /* value_string.c
2  * Routines for value_strings
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31
32 #ifdef NEED_SNPRINTF_H
33 # include "snprintf.h"
34 #endif
35 #include "to_str.h"
36 #include "value_string.h"
37
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. */
41 gchar*
42 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
43   gchar *ret;
44   static gchar  str[3][64];
45   static gchar *cur;
46
47   ret = match_strval(val, vs);
48   if (ret != NULL)
49     return ret;
50   if (cur == &str[0][0]) {
51     cur = &str[1][0];
52   } else if (cur == &str[1][0]) {
53     cur = &str[2][0];
54   } else {
55     cur = &str[0][0];
56   }
57   snprintf(cur, 64, fmt, val);
58   return cur;
59 }
60
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. */
63 gchar*
64 match_strval(guint32 val, const value_string *vs) {
65   gint i = 0;
66
67   while (vs[i].strptr) {
68     if (vs[i].value == val)
69       return(vs[i].strptr);
70     i++;
71   }
72
73   return(NULL);
74 }
75
76 /* Generate a string describing an enumerated bitfield (an N-bit field
77    with various specific values having particular names). */
78 const char *
79 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
80     const value_string *tab, const char *fmt)
81 {
82   static char buf[1025];
83   char *p;
84
85   p = decode_bitfield_value(buf, val, mask, width);
86   sprintf(p, fmt, val_to_str(val & mask, tab, "Unknown"));
87   return buf;
88 }
89
90
91 /* Generate a string describing an enumerated bitfield (an N-bit field
92    with various specific values having particular names). */
93 const char *
94 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
95     const value_string *tab, const char *fmt)
96 {
97   static char buf[1025];
98   char *p;
99   int shift = 0;
100
101   /* Compute the number of bits we have to shift the bitfield right
102      to extract its value. */
103   while ((mask & (1<<shift)) == 0)
104     shift++;
105
106   p = decode_bitfield_value(buf, val, mask, width);
107   sprintf(p, fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));
108   return buf;
109 }