From Francesco Fondelli:

I defined a range_string struct. It's like value_string
but stores range <-> string pairs.
Moreover I wrote rval_to_str(), match_strrval_idx()
match_strrval() which are behaving exactly as
val_to_str(), match_strval_idx() and match_strval().


svn path=/trunk/; revision=20061
This commit is contained in:
Stephen Fisher 2006-12-07 20:29:40 +00:00
parent f4e3b4c033
commit c980cede98
4 changed files with 120 additions and 0 deletions

View File

@ -2277,6 +2277,7 @@ Francesco Fondelli <francesco.fondelli [AT] gmail.com> {
MPLS OAM support, Y.1711
RSVP/OSPF Extensions for Support of Diffserv-aware MPLS-TE, RFC 4124
Linux Packet Generator support
rval_to_str() and alike
}
Bill Meier <wmeier [AT] newsguy.com> {

View File

@ -1569,6 +1569,28 @@ indicate the end of the array). The 'strings' field would be set to
If the field has a numeric rather than an enumerated type, the 'strings'
field would be set to NULL.
If the field has a numeric type that might logically fit in ranges of values
one can use a range_string struct.
Thus a 'range_string' structure is a way to map ranges to strings.
typedef struct _range_string {
guint32 value_min;
guint32 value_max;
const gchar *strptr;
} range_string;
For fields of that type, you would declare an array of "range_string"s:
static const range_string rvalstringname[] = {
{ INTVAL_MIN1, INTVALMAX1, "Descriptive String 1" },
{ INTVAL_MIN2, INTVALMAX2, "Descriptive String 2" },
{ 0, 0, NULL }
};
If INTVAL_MIN equals INTVAL_MAX for a given entry the range_string
behavior collapses to the one of value_string.
FT_BOOLEANS have a default map of 0 = "False", 1 (or anything else) = "True".
Sometimes it is useful to change the labels for boolean values (e.g.,
to "Yes"/"No", "Fast"/"Slow", etc.). For these mappings, a struct called
@ -2405,6 +2427,32 @@ to generate a string, and will return a pointer to that string.
them; this permits the results of up to three calls to 'val_to_str' to
be passed as arguments to a routine using those strings.)
1.7.2 match_strrval and rval_to_str.
A dissector may need to convert a range of values to a string, using a
'range_string' structure.
'match_strrval()' will do that:
gchar*
match_strrval(guint32 val, const range_string *rs)
It will look up the value 'val' in the 'range_string' table pointed to
by 'rs', and return either the corresponding string, or NULL if the
value could not be found in the table. Please note that its base
behavior is inherited from match_strval().
'rval_to_str()' can be used to generate a string for values not found in
the table:
gchar*
rval_to_str(guint32 val, const range_string *rs, const char *fmt)
If the value 'val' is found in the 'range_string' table pointed to by
'rs', 'rval_to_str' will return the corresponding string; otherwise, it
will use 'fmt' as an 'sprintf'-style format, with 'val' as an argument,
to generate a string, and will return a pointer to that string. Please
note that its base behavior is inherited from match_strval().
1.8 Calling Other Dissectors.

View File

@ -109,3 +109,50 @@ decode_enumerated_bitfield_shifted(guint32 val, guint32 mask, int width,
g_snprintf(p, 1024-(p-buf), fmt, val_to_str((val & mask) >> shift, tab, "Unknown"));
return buf;
}
/* FF: ranges aware versions */
/* Tries to match val against each range in the range_string array rs.
Returns the associated string ptr on a match.
Formats val with fmt, and returns the resulting string, on failure. */
const gchar *rval_to_str(guint32 val, const range_string *rs, const char *fmt)
{
const gchar *ret = NULL;
g_assert(fmt != NULL);
ret = match_strrval(val, rs);
if(ret != NULL)
return ret;
return ep_strdup_printf(fmt, val);
}
/* Tries to match val against each range in the range_string array rs.
Returns the associated string ptr, and sets "*idx" to the index in
that table, on a match, and returns NULL, and sets "*idx" to -1,
on failure. */
const gchar *match_strrval_idx(guint32 val, const range_string *rs, gint *idx)
{
gint i = 0;
while(rs[i].strptr) {
if( (val >= rs[i].value_min) && (val <= rs[i].value_max) ) {
*idx = i;
return (rs[i].strptr);
}
i++;
}
*idx = -1;
return (NULL);
}
/* Like match_strrval_idx(), but doesn't return the index. */
const gchar *match_strrval(guint32 val, const range_string *rs)
{
gint ignore_me = 0;
return match_strrval_idx(val, rs, &ignore_me);
}

View File

@ -34,6 +34,13 @@ typedef struct _value_string {
const gchar *strptr;
} value_string;
/* Struct for the rval_to_str, match_strrval_idx, and match_strrval functions */
typedef struct _range_string {
guint32 value_min;
guint32 value_max;
const gchar *strptr;
} range_string;
/* #define VS_DEF(x) { x, #x } */
/* #define VS_END { 0, NULL } */
@ -61,4 +68,21 @@ extern const char *decode_enumerated_bitfield(guint32 val, guint32 mask,
extern const char *decode_enumerated_bitfield_shifted(guint32 val, guint32 mask,
int width, const value_string *tab, const char *fmt);
/* ranges aware versions */
/* Tries to match val against each range in the range_string array rs.
Returns the associated string ptr on a match.
Formats val with fmt, and returns the resulting string, on failure. */
extern const gchar* rval_to_str(guint32 val, const range_string *rs, const char *fmt);
/* Tries to match val against each range in the range_string array rs.
Returns the associated string ptr, and sets "*idx" to the index in
that table, on a match, and returns NULL, and sets "*idx" to -1,
on failure. */
extern const gchar *match_strrval_idx(guint32 val, const range_string *rs, gint *idx);
/* Like match_strrval_idx(), but doesn't return the index. */
extern const gchar *match_strrval(guint32 val, const range_string *rs);
#endif /* __VALUE_STRING_H__ */