From c980cede98fada00249cc6d7218ef519302926f4 Mon Sep 17 00:00:00 2001 From: Stephen Fisher Date: Thu, 7 Dec 2006 20:29:40 +0000 Subject: 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 --- AUTHORS | 1 + doc/README.developer | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ epan/value_string.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ epan/value_string.h | 24 ++++++++++++++++++++++++ 4 files changed, 120 insertions(+) diff --git a/AUTHORS b/AUTHORS index 91c15582b3..2fcdf0ba14 100644 --- a/AUTHORS +++ b/AUTHORS @@ -2277,6 +2277,7 @@ Francesco Fondelli { 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 { diff --git a/doc/README.developer b/doc/README.developer index d74062621a..2573b62ac0 100644 --- a/doc/README.developer +++ b/doc/README.developer @@ -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. diff --git a/epan/value_string.c b/epan/value_string.c index a6fd925725..49d44d5b29 100644 --- a/epan/value_string.c +++ b/epan/value_string.c @@ -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); +} + diff --git a/epan/value_string.h b/epan/value_string.h index ff9c107ec6..546e8a7800 100644 --- a/epan/value_string.h +++ b/epan/value_string.h @@ -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__ */ -- cgit v1.2.3