Get rid of ber_last_created_item().
[obnox/wireshark/wip.git] / epan / value_string.c
1 /* value_string.c
2  * Routines for value_strings
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #include "to_str.h"
32 #include "emem.h"
33 #include "value_string.h"
34
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. */
38 const gchar*
39 val_to_str(guint32 val, const value_string *vs, const char *fmt) {
40   const gchar *ret;
41
42   g_assert(fmt != NULL);
43
44   ret = match_strval(val, vs);
45   if (ret != NULL)
46     return ret;
47
48   return ep_strdup_printf(fmt, val);
49 }
50
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,
54    on failure. */
55 const gchar*
56 match_strval_idx(guint32 val, const value_string *vs, gint *idx) {
57   gint i = 0;
58
59   if(vs) {
60     while (vs[i].strptr) {
61       if (vs[i].value == val) {
62         *idx = i;
63         return(vs[i].strptr);
64       }
65       i++;
66     }
67   }
68
69   *idx = -1;
70   return NULL;
71 }
72
73 /* Like match_strval_idx(), but doesn't return the index. */
74 const gchar*
75 match_strval(guint32 val, const value_string *vs) {
76     gint ignore_me;
77     return match_strval_idx(val, vs, &ignore_me);
78 }
79
80 /* Generate a string describing an enumerated bitfield (an N-bit field
81    with various specific values having particular names). */
82 const char *
83 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
84     const value_string *tab, const char *fmt)
85 {
86   static char buf[1025];
87   char *p;
88
89   p = decode_bitfield_value(buf, val, mask, width);
90   g_snprintf(p, 1024-(p-buf), fmt, val_to_str(val & mask, tab, "Unknown"));
91   return buf;
92 }
93
94
95 /* Generate a string describing an enumerated bitfield (an N-bit field
96    with various specific values having particular names). */
97 const char *
98 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
99     const value_string *tab, const char *fmt)
100 {
101   static char buf[1025];
102   char *p;
103   int shift = 0;
104
105   /* Compute the number of bits we have to shift the bitfield right
106      to extract its value. */
107   while ((mask & (1<<shift)) == 0)
108     shift++;
109
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"));
112   return buf;
113 }
114
115
116 /* FF: ranges aware versions */
117
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) 
122 {
123   const gchar *ret = NULL;
124
125   g_assert(fmt != NULL);
126
127   ret = match_strrval(val, rs);
128   if(ret != NULL)
129     return ret;
130
131   return ep_strdup_printf(fmt, val);
132 }
133
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,
137    on failure. */
138 const gchar *match_strrval_idx(guint32 val, const range_string *rs, gint *idx)
139 {
140   gint i = 0;
141
142   if(rs) {
143     while(rs[i].strptr) {
144       if( (val >= rs[i].value_min) && (val <= rs[i].value_max) ) {
145         *idx = i;
146         return (rs[i].strptr);
147       }
148       i++;
149     }
150   }
151
152   *idx = -1;
153   return NULL;
154 }
155
156 /* Like match_strrval_idx(), but doesn't return the index. */
157 const gchar *match_strrval(guint32 val, const range_string *rs)
158 {
159     gint ignore_me = 0;
160     return match_strrval_idx(val, rs, &ignore_me);
161 }
162