From Francesco Fondelli:
authorsfisher <sfisher@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 7 Dec 2006 20:29:40 +0000 (20:29 +0000)
committersfisher <sfisher@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 7 Dec 2006 20:29:40 +0000 (20:29 +0000)
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().

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@20061 f5534014-38df-0310-8fa8-9805f1628bb7

AUTHORS
doc/README.developer
epan/value_string.c
epan/value_string.h

diff --git a/AUTHORS b/AUTHORS
index 91c15582b39a3945642db0446647313e9e5d1106..2fcdf0ba149abed4121d3a3a27415436328ab6b4 100644 (file)
--- a/AUTHORS
+++ b/AUTHORS
@@ -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> {
index d74062621ae7f30b1005fde5bc9e945e45ad5a7c..2573b62ac0192bc829f377132ed14313eab47127 100644 (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.
 
index a6fd925725664f014483166353d3d3a009bf6d92..49d44d5b29f05c2339a6b6cbb90538a111c3996b 100644 (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);
+}
+
index ff9c107ec63cd94319e1c7f9101e9f38b90a45d3..546e8a78004d2d283cbfdaf607d948dec95b06a1 100644 (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__ */