removal of even more sprintf
[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@ethereal.com>
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   gchar *cur;
42
43   g_assert(fmt != NULL);
44
45   ret = match_strval(val, vs);
46   if (ret != NULL)
47     return ret;
48
49   cur=ep_alloc(64);
50   g_snprintf(cur, 64, fmt, val);
51   return cur;
52 }
53
54 /* Tries to match val against each element in the value_string array vs.
55    Returns the associated string ptr, and sets "*idx" to the index in
56    that table, on a match, and returns NULL, and sets "*idx" to -1,
57    on failure. */
58 const gchar*
59 match_strval_idx(guint32 val, const value_string *vs, gint *idx) {
60   gint i = 0;
61
62   while (vs[i].strptr) {
63     if (vs[i].value == val) {
64       *idx = i;
65       return(vs[i].strptr);
66     }
67     i++;
68   }
69
70   *idx = -1;
71   return(NULL);
72 }
73
74 /* Like match_strval_idx(), but doesn't return the index. */
75 const gchar*
76 match_strval(guint32 val, const value_string *vs) {
77     gint ignore_me;
78     return match_strval_idx(val, vs, &ignore_me);
79 }
80
81 /* Generate a string describing an enumerated bitfield (an N-bit field
82    with various specific values having particular names). */
83 const char *
84 decode_enumerated_bitfield(guint32 val, guint32 mask, int width,
85     const value_string *tab, const char *fmt)
86 {
87   static char buf[1025];
88   char *p;
89
90   p = decode_bitfield_value(buf, val, mask, width);
91   g_snprintf(p, 1024-(p-buf), fmt, val_to_str(val & mask, tab, "Unknown"));
92   return buf;
93 }
94
95
96 /* Generate a string describing an enumerated bitfield (an N-bit field
97    with various specific values having particular names). */
98 const char *
99 decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
100     const value_string *tab, const char *fmt)
101 {
102   static char buf[1025];
103   char *p;
104   int shift = 0;
105
106   /* Compute the number of bits we have to shift the bitfield right
107      to extract its value. */
108   while ((mask & (1<<shift)) == 0)
109     shift++;
110
111   p = decode_bitfield_value(buf, val, mask, width);
112   g_snprintf(p, 1024-(p-buf), fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));
113   return buf;
114 }