Re: [Wireshark-dev] [PATCH] range_string and OSPF bcmodelid

2006-12-07 Thread Stephen Fisher
On Wed, Dec 06, 2006 at 02:20:52PM +0100, Francesco Fondelli wrote:

 Could you write up some documentation changes to doc/README.developer 
 so others will know how to use this feature.  You can send a diff of 
 only README.developer since we already have the other patches.  We 
 will then review it for inclusion in Wireshark.
 
 You find it attached. My English sucks so check it out more carefully 
 than my code :-)

Both the documentation and code look fine.  I have committed the 
range_string feature as SVN revision 20061.  I committed your OSPF 
changes as revision 20063.  Thanks for your contributions!


Steve

___
Wireshark-dev mailing list
Wireshark-dev@wireshark.org
http://www.wireshark.org/mailman/listinfo/wireshark-dev


Re: [Wireshark-dev] [PATCH] range_string and OSPF bcmodelid

2006-12-06 Thread Stephen Fisher
On Mon, Dec 04, 2006 at 10:03:57AM +0100, Francesco Fondelli wrote:

 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 :-))

Question to the other committers: Do you think the code for this new 
range_string be in value_string.[ch] or have its own files 
range_string.[ch]?


Steve

___
Wireshark-dev mailing list
Wireshark-dev@wireshark.org
http://www.wireshark.org/mailman/listinfo/wireshark-dev


Re: [Wireshark-dev] [PATCH] range_string and OSPF bcmodelid

2006-12-06 Thread Anders Broman


-Ursprungligt meddelande-
Från: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] För Stephen Fisher
Skickat: den 7 december 2006 01:03
Till: Developer support list for Wireshark
Ämne: Re: [Wireshark-dev] [PATCH] range_string and OSPF bcmodelid

On Mon, Dec 04, 2006 at 10:03:57AM +0100, Francesco Fondelli wrote:

 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 :-))

Question to the other committers: Do you think the code for this new 
range_string be in value_string.[ch] or have its own files 
range_string.[ch]?


Steve

I think in value_string.[ch] is ok as it's really a variant of value string.
BR
Anders
___
Wireshark-dev mailing list
Wireshark-dev@wireshark.org
http://www.wireshark.org/mailman/listinfo/wireshark-dev

___
Wireshark-dev mailing list
Wireshark-dev@wireshark.org
http://www.wireshark.org/mailman/listinfo/wireshark-dev


Re: [Wireshark-dev] [PATCH] range_string and OSPF bcmodelid

2006-12-05 Thread Stephen Fisher
On Mon, Dec 04, 2006 at 10:03:57AM +0100, Francesco Fondelli wrote:

 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 :-))

This sounds like a great idea - I could use it in the VNC dissector too.  
Could you write up some documentation changes to doc/README.developer so 
others will know how to use this feature.  You can send a diff of only 
README.developer since we already have the other patches.  We will then 
review it for inclusion in Wireshark.


Thanks,
  Steve
___
Wireshark-dev mailing list
Wireshark-dev@wireshark.org
http://www.wireshark.org/mailman/listinfo/wireshark-dev


[Wireshark-dev] [PATCH] range_string and OSPF bcmodelid

2006-12-04 Thread Francesco Fondelli

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