Implement str_to_val, opposite of val_to_str for value_strings.
authorEvan Huus <eapache@gmail.com>
Fri, 29 Mar 2013 23:23:28 +0000 (23:23 -0000)
committerEvan Huus <eapache@gmail.com>
Fri, 29 Mar 2013 23:23:28 +0000 (23:23 -0000)
https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8467

svn path=/trunk/; revision=48645

epan/value_string.c
epan/value_string.h

index 64eddb7480fac86e347237c8e706dbd9dc132343..b81679be5435b2b2e5c926c68ad4efda0571b4db 100644 (file)
@@ -102,6 +102,48 @@ try_val_to_str(const guint32 val, const value_string *vs)
     return try_val_to_str_idx(val, vs, &ignore_me);
 }
 
+/* REVERSE VALUE STRING */
+
+/* We use the same struct as for regular value strings, but we look up strings
+ * and return values instead */
+
+/* Like val_to_str except backwards */
+guint32
+str_to_val(const gchar *val, const value_string *vs, const guint32 err_val)
+{
+    gint index;
+
+    index = str_to_val_idx(val, vs);
+
+    if (index >= 0) {
+        return vs[index].value;
+    }
+
+    return err_val;
+}
+
+/* Find the index of a string in a value_string, or -1 when not present */
+gint
+str_to_val_idx(const gchar *val, const value_string *vs)
+{
+    gint i = 0;
+
+    if(vs) {
+
+        while (vs[i].strptr) {
+
+            if (strcmp(vs[i].strptr, val) == 0) {
+                return i;
+            }
+
+            i++;
+        }
+
+    }
+
+    return -1;
+}
+
 /* EXTENDED VALUE STRING */
 
 /* Extended value strings allow fast(er) value_string array lookups by
index de12e52a52cd2f47b232c2b4475d4631fa1da91b..1759d0dfb19ac25b14105240125cd8c55637194d 100644 (file)
@@ -51,6 +51,16 @@ WS_DLL_PUBLIC
 const gchar*
 try_val_to_str_idx(const guint32 val, const value_string *vs, gint *idx);
 
+/* STRING TO VALUE MATCHING */
+
+WS_DLL_PUBLIC
+guint32
+str_to_val(const gchar *val, const value_string *vs, const guint32 err_val);
+
+WS_DLL_PUBLIC
+gint
+str_to_val_idx(const gchar *val, const value_string *vs);
+
 /* EXTENDED VALUE TO STRING MATCHING */
 
 struct _value_string_ext;