I'm repost my last mail, please have a look at it.
thanks
Ciao
FF
Hi all,
I needed to dissect a simple hf item in ospf, a integer one.
The possible values were in ranges like:
0 3 foo
1 4 bar
5 5 shark
6 100 reserved
101 200 private use
in such a case I normally use switch or if statements
and several proto_tree_add_uint_format() or
proto_tree_add_text() to properly handle the reserved and
private use cases. If I'm not wrong there is no
way to dissect such a hf in one shot using a
single proto_tree_add_xxx() call.
So 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().
(almost cut paste :-))
now we can do something like:
static const range_string rvals[] = {
{ 0, 3, foo},
{ 1, 4, bar},
{ 5, 5, shark },
{ 6, 100, reserved},
{ 101, 200, private},
{ 0, 0, NULL}
};
proto_tree_add_uint_format(tree,
hf_augh,
tvb,
offset,
1,
value,
bla bla: %u (%s),
value,
rval_to_str(value, rvals, Unknown));
This way people can write less lines of code (yes I'm lazy ;-)))
and IMHO it's more readable.
if you like this solution please apply
range_string_and_ospf_bcmodel_id.patch,
if you don't please apply ospf_bcmodel_id.patch. You
find both in attachment, they are diffed against
svn rev. 19892.
Thanks
Ciao
FF
Index: AUTHORS
===
--- AUTHORS (revision 19892)
+++ AUTHORS (working copy)
@@ -2277,6 +2277,7 @@
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 {
Index: epan/value_string.c
===
--- epan/value_string.c (revision 19892)
+++ epan/value_string.c (working copy)
@@ -109,3 +109,50 @@
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);
+}
+
Index: epan/value_string.h
===
--- epan/value_string.h (revision 19892)
+++ epan/value_string.h (working copy)
@@ -34,6 +34,13 @@
const gchar *strptr;
} value_string;
+/* Struct for the rval_to_str, match_strrval_idx, and match_strrval functions */
+typedef struct _range_string {
+ guint32value_min;
+ guint32value_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_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